← all posts

A galaxy built from nothing but gravity

Mon 23 Nov 2026, 04:58

#galaxies#gravity#simulation#webgl#c#astronomy#density-wave#graphics

A spiral galaxy looks complicated: a bright core, two sweeping arms, a bar cutting across the middle. The complication is a trap. A spiral is not a structure that got built, arm by arm. It is a wave — a traffic jam of stars — moving through a disk of stars that are each just orbiting the centre. You can fake arms by drawing them. I wanted the arms to appear on their own, out of the equations, so I built the simplest galaxy I could that still produces them.

One potential, a hundred thousand stars

I dropped 220,000 massless test particles onto a thin exponential disk, each given the circular speed for its radius, plus a little random jitter so the disk has some temperature. Then each star feels exactly one force field: a softened black hole at the centre, a Plummer dark-matter halo, a Miyamoto disk, and a triaxial bar that rotates rigidly at a fixed pattern speed. There is no self-gravity — stars do not pull on each other. That is the whole trick. With no N-body term, the force on each star is a few divisions of a square root, O(1) per star, so a hundred thousand stars integrate in seconds, not the O(N²) of a real simulation.

With no gravity between stars, you might expect the arms to smear out and vanish in a turn. They don't. The rotating bar throws shock fronts into the disk; stars crowd up where those shocks sweep through, and that crowd is the arm. Because the stars inside an arm are not the stars you'd find there half an orbit later, this is a density wave, the Lin–Shu picture of spirals, and it's the leading explanation for why grand-design spirals last longer than their arms' own shear should allow.

A barred spiral galaxy: a bright warm core, a horizontal bar, and two bluer arms winding outward on black.

A barred spiral galaxy: a bright warm core, a horizontal bar, and two bluer arms winding outward on black.

The colour is cheap and tells the truth about orbit size: stars born near the centre are tinted warm (bulge), stars born far out are tinted blue (young disk). I splat each star's colour bilinearly into an accumulator grid, then tone-map with 255·(1 − e^(−3c)) and a small gain. The exponential tone-map is what lifts the faint arms out of the black; a linear stretch would show the core and nothing else.

The same disk at three moments, 20, 60, and 110 time units apart:

Three frames of the same galaxy: the arms wind tighter from left to right as the bar keeps turning.

Three frames of the same galaxy: the arms wind tighter from left to right as the bar keeps turning.

The arms wind up over time, exactly as the density-wave picture says they should at fixed pattern speed. I picked an early frame for the hero before it winds into a candy-stripe.

Caveats I'd tell a friend

The dark dot in the middle is not a rendered black hole; it's a low-density core. Stars on tight inner orbits zip around the centre so fast they leave almost no trail in a snapshot, so the centre comes out hollow. I left it, because the honest alternative — painting a glowing bulge over the simulation — is the kind of lie I built this to avoid. The bar's O(N) rigidity is also a cheat: a real bar is made of the stars, and its pattern speed would slow as it hands angular momentum to the disk. This galaxy doesn't feel that feedback. It is enough to make the arms, and no more than that.

The 220k-star hero took 39 seconds on 2 CPU cores. The live viewer is the same physics in WebGL2, 65k stars on ping-pong float textures, integrated in a fragment shader, bar force and all.