DevTools Logo

2D Fluid Dynamics & Particle Physics Sandbox

About this tool

The Fluid & Particle Sandbox is a physics laboratory with two engines behind one canvas. The fluid mode is a genuine Eulerian Navier-Stokes solver in the 'stable fluids' tradition: semi-Lagrangian advection for unconditional stability, explicit diffusion for viscosity, a Jacobi-iterated pressure projection enforcing near-incompressibility (the Poisson step), and vorticity confinement to keep eddies alive instead of smearing out. Drag your pointer to inject dye and velocity, and watch vortices shed, curl and dissipate the way real smoke does.

The particle mode is a damped Verlet N-body engine: gravity wells and Coulomb-style attractors/repulsors with configurable falloff, spring links with stiffness and damping, wall restitution, and elastic circle-to-circle collision resolution. Verlet integration (position-based, storing only current and previous positions) is the scheme behind cloth, rope and soft-body physics in games — the same core you'd ship.

Interaction is pointer-first and touch-friendly. In fluid mode your stroke injects momentum and dye scaled by pointer velocity; in particle mode dragging spawns an attraction center that pulls the swarm toward your cursor. Gravity, vorticity strength and particle count are live sliders.

Device ceilings follow the PRD contract: the 100,000-particle target applies to WebGL2-capable desktops; compact devices cap at 40k GPU, mobile at 12k GPU, and CPU-fallback devices degrade to 2,000–5,000 so the page never becomes a slideshow. The stats line always shows your active class and renderer.

When you find a feel you like, export it as real code: a Matter.js world with walls and your particle count, a PixiJS ticker application with the Verlet loop inlined, or a standalone vanilla Canvas2D engine page with pointer attraction — runnable starting points, not pseudocode.

How to use this tool

  1. Choose an engine

    Fluid (Navier-Stokes) for smoke and vortices; Particles (N-body) for swarms, gravity and collisions.

  2. Paint the field

    In fluid mode, drag to inject dye and velocity — pointer speed scales the push.

  3. Herd the swarm

    In particle mode, drag to attract particles; release to let them fly under gravity.

  4. Tune the physics

    Gravity, vorticity and particle count sliders update the running simulation live.

  5. Watch the ceiling

    The stats line shows your device class (mobile/compact/desktop) and renderer (GPU/CPU) with the matching particle cap.

  6. Export the code

    Grab Matter.js, PixiJS or vanilla Canvas output sized to your particle count and gravity.

Use cases

Game feel prototyping

Dial in particle counts, gravity and restitution against your target device class, then export the exact configuration as engine code.

Learning computational fluid dynamics

Watch advection, diffusion and projection each do their part — every stage of the stable-fluids pipeline is a live parameter.

Landing-page FX

Pointer-reactive fluid trails and swarms are the signature of modern hero sections; prototype here, export the code.

Physics education

Inverse-square falloff, Coulomb repulsion and elastic collisions are all interactive here — no formulas-first ramp.

Common mistakes

Mistake:Judging fluid speed at the wrong grid scale

Fix:The solver runs at 96×96 regardless of canvas size — a maximized window shows the same physics, just upscaled; velocity magnitudes are in grid units.

Mistake:Expecting 100k particles on a phone

Fix:The ceiling adapts by device class and GPU presence; the stats line tells you which class you're in — the target is desktop WebGL2.

Mistake:Maxing vorticity for 'more fluid'

Fix:Vorticity confinement amplifies existing curl; it can't create swirl from a still field. Inject velocity first, then confine.

Mistake:Reading the preview as the export limit

Fix:Interactive drawing caps at 400 particles for smoothness; the slider value sizes the exported engines at up to your device ceiling.

Frequently asked questions

Is this a real fluid solver or a particle trick?

A real solver. The fluid mode discretizes velocity and dye on an Eulerian grid and runs the classic 'stable fluids' pipeline every frame: semi-Lagrangian advection (unconditionally stable), explicit diffusion, a Jacobi-iterated pressure projection solving the Poisson equation to keep the field divergence-free, and vorticity confinement to preserve eddies. The particles mode is a separate Lagrangian engine — damped Verlet integration with force centers, springs and collisions.

Why do my vortices fade away?

Numerical diffusion: advection on a coarse grid dissipates small swirls. That's exactly what the vorticity slider counteracts — confinement measures the local curl gradient and re-injects rotational force. Raise it for lively turbulence; note it amplifies existing curl, so inject velocity first.

How do the attractor and repulsor forces work?

Force centers apply an inverse-square (configurable falloff) pull or push. In gravity mode strength scales with particle mass; in Coulomb mode it's charge × center strength with a k constant, so like charges repel. The interactive canvas spawns an attractor at your pointer in particle mode; the exported engines hard-code equivalent force loops.

Why can't I set 100,000 particles on my phone?

The ceiling adapts to reality: 100k is the WebGL2 desktop target. Compact devices cap at 40k GPU, mobile at 12k GPU, and any device without WebGL drops to 2,000–5,000 CPU-mode particles — because a five-fps demo teaches nothing. The stats line always shows your class (mobile/compact/desktop), renderer (GPU/CPU) and resulting cap.

What's the difference between the preview and the exports?

The interactive canvas caps live drawing at 400 particles for interactivity; the exports are full engines sized to your slider values — Matter.js for rigid bodies, PixiJS for a ticker-driven render loop, and a zero-dependency Canvas2D page for anywhere JS runs. All three mirror your gravity, damping and restitution settings.

Can I use the fluid in my own project?

The vanilla Canvas export is the N-body engine; for fluids, the architecture (advection → diffusion → projection → confinement on a typed-array grid) ports directly to WebGL shaders the way GPU Gems chapter 38 describes. Start from the solver's step order — it's the same one this tool runs.

Does touch input work?

Yes — the canvas is pointer-event based (touch-action disabled), so finger drags inject dye/velocity exactly like mouse drags, including stroke speed scaling.

Is anything computed server-side?

No. Both engines are pure typed arrays running in your tab; there is no backend, no upload, and the exports are generated locally from your parameters.

References & standards