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) { ...