Game Development Reference

In-Depth Information

Some graphics libraries use a full 16-element matrix for transforms; most of those

(but not all) will also use four-element vectors for position. They allow the program-

mer to work in four dimensions. There are some interesting graphical effects that are

made possible this way, including the perspective transformations needed to model a

camera. If you are relying on the mathematics libraries that these APIs provide, you

will not need to worry about the number of entries in the matrix: chances are you'll

only be using the first 12 for your physics development. If you are implementing the

mathematics classes as I have been, then you have the choice of whether to use the

full or the 3

4matrix.

Whereas we added an extra padding element to our vector class so that it sits

nicely on machines with 128-bit math processors and 16-byte alignment, we don't

need to do the same for matrices because each row of the matrix is 128 bits long (as-

suming we're using 32-bit floating-point numbers, although running this at double

precision will be much slower in any case).

Thecodewilltakelessmemoryifyouuse3

×

4 matrices and rely on the last,

unstored line of every matrix being [0 0 0 1]. But check to see whether the machine

you are developing has built-in hardware-level support for matrix transformation.

Implementing your own routines and ignoring these will result in worse performance

(and take more effort) in the long run.

×

9.4.3

T
HE
M
ATRIX
I
NVERSE AND
T
RANSPOSE

A matrix represents a transformation, and we often need to find out how to reverse

the transformation. If we have a matrix that changes from an object's local coordi-

nates to world coordinates, it will be useful to be able to create a matrix that gets us

back again: converting world coordinates to local coordinates.

For example, if we determine that our car has collided with a barrier, we know

the position of the collision in world coordinates. We'd like to be able to turn this

position into local coordinates to see which bit of the car got hit.

If a matrix transforms vectors from one basis to another, then the inverse of the

matrix can convert them back. If we combine a matrix with its inverse, we get the

“identity matrix”: a matrix representing a transformation that has no effect. In other

words, if we transform a vector by a matrix and then by its inverse, we get back to

where we started:

M
−
1
M

=

I

×

For a 3

3 matrix, the identity matrix is

⎡

⎣

⎤

⎦

100

010

001

I

=

Inverting large matrices is a challenging computer science problem (in fact, it is

the fundamental problem that the most complex game physics engines try to solve,