Game Development Reference

In-Depth Information

MakeEulerAnglesFromQ

This function extracts the three Euler angles from a given quaternion.

You can extract the three Euler angles from a quaternion by first converting the qua‐

ternion to a rotation matrix and then extracting the Euler angles from the rotation

matrix. Let
R
be a nine-element rotation matrix:

and let
q
be a quaternion:

q
= [n, x
i
+ y
j
+ z
k
]

Then each element in
R
is calculated from
q
as follows:

r
11
= n
2
+ x
2
− y
2
− z
2

r
21
= 2xy+2zn

r
31
= 2zx − 2yn

r
12
= 2xy − 2zn

r
22
= n
2
− x
2
+ y
2
− z
2

r
32
= 2zy + 2xn

r
13
= 2xz + 2yn

r
23
= 2yz − 2xn

r
33
= n
2
− x
2
− y
2
+ z
2

To extract the Euler angles, yaw (ψ), pitch (τ), and roll (φ), from
R
, you can use these

relations:

tan ψ = r
21
/ r
11

sin τ = -r
31

tan φ = r
32
/ r
33

Here's the code that extracts the three Euler angles, returned in the form of a
Vector
,

from a given quaternion:

inline Vector MakeEulerAnglesFromQ(Quaternion q)

{

double r11, r21, r31, r32, r33, r12, r13;

double q00, q11, q22, q33;

double tmp;

Vector u;