Fluid demo: a little boat in a box of water
This page runs a real 2D incompressible fluid solver in your browser
via WebAssembly, compiled from the
fluid-sim
Rust code — Jos Stam's "Stable Fluids": semi-Lagrangian advection plus a
pressure solve that enforces incompressibility every frame.
Drag anywhere to stir dye into the water, or
grab the boat and drag it — or drive it like a real boat:
W/S throttle, A/D rudder (arrow keys work too)
— and watch the wake.
How it works (60 seconds)
- Advect. Every cell traces backwards along the
velocity field and interpolates — unconditionally stable, which is why
you can drag as violently as you like without it exploding. The price
is smearing: fine vortices fade unless vorticity confinement
(the slider) re-injects swirl.
- Project. Advection leaves the velocity field
compressed (nonzero divergence). The solver computes
∇²p = ∇·u with Jacobi
iteration and subtracts ∇p,
leaving the flow incompressible. This is the expensive step — drop
pressure iters to 4 and watch the sim turn to mush.
- Solids. The boat is a rasterized mask whose cells are
pinned to the boat's velocity each frame (no-slip in the body's
frame). The wake, the bow wave, the shedding eddies — all emergent.
The two equations being solved, every frame, on a 128×128 grid:
∂u/∂t = −(u·∇)u − ∇p/ρ + ν∇²u + f
∇·u = 0
Things to try
- Drag the pressure iters slider to minimum — that's the
incompressibility tax made visible.
- Crank vorticity to 40 and watch max div climb: the
confinement hack is positive feedback, fighting the projection.
- Push viscosity up and the water turns to honey.
- Switch to the divergence view while dragging the boat —
red is the bow wave being born, blue is the wake filling in.
- Drive the boat (W/S throttle, A/D rudder) with dye trail
on — the stern leaves a visible wake in dye mode. Toggle it off to
confirm that dye mode shows dye, not motion: clear water looks still
even when it moves.
Further reading
The full writeup lives in the
fluid-sim README.
Each piece has a deeper explainer, each readable in one sitting:
- Navier-Stokes — the equations, incompressibility, operator splitting
- Advection — why semi-Lagrangian is stable, what it smears, vorticity confinement
- Projection — the Poisson solve, Jacobi, collocated vs staggered grids
- Moving solids — rasterized masks, no-slip bodies, anti-tunneling
← back to demos