← 🔭 Space & Astronomy

☄️ N-Body

Bodies: 300
FPS:
Time: 0 s
Scenario
Algo: Barnes-Hut
Central star
Small bodies
High velocity
Drag — rotate · Scroll — zoom

☄️ N-Body — Gravitational Simulation

3D real-time N-body gravity: watch stars, planets, and galaxies orbit, collide, and merge under Newton's universal law of gravitation.

🔬 What It Demonstrates

Every particle gravitationally attracts every other (O(N²) interactions, optimised with a Barnes-Hut octree). Stable orbits, hyperbolic trajectories, orbital resonances, and three-body chaotic motion all emerge naturally.

🎮 How to Use

Click to add bodies; drag to give initial velocity. Choose presets: solar system, binary stars, figure-8 orbit, or galactic collision. Adjust gravitational constant G and time-step with sliders.

💡 Did You Know?

The three-body problem — predicting three gravitating masses — has no closed general solution. Henri Poincaré proved this in 1887, marking the birth of chaos theory, 76 years before Lorenz's weather work.

About this simulation

This is a real-time 3D model of the N-body problem, in which hundreds of masses each attract every other through Newton's inverse-square law of gravity. The accelerations are summed with a Barnes-Hut octree that reduces the cost from O(N²) to O(N log N), and positions are advanced by a leapfrog-style integrator run in small sub-steps. You can grow a galactic disk, smash two galaxies together, or watch a scaled Solar System, all governed by the same softened force law F = G·m₁·m₂ / (r² + ε²).

🔬 What it shows

Gravitational dynamics of up to 800 bodies. Each frame the net force on every body is computed from a Barnes-Hut octree (or exact O(N²) summation), then velocities and positions are updated by leapfrog integration. Stable circular orbits, merging discs and chaotic close encounters all emerge from the physics rather than being scripted.

🎮 How to use

Sliders set the body count (50–800), central star mass (500–8000), the gravitational constant G (0.1–3), the softening ε (0.5–5) and trail length (0–80). A dropdown switches between galaxy disk, galaxy collision and Solar System; a checkbox toggles Barnes-Hut versus direct summation. Use Restart to rebuild, drag to rotate and scroll to zoom.

💡 Did you know?

The three-body problem has no general closed-form solution. Henri Poincaré proved this in 1887, and his study of its sensitive, unpredictable motion sowed the seeds of chaos theory some 76 years before Edward Lorenz rediscovered chaos in weather models.

Frequently asked questions

What is an N-body simulation?

It is a numerical model of how many masses move under their mutual gravity. Because each body attracts every other, there is no tidy formula for the motion of more than two bodies, so the computer integrates Newton's equations step by step. This simulation handles up to 800 bodies at once and lets you watch galaxies, discs and planetary systems evolve in real time.

How does the Barnes-Hut algorithm speed things up?

A naive N-body calculation evaluates every pair of bodies, which costs O(N²) operations per frame. Barnes-Hut instead builds an octree that groups distant clusters of bodies into a single centre of mass, so a faraway group can be treated as one effective particle. This drops the cost to roughly O(N log N); you can toggle it off to compare against exact direct summation.

What do the sliders actually control?

Bodies sets how many particles populate the system (50–800). Star mass (500–8000) fixes the mass of the dominant central star and therefore the orbital speeds. G scales the strength of gravity (0.1–3), softening ε (0.5–5) smooths very close encounters, and Trail (0–80) sets how long a fading path is drawn behind each body. The Scenario dropdown swaps between a galaxy disk, a galaxy collision and the Solar System.

Why is there a softening term ε in the force law?

The Newtonian force grows without bound as the separation r approaches zero, so two bodies passing very close would receive an enormous, numerically unstable kick. The softened form F = G·m₁·m₂ / (r² + ε²) caps the force at small distances, keeping the integration stable. It is a standard trick in galactic simulations, where particles represent broad mass distributions rather than true point masses.

Is the simulation physically accurate?

The force law, Barnes-Hut grouping and orbital velocities are physically faithful, so the qualitative behaviour — stable discs, tidal tails in collisions and chaotic motion — is genuine. However, the masses, distances and the gravitational constant use simplified simulation units rather than real SI values, the softening slightly weakens close-range forces, and the leapfrog steps introduce small numerical drift over long runs. It is an accurate teaching model, not a research-grade ephemeris.