The science

The physics of a self-gravitating galaxy

Tens of thousands of stars, each pulling on every other with Newtonian gravity - the same force real galaxy simulations use, scaled to a browser. Nothing is scripted: the arms, bars, tidal bridges and merger remnants all fall out of the dynamics. Here's what's real, and where the model cuts corners.

01All-pairs gravity

Every body feels the pull of every other - the full all-pairs sum, with no tree code or particle-mesh shortcut. The acceleration of body i is Newtonian gravity summed over all j:

ai = Gj mj (rjri)(|rjri|² + ε²)3/2 with a Plummer softening length ε in the denominator

The ε² is the one liberty taken with Newton. A pure 1/r² force blows up when two bodies pass close, and at a finite timestep that throws spurious slingshots that wreck energy conservation. Softening spreads each point mass into a small Plummer sphere [6], capping the force at short range - standard in collisionless N-body work [8][9], and a fair stand-in for the fact that each body here represents millions of real stars.

In the sandbox

16,384 bodies by default, up to 163,840 with the Bodies slider. Per-body mass scales as 1/N, so adding bodies refines the same galaxy instead of piling on mass. All-pairs gravity is O(N²) - which is why a browser sim lives in the thousands, not the billions.

02Moving the stars: leapfrog

Integrating those accelerations is where naïve schemes fail. Galacto uses leapfrog (drift–kick–drift), advancing each body a fixed step dt in three moves:

xx + v·dt/2   →   vv + a·dt   →   xx + v·dt/2 drift a half-step, kick with gravity sampled at the midpoint, drift the second half

Leapfrog is symplectic [8]: total energy oscillates within a bound instead of drifting away. Plain Euler leaks energy every orbit - the cold disk would 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 of sim time and decoupled from the display by an accumulator, so a slow refresh rate never forces bigger, less accurate steps. Seeds are repeatable; the long-run path is not bit-for-bit across different GPUs.

03Physical units

The solver runs in arbitrary units with G = 1. Two anchors turn those into galaxy scales for the on-screen readouts:

The time unit then follows: ≈ 33 million years. The disk's scale length (35 units) reads ≈ 3.5 kpc and its stellar mass ≈ 7×1010 M - galaxy-plausible. The Speed readout shows Myr of simulated time per real second; the rotation-curve clock shows elapsed Myr.

Note

These factors are display-only; the solver never sees them. They re-label the visuals in physical units without touching the dynamics.

04Dark matter and the flat rotation curve

Outside most of a galaxy's visible mass, Newton says orbital speed should fall with radius, the way planets slow further from the Sun. Real galaxies don't - their rotation curves stay flat far beyond the visible disk [7]. The standard explanation is an invisible dark-matter halo whose enclosed mass keeps growing outward. Galacto adds one as a static background force, in two shapes:

Logarithmic:  vc² = v0² r²r² + rc² flat at large r; an unbounded potential, so the system stays bound and debris always returns
NFW (cold dark matter) [4]vc² ∝ ln(1+x) − x/(1+x)xx = r/rs rises to a peak near r ≈ 2.16 rs then declines; a finite potential, so fast debris can escape

The Curve overlay plots the circular speed against radius and splits it into its three contributions - bulge, disk and halo - which add in quadrature:

vc²(r) = vbulge² + vdisk² + vhalo²

The flat outer part is held up almost entirely by the halo - the original clue for dark matter, here as a live decomposition. Drag Strength (km/s) or Size (the scale radius, kpc) and the curve responds: a concentrated halo rises steeply, a diffuse one gently.

05Spiral arms and the Toomre Q

A cold, rotating, self-gravitating disk sits on a knife-edge. Too cold and its own gravity collapses it into clumps; too hot and random motions smear out any structure. Between them, small disturbances amplify into spiral arms. The control parameter is Toomre's Q [1]:

Q = σR κ3.36 G Σ σR = radial velocity dispersion · κ = epicyclic frequency · Σ = surface density

κ is the epicyclic frequency - how fast a slightly-disturbed star oscillates radially as it orbits - read straight off the rotation curve via κ² = 2Ω(Ω + dvc/dr). The sequence is sharp:

The Toomre Q slider sets the target for the next seed. The sim inverts the relation to give each radius the dispersion it needs - σR(r) = Q · 3.36 G Σ(r) / κ(r) - so the disk is born at your chosen stability, then evolves on its own.

