What Is an N-Body Simulation? A Guide From Someone Who Built One
the name is doing a lot of work
"N-body" sounds like it's hiding something. It isn't. The n is just a number that could be anything, the way it is in maths class. Two bodies is a two-body problem. Four hundred is a four-hundred-body problem. Nobody wanted to write a paper for each, so: n.
The problem itself is simply: here are some objects with mass, positions and velocities. Gravity is pulling all of them towards all the others simultaneously. Where is everything in an hour?
That's an easy question to ask and a genuinely hard one to answer, which is a combination I find very appealing and which has kept mathematicians busy for three hundred years.
two bodies is solved. three is not.
With exactly two objects, you can write down an equation that tells you where they'll be at any moment. Not "step forward and see". Actually solve it. Where will these two be in four thousand years? Here, plug in 4000. Done. Kepler and Newton sorted that out and it's why we can predict eclipses centuries ahead.
Add one more body and this collapses completely.
I want to be precise about why, because it's often described badly. It isn't that the maths is difficult, or that we haven't found the trick yet, or that computers aren't fast enough. It's that for three or more bodies, no such general formula exists to be found. Poincaré demonstrated this in the 1880s and won a prize for it, which must have been an odd feeling. Congratulations, you've proved we can't.
So we do the unglamorous thing instead. Work out all the forces right now. Move everything forward by a tiny slice of time. Work out all the forces again. Repeat sixty times a second, forever. That's the simulation.
what a step actually costs
Every step, every body has to consider every other body. That's where the work is, and it grows uncomfortably fast.
The pair count for n bodies is n(n−1)/2. Four bodies is six pairs, which is nothing. My galaxy preset spawns two black holes and eighty bits of orbiting dust. 82 bodies, which is 3,321 pairs, recalculated every single frame. At 60fps that's just under 200,000 gravity calculations a second, in JavaScript, in a browser tab, while also drawing everything.
It copes. But you can feel the shape of the problem: double the bodies and you roughly quadruple the work. This is why serious astrophysics simulations don't do it this way. They use approximations like Barnes-Hut that treat a distant clump of stars as one blurry object, because from far enough away, who's counting. I haven't implemented that, and I'm not going to pretend otherwise. At the scale a browser toy runs at, brute force is honest and fast enough.
what you can actually see
The reason I built a sandbox rather than writing more equations is that n-body systems do things that are very hard to intuit and very easy to watch.
Orbits are a balance, not a force. Nothing is holding a planet up. It's falling constantly and moving sideways fast enough to keep missing. Set the sideways speed slightly wrong and you get an ellipse. Set it very wrong and you get a goodbye.
Energy sloshes but doesn't leave. There are two bars in the corner of my sim showing kinetic and potential energy as a share of the total. A properly bound system settles at roughly one third kinetic, two thirds potential, and then just sits there. That's the virial theorem, a real result from statistical mechanics, showing up as a progress bar. Load the solar system preset and the K bar hovers between 31 and 36 per cent indefinitely.
And when a system is falling apart, you can see it in the numbers before you see it on screen. My binary star preset is broken. I picked the launch speed because it looked about right, and "about right" was 5 when a circular orbit needed roughly 2.9 and escape happened at 4.08. So they don't orbit. They leave. The K bar climbs from 60 per cent to 81 by frame 300 to 91 by frame 900, and that climb is the system escaping itself, rendered as a bar chart.
I've left that preset broken on purpose. Watching an orbit fail teaches more than being handed one that works, and you can fix it live by pushing the G slider to about 2.5, at which point the same two stars settle into a steady pair.
where this is actually used
Not just space toys, though space toys are the fun end:
- Planetary science: will this asteroid hit us, and where exactly will Voyager be in 2043.
- Galaxy formation: what happens when two galaxies collide, over timescales where the answer takes millions of years to arrive in real life.
- Molecular dynamics: same nested loop, different force law, proteins instead of planets. The code is startlingly similar.
- Games: every space game with orbital mechanics is running some version of this, usually with a lot of cheating.
want to poke one
The gravity sandbox runs in a browser, needs no signup, and lets you place planets, stars, neutron stars and black holes and fling them at each other. It's the fastest way to get a feel for any of this.
If you'd rather see how it's built, I've written up the actual JavaScript. It's about forty lines. And if you want the part where the universe stops being predictable, that's the three-body problem.
Keep reading
Written by Oliver
I build browser games and simulations on my own, everything here runs in a tab, with no installer and no account. The biggest is Oliver's Racers: procedural circuits in Godot 4, online multiplayer relayed by a Raspberry Pi in my room, and an Android build. Almost nothing here is imported artwork; the cars, trees and grandstands are built out of boxes and cylinders in code at load time.