Game Development Reference
In-Depth Information
Recall that to multiply a quaternion we use the following rules:
q 1 q 2=[ w 1 w 2- v 1• v 2, v 1
×
v 2 + w 1 v 2 + w 2 v 1]
Therefore,
qhqpqb = [cos( h /2)cos( p /2)cos( b /2) - sin( h /2)sin( p /2)sin( b /2),
(cos( h /2)sin( p /2)sin( h /2)sin( p /2) + cos( b /2)cos( p /2)sin( h /2),
cos( b /2)cos( h /2)sin( p /2) - cos( p /2)sin( h /2)sin( h /2)sin( p /2),
cos( b /2)sin( h /2)sin( p /2) + cos( h /2)cos( p /2)sin( b /2))]
Leading to this function:
void EulerAnglesToQuaternion( double h, double p,
double b, QUATERNION &q){
double h, p, b, ch, cp, cb, sh, sp, sb;
h = euler.h / 2.0;
p = euler.p / 2.0;
b = euler.b / 2.0;
ch = cos(h);
cp = cos(p);
cb = cos(b);
sh = sin(h);
sp = sin(p);
sb = sin(b);
q.w = ch * cp * cb - sh * sp * sb;
q.x = ch * sp * sh * sp + cb * cp * sh;
q.y = cb * ch * sp
- cp * sh * sh * sp;
q.z = cb * sh * sp
+ ch * cp * sb;
}
Now to interpolate a rotation we use spherical linear interpolation or
SLERP. The beauty of using quaternions is that the number of
calculations used for the interpolation of rotations is significantly reduced.
The code necessary to take the quaternions generated by Euler-
AnglesToQuaternions, and create an interpolated path, is as follows: