A spinning object: updating q from the turn rate
We know how fast an object turns: from a gyroscope, a physics simulation or a game controller. Every few milliseconds we turn q by that small amount. With quaternions, this update is four short lines with no sin, no cos and no gimbal lock, plus one square root to keep the length at 1.
9.1The equation
The turn rate, or angular velocity, is \(\boldsymbol\omega = (\omega_x, \omega_y, \omega_z)\), measured about the object’s own axes. Its direction is the axis the object spins about right now, and its length is the speed in radians per second.
Where does the update come from? In a short time Δt the object makes a small turn: the angle is \(|\boldsymbol\omega|\,\Delta t\), about the axis along ω. From lesson 4, that small turn is the quaternion
For a very small angle a: cos a ≈ 1 and sin a ≈ a. That is the only approximation we make.
It is a turn about the object’s own axes, so from lesson 7 we multiply it on the right:
We split [1, a] into [1, 0] + [0, a]. Multiplying by [1, 0, 0, 0], no turn, leaves q as it is.
So in each step, q changes by \(\tfrac12\, q \otimes [0, \boldsymbol\omega]\,\Delta t\). Divide by Δt and we get the speed of change of q, which is how books write it:
ω on the right, because these are turns about the object’s own axes (lesson 7).
In code we take one small step every time step Δt, and then normalise:
Written out with the four product lines from lesson 5:
Use the old w, x, y, z on the right side. Compute all four, then assign.
Check with numbers. Start at q = [1, 0, 0, 0] and spin at 90°/s about z, which is 1.571 rad/s. With Δt = 0.01 s, only the z line changes: z = 0 + 0.005 × 1 × 1.571 = 0.00785. The exact answer is a turn of 0.9°, so z = sin 0.45° = 0.00785. It matches.
9.2What the lab shows
- Normalise every step. Normalising means dividing q by its length, so the length is 1 again. Each small step makes the length of q slightly bigger than 1. Without normalising, the length keeps growing and the orientation gets distorted.
- Use a short time step. The step is a straight line, but the true motion is a curve. A short Δt (5 ms, 200 steps per second) keeps the error tiny. The lab starts at 20 ms. A long one makes it big.
- Errors add up. Adding up many small steps is called integration. If the turn rate is slightly wrong, it adds the error up forever. Half a degree per second is 30° per minute. This is called drift.
In a simulation or a game, the rate is exact and there is no drift. With a real gyroscope there always is. That’s why phones, drones and VR headsets add a second reference, such as gravity or a camera, and gently pull q toward it. The tool for that gentle pull is in the next lesson.
The equation needs radians per second. Many sensors and tools give degrees per second, so multiply by π/180 first. If the object turns 57 times too fast, this is the bug.
✓Check yourself
Does this update have a gimbal-lock angle?
No. The update has no tan θ and no division by cos θ, so there is no special angle at all.
The real object does not move, but your q slowly turns about z. What is wrong?
The measured z rate is not exactly zero, and the small error adds up. That’s drift. Subtract the constant error if you know it, and correct the rest with a second reference.
+Sources and further reading
- J. Solà, "Quaternion kinematics for the error-state Kalman filter", 2017
chapter 4: the time derivative of q and how to integrate it