Game Development Reference

In-Depth Information

The only remaining task is to generate a matrix from the new

quaternion. For this we can use the knowledge that the rotation matrix will

look like this:

1-2
y
2
-2
z
2

2
xy
+2
wz

2
xz
-2
wy

2
xy
-2
wz

1-2
x
2
-2
z
2

2
yz
-2
wx

2
xz
+2
wy

2
yz
+2
wx

1-2
x
2
-2
y
2

R
=

The code for converting a quaternion into a rotation matrix is therefore

given by this function:

//=============================================================

//QuaternionToMatrix

//Takes quaternion q and generates a rotation matrix stored in the

//three vectors right, up and forward

//=============================================================

void QuaternionToMatrix(QUATERNION &q,

VECTOR &right, VECTOR &up, VECTOR
↵

&forward){

double xx, yy, zz, xy, wz, xz, wy, yz, wx;

xx = q.x * q.x; yy = q.y * q.y; zz = q.z * q.z;

xy = q.x * q.y; wz = q.w * q.z; xz = q.x * q.z;

wy = q.w * q.y; yz = q.y * q.z; wx = q.w * q.x;

right.x = 1 - 2 * yy - 2 * zz;

right.y = 2 * xy - 2 * wz;

right.z = 2 * xz + 2 * wy;

up.x = 2 * xy + 2 * wz;

up.y = 1 - 2 * xx - 2 * zz;

up.z = 2 * yz - 2 * wx;

forward.x = 2 * xz - 2 * wy;

forward.y = 2 * yz + 2 * wx;

forward.z = 1 - 2 * xx - 2 * yy;

}

Try out the sample for this chapter, which illustrates the difference

between a rotation done using Hermite interpolation of the Euler angles

Search Nedrilad ::

Custom Search