Today’s post is only a small gem I accidentally came across while I was looking for something entirely different: a faster method of multiplying a quaternion by a vector.
I use quaternion-vector multiplication (rotating a vector by a quaternion) mostly in two places:
- When building the global pose of a skeleton from its local pose, as discussed in this blog post.
- In vertex shaders that are used with instanced rendering, so I only have to send one quaternion (float4) instead of a whole rotation matrix (float3x3).
The canonical way of multiplying a quaternion q by a vector v is given by the following formula:
v' = q * v * conjugate(q)
where the vector v is being treated as a quaternion with w=0, so the above essentially boils down to two quaternion multiplications, which are a bit expensive.
Turns out there is a faster way, which is the following:
t = 2 * cross(q.xyz, v) v' = v + q.w * t + cross(q.xyz, t)
In my SSE2 code path, the new method is about 35% faster than the original. Enjoy, and don’t forget to share this gem with other people!