Rotating a vector: the q v q* sandwich
We have a vector, for example a point on a 3D model or the direction a camera looks, and a quaternion. How do we turn the vector? We put it in a sandwich: q on the left, the vector in the middle, q* on the right.
6.1The sandwich
First we write the vector as a quaternion with zero real part, \(v = [0, v_x, v_y, v_z]\). We call it a pure quaternion. Then:
The result has w = 0 again. Its x, y, z part is the turned vector.
6.2Why two multiplications
Let’s do one by hand. Take q = 90° about z and the vector v = x axis. In i, j, k form: \(q = \frac{1}{\sqrt2}(1 + k)\), \(q^* = \frac{1}{\sqrt2}(1 - k)\) and \(v = i\). We only need the table from lesson 5.
x became y: a quarter turn about z. After the first step we were only halfway, at 45°.
Now a vector on the axis itself, v = k. A turn about z must not move it:
So one multiplication alone is not enough. It turns the part of v that is perpendicular to the axis by only half the angle, and it pushes the part along the axis into w. The second multiplication, by q*, adds the other half of the turn and brings the leaked part back. That’s why q holds θ/2, as we saw in lesson 4.
Try the "x" vector with the "z" axis: v is perpendicular to the axis, and w stays 0 even after the first step. With the tilted vector, part of v lies along the axis, and that part creates the leak. That’s the part the second multiplication removes.
6.3A faster formula for code
Two full quaternion products cost 32 multiplications. If we expand the sandwich once on paper, we get the same result with fewer operations. With \(\mathbf u = (x, y, z)\), the vector part of q:
This is what our C library uses in quat_rotate.
6.4Object frame to world, and back
A frame is just a set of three axes: the object’s own axes, or the world’s. The orientation q is the turn that takes the object from its start pose, where its axes lie on the world axes, to where it is now. So we have two directions:
- Object → world: \(v_{world} = q \otimes v_{object} \otimes q^*\). Example: where does the object’s arrow point in the world?
- World → object: \(v_{object} = q^* \otimes v_{world} \otimes q\). Example: the light comes from above in the world, so from which side does the object see it?
Mixing up the two directions is a very common bug. So always check with one easy case: a +90° turn about z must take the object’s x axis to the world’s y axis. If you get −y, you used the wrong direction.
✓Check yourself
q = [0.707, 0, 0, 0.707] (yaw 90°). Turn v = (1, 0, 0) from the object frame to the world.
(0, 1, 0). The x axis of the object now points along the world’s y.
Does rotating change the length of a vector?
No. A unit quaternion only turns, it never stretches. If the lengths change, q does not have length 1.
+Sources and further reading
- J. Solà, "Quaternion kinematics for the error-state Kalman filter", 2017
chapter 2: the rotation formula and why it needs q on both sides - G. Sanderson and B. Eater, "Visualizing quaternions: an explorable video series"
the same idea shown as two half turns