Slerp: Turning Smoothly Between Orientations
Slerp blends two orientations by turning about one fixed axis at a steady speed: it's lerp done along the great circle between two unit quaternions instead of straight across. Flip the sign of the end quaternion whenever , and it always takes the short way around.
What the symbols mean
- Start
The starting orientation, a unit quaternion.
- End
The ending orientation, sign chosen so q₀·q₁ ≥ 0.
- Angle between them
The angle between the quaternions as 4D unit vectors; the object itself turns 2Ω.
- Fraction
0 at the start, 1 at the end.
What the symbols mean
- Total turn
The angle of the single rotation from the start orientation to the end one.
Explanation
Slerp, short for spherical linear interpolation, is how you blend between two orientations. Give it a start, an end and a fraction , and it returns the orientation that's that fraction of the way there, turning about a single fixed axis at a perfectly steady speed. It's lerp, but along the surface of a sphere instead of along a straight line. In the formula, and are the start and end quaternions, is the angle between them (picture two arrows from the center of a sphere), and the two fractions with sines play the part that and play in ordinary lerp: how much of each end to mix in. It looks heavier than lerp, but it's the same idea with the sines doing the bending; bear with it.
Quaternions live on a sphere. A unit quaternion has , so it's a point on the unit sphere in 4D. We can't draw 4D, but nothing here uses the fourth dimension: two unit vectors, the angle between them and the plane they span behave the same way in 3D, and that's what the animation further down shows. The angle comes from the dot product, as always: (The Dot Product).
What plain lerp does wrong. Lerp the four numbers, , and you travel the straight chord between the two points, through the inside of the sphere. Halfway along, the length has dropped to , and a quaternion that isn't unit length scales the model by its length squared while turning it. Dividing by the length pushes it back onto the sphere: that's nlerp, normalized lerp. Nlerp follows the right path, but not at the right pace. Equal steps along the chord aren't equal steps along the arc: near the ends the arc barely moves while the chord point slides, and in the middle it's the other way around. So nlerp starts slow, rushes through the middle and slows again. For a turn () it's spinning about faster than steady at the midpoint.
Building slerp. Let's walk along the arc instead. In the plane of and , build a unit vector perpendicular to by removing 's share of (Gram–Schmidt):
Its length is 1, because . Now and act like the - and -axes of that plane, and the point at angle around the circle is
Substitute and collect the terms over :
by the sine subtraction formula, . At it gives and at it gives ; in between the angle from is exactly : constant speed. Ken Shoemake brought this formula to computer animation in 1985, and it's been the standard ever since.
The object turns twice as far as the quaternion. Because of the half angles, two quaternions apart are orientations apart: the relative turn has scalar part , which is of half of . So slerp turns the object about one fixed axis by , at the steady angular speed per unit of . Another way to write the same thing is : "do fraction of the turn that takes to ."
Take the short way. Since and are the same orientation, there are two arcs to choose from, and they're very different. If , then and the object would turn more than , the long way around. The fix is one line: if the dot product is negative, flip the sign of . Then and the turn is at most .
When they're nearly equal. If is tiny, is nearly and dividing by it magnifies rounding errors. But for a tiny the arc and the chord are practically the same, so code switches to nlerp when the dot product is very close to 1:
function slerp(a: Quat, b: Quat, t: number): Quat {
let d = a[0]*b[0] + a[1]*b[1] + a[2]*b[2] + a[3]*b[3];
if (d < 0) { b = [-b[0], -b[1], -b[2], -b[3]]; d = -d; } // the short way
if (d > 0.9995) { // nearly equal: nlerp
const q = a.map((v, i) => v + t * (b[i] - v));
const len = Math.hypot(...q);
return q.map((v) => v / len) as Quat;
}
const om = Math.acos(d), s = Math.sin(om);
const ka = Math.sin((1 - t) * om) / s, kb = Math.sin(t * om) / s;
return [ka*a[0] + kb*b[0], ka*a[1] + kb*b[1], ka*a[2] + kb*b[2], ka*a[3] + kb*b[3]];
}
A common mistake. Forgetting the sign check. Quaternions from a file, a physics engine or a previous blend can come with either sign, and about half the time the dot product is negative. Skip the check and every so often a character's head whips around to look slightly to its left. It's tempting to blame the animation data; it's the missing if (d < 0).
A second common mistake. Treating as the angle the object turns. It's half of it. Two orientations apart have , not .
Euler lerp vs slerp. There's a side-by-side animation further down. Both airplanes start level and both end in the same orientation, flipped end over end. One lerps its Euler angles from (yaw , roll ) to (yaw , roll ); the other slerps, which turns about the wing axis, the single axis that connects the two orientations. The Euler plane takes a twisting detour, swinging around while it rolls, and halfway there it has already turned while the slerp plane has turned exactly (3D Rotation and Euler Angles).
Many keyframes: squad. An animator keys a whole list of orientations . Slerping each neighboring pair works, but at every key the axis and speed change suddenly: the angular velocity jumps, the rotation version of joining line segments at a corner (Joining Curves Smoothly). Shoemake's fix, squad, is to the sphere what a cubic Bézier curve is to the plane. Between and it builds two inner control quaternions and from the neighbors and blends with nested slerps, de Casteljau style (Bézier Curves by Repeated Lerp):
with . The result still passes through every key, but the turning flows smoothly through them instead of changing direction at each one.
See it
Worked examples
Slerp from no turn, , to a turn about at . What orientation do you get?
- 1
Write the end quaternion with the half angle, : .
A character's head has orientation and should turn to . Find the halfway orientation along the short path, and how far the head turns in all.
- 1
Dot product first: .
Practice
Type your answer and check it. Solve them all to complete the lesson.
Two orientations are and . Slerping from one to the other, through what total angle does the object turn, in degrees?
Slerp from to (a turn about ) at . Enter the quaternion .
An animation blends from to . Taking the short path, how many degrees does the object turn in all?
Nlerp from no turn, , to a half turn about , . At , through what angle has the object turned, in degrees? (Slerp would give .)
A cutscene camera blends with slerp from orientation to over s, with increasing steadily from to . What is the component of its orientation s into the blend?
A programmer blends and with a plain lerp at , forgets to normalize and rotates a character's vertices with . By what factor are all distances from the character's center scaled?
Nlerp between two unit quaternions apart moves through the angle from the start, while slerp moves at the steady rate per unit of . At , how many times faster than slerp is nlerp moving, ?
A character's arm is keyed at three orientations, and its motion slerps from the first key to the second, then from the second to the third. What happens at the middle key?
The hardest one here. Get it, and you've got this lesson.
An animation blends from toward by slerp along the short path. At , where does the model's nose, which points along at rest, point? Enter the unit vector.
Where it's used
Slerp is everywhere orientations change over time. Animation systems slerp each joint between keyframes and blend whole poses (walk into run) joint by joint; game engines expose it as a built-in, and most of them quietly use nlerp with a renormalize when the angles are small, since it's cheaper and close enough. Camera systems slerp between shots in cutscenes. Robot arms plan the orientation of their tool by slerp while the position follows a separate path, so the tool turns at a steady rate about one axis. Spacecraft attitude maneuvers are often planned as one "eigenaxis" turn, which is exactly a slerp, because the single-axis turn is the shortest one.