Verlet integration with relaxation constraints
Stores current and previous positions instead of velocity and enforces constraints by repeatedly projecting positions, giving robust ropes, cloth and ragdolls in a few dozen lines.
Also known as Jakobsen method
In the family Simulation & animation, particle/constraint solvers
Where it runs
Built inOfficial add-onPluginNone foundNot researched
| Engine | Support | How |
|---|---|---|
| three.js r187 | Official add-on | examples/webgpu_compute_cloth.html docs Official WebGPU compute cloth example; not a library. |
| Babylon.js 9.38.0 | None found | |
| Godot 4.7 | Built in | SoftBody3D under GodotPhysics3D (GodotSoftBody3D::solve_constraints iteratively relaxes link constraints and updates velocity from position delta) docs |
| Unity | None found | |
| Unreal 5.7 | None found | |
| Blender 5.2 | None found | |
| WebGPU W3C | None found | |
| Metal | None found |
How it works
Positional Verlet updates x_next = x + (x - x_prev) + a*dt^2, so velocity is implicit. Constraints (fixed distance between particles, planes, pins) are satisfied by iterating over them several times per frame and moving the particles directly to satisfy each one, split by inverse mass. Because velocity is derived from the position change, projecting a particle out of a collider also removes the velocity into it, so collisions behave plausibly with no explicit impulse. Rigid bodies and ragdolls are built from particles linked by distance constraints into stiff tetrahedra or sticks.
When to choose it
Reach for it when
- need quick rope, cloth or cape in a few lines
- ragdoll simulation explodes with velocity integration
- want stable constraints that are easy to tune
- creative coding physics with simple code
Choose it for quick, stable, easily-tuned rope, cloth, cape or ragdoll effects in games and creative coding. Prefer Position Based Dynamics or Extended Position-Based Dynamics when you need volume, bending, or time-step-independent stiffness, and Sequential impulses (rigid bodies) when rigid bodies must stack and have accurate friction and restitution.
Field marks
- Stiffness depends on iteration count: long chains stretch
- Friction and restitution are crude; stacking stability is poor
- Angular momentum is not conserved for rigid shapes built from particles
- Fixed time step is assumed; variable dt changes the implicit velocity
Presets
Verlet Integration & Constraints — 2 relaxation sweeps (left) vs 16 Gauss-Seidel iterations (right)
{
"P_RELAX_ITERS": 14,
"P_WIND_FORCE": 0.95,
"P_WIRE_VIS": 0.85,
"P_SPLIT": 0.48
}Show GLSL (127 lines)
const float P_RELAX_ITERS = 14.0; // P Relaxation Iters [2.0, 24.0, 1.0]
const float P_WIND_FORCE = 0.95; // P Wind Gust [0.0, 2.0, 0.05]
const float P_WIRE_VIS = 0.85; // P Constraint Mesh [0.0, 1.5, 0.05]
const float P_SPLIT = 0.48; // P Low / High Iter Split [0.0, 1.0, 0.02]
// Analytic Verlet cloth surface height & sag modeled from Gauss-Seidel convergence rate (1 - c)^N
vec3 clothSurface(vec2 q, float iters, float wind, float t) {
// q.x in [-1, 1], q.y in [0, 1] (top pinned at y=1, hanging down to y=0)
float drop = 1.0 - q.y;
// Few iterations leave distance constraints stretched; high iterations converge to inextensible drape
float stretchFactor = 0.34 / (1.0 + 0.32 * iters);
float pinDip = (1.0 - abs(sin(q.x * 3.1415926 * 1.5))) * 0.06 * (1.0 + 2.4 * stretchFactor);
float y = q.y * 1.05 - 0.50 - drop * drop * stretchFactor * 0.85 - pinDip * drop;
float x = q.x * 0.82 * (1.0 - 0.06 * drop * (1.0 - stretchFactor));
// Travelling wind ripple obeying Verlet inertia x_new = 2x - x_prev + a*dt^2
float wave1 = sin(q.x * 4.2 - t * 2.6 + drop * 2.1) * 0.11;
float wave2 = sin(q.x * 8.6 + t * 3.7 - drop * 3.8) * 0.042;
float z = (wave1 + wave2) * wind * smoothstep(0.0, 0.35, drop) + 0.05 * drop * drop * wind;
return vec3(x, y, z);
}
// Signed distance to hanging cloth sheet
float mapCloth(vec3 p, float iters, float wind, float t, out vec2 uvCloth) {
float drop = clamp((0.55 - p.y) / 1.05, 0.0, 1.0);
float stretchFactor = 0.34 / (1.0 + 0.32 * iters);
float effH = 1.05 + stretchFactor * 0.85;
vec2 q = vec2(clamp(p.x / 0.82, -1.0, 1.0), clamp(1.0 - (0.55 - p.y) / effH, 0.0, 1.0));
uvCloth = q;
vec3 surf = clothSurface(q, iters, wind, t);
vec2 outBound = vec2(max(abs(p.x) - 0.82, 0.0), max(max(p.y - 0.55, surf.y - p.y - 0.01), 0.0));
float bottomY = clothSurface(vec2(q.x, 0.0), iters, wind, t).y;
float dY = max(p.y - 0.55, bottomY - p.y);
float dX = abs(p.x) - 0.82;
float dSheet = abs(p.z - surf.z) - 0.016;
return max(dSheet, max(dX, dY));
}
vec3 calcNormal(vec3 p, float iters, float wind, float t) {
vec2 e = vec2(0.004, 0.0);
vec2 tmp;
return normalize(vec3(
mapCloth(p + e.xyy, iters, wind, t, tmp) - mapCloth(p - e.xyy, iters, wind, t, tmp),
mapCloth(p + e.yxy, iters, wind, t, tmp) - mapCloth(p - e.yxy, iters, wind, t, tmp),
mapCloth(p + e.yyx, iters, wind, t, tmp) - mapCloth(p - e.yyx, iters, wind, t, tmp)
));
}
vec3 aces(vec3 x) {
x = max(x, 0.0);
return (x * (2.51 * x + 0.03)) / (x * (2.43 * x + 0.59) + 0.14);
}
void mainImage(out vec4 fragColor, in vec2 fragCoord) {
vec2 uv = (fragCoord - 0.5 * iResolution.xy) / iResolution.y;
float screenX = fragCoord.x / max(iResolution.x, 1.0);
float split = clamp(P_SPLIT, 0.0, 1.0);
bool isHigh = screenX >= split;
float iters = isHigh ? clamp(P_RELAX_ITERS, 2.0, 24.0) : 2.0;
float wind = max(P_WIND_FORCE, 0.0);
float wireVis = clamp(P_WIRE_VIS, 0.0, 1.5);
float t = iTime;
vec3 ro = vec3(0.0, 0.02, 2.25);
vec3 rd = normalize(vec3(uv, -1.55));
vec3 col = mix(vec3(0.028, 0.036, 0.056), vec3(0.075, 0.095, 0.142), smoothstep(-0.5, 0.55, uv.y));
// Top hanging rod (cylinder along x at y=0.56)
float tRod = (0.0 - ro.z) / rd.z;
vec3 pRod = ro + rd * tRod;
if (abs(pRod.y - 0.56) < 0.026 && abs(pRod.x) < 0.92) {
float cy = (pRod.y - 0.56) / 0.026;
float shading = 0.35 + 0.65 * sqrt(max(1.0 - cy * cy, 0.0));
col = vec3(0.45, 0.52, 0.64) * shading;
}
float tHit = 0.0;
bool hit = false;
vec2 cUV = vec2(0.0);
for (int i = 0; i < 64; i++) {
vec3 p = ro + rd * tHit;
float d = mapCloth(p, iters, wind, t, cUV);
if (d < 0.0025) { hit = true; break; }
tHit += d * 0.65;
if (tHit > 6.0) break;
}
if (hit) {
vec3 p = ro + rd * tHit;
vec3 n = calcNormal(p, iters, wind, t);
if (dot(n, rd) > 0.0) n = -n;
vec3 l1 = normalize(vec3(0.55, 0.72, 0.65));
vec3 l2 = normalize(vec3(-0.65, -0.25, 0.55));
float diff1 = max(dot(n, l1), 0.0);
float diff2 = max(dot(n, l2), 0.0);
float sheen = pow(1.0 - max(dot(n, -rd), 0.0), 3.0);
// Structural & shear Verlet distance constraint grid
vec2 g = cUV * vec2(12.0, 9.0);
vec2 gf = abs(fract(g - 0.5) - 0.5);
float structWire = smoothstep(0.065, 0.015, min(gf.x, gf.y));
float shearWire = smoothstep(0.055, 0.012, min(abs(fract(g.x + g.y) - 0.5), abs(fract(g.x - g.y) - 0.5)));
float nodeDot = smoothstep(0.14, 0.05, length(fract(g + 0.5) - 0.5));
// Strain coloring: left side stretches red/orange under gravity; right side holds crisp teal
float stretchAmt = (1.0 - cUV.y) * (isHigh ? 0.14 : 0.92);
vec3 fabricCol = isHigh
? mix(vec3(0.14, 0.64, 0.76), vec3(0.24, 0.84, 0.72), cUV.y)
: mix(vec3(0.86, 0.34, 0.22), vec3(0.92, 0.64, 0.28), cUV.y);
col = fabricCol * (0.28 + 0.82 * diff1 + 0.24 * diff2) + vec3(0.55, 0.78, 0.98) * sheen * 0.35;
col = mix(col, vec3(0.05, 0.10, 0.16), structWire * 0.55 * min(wireVis, 1.0));
col = mix(col, vec3(0.95, 0.88, 0.45), shearWire * 0.22 * min(wireVis, 1.0));
col = mix(col, vec3(0.96, 0.98, 1.0), nodeDot * 0.55 * min(wireVis, 1.0));
col += vec3(0.25, 0.05, 0.02) * stretchAmt * 0.25;
}
// Split divider
float sep = smoothstep(0.0035, 0.0008, abs(screenX - split));
col = mix(col, vec3(0.95, 0.97, 1.0), sep);
fragColor = vec4(pow(clamp(aces(col), 0.0, 1.0), vec3(1.0 / 2.2)), 1.0);
}Attribution: MIT — written for this index.
Measured GPU weight & timing
| Preset | GPU | Weight / p50 / p95 | Method |
|---|---|---|---|
| verlet-integration-constraints-live | Tesla T4 1920x1080 | 13.7× baseline (moderate) · 0.424 ms / 0.442 ms | opengl-timer-query · 36 frames · GPU Weight Atlas · Colab T4 sweep |
| verlet-integration-constraints-live | NVIDIA L4 1920x1080 | 10.8× baseline (moderate) · 0.172 ms / 0.175 ms · 2.5× faster on L4 | opengl-timer-query · Cross-architecture validation |
| verlet-integration-constraints-live | NVIDIA A100-SXM4-40GB 1920x1080 | 9.08× baseline (moderate) · 0.262 ms / 0.264 ms · 1.6× faster on A100 | opengl-timer-query · Cross-architecture validation |