Game Development Reference
In-Depth Information
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 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;
}
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: