MuJoCo articulated-body dynamics with convex contacts × Motion matching
A motion-matching database without mocap. Fill a motion-matching database with physically simulated locomotion covering the turns, slopes, pushes and stumbles that mocap sessions miss. The runtime stays a cheap nearest-neighbour search with no physics on the character.
Open in StudioReproduced213× · 6.61 ms
Verified proof

Split-screen WebGL2 proof comparing an articulated humanoid replaying a static flat-ground mocap stride across uneven sloped terrain (left, where feet clip into inclines and the torso ignores turn banking) against motion-matching nearest-neighbour retrieval from physics-simulated rollouts that adapt foot contacts, knee flexion, and centripetal torso lean along the curved target trajectory (right).
Chromium Headless ANGLE/SwiftShader WebGL2 · 512x512 · eliminates slope foot penetration and robotic flat-ground gait by matching terrain-aware physics rollouts with dynamic torso banking and contact lock
Show GLSL proof (211 lines)
const float P_SLOPE_AMP = 0.36; // P Terrain Slope Height [0.05, 0.65, 0.02]
const float P_GAIT_SPEED = 1.15; // P Stride Frequency [0.4, 2.0, 0.05]
const float P_LEAN_GAIN = 1.00; // P Physics Turn Banking [0.0, 1.8, 0.05]
const float P_PERTURB = 0.55; // P Lateral Push Recovery [0.0, 1.2, 0.05]
float rampHeight(vec2 xz) {
return P_SLOPE_AMP * sin(xz.x * 1.15 + 0.3) * cos(xz.y * 0.65) + 0.14 * P_SLOPE_AMP * xz.x;
}
vec2 rampGrad(vec2 xz) {
float e = 0.02;
return vec2(
rampHeight(xz + vec2(e, 0.0)) - rampHeight(xz - vec2(e, 0.0)),
rampHeight(xz + vec2(0.0, e)) - rampHeight(xz - vec2(0.0, e))
) / (2.0 * e);
}
float sdCapsule(vec3 p, vec3 a, vec3 b, float r) {
vec3 pa = p - a, ba = b - a;
float h = clamp(dot(pa, ba) / max(dot(ba, ba), 1e-5), 0.0, 1.0);
return length(pa - ba * h) - r;
}
// Solve 2-bone IK knee/elbow joint position in 3D
vec3 solveKnee(vec3 hip, vec3 foot, vec3 fwdHint, float l1, float l2) {
vec3 d = foot - hip;
float dist = clamp(length(d), 0.08, l1 + l2 - 0.01);
vec3 dir = d / max(length(d), 1e-4);
float cosA = clamp((l1 * l1 + dist * dist - l2 * l2) / max(2.0 * l1 * dist, 1e-4), -1.0, 1.0);
float sinA = sqrt(max(1.0 - cosA * cosA, 0.0));
vec3 ortho = normalize(fwdHint - dir * dot(fwdHint, dir) + vec3(1e-4, 0.0, 0.0));
return hip + dir * (l1 * cosA) + ortho * (l1 * sinA);
}
// Evaluate articulated humanoid SDF (1=body/limbs, 2=joints, 3=contact indicator)
float mapCharacter(vec3 p, float t, bool physicsMatched, out int partId) {
float phase = t * 2.8 * P_GAIT_SPEED;
float pathX = sin(t * 0.65) * 0.72;
float pathZ = sin(t * 1.30) * 0.36;
float vx = cos(t * 0.65) * 0.72 * 0.65;
float vz = cos(t * 1.30) * 0.36 * 1.30;
vec2 fwd2 = normalize(vec2(vx, vz) + vec2(1e-4));
vec2 rgt2 = vec2(-fwd2.y, fwd2.x);
vec3 fwd3 = vec3(fwd2.x, 0.0, fwd2.y);
vec3 rgt3 = vec3(rgt2.x, 0.0, rgt2.y);
float groundY = rampHeight(vec2(pathX, pathZ));
vec2 gSlope = rampGrad(vec2(pathX, pathZ));
float slopeFwd = dot(gSlope, fwd2);
float turnCurv = -cos(t * 0.65) * 0.32 * P_LEAN_GAIN;
float pushWave = sin(t * 2.1) * 0.14 * P_PERTURB;
// Pelvis root & upper torso orientation
float pelvisY = physicsMatched ? (groundY + 0.86 - 0.08 * abs(slopeFwd) + 0.045 * abs(sin(phase))) : (0.86 + 0.04 * abs(sin(phase)));
vec3 pelvis = vec3(pathX, pelvisY, pathZ);
vec3 torsoLean = physicsMatched ? (fwd3 * (0.14 + slopeFwd * 0.32) + rgt3 * (turnCurv + pushWave)) : vec3(0.0);
vec3 chest = pelvis + normalize(vec3(0.0, 1.0, 0.0) + torsoLean) * 0.50;
vec3 head = chest + normalize(vec3(0.0, 1.0, 0.0) + torsoLean * 0.5) * 0.24;
// Left & right foot targets: flat-ground playback ignores ramp height; physics-matched locks to slope
float sL = sin(phase);
float sR = sin(phase + 3.14159);
float liftL = max(0.0, cos(phase)) * 0.19;
float liftR = max(0.0, cos(phase + 3.14159)) * 0.19;
vec2 fLxz = vec2(pathX, pathZ) + fwd2 * (sL * 0.32) - rgt2 * 0.13;
vec2 fRxz = vec2(pathX, pathZ) + fwd2 * (sR * 0.32) + rgt2 * 0.13;
float fLy = physicsMatched ? (rampHeight(fLxz) + 0.055 + liftL) : (0.055 + liftL);
float fRy = physicsMatched ? (rampHeight(fRxz) + 0.055 + liftR) : (0.055 + liftR);
vec3 footL = vec3(fLxz.x, fLy, fLxz.y);
vec3 footR = vec3(fRxz.x, fRy, fRxz.y);
vec3 hipL = pelvis - rgt3 * 0.11;
vec3 hipR = pelvis + rgt3 * 0.11;
vec3 kneeL = solveKnee(hipL, footL, fwd3, 0.46, 0.46);
vec3 kneeR = solveKnee(hipR, footR, fwd3, 0.46, 0.46);
// Counter-swinging arms (physics-matched adds wider balance counterbalance on pushes)
float armSpread = physicsMatched ? (0.24 + abs(pushWave) * 0.65) : 0.18;
vec3 shL = chest - rgt3 * 0.19;
vec3 shR = chest + rgt3 * 0.19;
vec3 handL = shL - vec3(0.0, 0.36, 0.0) + fwd3 * (sR * 0.24) - rgt3 * armSpread * 0.45;
vec3 handR = shR - vec3(0.0, 0.36, 0.0) + fwd3 * (sL * 0.24) + rgt3 * armSpread * 0.45;
float dBody = sdCapsule(p, pelvis, chest, 0.125);
dBody = min(dBody, length(p - head) - 0.12);
dBody = min(dBody, sdCapsule(p, hipL, kneeL, 0.068));
dBody = min(dBody, sdCapsule(p, kneeL, footL, 0.056));
dBody = min(dBody, sdCapsule(p, hipR, kneeR, 0.068));
dBody = min(dBody, sdCapsule(p, kneeR, footR, 0.056));
dBody = min(dBody, sdCapsule(p, shL, handL, 0.052));
dBody = min(dBody, sdCapsule(p, shR, handR, 0.052));
float dJoint = min(min(length(p - kneeL), length(p - kneeR)), min(length(p - footL), length(p - footR))) - 0.068;
dJoint = min(dJoint, min(length(p - shL), length(p - shR)) - 0.065);
if (dJoint < dBody) { partId = 2; return dJoint; }
partId = 1;
return dBody;
}
float mapScene(vec3 p, float t, bool physicsMatched, out int matId) {
float dFloor = p.y - rampHeight(p.xz);
int cPart;
float dChar = mapCharacter(p, t, physicsMatched, cPart);
if (dChar < dFloor) {
matId = cPart;
return dChar;
}
matId = 0;
return dFloor;
}
vec3 calcN(vec3 p, float t, bool pm) {
float e = 0.004;
int m;
return normalize(vec3(
mapScene(p + vec3(e, 0.0, 0.0), t, pm, m) - mapScene(p - vec3(e, 0.0, 0.0), t, pm, m),
mapScene(p + vec3(0.0, e, 0.0), t, pm, m) - mapScene(p - vec3(0.0, e, 0.0), t, pm, m),
mapScene(p + vec3(0.0, 0.0, e), t, pm, m) - mapScene(p - vec3(0.0, 0.0, e), t, pm, m)
));
}
vec3 acesTone(vec3 x) {
return clamp((x * (2.51 * x + 0.03)) / (x * (2.43 * x + 0.59) + 0.14), 0.0, 1.0);
}
void mainImage(out vec4 fragColor, in vec2 fragCoord) {
vec2 uv = (fragCoord - 0.5 * iResolution.xy) / iResolution.y;
bool usePhys = uv.x > 0.0;
vec2 suv = vec2(usePhys ? (uv.x - 0.25) : (uv.x + 0.25), uv.y);
suv.x *= 1.85;
vec3 ro = vec3(0.15, 1.85, 3.65);
vec3 target = vec3(0.0, 0.62, 0.0);
vec3 fwd = normalize(target - ro);
vec3 right = normalize(cross(vec3(0.0, 1.0, 0.0), fwd));
vec3 up = cross(fwd, right);
vec3 rd = normalize(fwd * 1.65 + right * suv.x + up * suv.y);
vec3 sky = mix(vec3(0.05, 0.07, 0.11), vec3(0.14, 0.18, 0.26), clamp(rd.y * 1.4 + 0.4, 0.0, 1.0));
vec3 col = sky;
float t = 0.0;
int matId = -1;
for (int i = 0; i < 68; i++) {
vec3 p = ro + rd * t;
float d = mapScene(p, iTime, usePhys, matId);
if (d < 0.003 || t > 12.0) break;
t += d * 0.78;
}
vec3 sunDir = normalize(vec3(0.55, 0.82, 0.42));
if (t < 11.5) {
vec3 wp = ro + rd * t;
vec3 N = calcN(wp, iTime, usePhys);
float ndl = max(dot(N, sunDir), 0.0);
vec3 V = -rd;
float spec = pow(max(dot(N, normalize(sunDir + V)), 0.0), 36.0);
if (matId == 0) {
// Lab floor with slope grid & glowing future trajectory path + motion-match contact rings
vec2 g = abs(fract(wp.xz * 2.0) - 0.5);
float line = smoothstep(0.45, 0.49, max(g.x, g.y));
vec3 floorAlb = mix(vec3(0.16, 0.18, 0.22), vec3(0.28, 0.32, 0.38), line);
// Soft drop shadow under character
float pathX = sin(iTime * 0.65) * 0.72;
float pathZ = sin(iTime * 1.30) * 0.36;
float shad = 1.0 - 0.55 * exp(-dot(wp.xz - vec2(pathX, pathZ), wp.xz - vec2(pathX, pathZ)) * 3.8);
col = floorAlb * (0.30 + 0.85 * ndl) * shad;
// Future motion-matching target trajectory curve painted onto terrain
float minTraj = 9.0;
for (int k = 0; k < 7; k++) {
float tk = iTime + float(k) * 0.18;
vec2 tp = vec2(sin(tk * 0.65) * 0.72, sin(tk * 1.30) * 0.36);
minTraj = min(minTraj, length(wp.xz - tp));
}
vec3 trajCol = usePhys ? vec3(0.18, 0.92, 0.72) : vec3(0.92, 0.32, 0.24);
col += trajCol * smoothstep(0.12, 0.02, minTraj) * 0.75;
float fog = 1.0 - exp(-t * 0.09);
col = mix(col, sky, clamp(fog, 0.0, 0.85));
} else if (matId == 1) {
vec3 bodyAlb = usePhys ? vec3(0.22, 0.54, 0.82) : vec3(0.58, 0.44, 0.38);
float fres = pow(1.0 - max(dot(N, V), 0.0), 3.0);
col = bodyAlb * (0.28 + 1.05 * ndl) + vec3(0.95) * spec * 0.45 + vec3(0.25, 0.65, 0.95) * fres * (usePhys ? 0.45 : 0.12);
} else {
vec3 jointAlb = usePhys ? vec3(0.95, 0.72, 0.24) : vec3(0.82, 0.26, 0.22);
col = jointAlb * (0.35 + 0.95 * ndl) + vec3(1.0) * spec * 0.6;
}
}
// Right side: subtle translucent hologram trail of earlier physics rollout pose in database
if (usePhys) {
float tG = 0.0;
int gm = 0;
for (int i = 0; i < 28; i++) {
vec3 p = ro + rd * tG;
float d = mapCharacter(p, iTime - 0.24, true, gm);
if (d < 0.015) {
if (tG < t) col += vec3(0.14, 0.62, 0.58) * 0.22;
break;
}
tG += max(d * 0.85, 0.03);
if (tG > 9.0) break;
}
}
col = acesTone(col * 1.10);
if (abs(uv.x) < 0.003) col = vec3(0.95, 0.93, 0.89);
fragColor = vec4(pow(clamp(col, 0.0, 1.0), vec3(1.0 / 2.2)), 1.0);
}Why they connect
MuJoCo articulated-body dynamics with convex contacts simulates a humanoid driven by a controller (trajectory optimisation or a trained policy) and outputs animation and skeleton states for every rollout. Motion matching takes animation and skeleton as its database input: each frame gets a feature vector of joint positions, velocities and future root trajectory, and the runtime search picks the best frame for the controller's desired path. Because rollouts can be generated on demand, the database grows exactly where the search finds poor matches.
How to build it
Model a humanoid in MuJoCo and drive it with a model-predictive controller (MuJoCo has its own predictive-control tool) or a pretrained locomotion policy. Sweep target speeds, headings, turn rates and terrain slopes, and add perturbation pushes. Retarget each rollout onto the game skeleton, clean foot contacts with a contact-aware filter, and export clips. Build features and search in Unreal's pose search or a Unity motion-matching package. Log runtime queries with a high match cost and generate new rollouts for those states overnight. Blend the simulated clips with a small mocap set when style matters.
Field marks
- Simulated motion often looks stiff or robotic next to mocap.
- Soft contacts produce foot sliding that survives retargeting.
- Retargeting between simulation and game proportions distorts balance.
- Large generated databases need pruning to keep memory in check.
Prior art
- Perceptive Humanoid Parkour: Chaining Dynamic Human Skills via Motion Matching. Uses A with B for another goal, in the opposite direction: motion matching composes kinematic references that physics-trained policies then track, instead of simulation filling the matching database.
- MathiasLindberg/ResponsiveCharacterMotion. Uses motion matching together with reinforcement-learned physics control in one project (per its README); it does not generate the matching database from simulation.
Searches run (4)
- arXiv: all:"motion matching" AND all:"physics simulation" → 1 result
- GitHub repos: "motion matching" "physics simulation" → 1 result
- arXiv: all:"motion matching" AND all:"reinforcement learning" → 5 results
- GitHub repos: "motion matching" "reinforcement learning" → 1 result