Game Development Reference
In-Depth Information
/**
* Holds the third complex component of the quaternion.
*/
real k;
};
/**
* Holds the quaternion data in array form.
*/
real data[4];
};
};
9.4.8
N ORMALIZING Q UATERNIONS
As we saw in the earlier discussion, quaternions only represent a rotation if they have a
magnitude of 1. All the operations we will be performing keep the magnitude at 1, but
numerical inaccuracies and rounding errors can cause this constraint to be violated
over time. Once in a while it is a good idea to renormalize the quaternion. We can
perform this with the following method:
Excerpt from include/cyclone/core.h
/**
* Holds a three degree of freedom orientation.
*/
class Quaternion
{
// ... other Quaternion code as before ...
/**
* Normalizes the quaternion to unit length, making it a valid
* orientation quaternion.
*/
void normalize()
{
real d = r*r+i*i+j*j+k*k;
// Check for zero length quaternion, and use the no-rotation
// quaternion in that case.
if (d == 0) {
r=1;
return;
}