What are the stages of one position-based dynamics (PBD) step for cloth particles, and how are velocities obtained afterwards?
answer
- positions first, velocities later
- predict with external forces
- project constraints in a loop
- inverse-mass weighted correction
- velocity from position change over dt
basics
~10 sPBD predicts each particle's position from its velocity and external forces, repeatedly projects those predicted positions onto the constraints, then sets each velocity to the position change divided by the step length.
solid answer
~40 sFirst apply external forces such as gravity to each free particle's velocity and predict `p = x + dt*v`. Generate collision constraints against the predicted positions. Then run a few **iterations**, each projecting every constraint in turn: for a distance constraint, measure `C = |p1 - p2| - L` and move both particles along the line between them, split by inverse mass, so that the error vanishes. Finally set `v = (p - x) / dt` and `x = p`. Because velocities are derived from corrected positions, a constraint never injects the huge forces a very stiff spring would, so stiff cloth stays stable at ordinary game step lengths. The cost is that the result is only as converged as the iteration count allows: leftover error shows up as stretch.
code
pseudocode · 23 linesfunction pbdStep(particles, distanceConstraints, dt, iterations):
for each particle q in particles:
if q.invMass > 0:
q.v = q.v + dt * gravity
q.p = q.x + dt * q.v // predicted position
// collision constraints would be generated here from q.p
repeat iterations times:
for each constraint c in distanceConstraints:
projectDistance(particles[c.a], particles[c.b], c.restLength)
for each particle q in particles:
q.v = (q.p - q.x) / dt
q.x = q.p
function projectDistance(A, B, restLength):
wSum = A.invMass + B.invMass
if wSum == 0: return // both pinned
d = A.p - B.p
len = length(d)
if len < 1e-9: return // coincident, no direction
n = d / len
C = len - restLength // > 0 stretched, < 0 compressed
A.p = A.p - (A.invMass / wSum) * C * n
B.p = B.p + (B.invMass / wSum) * C * ngo deeper
Remember the order: forces, predict, project constraints several times, then velocity from position change. Know what a distance constraint is.
Derive the distance-constraint correction with inverse-mass weights and show why the gradients sum to zero. Explain why velocities come from positions and what that buys in stability.
Connect the loop to symptoms: unconverged constraints read as stretch, deep collision push-outs become launch velocity, and damping or friction must be added as explicit velocity edits.
Weigh PBD's controllability and stability against its weaker physical accuracy, and decide where that trade is right for a game's cloth and ropes.
## What a step works on **Position-based dynamics (PBD)** simulates particles by editing positions directly. Each particle has a position `x`, a velocity `v` and an inverse mass `w = 1/m` (zero for pinned particles). During a step it also has a **predicted position** `p`, the scratch value the constraints work on. Constraints are functions of positions, such as a distance constraint `C = |p1 - p2| - L` between two cloth particles with rest length `L`. ## The loop, stage by stage One PBD step of length `dt` runs these stages: 1. **Apply external forces.** For each particle with `w > 0`, add `dt * w * f_ext` to its velocity; for gravity that is simply `dt * g`. Optional velocity damping goes here. 2. **Predict.** Set `p = x + dt * v` for every particle. Pinned particles get the position gameplay or animation wants this step. 3. **Generate collision constraints.** Test the predicted positions against colliders and add an inequality constraint for each particle that would end up inside one. 4. **Project, several times.** Repeat for a chosen number of **iterations**: visit every constraint and move its particles' predicted positions so that it holds. 5. **Derive velocities.** For each particle set `v = (p - x) / dt`. 6. **Commit.** Set `x = p`. Friction and restitution, if any, adjust the new velocities here. Velocities are never touched by the constraints directly. They are recovered from how far each particle actually moved. ## Projecting one distance constraint The projection asks for the smallest correction, weighted by inverse mass, that makes `C` zero along the constraint's gradient. For a general constraint the correction to particle `i` is `dp_i = -s * w_i * grad_i C`, with `s = C / sum_j (w_j * |grad_j C|^2)`. For a distance constraint, let `n = (p1 - p2) / |p1 - p2|`. Then `grad_1 C = n` and `grad_2 C = -n`, each of length one, so `s = C / (w1 + w2)` and: - `dp1 = -(w1 / (w1 + w2)) * C * n` - `dp2 = +(w2 / (w1 + w2)) * C * n` If the pair is stretched, `C > 0`, and both particles move toward each other; if compressed, they move apart. The split follows inverse mass, so the lighter particle moves more and a pinned one moves not at all. Because the gradients sum to zero, `m1*dp1 + m2*dp2 = 0`: projection conserves linear momentum unless a pinned particle is involved. ## Why derive velocity from positions A spring-mass cloth turns stretch into a force, and a very stiff spring integrated explicitly over a long step overshoots and gains energy. PBD never computes that force. It moves particles onto the constraint and lets the velocity be whatever the move implies. However stiff the constraint, the correction is bounded by the constraint error, so the step stays stable at game step lengths. The same mechanism has consequences worth knowing: - a collision pushed out deep in one step turns into outward velocity on the next, so collisions are generated against predicted positions to catch particles before they sink deep; - leftover constraint error becomes visible stretch, not an explosion; - damping, friction and restitution are applied as explicit velocity edits, because nothing else in the loop provides them. ## Projection order The usual loop is **Gauss-Seidel** style: each projection immediately sees positions already corrected by earlier ones, which converges quickly but is order-dependent and sequential. A parallel solver instead uses Jacobi-style accumulation or groups of constraints that share no particle. Either way, a fixed iteration count may stop before every constraint is satisfied, and that is where most of PBD's tuning questions start. ## Checking an implementation When reviewing a PBD step, or writing one at a whiteboard, these are the details that usually go wrong: 1. The correction's sign: a stretched pair must move together, so `p1` moves along `-n` and `p2` along `+n` when `n` points from `p2` to `p1`. 2. The weights: each particle takes `w_i / (w1 + w2)` of the error, so a pinned particle takes none and two pinned particles skip the constraint. 3. The guards: a zero-length separation has no direction and must be skipped rather than divided by. 4. The velocity update: it must use the start-of-step position `x`, not the position from the previous iteration. 5. The pinned particles: their positions are written by gameplay or animation before projection, so the free particles see where the attachment really is.
- Why does projecting a distance constraint conserve linear momentum?The corrections are `-s*w1*n` and `+s*w2*n`. Multiplying each by its particle's mass gives `-s*n` and `+s*n`, which sum to zero, so the pair's total momentum is unchanged. The argument fails only when one particle is pinned, because an infinite mass absorbs momentum.
- Why should collision constraints be generated against the predicted positions rather than the start-of-step positions?The predicted positions are where particles would end up. Testing them catches a particle before it sinks into a collider, so projection stops it at the surface. Testing only start positions misses the step's motion, so particles end deep inside and the push-out turns into a launch velocity next step.
- What does a projection stiffness factor below one do in classic PBD?It scales each correction, so a constraint only moves part of the way toward satisfaction per iteration. That softens the cloth, but the softness then depends on how many iterations and steps run, which is the weakness the compliance-based variant fixes.
- PBD turns constraint error into spring forces and integrates them like a spring-mass system.
- Constraint projection leaves velocities untouched, so the next step ignores the corrections.
- Each constraint is solved once per step, so every constraint is exactly satisfied afterwards.
- The correction should be split equally between two particles whatever their masses.
- A pinned particle takes half of each correction like any other particle.