↑ ↓ to move · Enter to open

16,000 stars in a browser tab

Portrait of Vincent MaderVincent Mader5 Dec 2026 · 3 min read
Two disc galaxies colliding, recorded from the same simulation that runs in the Lab.

Two galaxies fall toward each other, swing around and end up as one cloud of stars. You can run it yourself in the Lab. With WebGPU, the simulation has 16,002 bodies: 8,000 stars and one heavy core per galaxy. Until early December it had 562.

Every body pulls on every other

Gravity has no cutoff. To move one star, you add up the pull of all the others, so twice the stars means four times the work. At 16,002 bodies that is 256 million forces per step, and the page takes 60 steps a second: about 15 billion forces every second.

There are clever ways around this. A tree code treats a distant clump of stars as one heavy point and skips most of the sum. I left it out, because the plain sum has no approximation to tune and is exactly the kind of work a graphics card is built for: the same short calculation, over and over.

On my M1 Pro a step takes about 4 milliseconds. At 60 frames a second there are 16 to spend.

Reading memory less often

Most of the shader is an ordinary double loop. The part that matters for speed is where the stars are read from. The card computes the forces on 256 bodies at a time, and each group first copies the next 256 stars into its own small, fast memory. All 256 bodies then use that copy before the group fetches the next one, so a star is read from main memory once per group instead of 256 times.

More stars were not enough

More stars made a better picture of the same thing: the cores kept flying past each other. The discs were too light to matter. Each one had 5.6% of the mass of its core, so the stars were little more than decoration.

Real galaxies merge because their stars pull back. A heavy core moving through a disc drags a wake of stars behind it, and the wake slows it down. Astronomers call this dynamical friction. I raised each disc to a quarter of its core’s mass, and the cores now sink from 355 units apart to a close pair about 90 units wide. They keep orbiting each other there and do not merge.

The old version is still on the page as the Flyby preset. Its tails are sharper, so it is worth a look too.

What happens without WebGPU

The page falls back to the CPU and the 562 bodies from before, with the same total mass. The two versions are tested against each other: the same bodies, stepped on both, stay within a twentieth of a pixel after 200 steps.