Game Development Reference

In-Depth Information

Here
M
−1
is the inverse of matrix
M
, and
I
is the identity matrix. For a 3×3 matrix, we

find the inverse as follows:

Here
E
ij
represents the cofactor of element
e
ij
, which we can find by taking the deter‐

minant of the minor of each element. Only square matrices, those with the same number

of rows as columns, can be inverted. Note, however, that not all square matrices can be

inverted. A matrix can be inverted only if its determinant is nonzero.

The follow relation applies to matrix inversion:

(
M N
)
−1
=
N
−1
M
−1

Here's how matrix inversion looks in code for our
Matrix3x3
class:

inline Matrix3x3 Matrix3x3::Inverse(void)

{

float d = e11*e22*e33 -

e11*e32*e23 +

e21*e32*e13 -

e21*e12*e33 +

e31*e12*e23 -

e31*e22*e13;

if (d == 0) d = 1;

return Matrix3x3( (e22*e33-e23*e32)/d,

-(e12*e33-e13*e32)/d,

(e12*e23-e13*e22)/d,

-(e21*e33-e23*e31)/d,

(e11*e33-e13*e31)/d,

-(e11*e23-e13*e21)/d,

(e21*e32-e22*e31)/d,

-(e11*e32-e12*e31)/d,

(e11*e22-e12*e21)/d );

}

Matrix Addition: The += Operator

This operator simply adds the passed matrix to the current one on an element-by-

element basis. For two matrices to be added, they must be of the same
order
—that is,

they must have the same number of rows and columns:

inline Matrix3x3& Matrix3x3::operator+=(Matrix3x3 m)

{

e11 += m.e11;