01Gravity, all of it
Every body carries a mass and feels the pull of every other body — the full all-pairs sum, with no tree code or particle-mesh approximation to the force. The acceleration of body i is Newtonian gravity summed over all j:
That ε² is the one liberty taken with Newton. A pure 1/r² force diverges when two bodies pass arbitrarily close, and with a finite timestep that produces spurious slingshots that wreck energy conservation. Softening replaces each point mass with a small Plummer sphere, capping the force at short range — the standard, physically-motivated fix in collisionless N-body work. It also stands in for the fact that each simulated body represents millions of real stars, not one.
16,384 bodies by default, adjustable up to ~164,000 with the Bodies slider. Per-body mass scales as 1/N, so adding bodies refines the same galaxy rather than piling on mass. The cost is real: all-pairs gravity is O(N²), which is exactly why a browser sim lives in the thousands, not the billions.
02Moving the stars: the leapfrog
Integrating those accelerations forward in time is where naïve methods quietly fail. Galacto uses the leapfrog (drift–kick–drift) scheme, advancing each body by a fixed step dt in three moves:
Leapfrog is symplectic: it preserves the geometric structure of Hamiltonian (energy-conserving) flow, so the total energy oscillates within a bound instead of drifting away. A simple Euler step, by contrast, leaks energy every orbit and would let the cold disk puff up or spiral in within a few rotations. That stability is what lets a thin disk survive long enough to grow arms.
The step is fixed at dt = 1/60 in simulation time and decoupled from your screen's refresh rate by an accumulator, so a slow display does not make the integrator take bigger, less accurate steps. The seeded initial conditions are repeatable; the long-run path is not promised bit-for-bit across different GPUs.
03Putting numbers on it
The solver runs in arbitrary units with G = 1. Two anchors turn those into recognisable galaxy scales for the on-screen readouts:
- one length unit ≡ 0.1 kpc, and
- the default halo's circular speed ≡ 220 km/s — a Milky-Way-like flat rotation curve.
Because G = 1, the time scale then follows from length ÷ velocity: one time unit ≈ 33 million years. With those, the disk's scale length (35 units) reads ≈ 3.5 kpc and its mass works out to ≈ 7×1010 solar masses — all squarely galaxy-plausible. The Speed readout shows millions of years of simulated time per real second, and the clock on the rotation-curve overlay shows elapsed Myr.
These factors are for display only — the solver never sees them. They re-label the visuals in physical units without touching the dynamics, so the equations hold in real units too.
04Dark matter and the flat rotation curve
Spin a galaxy's visible mass and Newton predicts the orbital speed should fall with radius once you're outside most of the light — the way planets slow down further from the Sun. Real galaxies don't: their rotation curves stay flat far beyond the visible disk. The standard explanation is an enormous, invisible dark-matter halo whose mass keeps growing with radius. Galacto adds that halo as a static background force, in two selectable shapes:
The Curve overlay plots the circular speed against radius and splits it into its three contributions — bulge, disk and halo — which add in quadrature:
The flat outer part is held up almost entirely by the halo — the classic observational clue behind dark matter, made visible as a live decomposition. Drag the Strength slider (in km/s) or the Size slider (the halo's scale radius, in kpc) and watch the curve respond: a concentrated halo rises steeply in the centre, a diffuse one gently.
05Why spiral arms form: the Toomre Q
A cold, rotating, self-gravitating disk is poised on a knife-edge. Too cold and its own gravity wins, collapsing it into clumps; too hot and random motions smear out any structure. In between lies the regime where small disturbances get amplified into the grand sweeping arms of a spiral. The control parameter is the Toomre stability parameter:
Here κ is the epicyclic frequency — the rate at which a slightly-disturbed star oscillates radially as it orbits — read straight off the rotation curve via κ² = 2Ω(Ω + dvc/dr). The stability sequence is sharp:
- Q ≲ 1 — gravity dominates; the disk fragments into clumps and rings.
- Q ≈ 1–2 — the sweet spot; swing amplification turns noise into trailing spiral density waves.
- Q ≫ 2 — pressure and rotation win; a smooth, featureless disk.
The Toomre Q slider sets the target Q for the next seed. The sim inverts the relation to assign each radius the dispersion it needs — σR(r) = Q · 3.36 G Σ(r) / κ(r) — so the disk is born at your chosen stability and then evolves on its own after Restart or a scenario switch.
Softening, the disk's finite thickness, and the modest particle count all make this disk more stable than razor-thin Toomre theory predicts. So an empirical correction is applied to make the slider read an effective Q, calibrated so the spiral sweet spot lands near Q ≈ 1.3. The radial shape of the dispersion and the stability sequence are physically right; the absolute number is tuned to this softened, finite-N disk.
06Cold gas, and the blue arms
Stars are collisionless — they pass through one another's neighbourhoods without colliding — so a purely stellar disk slowly heats up and its arms fade. Spiral galaxies keep sharp arms because of their cold gas, which is collisional: it radiates energy away, shocks, and settles into a thin, cold layer that keeps responding sharply to the disk's gravity. That gas is where new, hot, blue stars are born — which is why the arms of real spirals glow blue.
In the spiral and M51 scenarios, Galacto seeds about a quarter of the disk as a gas component by default (set it with the Gas slider). Each step, the gas damps radial and vertical motion while keeping its tangential circulation — a "sticky gas" stand-in for radiative cooling — so it stays cold while the stars heat and blur. It gathers into the arms and is drawn a bright blue, tracing the star-forming ridges over a warmer, redder stellar disk.
This is a velocity-damping caricature, not hydrodynamics: there is no gas pressure, no genuine shocks, and no actual conversion of gas into stars with feedback. It captures the one effect that matters for the look and the dynamics — a cold, dissipative component that sharpens and sustains the arms.
07Mergers, and why galaxies fall together
When two galaxies pass close, why can they merge into one remnant rather than simply swinging past? One key mechanism is dynamical friction. A massive body ploughing through a sea of lighter ones — here, the dark-matter halo — gathers an over-dense gravitational wake behind it, and that wake pulls back, draining the body's orbital energy. Chandrasekhar's classic result gives the drag:
Because the drag scales with the perturber's mass, it bites hard on the heavy galaxy cores and is negligible for the featherweight disk stars. So colliding galaxies genuinely lose orbital energy and spiral together — one real merger mechanism — rather than sailing past. The Grand-design (M51) scenario layers a second classic effect on top: a companion's tidal field sweeping past a disk draws out a two-armed grand-design pattern and a luminous bridge, just like the real M51.
The halo is a static background force, so it can't grow its own wake — the friction is added analytically from Chandrasekhar's formula instead, with the Coulomb logarithm fixed at a galaxy-merger-scale lnΛ ≈ 3 and the local density estimated from the halo's enclosed-mass gradient.
08Bulges and the shape of a galaxy
The Bulge slider sets how much of the galaxy's mass sits in a dense central concentration. Turn it down and you get a disk-dominated late-type spiral with a gently rising rotation curve; turn it up and you get a bulge-dominated early-type galaxy whose rotation curve spikes sharply in the centre. That single axis — how centrally concentrated the light is — is essentially the backbone of the Hubble tuning-fork classification, and you can watch it reshape the decomposed rotation curve live.
09What's real, and what's illustrative
Galacto is a compact model, not a claim to simulate every piece of galaxy physics. The deliberate simplifications are:
- Finite N. Tens of thousands of bodies stand in for ~1011 stars, so each "star" is enormously massive and two-body scattering is artificially fast — softening mitigates it, but this is a collisionless approximation, not a star-by-star model.
- Effective Toomre Q. Calibrated to this softened, finite-thickness disk rather than read from razor-thin theory (see §05).
- "Sticky gas," not hydrodynamics. Velocity damping, with no pressure, shocks, or real star formation (see §06).
- Analytic dynamical friction. Chandrasekhar's formula with a fixed Coulomb logarithm, not friction emerging from live halo particles (see §07).
- Static halo — a fixed background potential, so the halo neither responds to the disk nor sloshes during a merger.
- Spherical enclosed-mass approximation for the disk's equilibrium circular speeds — close enough that the disk settles and ripples, but not the exact potential of a flattened disk.
And what is genuinely dynamic, arising from the gravitating bodies with nothing scripted:
- spiral arms and bars from disk instability and swing amplification;
- tidal tails and bridges in close encounters;
- violent relaxation of galaxies into a single rotating remnant;
- gas collecting into the same spiral ridges the stellar disk raises.
The visible structures are consequences of the simulated gravity, not animation. The approximations are the price of running the model in real time in a browser tab, and they are chosen to preserve the behaviours that matter on screen.