Quaternion Inverse
If a quaternion is multiplied by it's inverse, the result is no rotation \(0, 0, 0, 1\). To figure out what this multiplicitive inverse is, remember that a quaternion represents some rotation around some axis. To undo that rotation, we can either negate the axis, or negate the rotation.
Negating the rotation means rotating about the same axis, just by a negative ammount. Negating the axis on the other hand rotates the same ammount, but the plane is flipped. It's convention to negate the axis, but there is one potential problem: finding the inverse of a quaternion by negating the axis of rotation only works for unit quaternions. Negating the axis actually results in the conjugate of the quaternion, often denoted as \(q^{*}\).
Quaternion Conjugate(Quaternion quat) { return Quaternion( quat.x * -1.0, quat.y * -1.0, quat.z * -1.0, quat.w ); }
Finding the proper inverse (\(q^{1}\)) of a quaternion is similar to finding it's conjugate. To find the inverse, divide each component of the quaternions conjugate by it's norm. The norm of a quaternion is it's squared length, or dot product with its self. The norm can be expressed as \(q \cdot q\), or \(q_x^2 + q_y^2 + q_z^2 + q_w^2\).
Quaternion Inverse(Quaternion quat) { float norm = quat.x * quat.x + quat.y * quat.y + quat.z * quat.z + quat.w * quat.w; float recip = 1.0 / norm; return Quaternion( quat.x * -1.0 * recip, quat.y * -1.0 * recip, quat.z * -1.0 * recip, quat.w * recip ); }
Games almost always unit length quaternions, when using unit quaternions the length and conjugate are the same \(q^{-1} = q^{*}\). When working with games, the two are usually treated interchangeably.