3D Galaxy N-body Simulation

Up to 2,000 stars, every pair pulling on every other. Flip the integrator between RK4 and Verlet on the same galaxy and watch the energy readout.

Loading 3D galaxy (WebGL 2.0)

Illustrative browser model: a softened N-body sketch, not a galaxy-formation result

Gravitational dynamics with up to 2,000 particles, every pair summed directly. The toggle switches the integrator between RK4 and velocity Verlet on the same seeded initial conditions, so the energy readout compares integrators and nothing else. The disks start from a rough enclosed-mass estimate, not an equilibrium model.

Direct Summation

Every pair interacts: O(N^2) per force pass, and RK4 makes four passes per step. That cost is why N is capped at 2,000; the panel shows the measured physics time.

RK4 vs Verlet

Verlet is symplectic: its energy error oscillates instead of accumulating. RK4 is more accurate per step but drifts over long runs. Watch the energy readout.

Interactive Computation

Orbit the camera, change particle counts, switch integrators, and Reset to replay the same initial conditions.

Technical Details

The 1/r^2 Singularity Problem

Gravitational force follows an inverse-square law: F = Gm1m2/r^2. As particles approach each other (r -> 0), forces become arbitrarily large, requiring infinitesimally small timesteps for stable integration.

Plummer Softening

Both integrators use the same softened force law. It is a standard numerical choice in N-body codes, not a meta-calculus result, and it changes the physics below the softening length:

F = G * m1 * m2 * r / (r^2 + r_soft^2)^(3/2)

r_soft = 1 (code units), G = 1e-4.
The force has a finite maximum, so close pairs do not
force tiny timesteps; physical gravity is still 1/r^2.

Try It Yourself

  • Toggle integrator: Compare RK4 and Verlet energy drift on the same galaxy
  • Rotate: Click and drag to orbit the galaxy
  • Zoom: Scroll wheel to zoom in/out
  • Presets: Try different galaxy configurations
  • Pause: Click the scene, then press Space