This simulation needs WebAssembly, and it could not start in this browser.
The image is a frame from a real run. Check that JavaScript is on, or try another browser.
Every body in a star cluster pulls on every other, so computing the forces directly takes pair terms per step: half a million for a thousand bodies, for the ten billion particles of a cosmological simulation. Josh Barnes and Piet Hut cut this to in 1986 with a tree. Seen from far enough away, a whole group of bodies pulls almost exactly like one mass at its center of mass, so the force from a distant cluster costs one term instead of hundreds. Cosmological codes such as GADGET still rest on this idea, in three dimensions with an octree.
What you see
A thousand bodies start in a slowly rotating disk that collapses under its own gravity into clumps and filaments. Over them, the quadtree: the square around all bodies is split into four, and every square holding more than one body is split again, so the cells are small where bodies crowd and large where they are sparse. The tree is rebuilt every step. The readout compares the force terms per body with direct summation and shows the force error that saving costs, measured each frame against the exact sum for a sample of bodies.
Method
For the force on one body the tree is walked from the root. A cell of size whose center of mass lies at distance is used as a single mass when
and the body is not inside it; otherwise its four children are visited. Smaller opens more cells and is more accurate. At every cell is opened and the result is the exact direct sum. In a test with 2000 clustered bodies the rms force error is below 1% at and below 5% at , and going from 500 to 4000 bodies raises the terms per body by less than a factor 2.5, where direct summation would raise it by 8.
Bodies move with velocity Verlet, which keeps the energy bounded over long runs. Their pull is softened over 3 pixels so close passes stay smooth.
Presets
Collapsing disk: 1000 bodies at , the usual choice in practice.
Few bodies, large cells: 150 bodies, so every cell of the tree is easy to follow.
Coarse tree, θ = 1: 2000 bodies with an aggressive opening angle: far fewer terms, a larger error.
Direct summation, θ = 0: the exact forces for comparison, with 500 bodies to keep the frame rate; the tree is still drawn but every cell is opened.
Parameters
Bodies: 100 to 3000, with the same total mass. Applies on Reset.
Opening angle θ: 0 to 1.2. Applies live, so the readout shows cost and error change as you drag it.