Visualizations
166 / 500

166 · Fluids

SPH Water

Thousands of particles that slosh, splash and pour like a liquid.

Smoothed particle hydrodynamics treats water as particles that each spread their mass over a small radius. Each step computes density from neighbors, turns density into pressure, and pushes particles apart or together, with a bit of viscosity to calm them. The tank rocks back and forth so the water sloshes, a spout pours in more, and the particles are drawn as one smooth surface that bends the tiles behind it, with a glossy highlight and foam where the water moves fast.

Try it. Drag through the water to push it. Click to pour in more. Use the arrow keys to tilt the tank.

  • Smoothed particle hydrodynamics
  • Double density relaxation
  • Density field surface rendering

View the source · one module, plus a small shared runtime for sizing, the animation loop and input

Build your own

Paste this into Claude Code, Codex or any coding agent to get a simple version running, then take it wherever you like.

Build a particle water simulation with JavaScript and the HTML canvas element. Put everything in a single index.html file with no libraries or build step, so I can open it directly in a browser.

Start simple:
- Make a canvas that fills the window, stays sharp on high-DPI screens (scale by devicePixelRatio), and resizes with the window.
- Fill part of a tank with about 1,500 particles stored in typed arrays.
- Each step: add gravity to the velocities, remember the old positions, move every particle by its velocity, then fix up positions with double density relaxation (Clavet et al.): for each pair closer than a radius h, sum a density and a near density from (1 - r/h) squared and cubed, turn them into pressure and near pressure, and push the pair apart along the line between them.
- Find neighbors with a uniform grid of cell size h instead of testing every pair.
- Clamp particles inside the tank, then set each velocity to its new position minus its old one, divided by the time step. Use two or three substeps per frame.

Once that works, make it beautiful:
- Tilt the tank back and forth by rotating its walls, so the water sloshes.
- Stop drawing dots: add a smooth kernel for every particle into a low-resolution density grid, draw water wherever the density passes a threshold, and scale the image up.
- Use the grid's gradient as a surface normal for highlights, darken with depth, and turn fast water into foam.
- Let me drag through the water to push it and click to pour more in.

Explain the key ideas in short code comments. When you're done, tell me how to open it and suggest three directions I could take it next, such as fake refraction of the background through the surface, viscosity for honey, or floating objects that bob on the waves.
PreviousParticle Filter RobotThousands of pose guesses condense out of fog onto a lost robot's true position. NextSector EngineA 2.5D portal engine in the mid-90s style: a slow walk round a castle at golden hour.

Related visualizations

  • Water RipplesFluids Raindrops on a koi pond, with ripples that bend the scene below.
  • Snow and SlushPhysics Snowballs, jelly, sand and water smash and tumble down a slope, simulated with MLS-MPM.
  • Ink in WaterFluids A real fluid solver swirls colored dye like ink in water.
  • Coffee RingFluids Coffee drops dry into dark rings, beside two tricks that make them dry evenly.
  • Glowing SurfFluids Moonlit waves break on a beach and the plankton in them flash electric blue.
  • Rain on GlassFluids Raindrops bead, merge and run down a fogged window, each a tiny upside-down city.
  • Flood DioramaFluids A dam fails above a miniature town and a muddy flood spreads through the valley.
  • Faraday WavesFluids A shaken dish of liquid locks into stripes, squares, hexagons and a 12-fold quasicrystal.
  • Rayleigh-Taylor FingersFluids Heavy ink collapses into mushroom fingers in a glass tank you can turn upside down.

Use ← and → to move between demos. While the canvas has focus, keys go to the demo instead.

← More from Emergent Mind Labs