Game Development Reference

In-Depth Information

q00 = q.n * q.n;

q11 = q.v.x * q.v.x;

q22 = q.v.y * q.v.y;

q33 = q.v.z * q.v.z;

r11 = q00 + q11 - q22 - q33;

r21 = 2 * (q.v.x*q.v.y + q.n*q.v.z);

r31 = 2 * (q.v.x*q.v.z - q.n*q.v.y);

r32 = 2 * (q.v.y*q.v.z + q.n*q.v.x);

r33 = q00 - q11 - q22 + q33;

tmp = fabs(r31);

if(tmp > 0.999999)

{

r12 = 2 * (q.v.x*q.v.y - q.n*q.v.z);

r13 = 2 * (q.v.x*q.v.z + q.n*q.v.y);

u.x = RadiansToDegrees(0.0f); //roll

u.y = RadiansToDegrees((float) (-(pi/2) * r31/tmp)); // pitch

u.z = RadiansToDegrees((float) atan2(-r12, -r31*r13)); // yaw

return u;

}

u.x = RadiansToDegrees((float) atan2(r32, r33)); // roll

u.y = RadiansToDegrees((float) asin(-r31)); // pitch

u.z = RadiansToDegrees((float) atan2(r21, r11)); // yaw

return u;

}

Quaternions in 3D Simulators

The quaternion operations just presented are required when you are using quaternions

to represent orientation in 3D simulations. All the 3D simulations discussed in this topic

use these quaternion operations, and in this section we'll highlight where they are used

in the context of the airplane example presented in
Chapter 15
.

When initializing the orientation of the airplane, you have to set its orientation qua‐

ternion to something corresponding to the Euler angles you desire. You do so as follows:

Airplane.qOrientation = MakeQFromEulerAngles(iRoll, iPitch, iYaw);

In this code sample,
Airplane
is a rigid-body class with the property
qOrientation
,

which represents the orientation quaternion, which is a
Quaternion
class.
iRoll
,

iPitch
, and
iYaw
are the three Euler angles describing the orientation of the airplane.

If at any time you want to report the Euler angles—for example, in a heads-up display-

like interface for the game player—you can use
MakeEulerAnglesFromQ
, as follows: