Game Development Reference

In-Depth Information

Here, ~
q
is the conjugate of the unit quaternion
q
. Here's the code:

inline Quaternion QRotate(Quaternion q1, Quaternion q2)

{

return q1*q2*(~q1);

}

QVRotate

This function rotates the vector
v
by the unit quaternion
q
according to the formula:

p
' = (
q
)(
v
)(~
q
)

Here, ~
q
is the conjugate of the unit quaternion
q
. Here's the code:

inline Vector QVRotate(Quaternion q, Vector v)

{

Quaternion t;

t = q*v*(~q);

return t.GetVector();

}

MakeQFromEulerAngles

This function constructs a quaternion from a set of Euler angles.

For a given set of Euler angles, yaw (ψ), pitch (τ), and roll (φ), defining rotation about

the z-axis, then the y-axis, and then the z-axis, you can construct the representative

rotation quaternion. You do this by first constructing a quaternion for each Euler angle

and then multiplying the three quaternions following the rules of quaternion multipli‐

cation. Here are the three quaternions representing each Euler rotation angle:

q
roll
= [cos(φ/2), (sin(φ/2))
i
+ 0
j
+ 0
k
]

q
pitch
= [cos(τ /2), 0
i
+ (sin(τ /2))
j
+ 0
k
]

q
yaw
= [cos(ψ /2), 0
i
+ 0
j
+ (sin(ψ /2))
k
]

Each one of these quaternions is of unit length.
2

Now you can multiply these quaternions to obtain a single one that represents the ro‐

tation, or orientation, defined by the three Euler angles:

q
=
q
yaw
q
pitch
q
roll

2. You can verify this by recalling the trigonometric relation cos
2
θ + sin
2
θ = 1.