152 4. 3D Math for Games
or vectors to which it is applied. This is particularly useful in game program-
ming, because we can precalculate a single matrix that performs a whole se-
quence of transformations and then apply all of those transformations to a
large number of vectors effi ciently.
To calculate a matrix product, we simply take dot products between the
rows of the nA × mA matrix A and the columns of the nB × mB matrix B. Each dot
product becomes one component of the resulting matrix P. The two matrices
can be multiplied as long as the inner dimensions are equal (i.e., mA = nB). For
example, if A and B are 3 × 3 matrices, then
P = AB,
Matrix multiplication is not commutative. In other words, the order in
which matrix multiplication is done matt ers:
AB ≠ BA.
We’ll see exactly why this matt ers in Section 4.3.2.
Matrix multiplication is oft en called concatenation, because the product
of n transformation matrices is a matrix that concatenates, or chains together,
the original sequence of transformations in the order the matrices were mul-
tiplied.
4.3.2. Representing Points and Vectors as Matrices
Points and vectors can be represented as row matrices (1 × n) or column matrices
(n × 1), where n is the dimension of the space we’re working with (usually 2 or
3). For example, the vector v = (3, 4, –1) can be writt en either as
or as
The choice between column and row vectors is a completely arbitrary
one, but it does aff ect the order in which matrix multiplications are writt en.
This happens because when multiplying matrices, the inner dimensions of the
two matrices must be equal, so:
11 row1 col1
21 row 2 col1
31 row 3 col1
;
;
;
P
P
P
=⋅
=⋅
=⋅
AB
AB
AB
12 row1 col 2
22 row 2 col 2
32 row 3 col 2
;
;
;
P
P
P
=⋅
=⋅
=⋅
AB
AB
AB
13 row1 col 3
23 row 2 col 3
33 row 3 col 3
;
;
.
P
P
P
=⋅
=⋅
=⋅
AB
AB
AB
v 1 =−[3 4 1] ,
21 T
3
4.
1
⎡⎤
==⎢⎥
⎢⎥
⎢⎥⎣⎦−
vv