Slerp
Smoothly turning from one orientation to another (a camera blending between two shots, a character turning to face a target, an animation blending two poses) needs rotation interpolation. The standard tool is slerp, spherical linear interpolation: it turns at a constant angular speed along the shortest arc between two quaternions.
Why not lerp the components?
Linearly interpolating the four numbers and renormalizing (nlerp) does follow the right arc, but not at a constant speed: it moves slowly at the ends and quickly in the middle, more so for large angles. Slerp fixes the speed by interpolating the angle itself:
Ω = acos(q0 · q1) the angle between the quaternions (half the rotation angle)
slerp(q0, q1, t) = q0 * sin((1-t)Ω)/sin Ω + q1 * sin(tΩ)/sin ΩTwo practical details make it robust:
- If
q0 · q1 < 0, negateq1. Thanks to the double cover,-q1is the same rotation, and this picks the short way round instead of a 300° detour. - If the angle is tiny,
sin Ωis near 0. Fall back to nlerp there.
#include <iostream>
#include <iomanip>
#include <cmath>
#include <numbers>
struct Quat { double w, x, y, z; };
Quat operator*(Quat q, double s) { return {q.w * s, q.x * s, q.y * s, q.z * s}; }
Quat operator+(Quat a, Quat b) { return {a.w + b.w, a.x + b.x, a.y + b.y, a.z + b.z}; }
double dot(Quat a, Quat b) { return a.w * b.w + a.x * b.x + a.y * b.y + a.z * b.z; }
Quat normalize(Quat q) { return q * (1 / std::sqrt(dot(q, q))); }
Quat nlerp(Quat a, Quat b, double t) {
if (dot(a, b) < 0) b = b * -1;
return normalize(a * (1 - t) + b * t);
}
Quat slerp(Quat a, Quat b, double t) {
double d = dot(a, b);
if (d < 0) { b = b * -1; d = -d; } // take the short way round
if (d > 0.9995) return nlerp(a, b, t); // nearly identical: avoid dividing by ~0
double omega = std::acos(d);
double s = std::sin(omega);
return a * (std::sin((1 - t) * omega) / s) + b * (std::sin(t * omega) / s);
}
Quat yaw(double deg) { double h = deg * std::numbers::pi / 360; return {std::cos(h), 0, std::sin(h), 0}; }
double yawOf(Quat q) { return 2 * std::atan2(q.y, q.w) * 180 / std::numbers::pi; }
int main() {
std::cout << std::fixed << std::setprecision(2);
Quat a = yaw(0), b = yaw(160);
std::cout << " t slerp nlerp\n";
for (int i = 0; i <= 4; i++) {
double t = i / 4.0;
std::cout << std::setw(5) << t << std::setw(8) << yawOf(slerp(a, b, t))
<< std::setw(8) << yawOf(nlerp(a, b, t)) << "\n";
}
} t slerp nlerp
0.00 0.00 0.00
0.25 40.00 34.48
0.50 80.00 80.00
0.75 120.00 125.52
1.00 160.00 160.00
Slerp hits 40°, 80°, 120°: equal steps. Nlerp lags at the start and rushes in the middle. For small per-frame steps the difference is invisible, which is why animation systems often use the cheaper nlerp when blending poses, and slerp when the angle can be large.
The short way round
Interpolating yaw 350° to yaw 10° with Euler angles goes through 180°. With quaternions plus the sign check, it goes through 0°, a 20° turn:
#include <iostream>
#include <iomanip>
#include <cmath>
#include <numbers>
struct Quat { double w, x, y, z; };
Quat operator*(Quat q, double s) { return {q.w * s, q.x * s, q.y * s, q.z * s}; }
Quat operator+(Quat a, Quat b) { return {a.w + b.w, a.x + b.x, a.y + b.y, a.z + b.z}; }
double dot(Quat a, Quat b) { return a.w * b.w + a.x * b.x + a.y * b.y + a.z * b.z; }
Quat normalize(Quat q) { return q * (1 / std::sqrt(dot(q, q))); }
Quat slerp(Quat a, Quat b, double t, bool shortest) {
double d = dot(a, b);
if (shortest && d < 0) { b = b * -1; d = -d; }
if (std::fabs(d) > 0.9995) return normalize(a * (1 - t) + b * t);
double o = std::acos(d), s = std::sin(o);
return a * (std::sin((1 - t) * o) / s) + b * (std::sin(t * o) / s);
}
Quat yaw(double deg) { double h = deg * std::numbers::pi / 360; return {std::cos(h), 0, std::sin(h), 0}; }
double yawOf(Quat q) {
double d = 2 * std::atan2(q.y, q.w) * 180 / std::numbers::pi; // -360..360
d = std::fmod(d + 360, 360);
return d >= 359.995 ? 0 : d; // print 0.00, not 360.00
}
int main() {
std::cout << std::fixed << std::setprecision(2);
Quat a = yaw(350), b = yaw(10);
std::cout << " t shortest without sign check\n";
for (int i = 0; i <= 4; i++) {
double t = i / 4.0;
std::cout << std::setw(5) << t << std::setw(10) << yawOf(slerp(a, b, t, true))
<< std::setw(12) << yawOf(slerp(a, b, t, false)) << "\n";
}
} t shortest without sign check
0.00 350.00 350.00
0.25 355.00 265.00
0.50 0.00 180.00
0.75 5.00 95.00
1.00 10.00 10.00
Turning toward a target at a fixed speed
slerp(current, target, t) with a fixed t each frame is a common mistake (the Damping lesson explains why). For "turn at most 180° per second", engines provide rotate towards: compute the angle between the two rotations and slerp by min(1, maxStep / angle). Unity calls it Quaternion.RotateTowards, Unreal has FMath::QInterpConstantTo.
#include <iostream>
#include <iomanip>
#include <cmath>
#include <algorithm>
#include <numbers>
struct Quat { double w, x, y, z; };
Quat operator*(Quat q, double s) { return {q.w * s, q.x * s, q.y * s, q.z * s}; }
Quat operator+(Quat a, Quat b) { return {a.w + b.w, a.x + b.x, a.y + b.y, a.z + b.z}; }
double dot(Quat a, Quat b) { return a.w * b.w + a.x * b.x + a.y * b.y + a.z * b.z; }
Quat normalize(Quat q) { return q * (1 / std::sqrt(dot(q, q))); }
Quat slerp(Quat a, Quat b, double t) {
double d = dot(a, b);
if (d < 0) { b = b * -1; d = -d; }
if (d > 0.9995) return normalize(a * (1 - t) + b * t);
double o = std::acos(d), s = std::sin(o);
return a * (std::sin((1 - t) * o) / s) + b * (std::sin(t * o) / s);
}
double angleBetween(Quat a, Quat b) { // full rotation angle in radians
return 2 * std::acos(std::min(1.0, std::fabs(dot(a, b))));
}
Quat rotateTowards(Quat from, Quat to, double maxRadians) {
double angle = angleBetween(from, to);
if (angle < 1e-9) return to;
return slerp(from, to, std::min(1.0, maxRadians / angle));
}
Quat yaw(double deg) { double h = deg * std::numbers::pi / 360; return {std::cos(h), 0, std::sin(h), 0}; }
int main() {
std::cout << std::fixed << std::setprecision(1);
const double toDeg = 180 / std::numbers::pi;
Quat current = yaw(0), target = yaw(100);
const double maxSpeed = 180 / toDeg, dt = 0.125; // 180 deg/s, 8 FPS to keep it short
for (int frame = 1; frame <= 6; frame++) {
current = rotateTowards(current, target, maxSpeed * dt);
std::cout << "frame " << frame << ": " << angleBetween(yaw(0), current) * toDeg
<< " deg turned, " << angleBetween(current, target) * toDeg << " deg left\n";
}
}frame 1: 22.5 deg turned, 77.5 deg left
frame 2: 45.0 deg turned, 55.0 deg left
frame 3: 67.5 deg turned, 32.5 deg left
frame 4: 90.0 deg turned, 10.0 deg left
frame 5: 100.0 deg turned, 0.0 deg left
frame 6: 100.0 deg turned, 0.0 deg left
The turret turns 22.5° per frame (180°/s × 0.125 s) and stops exactly on the target.
Tip:
Quaternion.Slerpin Unity andFQuat::Slerpin Unreal both take the short path.Quaternion.SlerpUnclamped(Unity) letstgo beyond 0..1 for overshoot effects. If a blend ever takes the long way round, someone wrote their own slerp without the sign check.
Test yourself
What does slerp guarantee that nlerp does not?
- A unit-length result
- Constant angular speed along the arc
- The long way round
- No trigonometry
Why negate one quaternion when their dot product is negative?
- To invert the rotation
- So interpolation takes the shorter path; -q is the same rotation
- To normalize it
- Because slerp requires positive inputs
Why does a robust slerp fall back to nlerp when the quaternions are almost equal?
- nlerp is more accurate for large angles
- sin(Ω) is nearly 0, and dividing by it is unstable
- slerp cannot handle the identity
- It is required by the standard