0
101 Sep 08, 2004 at 17:05

matrix inversion in general is a slow and tedious process. however in computer graphics we mostly deal with special kinds of matrices that allow for much faster inversion. one such type of matrix is the standard 4 by 4 transformation matrix that contains a rotation and a translation. conveniantly we can invert the rotation and the translation seperatly. the 3x3 rotation matrix is easily inverted by taking the dot product between each pair of row vectors, since it is orthogonal (the vector dot itself is 1 and and 0 for all the others). taking the dot product of the row vectors is equivalent to multiplying the matrix with it’s transpose. so the inversion of the rotation part is simply it’s transpose

void Matrix4x4::FastInvert()
{
swap(m[0][1], m[1][0]);
swap(m[0][2], m[2][0]);
swap(m[1][2], m[2][1]);


the translation part is then equally easy inverted (by taking the negative translation and multiplying it with the inverted rotation matrix, which is the transpose we just calculated).

  float m30 = -(m[0][0] * m[3][0] + m[1][0] * m[3][1] + m[2][0] * m[3][2]);
float m31 = -(m[0][1] * m[3][0] + m[1][1] * m[3][1] + m[2][1] * m[3][2]);
m[3][2] = -(m[0][2] * m[3][0] + m[1][2] * m[3][1] + m[2][2] * m[3][2]);
m[3][1] = m31;
m[3][0] = m30;
}


it should be noted that this only works if the matrix is unscaled because the row vectors of the rotation matrix are required to have unit length. the calculation should be extendable though if the matrix is scaled equally in all directions.

#### 3 Replies

0
101 Sep 08, 2004 at 17:33

i eddited the smileys out because they only showed up as “smiley.gif” when i accessed the code from the front page

0
101 Sep 08, 2004 at 17:44

@anubis

  m[3][0] = -(m[0][0] * m[3][0] + m[1][0] * m[3][1] + m[2][0] * m[3][2]);
m[3][1] = -(m[0][1] * m[3][0] + m[1][1] * m[3][1] + m[2][1] * m[3][2]);
m[3][2] = -(m[0][2] * m[3][0] + m[1][2] * m[3][1] + m[2][2] * m[3][2]);
}


[snapback]10974[/snapback]

Hmm… dont you need some temp variables for m[3][0] and m[3][1] so you dont use their new updated values when m[3][1] and m[3][2] is computed?

0
101 Sep 08, 2004 at 18:06

right…
i converted the code from my own, which just returns a new matrix so that’s how the erorr sneaked in… i will edit it right away. thanks anyway