How It Works
Tiled Shared-Memory N-Body
All N particles attract each other via Newtonian gravity. The naïve approach is O(N²) — every particle
must sum forces from every other. On the GPU, we use spatial tiling with workgroup shared memory:
particles are processed in tiles of 256. Each tile is loaded once into fast shared memory, then every thread
in the workgroup computes forces against that tile. This reduces slow global memory reads by ~256×, making
the O(N²) computation dramatically faster in practice.
Integration: Leapfrog (velocity Verlet) — kick velocity by acceleration, then drift position
by velocity. This is symplectic and conserves energy well over long runs.
Softening: A small ε² is added to distance² to prevent infinite forces at zero separation.
This models extended bodies rather than point masses.
WebGPU path: Compute shader runs the physics. Positions are read directly by the render
shader from GPU storage buffers — zero CPU readback. Particles are drawn as screen-aligned billboard
quads with additive blending.
WebGL2 fallback: Physics runs via fragment shaders writing to floating-point textures
(ping-pong FBOs). Positions must be read back to CPU each frame for rendering.
Controls
- Particles
- Total body count. Higher = more beautiful but slower (O(N²) scaling). WebGPU handles 65K–262K; WebGL2 caps around 16K.
- Scenario
- Sphere — random uniform cloud with small random velocities and zero net momentum. Collapses under gravity.
Disk — flat rotating galaxy. Particles have approximate circular orbital velocities.
Merger — two separate clusters on a collision course.
- Random Seed
- Determines the exact initial positions. Same seed = same simulation every time.
- Timestep (dt)
- How far physics advances per frame. Smaller = more accurate & stable, but the simulation evolves slower. Too large → particles fly apart. Sweet spot: 0.005–0.015.
- Gravity (G)
- Gravitational constant. Higher = stronger pull between particles. Scales the force directly.
- Softening (ε)
- Prevents singularities when particles get very close. Higher = softer, smoother orbits but less realistic close encounters. Lower = more dynamic but risk of numerical blow-ups.
- Mass Scale
- Multiplier on every particle's mass. Higher = stronger gravity overall (like increasing G, but per-particle).
- Mass Jitter
- Random variation in particle masses (0 = all identical, 1 = masses vary from 0× to 2× the scale). Adds visual variety.
- Particle Size
- Visual size of rendered particles. Does not affect physics.
- Glow Intensity
- Brightness of the additive glow around particles. 0 = solid dots, higher = more bloom-like.
- Paused
- Freezes the physics simulation. Camera still works.
- Reset
- Regenerates initial conditions with current settings.
GPU Compute Not Available
Your browser supports neither WebGPU nor WebGL2 with float textures.
Please use Chrome 113+, Edge 113+, or Firefox 120+ for the best experience.