Home/Topic

Verlet Integration

Why Verlet?

Verlet integration is a numerical method for integrating Newton's equations of motion. It's popular in game physics and cloth simulation because it's simple, stable, and naturally handles constraints. Unlike Euler integration, it doesn't need to store velocity explicitly — velocity is implicit in the difference between current and previous positions.

Euler vs. Verlet

In Euler integration, you update velocity then position: v += a*dt, x += v*dt. This accumulates error and can become unstable with large timesteps. Verlet computes the new position directly from the current and previous positions plus acceleration, which is second-order accurate and time-reversible.

javascript
// Euler (explicit) — can be unstable
velocity += acceleration * dt;
position += velocity * dt;

// Verlet — more stable, no explicit velocity
const temp = position;
position = 2 * position - previousPosition + acceleration * dt * dt;
previousPosition = temp;

// Or equivalently:
const velocity = position - previousPosition;
previousPosition = position;
position += velocity + acceleration * dt * dt;

How constraints work with Verlet

The key insight of Verlet + constraints: you can directly move particles to satisfy constraints, and the integration will naturally compute the correct velocity response. This is called "position-based dynamics" and it's why Verlet is used for cloth, ropes, ragdolls, and soft bodies.

javascript
// Distance constraint between two particles
function satisfyConstraint(p1, p2, restLength) {
  const dx = p2.x - p1.x;
  const dy = p2.y - p1.y;
  const currentLength = Math.sqrt(dx*dx + dy*dy);
  const error = currentLength - restLength;

  // Move each particle by half the error
  const correction = error / currentLength * 0.5;
  p1.x += dx * correction;
  p1.y += dy * correction;
  p2.x -= dx * correction;
  p2.y -= dy * correction;
}

// Run multiple iterations for stiffer constraints
for (let iter = 0; iter < 5; iter++) {
  for (const constraint of constraints) {
    satisfyConstraint(constraint.a, constraint.b, constraint.rest);
  }
}

Adding damping

Without damping, a Verlet system conserves energy perfectly and oscillates forever. To add damping, multiply the implicit velocity by a factor slightly less than 1. A value of 0.99 gives gentle air resistance; 0.95 gives heavy damping.

javascript
const damping = 0.99;

function integrate(particle, dt, gravity) {
  const vx = (particle.x - particle.prevX) * damping;
  const vy = (particle.y - particle.prevY) * damping;

  particle.prevX = particle.x;
  particle.prevY = particle.y;

  particle.x += vx + gravity.x * dt * dt;
  particle.y += vy + gravity.y * dt * dt;
}

Common applications

Verlet integration is used for: cloth simulation (grid of particles with distance constraints), rope/chain physics (linear chain of particles), ragdoll physics (skeleton joints as constraints), soft body deformation, and particle-based fluid approximations. Its simplicity makes it ideal for real-time applications.

Try it in the editor

The cloth simulation in ThinkInsideTheBox uses Verlet integration with distance constraints and wind forces. Open it and try adjusting stiffness iterations to see the difference between 1 iteration (very stretchy) and 10+ iterations (stiff fabric). You can also pin/unpin corners to see how constraints propagate.

Try it yourself

Open the editor and experiment with what you just learned. No signup needed.

Open Cloth Demo (uses Verlet)