Cloth simulation with XPBD

I’ve been writing a small native 3D engine called drie for the past couple weeks. It uses sokol_gfx for rendering, and I recently decided to take a stab at implementing a cloth simulation system.

It’s an XPBD (extended position-based dynamics) solver; the version you see here runs in compute shaders, but was originally running in the CPU — surprisingly the performance gap was not that large, but doing in the GPU makes it less susceptible to stutter.

Features

  • deterministic solver (helpful for replaying game scenes)
  • crease preserving, so pleated curtains remember their shape
  • coarse simulation, fine display for efficiency (tunable)
  • two-way collision with objects and other cloth
  • aerodynamic drag
  • sleep - saves compute when nothing is going on

The two-way collision is pretty sweet - as far as I’ve researched, very few engines have it without add-ons. The “Handkerchief” demo below shows what is possible with that.

Problem 1: tangles

The tricky part is keeping the simulation from tangling itself, as point-based collision still lets individual points slip through gaps in the coarse mesh (aka the cloth folds onto itself irreversibly). After a lot of manual tuning, I’ve found a setup that seems to prevent them most of the time, and the crease-remembering feature helps prevent these in the first place.

The same problem causes two sheets to “merge” together when laid on top of each other. To stop this, a spatial hash is used to compute collisions between cloth entities; points that come into the same region are pushed apart.

Cloth folding and tangling on itself Layered cloth sheets tangling together

Problem 2: rips

In a similar fashion, points can slip through another object, especially sharp corners or thin features (the spacing between points is roughly 1.7cm in these demos). It is made worse by the fact the drag handles are implemented as a tether, where other points of the cloth are connected directly to it and move in the direction of the pull. The “right” way to do it, allowing the force to cascade through each of the points, unfortunately makes everything feel like jello; more accurate cloth sims avoid this by calculating the entire thing on every frame, which is unfeasible in real-time.

Cloth ripping through the corner of a table

I am currently experiment with using SDFs (Signed Distance Fields - short intro here) to replace the collision meshes, as they naturally round out all the corners, but it’s still an unresolved issue - try dragging the cloth across the small table in the furniture demo and you’ll see it.

Demos

These require a WebGPU enabled browser. Should work fine in Firefox, Chrome, Helium, Safari on Mac and iPhone. Otherwise click the “Video” option to see a short capture.

Handkerchiefs

A good test for the self-collision. Also shows some of the engine’s features: sunlight, volumetric fog, dynamic shadows, contact shadows, ambient occlusion, displacement maps, and more.

Use WASD + Q/E to move the camera, or click-and-hold with the right mouse button. Drag a cloth to pick it up.

or open this in fullscreen

Furniture drape

R to restart, B to switch between two bend models for the cloth. “Signed” mode holds up better (check the creases around the sofa), but is a bit more expensive to compute.

Banners

Click to fire off spheres, C to cut them off.

Tech

All examples are written in C3, and compiled for web using sokol’s WebGPU backend and the native c3c compiler emscripten output.

I used Claude Code to write most of these (but not to write any part of this post 🤮), the engine itself is developed using a melting pot of LLMs. Lazy ideas like “can we sample a more coarse grid in a separate pass” become a 5 minute experiment instead of a days-long quest.