170 4. 3D Math for Games
by the sine of the half-angle of the rotation. The scalar part qS is the cosine of
the half-angle. So the unit quaternion q can be writt en as follows:
where a is a unit vector along the axis of rotation, and θ is the angle of rota-
tion. The direction of the rotation follows the right-hand rule , so if your thumb
points in the direction of a, positive rotations will be in the direction of your
curved fi ngers.
Of course, we can also write q as a simple four-element vector:
A unit quaternion is very much like an axis+angle representation of a ro-
tation (i.e., a four-element vector of the form [ a θ ]). However, quaternions
are more convenient mathematically than their axis+angle counterparts, as we
shall see below.
4.4.2. Quaternion Operations
Quaternions support some of the familiar operations from vector algebra,
such as magnitude and vector addition. However, we must remember that the
sum of two unit quaternions does not represent a 3D rotation, because such a
quaternion would not be of unit length. As a result, you won’t see any quater-
nion sums in a game engine, unless they are scaled in some way to preserve
the unit length requirement.
4.4.2.1. Quaternion Multiplication
One of the most important operations we will perform on quaternions is that
of multiplication. Given two quaternions p and q representing two rotations P
and Q, respectively, the product pq represents the composite rotation (i.e., ro-
tation Q followed by rotation P). There are actually quite a few diff erent kinds
of quaternion multiplication, but we’ll restrict this discussion to the variety
used in conjunction with 3D rotations, namely the Grassman product. Using
this defi nition, the product pq is defi ned as follows:
pq=⎡⎤⎣⎦(pqS Vq+ +×S Vp p qV V)(pqSS−⋅pqV V).
22
q[ ]
[ sin cos ] ,
VSq
θθ
=
=
q a 2 2 2 2
q[ ], where
sin ,
sin ,
sin ,
cos.
xyzw
x Vx x
y Vy y
z Vz z
wS
qqqq
qq a
qq a
qq a
qq
θ
θ
θ
θ