Particle Fluid
2026-11-11
Move the mouse through the fluid above, or use your fingers on a touch screen, several at once if you like. Turn gravity off and it becomes a blob of water floating in a space station. Switch the view to see the particles the liquid is made of.
This is my own take on the fluid simulations of Grant Kot, whose work I find hard to stop playing with. His simulations push hundreds of thousands of particles and are compiled to WebAssembly. Mine is a much smaller cousin, but it is built the same way: the simulation is a few hundred lines of Rust compiled to WebAssembly, and the graphics card does the drawing.
There are two obvious ways to simulate a fluid. You can follow particles around, which makes movement trivial but forces every particle to go looking for its neighbours in order to know how crowded it is. Or you can divide space into a grid and track what flows through each cell, which makes pressure easy to compute but tends to smear out the fine detail, the splashes and droplets that make a fluid look like a fluid.
The material point method does both. The fluid lives in the particles, which carry mass and momentum. At every step they hand these over to a grid, each particle sharing its contribution between the nine cells closest to it. Then each particle reads back how much mass has gathered around it. Where there is more than the fluid is comfortable with, pressure pushes outward, and that push is written to the grid as well. The grid adds gravity, stops anything from moving through the walls, and hands the resulting velocities back to the particles, which move along. After that the grid is wiped clean and everything starts over. The grid remembers nothing between steps, it is only scratch paper on which the particles work out how to get out of each other's way.
The variant used here is called MLS-MPM, introduced by Hu et al. (2018). In addition to its velocity, each particle remembers how the velocity changes around it, whether its surroundings are rotating, stretching or shearing. This is what keeps the swirls alive rather than letting them fade into mush after a few transfers back and forth, and it gives viscosity almost for free. The colour shows how fast the fluid is moving.
Nowhere in the code is there a rule for waves, splashes or vortices. There is only the rule that particles dislike being crowded, applied a few hundred times per second.