skip to content

A turret slerps toward its target and sometimes swings almost 360 degrees the long way round; how do quaternion double cover and a dot-product check explain and fix it?

level: seniorimportance: should knowfreq 58%

answer

  1. two quaternions per orientation
  2. the angle wraps at plus or minus 180
  3. sign of the four-component dot product
  4. negate one endpoint

basics

~20 s

Every rotation has two unit quaternions, q and -q. When the turret's current and target quaternions have a negative dot product, slerp takes the arc of more than 180 degrees; negating the target first restores the short turn.

solid answer

~50 s

Unit quaternions **double cover** rotations: `q` and `-q` give the same sandwich `q v q*`, so each orientation has two representatives. Slerp interpolates on the quaternion sphere, where an arc angle `W` means a rotation of `2W`, so if `dot(q0, q1) < 0` the arc exceeds 90 degrees and the turret turns more than 180 degrees. The bug appears when the target is built from an angle that wraps: current heading 170 degrees, target found by a two-argument arctangent as -170 degrees; the target quaternion is the negative of the one for 190 degrees, the dot product is about -0.98, and slerp sweeps 340 degrees. The fix is one line: if the dot product is negative, negate `q1` (and the dot) before interpolating. The same check belongs in nlerp and in any measure of the angle between orientations, which is `2*acos(|dot|)`.

code

pseudocode · 10 lines
pseudocode
function slerp(q0, q1, t):
    d = dot(q0, q1)
    if d < 0:                      // q1 and -q1 are the same rotation;
        q1 = -q1                   // take the copy on q0's side
        d = -d
    if d > 1 - EPSILON:            // nearly equal: sin(W) is tiny
        return normalise((1 - t) * q0 + t * q1)
    w = acos(d)                    // arc angle, at most 90 degrees here
    s = sin(w)
    return (sin((1 - t) * w) / s) * q0 + (sin(t * w) / s) * q1

go deeper

for a junior

Recall that q and -q are the same rotation and that a negative dot product means the interpolation goes the long way.

for a middle

Show why a quaternion arc of W is a rotation of 2W, and put the sign check into both slerp and nlerp.

for a senior

Diagnose the intermittent long swing from its symptom, tie it to an angle that wraps at plus or minus 180, and fix the angle measure with the absolute value as well.

for a principal

Make the sign check part of the shared interpolation routine rather than every caller, so no gameplay code can interpolate orientations without it.

## Two quaternions for every orientation A unit quaternion `q = (w, x, y, z)` (scalar first) rotates a vector by the sandwich `q v q*`. Negating every component changes nothing, because the two minus signs cancel: `(-q) v (-q)* = q v q*`. So every orientation has **two** unit quaternions, `q` and `-q`, on opposite sides of the four-dimensional unit sphere. This is called **double cover**. Geometrically, `-q` is a rotation by `360 - a` degrees about the reversed axis, which ends in the same place as a rotation by `a` about the original axis. Double cover is harmless for composing and applying rotations. It matters for **interpolation**, because slerp and nlerp move through quaternion space, and the two copies of the target are not equally far from the start. ## Why the turret takes the long way On the quaternion sphere, the arc angle `W` between `q0` and `q1` satisfies `cos(W) = dot(q0, q1)`, and the rotation it represents is `2W`. Consequently: - a **positive** dot product means `W` is under 90 degrees, a rotation under 180 degrees, the short way; - a **negative** dot product means `W` is over 90 degrees, a rotation over 180 degrees, the long way; - a dot product of exactly zero is a 180-degree turn, where both ways are equally long. The turret's code typically builds the target from an angle, for example a heading found with a two-argument arctangent, which wraps from +180 to -180 degrees. Take a y-up turret with the current heading 170 degrees and a target just across its back at 190 degrees, which the arctangent reports as -170 degrees: 1. Current: `q0 = (cos 85, 0, sin 85, 0)`, about `(0.087, 0, 0.996, 0)`. 2. Target from -170 degrees: `q1 = (cos -85, 0, sin -85, 0)`, about `(0.087, 0, -0.996, 0)`. 3. `dot(q0, q1) = cos 85 * cos 85 - sin 85 * sin 85 = cos 170`, about -0.985. 4. The arc is about 170 degrees on the sphere, so slerp turns the turret about 340 degrees. 5. Negating `q1` gives `(-0.087, 0, 0.996, 0)`, the quaternion for +190 degrees. The dot product becomes about +0.985, the arc 10 degrees, and the turret turns the 20 degrees it should. The symptom is intermittent because it appears only when the target crosses the wrap point, which is why it often survives testing. ## The fix Before interpolating, compare signs and flip: - if `dot(q0, q1) < 0`, use `-q1` (and `-dot`) instead; - apply the check in **nlerp** too, since it shares the arc; - measure the angle between orientations as `2*acos(|dot(q0, q1)|)`, with the absolute value, or a turn-rate limit will think a 20-degree turn is 340 degrees; - when a sequence of stored orientations is interpolated pair by pair, make each one agree in sign with its predecessor once, up front. ## Related traps | Symptom | Cause | Fix | |---|---|---| | Swing of more than 180 degrees through slerp | negative quaternion dot product | negate one endpoint | | Swing of more than 180 degrees through angle lerp | lerping 170 to -170 numerically | wrap the difference into the range -180 to 180 first | | Turn-rate limiter stalls or overshoots | angle computed without the absolute value | use `2*acos(abs(dot))` | | Short turn chosen at random near the back | dot product hovering around zero | expected at exactly 180 degrees; add hysteresis if it flickers | The common thread is that rotation angles wrap and quaternions double up, so "the difference between two orientations" always needs a choice of representative before it is interpolated.

  • Does negating one quaternion change the orientation the turret ends at?
    No. `q` and `-q` produce the same sandwich product, so the end orientation is identical. Only the path taken by the interpolation changes, from the arc of more than 180 degrees to the arc of less than 180 degrees.
  • What happens when the turret's target is almost exactly behind it?
    The dot product hovers near zero and both ways round are nearly equally long, so the chosen direction can flip between frames as the target jitters. That is correct behaviour for each frame, but if it looks wrong, add hysteresis: keep the previous turn direction until the other way is clearly shorter.

Averaging compass bearings of 350 and 10 degrees as plain numbers gives 180, due south, although both point roughly north; the numbers must be put on the same side of the wrap before you blend them.

  • A negative dot product means the quaternions need normalising.
  • Negating a quaternion produces the inverse rotation.
  • Slerp always takes the shortest path between orientations without any check.
  • The angle between orientations is acos of the quaternion dot product.
  • Switching from slerp to nlerp removes the long-way problem.