158 4. 3D Math for Games
The matrix below represents rotation about the y-axis by an angle θ. Notice
that this one is transposed relative to the other two—the positive and negative
sine terms have been refl ected across the diagonal:
This matrix represents rotation about the z-axis by an angle γ:
Here are a few observations about these matrices:
z The 1 within the upper 3 × 3 always appears on the axis we’re rotating
about, while the sine and cosine terms are off -axis.
z Positive rotations go from x to y (about z), from y to z (about x), and from
z to x (about y). The z to x rotation “wraps around,” which is why the
rotation matrix about the y-axis is transposed relative to the other two.
(Use the right-hand or left -hand rule to remember this.)
z The inverse of a pure rotation is just its transpose. This works because
inverting a rotation is equivalent to rotating by the negative angle. You
may recall that cos(–θ) = cos(θ) while sin(–θ) = –sin(θ), so negating the
angle causes the two sine terms to eff ectively switch places, while the
cosine terms stay put.
4.3.7.3. Scale
The following matrix scales the point r by a factor of sx along the x-axis, sy
along the y-axis, and sz along the z-axis:
cos 0 sin 0
01 0 0
rotate ( , ) [ 1] .sin 0 cos 0
00 0 1
y rrrxyz
⎡⎤θ −θ
⎢⎥
θ= ⎢⎥
⎢⎥θθ
⎢⎥
⎣⎦
r
cos sin 0 0
sin cos 0 0
rotate ( , ) [ 1]. 0 0 10
0 0 01
z rrrxyz
⎡⎤γγ
⎢⎥−γ γ
γ= ⎢⎥
⎢⎥
⎢⎥
⎣⎦
r
0 00
0 00
[1]
00 0
0 0 01
[ 1].
x
y
xyz z
xxyyzz
s
s
rrr
s
sr sr sr
⎡⎤
⎢⎥
= ⎢⎥
⎢⎥
⎢⎥
⎣⎦
=
rS