Blending Poses and Quaternions
A character rarely plays one clip at a time. It crossfades from walk to run, runs while aiming a rifle with the upper body, leans into turns and breathes on top of everything. All of that is pose blending: combining several poses (one local transform per bone, see Skeletons and Poses) into one, bone by bone.
For each bone:
- translations and scales are blended with a plain lerp;
- rotations are blended as quaternions, with nlerp (lerp the four numbers, then normalize) or slerp (Slerp).
The hemisphere check
Every rotation has two quaternions: q and -q describe exactly the same orientation. Blending works on the four numbers, though, and blending toward -q goes the long way around. Clips exported from different tools, or a character that turned past 180°, produce exactly that situation.
The fix is one line: if dot(a, b) < 0, negate b before blending. Below, two clips disagree slightly about the facing of a character turned around: 170° and -170°, only 20° apart.
#include <iostream>
#include <iomanip>
#include <cmath>
const double PI = 3.141592653589793, DEG = PI / 180;
struct Quat { double w, x, y, 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) { double n = std::sqrt(dot(q, q)); return {q.w / n, q.x / n, q.y / n, q.z / n}; }
Quat yaw(double deg) { return {std::cos(deg * DEG / 2), 0, std::sin(deg * DEG / 2), 0}; }
double yawDeg(Quat q) { // read the angle back, in (-180, 180]
double a = 2 * std::atan2(q.y, q.w) / DEG;
while (a > 180) a -= 360;
while (a <= -180) a += 360;
return std::fabs(a) < 5e-4 ? 0.0 : a;
}
Quat nlerp(Quat a, Quat b, double t, bool fixHemisphere) {
if (fixHemisphere && dot(a, b) < 0) b = {-b.w, -b.x, -b.y, -b.z}; // same rotation, nearer to a
return normalize({a.w + (b.w - a.w) * t, a.x + (b.x - a.x) * t, a.y + (b.y - a.y) * t, a.z + (b.z - a.z) * t});
}
Quat slerp(Quat a, Quat b, double t) {
double d = dot(a, b);
if (d < 0) { b = {-b.w, -b.x, -b.y, -b.z}; d = -d; }
if (d > 0.9995) return nlerp(a, b, t, false); // nearly equal: nlerp is exact enough
double theta = std::acos(d), s = std::sin(theta);
double wa = std::sin((1 - t) * theta) / s, wb = std::sin(t * theta) / s;
return {wa * a.w + wb * b.w, wa * a.x + wb * b.x, wa * a.y + wb * b.y, wa * a.z + wb * b.z};
}
int main() {
std::cout << std::fixed << std::setprecision(3);
// Two clips disagree on the character's facing: 170 and -170 degrees (20 degrees apart).
Quat a = yaw(170), b = yaw(-170);
std::cout << "dot(a, b) = " << dot(a, b) << "\n";
std::cout << " t nlerp(no fix) nlerp(fixed) slerp\n";
for (double t : {0.0, 0.25, 0.5, 0.75, 1.0})
std::cout << std::setw(5) << t << std::setw(13) << yawDeg(nlerp(a, b, t, false))
<< std::setw(14) << yawDeg(nlerp(a, b, t, true)) << std::setw(9) << yawDeg(slerp(a, b, t)) << "\n";
// nlerp's speed is not constant: compare the angle reached at t = 0.25 for a wide 160-degree blend.
Quat c = yaw(0), d = yaw(160);
std::cout << "0 -> 160 at t=0.25: nlerp " << yawDeg(nlerp(c, d, 0.25, true)) << ", slerp " << yawDeg(slerp(c, d, 0.25)) << "\n";
}dot(a, b) = -0.985
t nlerp(no fix) nlerp(fixed) slerp
0.000 170.000 170.000 170.000
0.250 160.150 174.990 175.000
0.500 0.000 180.000 180.000
0.750 -160.150 -174.990 -175.000
1.000 -170.000 -170.000 -170.000
0 -> 160 at t=0.25: nlerp 34.479, slerp 40.000
The negative dot product is the warning sign. Without the fix, the halfway pose faces 0°: the character spins all the way around for one frame of a crossfade, which players notice instantly. With the fix, nlerp and slerp both pass through 180°.
The last line shows the price of nlerp: its angular speed is not constant. A quarter of the way through a 160° blend it has turned 34.5° instead of 40°. For the small angle differences between neighbouring animation poses this error is tiny, and nlerp is cheaper and easy to weight across many inputs (sum the weighted quaternions, after the hemisphere check, and normalize once), so animation runtimes use nlerp for pose blending and keep slerp for large, time-based rotations.
Crossfades
A crossfade (transition) blends from the old pose to the new one with a weight that goes from 0 to 1 over 0.1–0.3 s: pose = blend(oldPose, newPose, w). Both clips keep playing during the fade. Two refinements:
- Inertialization (used by Unreal's "Inertialization" blend node, among others) skips evaluating the old clip: it records the old pose's offset and velocity at the switch and decays that offset to zero. Cheaper, and good for very responsive transitions.
- Sync the clips when fading between cycles (walk to run) so the feet stay in phase; that is the topic of the next lesson.
Layers, masks and additive animation
Games split the body into layers:
- An override layer with a mask replaces part of the pose. A bone mask (Unity's Avatar Mask, Unreal's "Layered blend per bone", Godot's filters on
AnimationTreeblend nodes) gives each bone a weight, so "aim" can own the arms fully and the spine partly, while the legs keep running. - An additive layer stores the difference between a pose and a reference pose,
additive = inverse(reference) * pose, and adds it on top of whatever plays below:result = base * additive^weight. One "lean forward" or "breathing" clip then works on top of idle, walk and run.
#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
#include <vector>
const double DEG = 3.141592653589793 / 180;
struct Quat { double w, x, y, 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) { double n = std::sqrt(dot(q, q)); return {q.w / n, q.x / n, q.y / n, q.z / n}; }
Quat mul(Quat a, Quat b) {
return {a.w * b.w - a.x * b.x - a.y * b.y - a.z * b.z, a.w * b.x + a.x * b.w + a.y * b.z - a.z * b.y,
a.w * b.y - a.x * b.z + a.y * b.w + a.z * b.x, a.w * b.z + a.x * b.y - a.y * b.x + a.z * b.w};
}
Quat conj(Quat q) { return {q.w, -q.x, -q.y, -q.z}; } // inverse of a unit quaternion
Quat nlerp(Quat a, Quat b, double t) {
if (dot(a, b) < 0) b = {-b.w, -b.x, -b.y, -b.z};
return normalize({a.w + (b.w - a.w) * t, a.x + (b.x - a.x) * t, a.y + (b.y - a.y) * t, a.z + (b.z - a.z) * t});
}
// Rotations about one axis (z, "bend forward/back") keep the printout readable.
Quat bend(double deg) { return {std::cos(deg * DEG / 2), 0, 0, std::sin(deg * DEG / 2)}; }
double bendDeg(Quat q) { double a = 2 * std::atan2(q.z, q.w) / DEG; return std::fabs(a) < 5e-4 ? 0.0 : a; }
struct Joint { std::string name; Quat rot; };
using Pose = std::vector<Joint>;
int main() {
std::cout << std::fixed << std::setprecision(1);
Pose run = {{"hips", bend(5)}, {"spine", bend(8)}, {"chest", bend(4)}, {"arm", bend(-40)}, {"leg", bend(30)}};
Pose aim = {{"hips", bend(0)}, {"spine", bend(-4)}, {"chest", bend(-6)}, {"arm", bend(80)}, {"leg", bend(0)}};
Pose leanRef = {{"hips", bend(0)}, {"spine", bend(0)}, {"chest", bend(0)}, {"arm", bend(0)}, {"leg", bend(0)}};
Pose lean = {{"hips", bend(0)}, {"spine", bend(10)}, {"chest", bend(5)}, {"arm", bend(0)}, {"leg", bend(0)}};
// Upper-body mask: how much of the aim layer each joint takes.
std::vector<double> mask = {0.0, 0.3, 0.7, 1.0, 0.0};
std::cout << "joint run aim masked(run+aim) +lean x0.5 +lean x1\n";
for (size_t i = 0; i < run.size(); i++) {
Quat layered = nlerp(run[i].rot, aim[i].rot, mask[i]); // override layer with a mask
Quat additive = mul(conj(leanRef[i].rot), lean[i].rot); // difference from the reference pose
Quat half = mul(layered, nlerp({1, 0, 0, 0}, additive, 0.5)); // apply the difference at 50%
Quat full = mul(layered, additive);
std::cout << std::setw(5) << run[i].name << std::setw(7) << bendDeg(run[i].rot) << std::setw(6) << bendDeg(aim[i].rot)
<< std::setw(13) << bendDeg(layered) << std::setw(14) << bendDeg(half) << std::setw(10) << bendDeg(full) << "\n";
}
}joint run aim masked(run+aim) +lean x0.5 +lean x1
hips 5.0 0.0 5.0 5.0 5.0
spine 8.0 -4.0 4.4 9.4 14.4
chest 4.0 -6.0 -3.0 -0.5 2.0
arm -40.0 80.0 80.0 80.0 80.0
leg 30.0 0.0 30.0 30.0 30.0
Read the columns from left to right:
- The mask keeps the run's legs and hips (weight 0), gives the arm completely to the aim pose (weight 1) and splits the spine (30%) and chest (70%), so the upper body twists smoothly into the aim instead of breaking at one joint.
- The additive lean adds 10° to the spine and 5° to the chest on top of the layered result: at weight 1 the spine goes from 4.4° to 14.4°. The same lean data would add the same bend on top of any other base pose.
Note: Additive rotations are multiplied in local bone space. Applying them in model space, or authoring them against a different reference pose than the engine uses, makes the lean twist sideways. Engines ask for the reference explicitly: Unreal's additive settings on the sequence (a reference pose or frame), Unity's additive layers with a reference pose option on the clip.
Weights should add up
When three or more poses blend (a 2D blend space, several layers), normalize the weights so they sum to 1 before blending translations; otherwise a character between three clips at 0.4 each ends up 1.2 times as tall as it should be. Rotations are normalized anyway by nlerp, which hides the mistake there and makes it harder to find.
Test yourself
Two quaternions to blend have dot(a, b) = -0.9. What should you do first?
- Nothing; blend them directly
- Negate
b, which is the same rotation but ina's hemisphere - Normalize
a - Switch to Euler angles
Why do animation runtimes usually prefer nlerp to slerp for pose blending?
- nlerp is more accurate for large angles
- It is cheaper, and for the small differences between poses its non-constant speed is negligible
- slerp does not work on unit quaternions
- nlerp avoids the need for a hemisphere check
What does an additive animation layer store?
- A complete pose that replaces the base
- The difference between a pose and a reference pose, applied on top of any base
- Only root motion
- A mask of bone weights