I have always found simulations interesting. It breaks up the monotonous day of working on libraries and has something visually s(t)imulating about it. This time I tried to implement a cloth simulation in the Bevy Engine using Rust.

For the simulation I took a simple mass-spring approach. The final equations came from the paper A hybrid evolutionary algorithm for tuning a cloth-simulation model. It took me some time to stabilize the simulation. The most critical part was solving edge constraints multiple times per time step and setting the node positions (not the force) directly in that step. Otherwise, the simulation was very unstable when an external force was applied. A web demo is available, but works best natively. I find WebAssembly quite amazing - I could easily create a browser, especially since Bevy supports it out of the box.

At first I used bevy_prototype_lyon to render lines, but it was too slow. Eventually I switched to bevy_debug_lines, which speeds up rendering considerably.

The source code for the simulation is available on my Github.