Game Development Reference
In-Depth Information
as we'll see in chapter 18). Techniques involve walking through the matrix and rear-
ranging its elements using a range of mathematical manipulations. Fortunately, for
3
×
3and4
×
4 matrices we can write the solutions directly. For a 3
×
3matrix,
abc
def
ghi
M
=
the inverse is
ei
fh
ch
bi bf
ce
1
det M
M 1
=
[9.7]
fg
di
ai
cg cd
af
dh
eg bg
ah ae
bd
where det M is the determinant of the matrix, which for a 3
×
3matrixis
det M
=
aei
+
dhc
+
gbf
ahf
gec
dbi
Because we take 1 over the determinant in equation 9.7, the inverse only exists if the
determinant is non-zero.
You can verify for yourself that the inverse matrix, when multiplied by the original
matrix, does give the identity matrix. The reason the inverse has the form it does, and
what the meaning of the determinant is, are beyond the scope of this topic. 2 To u n -
derstand why the preceding equations work, we'd have to cover various bits of matrix
mathematics that we otherwise wouldn't need. If you are interested in the features
and mathematics of matrices, any undergraduate textbook on matrix analysis will
provide more details. For an even more exhaustive (if considerably tougher) treat-
ment, I'd recommend Horn and Johnson [1990] and [1994], two highly respected
references on the topic.
We can implement our 3
3 matrix inverse as follows:
Excerpt from include/cyclone/core.h
×
/**
* Holds an inertia tensor, consisting of a 3x3 row-major matrix.
* This matrix is not padding to produce an aligned structure, since
* it is most commonly used with a mass (single real) and two
* damping coefficients to make the 12-element characteristics array
2. A good rule of thumb I use (which may offend mathematical purists) is to think of the determinant
as the “size” of the matrix. In fact, for a 2
2 dimensional matrix the determinant is the area of the
parallelogram formed from its column vectors, and for a 3
×
×
3 matrix it is the area of the parallelepiped
formed from its three columns.
The inverse formula of equation 9.7 can then be thought of as adjusting the elements and dividing
by the size of the matrix. Thinking this way can cause problems with more advanced matrix math, so
remember that it's only a mnemonic.