Unit quaternions
Quaternions can be normalized just like vectors. Normalized quaternions represent only a rotation and non-normalized quaternions introduce a skew. In the context of game animation, quaternions should be normalized to avoid adding a skew to the transform.
To normalize a quaternion, divide each component of the quaternion by its length. The resulting quaternion's length will be 1. This can be implemented as follows:
- Implement the
normalize
function inquat.cpp
and declare it inquat.h
:void normalize(quat& q) { Â Â float lenSq = q.x*q.x + q.y*q.y + q.z*q.z + q.w*q.w; Â Â if (lenSq < QUAT_EPSILON) { Â Â Â Â Â Â return; Â Â } Â Â float i_len = 1.0f / sqrtf(lenSq); Â Â q.x *= i_len; Â Â q.y *= i_len; Â Â q.z *= i_len; Â Â q.w *= i_len; }
- Implement the
normalized
function inquat.cpp
, and declare it inquat.h
:quat normalized(const quat& q) { Â ...