Game Development Reference

In-Depth Information

It is important to realize that matrix multiplication of all kinds is not commu-

tative; in general
ab

=

ba
. In particular, to multiply two matrices the number of

columns in the first matrix needs to be the same as the number of rows in the second.

So if we wanted to do

v
M

where
M
is a 3

3matrixand
v
is a three-element vector, we would have a mis-

match. The vector has one column, and the matrix has three rows. We cannot perform

this multiplication: it is undefined. Some game engines do use a pre-multiplication

scheme, but they do so by treating vectors as having one row and three columns,

×

[
xyz
]

rather than the column form we have used. With a row vector we can perform

pre-multiplication, but not post-multiplication. Confusingly, I have also seen pre-

multiplication mathematics written with the vector after the matrix (i.e., a matrix

and then a row vector), so it's worth taking care if you are working with existing code.

I will use post-multiplication and column vectors exclusively in this topic. If you are

working with an engine that uses pre-multiplication, you will have to adapt the order

of your code accordingly.

The result of matrix multiplication is a new matrix with the same number of

rows as the first matrix in the multiplication, and the same number of columns as the

second. So, if we multiply a 3

1 vector, we get a matrix with three

rows and one column (i.e., another vector). If we multiply a 3

×

3matrixbya3

×

×

3 matrix by another

3

3matrix.

If we are multiplying matrices
A
and
B
to give matrix
C
, each element in
C
is

found by the formula

×

3matrix,weendupwitha3

×

C
(i,j)
=

A
(i,k)
B
(k,j)

k

where
C
(i,j)
is the entry in matrix
C
at the
i
th row and the
j
th column, and where
k

ranges up to the number of columns in the first matrix (i.e., the number of rows in

the second—this is why they need to be the same).

For a 3

×

3 matrix multiplied by a vector we get

⎡

⎣

⎤

⎦

⎡

⎣

⎤

⎦
=

⎡

⎣

⎤

⎦

abc

def

ghi

x

y

z

ax

+

by

+

cz

dx

+

ey

+

fz

gx

+

hy

+

iz

With this result we can implement multiplication of a vector by a matrix. I have over-

loaded the
*
operator for the
matrix
class to perform this.

Excerpt from include/cyclone/core.h

/**

* Holds an inertia tensor, consisting of a 3x3 row-major matrix.