How do you build the shortest-arc quaternion that rotates one unit direction onto another, and what breaks when the two directions are opposite?
answer
- axis from one product, angle from another
- the half-angle trick
- no inverse cosine needed
- opposite directions, no unique axis
basics
~20 sFor unit directions a and b, normalising the quaternion (1 + dot(a, b), cross(a, b)) gives the shortest-arc rotation; when a and b are opposite both parts vanish, so pick any axis perpendicular to a and rotate 180 degrees.
solid answer
~50 sThe shortest rotation from unit vector `a` to unit vector `b` turns about `cross(a, b)` by the angle whose cosine is `dot(a, b)`. Building `(cos(t/2), sin(t/2)*n)` directly needs an inverse cosine and a half-angle sine, but the half-angle trick avoids both: `(1 + dot(a, b), cross(a, b))` has length `2*cos(t/2)`, so normalising it gives exactly the right quaternion. When `a` and `b` point in opposite directions, `1 + dot` and the cross product are both zero and normalising divides by zero; any axis perpendicular to `a` is a valid answer, so pick one (cross `a` with a world axis it is not parallel to) and return `(0, p)`, a 180-degree turn. The result leaves twist about the target direction free, so applying it each frame lets an aiming turret's up direction drift; constrain the up axis separately when roll matters.
code
pseudocode · 10 linesfunction fromTo(a, b): // a, b unit vectors
d = dot(a, b)
if d < -1 + EPSILON: // opposite: any perpendicular axis works
p = cross(a, (1, 0, 0))
if lengthSquared(p) < EPSILON:
p = cross(a, (0, 1, 0))
p = normalise(p)
return (0, p.x, p.y, p.z) // 180 degrees about p
c = cross(a, b)
return normalise((1 + d, c.x, c.y, c.z))go deeper
Recall that the shortest rotation from one direction to another turns about their cross product by the angle their dot product gives.
Write the half-angle form normalise((1 + dot, cross)) and check that the parallel case returns the identity on its own.
Derive the half-angle form, handle the opposite-direction case with a deliberate perpendicular axis, and explain why per-frame shortest-arc corrections let an aiming head roll.
Provide one vetted from-to and look-rotation pair in the shared math code, with the degenerate cases tested, so gameplay code stops hand-rolling them.
## The question being asked Many gameplay systems need "the rotation that turns this direction into that one": a turret's barrel direction onto the direction to its target, a hover vehicle's up axis onto the ground normal, a projectile's model onto its velocity. Infinitely many rotations map one direction onto another (any of them followed by a spin about the target direction still works). The **shortest-arc** rotation is the one with the smallest angle, and its axis is perpendicular to both directions. Throughout, `a` and `b` are unit vectors and quaternions are written scalar first, `(w, x, y, z)`. ## The direct construction The axis is `n = cross(a, b) / |cross(a, b)|` and the angle `t` satisfies `cos t = dot(a, b)`. The textbook quaternion is then `(cos(t/2), sin(t/2)*n)`. Building it that way costs an inverse cosine, a sine, a cosine and a normalisation of the axis, and it is fragile when `a` and `b` are nearly parallel, because the cross product is tiny and normalising it amplifies rounding error. ## The half-angle trick Notice that `dot(a, b) = cos t` and `|cross(a, b)| = sin t`. Form the quaternion `h = (1 + cos t, sin t * n)`, which is `(1 + dot(a, b), cross(a, b))` and needs no trigonometry. Its length is: 1. `|h|² = (1 + cos t)² + sin² t = 2 + 2 cos t`; 2. using `1 + cos t = 2 cos²(t/2)`, this is `4 cos²(t/2)`, so `|h| = 2 cos(t/2)`; 3. dividing: the scalar becomes `2 cos²(t/2) / (2 cos(t/2)) = cos(t/2)`; 4. the vector becomes `2 sin(t/2) cos(t/2) n / (2 cos(t/2)) = sin(t/2) n`. So `normalise((1 + dot(a, b), cross(a, b)))` is exactly the shortest-arc quaternion. When `a` and `b` coincide it gives `(2, 0, 0, 0)` normalised, the identity, with no special case. If the inputs are not unit length, use `(|a| |b| + dot(a, b), cross(a, b))` instead. The algebra maps `a` onto `b` in either handedness, because handedness changes how the cross product is drawn, not the identities used above. ## The opposite-direction case When `b = -a`, the rotation angle is 180 degrees and **every** axis perpendicular to `a` is a valid shortest arc, so there is no unique answer. The formula shows the problem directly: - `1 + dot(a, b) = 0`; - `cross(a, b) = 0`; - normalising the zero quaternion divides by zero and yields not-a-number values that then spread through the transform hierarchy. The fix is to detect `dot(a, b)` near -1 and choose an axis: cross `a` with a world axis, falling back to a second world axis if `a` is nearly parallel to the first, normalise it to `p`, and return `(0, p)`. The choice is arbitrary, so where the result is visible (a turret flipping round), prefer an axis with meaning, such as the turret's own up axis. ## What shortest arc does not give you | Need | Shortest arc alone | Better tool | |---|---|---| | Point a direction at a target once | yes | from-to rotation | | Keep an aiming head upright over time | no, twist drifts | build the orientation from the target direction and a reference up | | Limit yaw and pitch separately | no, it mixes them | decompose into a swing and a twist, then clamp | | Avoid the 180-degree ambiguity | no | pick the axis deliberately, as above | The drift deserves a sentence: composing many minimal rotations along a curving path of directions accumulates a net spin about the direction itself, so an aiming turret head that applies a fresh shortest-arc correction every frame slowly rolls. When its up axis matters, rebuild the orientation from the target direction and a fixed reference up each frame instead of accumulating corrections.
- Why is the half-angle form more robust than normalising the cross product to get an axis?When `a` and `b` are nearly parallel the cross product is tiny, so normalising it to a unit axis amplifies rounding error into a wild axis. The half-angle form never normalises the cross product alone: the scalar `1 + dot` is close to 2 there, so the final normalise is well conditioned and the result tends smoothly to the identity.
- How would you apply the from-to rotation to a turret that must turn at a limited angular speed?Compute the full shortest-arc quaternion to the target and its angle, `2*acos(w)`. If that exceeds the frame's allowance, slerp from the identity toward it by the ratio of allowance to angle, or nlerp for small steps, and apply that partial rotation about world axes, as `r q` with the sandwich convention.
- The rotation from a to b is unique, so there is nothing to choose.
- The from-to rotation always needs an inverse cosine for the angle.
- Normalising (dot(a, b), cross(a, b)) gives the shortest-arc quaternion.
- When a and b are opposite the formula simply returns the identity.
- Applying shortest-arc corrections each frame keeps an aiming head upright.