project · simulation · 2026

The N-Body Problem

Every body pulls on every other. One simple law, no tidy answer, and a live GPU simulation you can run in your browser.

Every body pulls on every other.

Point a telescope at almost anything and you are looking at an unsolved problem. Two stars circling each other trace a clean, predictable ellipse, which Newton worked out in the 1680s. Add a third body and the tidy math falls apart. There is no formula that tells you where three or more gravitating masses will sit far in the future. You have to simulate them, one small step at a time.

the one law

Everything starts from a single rule. Newton’s law of gravitation says every pair of masses pulls on each other along the line between them. The pull grows with the masses and shrinks with the square of the distance.

F=Gm1m2r2F = \frac{G\,m_1\,m_2}{r^2}
Drag either body. The force points along the line between them, pulls both equally, and weakens with the square of the distance: pull them twice as far apart and it drops to a quarter.

Read the formula as a sentence. The two masses on top mean heavier things pull harder. The r² on the bottom means the pull fades fast: move twice as far apart and it drops to a quarter. That one attractive tug is the entire physics of the problem. Everything else is just having a lot of bodies do it at once.

add them up

A body never feels just one pull. It feels all of them at the same time. To find how it moves we use acceleration = force / mass, and we add up the tug from every other body.

ai=Gjmjrjrirjri3\vec{a}_i = G \sum_j m_j\,\frac{\vec{r}_j-\vec{r}_i}{\lVert\vec{r}_j-\vec{r}_i\rVert^{3}}
Drag the orange body. Each thin arrow is one pull, longer when another body is nearer (the inverse-square law). The bold arrow is their head-to-tail sum: the single direction it accelerates.

Do not let the symbols intimidate you. Each piece inside the sum is just “point toward another body, with a strength set by the inverse-square law.” The Σ, a Greek capital sigma, means “add those little arrows together.” What comes out is one net arrow: the direction this body speeds up, right now. Do that for every body and you know how the whole cloud moves for one instant.

why three is hard

With two bodies you get two arrows and a clean orbit you can write on paper. With many bodies, every one pulls on every other, so the bookkeeping grows with the square of the count. And from three bodies onward the motion turns chaotic: shift a starting position by a hair and the entire future rearranges.

6
n bodies  n(n1)2 pairs  n2n\ \text{bodies}\ \longrightarrow\ \dfrac{n(n-1)}{2}\ \text{pairs}\ \sim\ n^2
Scrub the body count. Every body pulls on every other, so the links grow like n²: two bodies make one, six make fifteen, twelve make sixty-six. And from three up the motion is chaotic, which Poincaré proved in the 1890s no formula can capture.

This is not a gap in our cleverness. Henri Poincaré proved in the 1890s that no neat formula can ever capture three or more bodies in general. The future is still completely determined by the present, it just cannot be shortcut. The only honest way to find out what happens is to play it forward.

how we cheat

If there is no formula, we fall back on arithmetic. Slice time into tiny steps of size Δt. On each step: add up the accelerations, nudge every velocity, then nudge every position, and repeat. Take small enough steps and the path you trace stays faithful to the real one.

repeat every Δt ① sum the pulls a ② velocity v += a · Δt ③ position x += v · Δt rjri2+ε2softened distance\underbrace{\lVert\vec{r}_j-\vec{r}_i\rVert^{2}+\varepsilon^{2}}_{\text{softened distance}}
No formula, so we step. Each tick of Δt: total the accelerations, move the velocities, move the positions, repeat. The ε is softening, a small cushion so two bodies that nearly touch do not send the force to infinity.

This little loop is the whole simulator. The only subtlety is the softening ε: when two bodies nearly collide, their distance heads toward zero and the inverse-square force would explode. The cushion keeps it finite and the simulation stable. Which means you are now ready to read the real thing.

see it run

Here is that exact loop, running live on your graphics card. Turn gravity up to 4 and watch a seeded disk find its own structure: spiral arms, dense knots, a slowly turning core. The softening slider is the ε from a moment ago, and bodies is n.

10
0.20
14,000
Real GPU gravity: every body pulls on every other, all-pairs, recomputed each frame in WebGPU compute shaders. Position and velocity live on the GPU and never return to the CPU. Bodies are colored by speed (slow to fast). This is the kernel from my Particle Lab, lifted straight into the page.

Every frame, your GPU evaluates that sum of pulls for thousands of bodies against thousands of others, all at once, and never copies a single position back to the main processor. The same math, scaled up with shortcuts like the Barnes-Hut tree to dodge the n² cost, is how astronomers simulate galaxies of billions of stars. It is one of the oldest hard problems in physics, and you just ran it.

Open the full Particle Lab for more models, live measurements, and recording.