TExcelTAD
Home › Game Physics & Animation › Ropes and Chains

Ropes and Chains

A rope is the classic first project for distance constraints: a row of points (particles), each pair of neighbours held at a fixed distance, pulled down by gravity. The same structure, with more constraints, becomes a chain, a bridge, a cable, a tail, hair or a strip of cloth. Games use ropes for grappling hooks, swinging lamps, cut-the-rope puzzles, power cables and wobbly antennas.

Verlet integration: velocity for free

Rope solvers almost always use position Verlet integration (see Euler, Semi-Implicit Euler and Verlet). Each particle stores its current and its previous position, and the velocity is implied by the difference:

next = x + (x - previous) · damping + a · dt²
previous = x
x = next

That suits constraints perfectly. When the solver moves a particle to satisfy a constraint, it also changes the implied velocity, so the particle "knows" it was stopped without any velocity bookkeeping. One step of a rope is:

  1. Verlet-integrate every free particle (pinned particles don't move).
  2. Repeat N times: project every distance constraint (and collision constraints against the world).
  3. Draw the polyline through the particles.

How many iterations does a rope need?

A rope pinned at both ends hangs in a catenary curve. For a 5 m rope between two pins 4 m apart, the exact sag can be computed (the program finds it with a bisection), so we can measure how close the simulated rope gets:

cpp · try it yourself
#include <iostream>
#include <iomanip>
#include <cmath>
#include <vector>

struct Vec2 { double x, y; };

// A 5 m rope of 20 segments hanging between two pins 4 m apart. Verlet + distance constraints.
struct Rope {
    std::vector<Vec2> x, prev;
    std::vector<bool> pinned;
    double seg;
    Rope(int segments, double length, Vec2 a, Vec2 b) : seg(length / segments) {
        for (int i = 0; i <= segments; i++) {                       // start as a straight (squashed) line
            double t = double(i) / segments;
            x.push_back({a.x + (b.x - a.x) * t, a.y + (b.y - a.y) * t});
            pinned.push_back(i == 0 || i == segments);
        }
        prev = x;
    }
    void step(double dt, int iterations) {
        const double g = -9.81, damping = 0.99;
        for (size_t i = 0; i < x.size(); i++) {
            if (pinned[i]) continue;
            Vec2 v{(x[i].x - prev[i].x) * damping, (x[i].y - prev[i].y) * damping};   // velocity is implicit
            prev[i] = x[i];
            x[i] = {x[i].x + v.x, x[i].y + v.y + g * dt * dt};
        }
        for (int it = 0; it < iterations; it++)
            for (size_t i = 0; i + 1 < x.size(); i++) {
                Vec2 d{x[i + 1].x - x[i].x, x[i + 1].y - x[i].y};
                double len = std::sqrt(d.x * d.x + d.y * d.y);
                double w1 = pinned[i] ? 0 : 1, w2 = pinned[i + 1] ? 0 : 1;
                if (w1 + w2 == 0 || len == 0) continue;
                double s = (len - seg) / len / (w1 + w2);
                x[i].x += w1 * s * d.x; x[i].y += w1 * s * d.y;
                x[i + 1].x -= w2 * s * d.x; x[i + 1].y -= w2 * s * d.y;
            }
    }
    double length() const {
        double L = 0;
        for (size_t i = 0; i + 1 < x.size(); i++) L += std::hypot(x[i + 1].x - x[i].x, x[i + 1].y - x[i].y);
        return L;
    }
    double lowest() const { double m = 0; for (Vec2 p : x) m = std::min(m, p.y); return m; }
};

// The exact hanging shape is a catenary y = a cosh(x / a); find a so that its length over the span is 5 m.
double catenarySag(double span, double length) {
    double lo = 0.01, hi = 100;
    for (int i = 0; i < 100; i++) {
        double a = (lo + hi) / 2;
        (2 * a * std::sinh(span / (2 * a)) > length ? lo : hi) = a;
    }
    double a = (lo + hi) / 2;
    return a * (std::cosh(span / (2 * a)) - 1);
}

int main() {
    std::cout << std::fixed << std::setprecision(3);
    std::cout << "exact catenary sag: " << catenarySag(4, 5) << " m\n";
    std::cout << "iterations | sag     | rope length (rest 5.000)\n";
    for (int it : {1, 2, 5, 10, 20, 50}) {
        Rope r(20, 5.0, {0, 0}, {4, 0});
        for (int s = 0; s < 600; s++) r.step(1.0 / 60, it);       // 10 seconds to settle
        std::cout << std::setw(10) << it << " | " << -r.lowest() << " m | " << r.length() << " m\n";
    }
}
exact catenary sag: 1.327 m
iterations | sag     | rope length (rest 5.000)
         1 | 1.611 m | 5.387 m
         2 | 1.481 m | 5.203 m
         5 | 1.393 m | 5.084 m
        10 | 1.361 m | 5.041 m
        20 | 1.345 m | 5.020 m
        50 | 1.334 m | 5.007 m

With one iteration the rope stretches almost 8% and hangs 28 cm too low. It looks like a rubber band, not a rope. At 10–20 iterations the stretch drops to 0.4–0.8% and the sag is within 2–3 cm of the exact curve. As in the distance constraints lesson, the error shrinks with the number of iterations, and longer ropes (more links) need more. A rope with 100 links and 10 iterations is visibly stretchy. Options, from cheap to expensive:

  • Substeps: split the frame into 2–4 smaller steps; usually more effective than the same number of extra iterations.
  • Fewer, longer links: a rope of 20 links that looks smooth with a spline drawn through it (see Catmull-Rom Splines).
  • A length clamp: after the iterations, walk from the pinned end and pull each particle back to at most the segment length. Not physical, but it hides stretch completely; many games do this for cables.
  • A direct solver for the whole chain (tridiagonal for a rope), which is exact in one pass.

Cutting a rope

Removing a constraint is all it takes to cut a rope. The two halves simply become two shorter ropes, each hanging from its own pin. This is the heart of every "cut the rope" puzzle:

cpp · try it yourself
#include <iostream>
#include <iomanip>
#include <cmath>
#include <vector>
#include <string>

struct Vec2 { double x, y; };

// A 5 m rope of 20 segments hanging between two pins 4 m apart. Verlet + distance constraints.
struct Rope {
    std::vector<Vec2> x, prev;
    std::vector<bool> pinned;
    double seg;
    size_t cut = size_t(-1);
    Rope(int segments, double length, Vec2 a, Vec2 b) : seg(length / segments) {
        for (int i = 0; i <= segments; i++) {                       // start as a straight (squashed) line
            double t = double(i) / segments;
            x.push_back({a.x + (b.x - a.x) * t, a.y + (b.y - a.y) * t});
            pinned.push_back(i == 0 || i == segments);
        }
        prev = x;
    }
    void step(double dt, int iterations) {
        const double g = -9.81, damping = 0.99;
        for (size_t i = 0; i < x.size(); i++) {
            if (pinned[i]) continue;
            Vec2 v{(x[i].x - prev[i].x) * damping, (x[i].y - prev[i].y) * damping};   // velocity is implicit
            prev[i] = x[i];
            x[i] = {x[i].x + v.x, x[i].y + v.y + g * dt * dt};
        }
        for (int it = 0; it < iterations; it++)
            for (size_t i = 0; i + 1 < x.size(); i++) {
                if (i == cut) continue;                            // this link was cut
                Vec2 d{x[i + 1].x - x[i].x, x[i + 1].y - x[i].y};
                double len = std::sqrt(d.x * d.x + d.y * d.y);
                double w1 = pinned[i] ? 0 : 1, w2 = pinned[i + 1] ? 0 : 1;
                if (w1 + w2 == 0 || len == 0) continue;
                double s = (len - seg) / len / (w1 + w2);
                x[i].x += w1 * s * d.x; x[i].y += w1 * s * d.y;
                x[i + 1].x -= w2 * s * d.x; x[i + 1].y -= w2 * s * d.y;
            }
    }
    double length() const {
        double L = 0;
        for (size_t i = 0; i + 1 < x.size(); i++) L += std::hypot(x[i + 1].x - x[i].x, x[i + 1].y - x[i].y);
        return L;
    }
    double lowest() const { double m = 0; for (Vec2 p : x) m = std::min(m, p.y); return m; }
};

void draw(const Rope& r, const char* title) {
    std::vector<std::string> grid(15, std::string(61, ' '));
    for (size_t i = 0; i + 1 < r.x.size(); i++) {
        if (i == r.cut) continue;
        for (int k = 0; k <= 8; k++) {                              // sample along each link
            double t = k / 8.0;
            double px = r.x[i].x + (r.x[i + 1].x - r.x[i].x) * t, py = r.x[i].y + (r.x[i + 1].y - r.x[i].y) * t;
            int col = int(std::lround((px + 1) * 10)), row = int(std::lround(-py * 4));
            if (col >= 0 && col < 61 && row >= 0 && row < 15) grid[row][col] = '#';
        }
    }
    grid[0][10] = 'O'; grid[0][50] = 'O';                            // the pins
    std::cout << title << "\n";
    for (const std::string& line : grid) std::cout << "|" << line << "|\n";
}

int main() {
    Rope r(20, 5.0, {0, 0}, {4, 0});
    for (int s = 0; s < 600; s++) r.step(1.0 / 60, 20);
    draw(r, "settled (20 iterations):");
    r.cut = 6;                                                      // cut the 7th link
    for (int s = 0; s < 45; s++) r.step(1.0 / 60, 20);
    draw(r, "0.75 s after cutting link 7:");
}
settled (20 iterations):
|          O#                                     #O          |
|           ###                                 ###           |
|             ###                             ###             |
|               ####                       ####               |
|                  #####               #####                  |
|                      #################                      |
|                                                             |
|                                                             |
|                                                             |
|                                                             |
|                                                             |
|                                                             |
|                                                             |
|                                                             |
|                                                             |
0.75 s after cutting link 7:
|          O                                       O          |
|         ##                                      ##          |
|         #                                      ##           |
|        ##                                      #            |
|       ##                                      ##            |
|      ##                                      ##             |
|      #                                       #              |
|                                             #               |
|                                            ##               |
|                                            #                |
|                                           ##                |
|                                           #                 |
|                                           #                 |
|                                           #                 |
|                                                             |

The short left piece (6 links) and the long right piece (14 links) both swing down toward their pins. Tearing works the same way: when a link's length exceeds, say, 150% of its rest length, delete it.

Play with a rope

Drag any point of the rope with the pointer (or press anywhere else to grab the free end). The rope is 30 Verlet particles with distance constraints, plus two collision constraints: the ball and the floor push particles out, inside the same iteration loop. Lower the iterations to 1–2 and the rope turns into elastic. Drag the end far away: the grabbed point has infinite mass (it is set to the pointer every iteration), so the rope stretches instead of pulling it back, the same mass-ratio effect as a heavy weight on a light chain.

html · try it yourself
<style>
  html, body { margin: 0; height: 100%; background: #0e1018; overflow: hidden; }
  canvas { display: block; margin: 0 auto; width: min(100vw, 177.78vh); height: auto; outline: none; touch-action: none; }
</style>
<canvas width="480" height="270"></canvas>
<script>
const cv = document.querySelector('canvas'), ctx = cv.getContext('2d');
const W = cv.width, H = cv.height;
const STEP = 1 / 60, GRAVITY = 600, DAMPING = 0.995;      // pixels and seconds
const N = 30, SEG = 9;                                     // 30 links of 9 px
const ball = { x: 300, y: 170, r: 34 };                    // an obstacle the rope drapes over
let iterations = 10, grabbed = -1, target = null;

let pts = [];
function reset() {
  pts = [];
  for (let i = 0; i <= N; i++) pts.push({ x: 120 + i * SEG, y: 40, px: 120 + i * SEG, py: 40, pinned: i === 0 });
}
reset();

function update(dt) {
  for (const p of pts) {                                    // Verlet: velocity = x - previous x
    if (p.pinned) continue;
    const vx = (p.x - p.px) * DAMPING, vy = (p.y - p.py) * DAMPING;
    p.px = p.x; p.py = p.y;
    p.x += vx; p.y += vy + GRAVITY * dt * dt;
  }
  for (let it = 0; it < iterations; it++) {
    if (grabbed >= 0) { pts[grabbed].x = target.x; pts[grabbed].y = target.y; }
    for (let i = 0; i < N; i++) {                           // distance constraints
      const a = pts[i], b = pts[i + 1];
      const wa = a.pinned || i === grabbed ? 0 : 1, wb = b.pinned || i + 1 === grabbed ? 0 : 1;
      if (wa + wb === 0) continue;
      const dx = b.x - a.x, dy = b.y - a.y, len = Math.hypot(dx, dy) || 1e-6;
      const s = (len - SEG) / len / (wa + wb);
      a.x += wa * s * dx; a.y += wa * s * dy;
      b.x -= wb * s * dx; b.y -= wb * s * dy;
    }
    for (const p of pts) {                                  // collision constraints: ball and floor
      const dx = p.x - ball.x, dy = p.y - ball.y, d = Math.hypot(dx, dy);
      if (d < ball.r + 2 && d > 0) { p.x = ball.x + dx / d * (ball.r + 2); p.y = ball.y + dy / d * (ball.r + 2); }
      if (p.y > H - 12) p.y = H - 12;
      p.x = Math.max(2, Math.min(W - 2, p.x));
    }
  }
}

function canvasPoint(e) {
  const r = cv.getBoundingClientRect();
  return { x: (e.clientX - r.left) * W / r.width, y: (e.clientY - r.top) * H / r.height };
}
const buttons = [
  { x: 10, y: 8, w: 22, label: '−', act: () => { iterations = Math.max(1, iterations - 1); } },
  { x: 128, y: 8, w: 22, label: '+', act: () => { iterations = Math.min(60, iterations + 1); } },
  { x: 160, y: 8, w: 60, label: 'Reset', act: reset },
];
cv.addEventListener('pointerdown', (e) => {
  const p = canvasPoint(e);
  for (const btn of buttons) if (p.x >= btn.x && p.x <= btn.x + btn.w && p.y >= btn.y && p.y <= btn.y + 20) { btn.act(); return; }
  let best = -1, bestD = 30;                                // grab the nearest particle (not the pin)
  pts.forEach((q, i) => { const d = Math.hypot(q.x - p.x, q.y - p.y); if (!q.pinned && d < bestD) { best = i; bestD = d; } });
  if (best < 0) best = N;                                   // nothing near: grab the free end
  grabbed = best; target = p;
  cv.setPointerCapture(e.pointerId);
});
cv.addEventListener('pointermove', (e) => { if (grabbed >= 0) target = canvasPoint(e); });
const release = () => { grabbed = -1; };
cv.addEventListener('pointerup', release);
cv.addEventListener('pointercancel', release);

function draw() {
  ctx.fillStyle = '#0e1018'; ctx.fillRect(0, 0, W, H);
  ctx.fillStyle = '#455a64'; ctx.fillRect(0, H - 10, W, 10);
  ctx.beginPath(); ctx.arc(ball.x, ball.y, ball.r, 0, Math.PI * 2); ctx.fill();
  let length = 0;
  ctx.strokeStyle = '#ffb74d'; ctx.lineWidth = 3; ctx.lineJoin = 'round';
  ctx.beginPath(); ctx.moveTo(pts[0].x, pts[0].y);
  for (let i = 1; i <= N; i++) { ctx.lineTo(pts[i].x, pts[i].y); length += Math.hypot(pts[i].x - pts[i - 1].x, pts[i].y - pts[i - 1].y); }
  ctx.stroke();
  ctx.fillStyle = '#90caf9'; ctx.fillRect(pts[0].x - 4, pts[0].y - 4, 8, 8);
  if (grabbed >= 0) { ctx.fillStyle = '#fff'; ctx.beginPath(); ctx.arc(pts[grabbed].x, pts[grabbed].y, 5, 0, Math.PI * 2); ctx.fill(); }
  ctx.font = '13px system-ui, sans-serif'; ctx.textBaseline = 'middle';
  for (const btn of buttons) {
    ctx.fillStyle = '#263238'; ctx.fillRect(btn.x, btn.y, btn.w, 20);
    ctx.fillStyle = '#fff'; ctx.textAlign = 'center'; ctx.fillText(btn.label, btn.x + btn.w / 2, btn.y + 10);
  }
  ctx.textAlign = 'center'; ctx.fillText(`iterations: ${iterations}`, 80, 18);
  ctx.textAlign = 'left';
  const stretch = (length / (N * SEG) - 1) * 100;
  ctx.fillText(`stretch ${stretch.toFixed(1)}%`, 230, 18);
  ctx.fillText('drag the rope with the pointer (empty space grabs the free end)', 10, 40);
}

let acc = 0, last = performance.now();
requestAnimationFrame(function frame(now) {
  acc += Math.min((now - last) / 1000, 0.1);
  last = now;
  while (acc >= STEP) { update(STEP); acc -= STEP; }
  draw();
  requestAnimationFrame(frame);
});
</script>

Ropes in engines

Engine Rope options
Unity no rope component: a chain of rigidbodies with HingeJoint/ConfigurableJoint (or HingeJoint2D in 2D), LineRenderer to draw; asset store solvers for real ropes
Unreal Cable Component plugin (UCableComponent): Verlet particles with distance constraints, cosmetic only by default; or a chain of Physics Constraints
Godot no rope node: PinJoint2D/PinJoint3D chains of bodies, or your own Verlet rope drawn with Line2D
Box2D v3 chains of bodies with revolute joints, or distance joints with maxLength for slack ropes

A chain of rigid bodies with joints interacts properly with other physics objects (a crate can hang on it) but is expensive and needs many iterations to avoid stretching. A Verlet rope is cheap and stable, but other bodies don't feel it unless you add the coupling yourself. Pick the cosmetic rope unless gameplay depends on the rope pushing things.

Test yourself

Why is position Verlet popular for ropes?

  1. It doesn't need gravity
  2. Velocity is the difference between current and previous position, so moving a particle in a constraint also updates its velocity automatically
  3. It is the most accurate integrator
  4. It needs no constraints

A 100-link rope looks stretchy with 10 iterations. Which change usually helps most for its cost?

  1. Raising gravity
  2. Substeps, fewer links with a spline, or a final length clamp
  3. Lowering the frame rate
  4. Removing the pins

How do you cut a simulated Verlet rope?

  1. Set gravity to zero
  2. Remove (stop solving) the distance constraint between two particles
  3. Pin every particle
  4. Double the segment length