Game Engine Architecture

(Ben Green) #1

4.3. Matrices 155


It’s probably no surprise that rotations in three dimensions can be represented
by a 3 × 3 matrix. The two-dimensional example above is really just a three-
dimensional rotation about the z-axis, so we can write


The question naturally arises: Can a 3 × 3 matrix be used to represent
translations? Sadly, the answer is no. The result of translating a point r by a
translation t requires adding the components of t to the components of r in-
dividually:


Matrix multiplication involves multiplication and addition of matrix ele-
ments, so the idea of using multiplication for translation seems promising.
But, unfortunately, there is no way to arrange the components of t within a 3 ×
3 matrix such that the result of multiplying it with the column vector r yields
sums like (rx + tx).
The good news is that we can obtain sums like this if we use a 4 × 4 matrix.
What would such a matrix look like? Well, we know that we don’t want any
rotational eff ects, so the upper 3 × 3 should contain an identity matrix. If we
arrange the components of t across the bott om-most row of the matrix and
set the fourth element of the r vector (usually called w) equal to 1, then taking
the dot product of the vector r with column 1 of the matrix will yield (1 × rx) +
(0 × ry) + (0 × rz) + (tx × 1) = (rx + tx), which is exactly what we want. If the bott om
right-hand corner of the matrix contains a 1 and the rest of the fourth column
contains zeros, then the resulting vector will also have a 1 in its w component.
Here’s what the fi nal 4 × 4 translation matrix looks like:


When a point or vector is extended from three dimensions to four in this
manner, we say that it has been writt en in homogeneous coordinates. A point in
homogeneous coordinates always has w = 1. Most of the 3D matrix math done
by game engines is performed using 4 × 4 matrices with four-element points
and vectors writt en in homogeneous coordinates.


cos sin 0
[][] sincos0.
0 01

rrr rrrxyz xyz

⎡⎤φφ
′′′= ⎢⎥−φ φ
⎢⎥
⎢⎥⎣⎦

rt+= +[(rt rt rtxxyyzz)(+)(+)].

1000


0 1 00


[1] 0010


1


[( )( )( )1].


xyz

xyz
xxyyzz

rrr

t
rt rt rt

⎡⎤


⎢⎥


+= ⎢⎥


⎢⎥


⎢⎥


⎣⎦


=+ + +


rt

tt
Free download pdf