Game Development Reference
In-Depth Information
//=============================================================
//SlerpQuaternions
//start and end are the quaternion key positions. t is a time factor
//between 0 and 1.The result is returned in result
//=============================================================
Void SlerpQuaternions(QUATERNION &start, QUATERNION &end,
double t, QUATERNION &result){
double theta, ct, st, scalestart, scaleend;
//Calculate the cosine using the vector dot product of start
//and end
ct = start.w * end.w + start.x * end.x +
start.y * end.y + start.z * end.z;
//If ct is less than 0 then we need to adjust the signs to
//ensure that we are taking the shortest route.
if ( ct < 0.0){
theta = acos(-ct);
st = sin(theta);
startscale = sin((1.0 -t) * theta) / st;
endscale = sin(t * theta) / st;
result.w = startscale * start.w - endscale * end.w;
result.x = startscale * start.x - endscale * end.x;
result.y = startscale * start.y - endscale * end.y;
result.z = startscale * start.z - endscale * end.z;
}else{
theta = acos(ct);
st = sin(theta);
startscale = sin((1.0 -t) * theta) / st;
endscale = sin(t * theta) / st;
result.w = startscale * start.w + endscale * end.w;
result.x = startscale * start.x + endscale * end.x;
result.y = startscale * start.y + endscale * end.y;
result.z = startscale * start.z + endscale * end.z;
}
}