Honest about the model

Softening, finite disk thickness, and the modest particle count all make this disk more stable than razor-thin theory predicts. So the slider reads 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 right; the absolute number is tuned to this softened, finite-N disk.

06Cold gas, and the blue arms

Stars are collisionless - they pass through each other's neighbourhoods without colliding - so a purely stellar disk slowly heats and its arms fade. Real spirals keep sharp arms because of cold gas, which is collisional: it radiates energy away and settles into a thin layer that keeps responding sharply to the disk's gravity. That gas is where blue stars are born - which is why spiral arms glow blue.

In the spiral and M51 scenarios about a quarter of the disk is gas by default (the Gas slider). Each step it damps radial and vertical motion while keeping its circulation - a "sticky gas" stand-in for cooling - so it stays cold while the stars heat and blur. It gathers into the arms and is drawn bright blue over the warmer stellar disk.

Honest about the model

This is velocity damping, not hydrodynamics: no gas pressure, no real shocks, no star formation with feedback. It captures the one effect that matters on screen - a cold, dissipative component that sharpens the arms.

07Mergers and dynamical friction

Two galaxies that pass close can merge instead of swinging past. One mechanism is dynamical friction: a massive body ploughing through lighter ones - here, the halo - gathers an over-dense wake behind it, and the wake pulls back, draining its orbital energy. Chandrasekhar's result gives the drag [5]:

adf ∝ − G² M ρ lnΛv² f(X) ∝ the body's own mass M, the local halo density ρ, the Coulomb logarithm lnΛ, and a velocity factor f(X)

Because the drag scales with the perturber's mass, it bites the heavy galaxy cores and is negligible for disk stars. So colliding galaxies lose orbital energy and spiral together rather than sailing past. The Grand-design (M51) scenario adds a second classic effect: a companion's tidal field sweeping a disk draws out a two-armed pattern and a luminous bridge, like the real M51.

Honest about the model

The halo is a static force, so it can't grow its own wake - the friction is added analytically from Chandrasekhar's formula, with lnΛ ≈ 3 and the local density read from the halo's enclosed-mass gradient.

08Bulges and the shape of a galaxy

The Bulge slider sets how much mass sits in a dense central concentration. Low gives a disk-dominated late-type spiral with a gently rising rotation curve; high gives a bulge-dominated early-type galaxy whose curve spikes in the centre. That one axis - central concentration - is the backbone of the Hubble tuning fork, and you can watch it reshape the decomposed curve live.

09What's real, and what's illustrative

Galacto is a compact model, not every piece of galaxy physics. The deliberate simplifications:

What's genuinely dynamic, straight from the gravitating bodies:

The takeaway

The structures you see are consequences of the simulated gravity, not animation. The approximations are the price of running it live in a browser tab, chosen to keep the behaviours that matter on screen.

10References

  1. Toomre, A. (1964). “On the gravitational stability of a disk of stars.” ApJ 139, 1217 - the Q parameter.
  2. Toomre, A. (1981). “What amplifies the spiral arms?” in The Structure and Evolution of Normal Galaxies - swing amplification.
  3. Lin, C. C. & Shu, F. H. (1964). “On the spiral structure of disk galaxies.” ApJ 140, 646 - density-wave theory.
  4. Navarro, J. F., Frenk, C. S. & White, S. D. M. (1997). “A universal density profile from hierarchical clustering.” ApJ 490, 493 - the NFW halo.
  5. Chandrasekhar, S. (1943). “Dynamical friction. I.” ApJ 97, 255.
  6. Plummer, H. C. (1911). “On the problem of distribution in globular star clusters.” MNRAS 71, 460 - the Plummer model behind softening.
  7. Rubin, V. C., Ford, W. K. & Thonnard, N. (1980). “Rotational properties of 21 Sc galaxies…” ApJ 238, 471 - flat rotation curves.
  8. Dehnen, W. & Read, J. I. (2011). “N-body simulations of gravitational dynamics.” Eur. Phys. J. Plus 126, 55 - softening, leapfrog, and the rest of the method.
  9. Binney, J. & Tremaine, S. (2008). Galactic Dynamics (2nd ed.), Princeton - the standard text for all of the above.

The sound of it →   How it's built →