A cloth is a grid of particles joined by springs that must not stretch. This lesson is about solving those links by moving positions directly instead of computing forces. This lesson pairs with 14 - The secret of cloth simulation by Ten Minute Physics, linked under Watch & read. In NumPy you build a pinned grid, project every edge toward its rest length with a compliance term (XPBD), and check the solver against cases you can work out by hand.
A second video is listed there, 09 Getting ready to simulate the world with XPBD. It covers the general method that leads from forces to positions, and is good background for the formulas below.
Each particle has a position x, a velocity v and an inverse mass w = 1 / m. A pinned particle has w = 0: gravity does not move it and no correction moves it. Using w instead of m means that pinning needs no special case anywhere in the solver.
One substep of length h does four things. First it remembers the old positions. Then it predicts: v += h·g for every particle with w > 0, and x += h·v. Then it visits every edge once and moves the two ends so the edge gets back to its rest length. Last it sets v = (x - old) / h, so whatever the constraints did to the positions becomes part of the motion.
The edge correction is the heart of it. For an edge between particles i and j let d = xᵢ - xⱼ and C = |d| - L, where L is the rest length. C is positive when the edge is stretched. Let n = d / |d| and α = compliance / h². The multiplier is λ = -C / (wᵢ + wⱼ + α), and the move is xᵢ += wᵢ·λ·n, xⱼ -= wⱼ·λ·n. Each end moves in proportion to its inverse mass, so a heavy particle moves less and a pinned one not at all.
With compliance 0, α is 0 and the edge ends with exactly length L. A positive compliance leaves it partly stretched: it is the inverse of a stiffness, and dividing by h² is what lets the same value mean the same softness at any step size. Here is one edge of length 3 with rest length 1, solved hard and then soft.
import numpy as np
def solve(x, w, rest, compliance, h):
d = x[0] - x[1]
n = d / np.linalg.norm(d)
lam = -(np.linalg.norm(d) - rest) / (w.sum() + compliance / h**2)
return x[0] + w[0] * lam * n, x[1] - w[1] * lam * n
x = np.array([[0.0, 0.0], [3.0, 0.0]])
w = np.ones(2)
for c in (0.0, 0.01):
a, b = solve(x, w, 1.0, c, 0.1)
print(c, a[0].round(4), b[0].round(4), np.linalg.norm(a - b).round(4))With w = (1, 1) and C = 2, the hard solve moves each end by 1. With compliance 0.01 and h = 0.1 the term α is 1, so λ = -2 / 3 and each end moves only two thirds. The edge stays stretched to about 1.67.
Why trust the soft edge? A mass m hanging from a pinned point settles when the constraint force balances its weight, and in XPBD that force is -C / compliance. So the edge should settle at an extension of m·g·compliance. The test suite checks exactly that closed form, and so does this run, which uses one particle with w = 1 and g = 10.
y, v, g, compliance, h = -1.0, 0.0, -10.0, 0.001, 1 / 60 / 8
for _ in range(2000 * 8):
old = y
v += h * g
y += h * v
lam = -(-y - 1.0) / (1.0 + compliance / h**2)
y -= lam
v = (y - old) / h
print(round(-y - 1.0, 4), "expected", 1.0 * 10 * compliance)Most of the cloth in the video is the same edge solve repeated over a grid. A structural edge links horizontal and vertical neighbours; a shear edge links the diagonals so the sheet resists skewing. A grid with r rows and c columns has r(c - 1) + (r - 1)c structural edges and 2(r - 1)(c - 1) shear edges, and each pair must appear once. Solving the edges in order, each using positions already corrected by the previous one, is Gauss-Seidel. The result depends on the order, which is why the tests compare against a plain loop that uses the same order.
Two mistakes are common. The first is forgetting that the compliance is divided by the square of the substep length. With compliance 0 nothing looks wrong, but a soft edge then behaves far stiffer than asked and its stiffness changes whenever you change the number of substeps. The second is deriving the velocity badly: leaving out the division by the step length, or never rebuilding it from the corrected positions. A free particle then falls at the wrong speed, and momentum is no longer what the forces gave it.
Small substeps with one pass each stay much better behaved than a few big steps with many passes. In a 5 by 5 sheet pinned along its top row, ten substeps per frame keep every edge within about 0.1 percent of its rest length after one second of hanging, while a single substep per frame leaves the stretch near 5 percent.
The tests verify the solver against an exact one-edge case, a scalar reference loop, momentum conservation and the closed-form extension. They do not show that the cloth looks right: there is no collision, no bending resistance and no air drag. They also cannot tell you whether the stiffness you chose is the stiffness of any real fabric.