Game Development Reference

In-Depth Information

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

Performing this multiplication yields:

q
= [{cos(φ/2) cos(τ /2) cos(ψ /2) + sin(φ/2) sin(τ /2) sin(ψ /2)},

{sin(φ/2) cos(τ /2) cos(ψ /2) − cos(φ/2) sin(τ /2) sin(ψ /2)}
i
+

{cos(φ/2) sin(τ /2) cos(ψ /2) + sin(φ/2) cos(τ /2) sin(ψ /2)}
j
+

{cos(φ/2) cos(τ /2) sin(ψ /2) − sin(φ/2) sin(τ /2) cos(ψ /2)}
k
]

Here's the code that takes three Euler angles and returns a quaternion:

inline Quaternion MakeQFromEulerAngles(float x, float y, float z)

{

Quaternion q;

double roll = DegreesToRadians(x);

double pitch = DegreesToRadians(y);

double yaw = DegreesToRadians(z);

double cyaw, cpitch, croll, syaw, spitch, sroll;

double cyawcpitch, syawspitch, cyawspitch, syawcpitch;

cyaw = cos(0.5f * yaw);

cpitch = cos(0.5f * pitch);

croll = cos(0.5f * roll);

syaw = sin(0.5f * yaw);

spitch = sin(0.5f * pitch);

sroll = sin(0.5f * roll);

cyawcpitch = cyaw*cpitch;

syawspitch = syaw*spitch;

cyawspitch = cyaw*spitch;

syawcpitch = syaw*cpitch;

q.n = (float) (cyawcpitch * croll + syawspitch * sroll);

q.v.x = (float) (cyawcpitch * sroll - syawspitch * croll);

q.v.y = (float) (cyawspitch * croll + syawcpitch * sroll);

q.v.z = (float) (syawcpitch * croll - cyawspitch * sroll);

return q;

}

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