Home▸Space & Astronomy▸2D Galaxy — Top-Down N-Body Simulation

🌌 2D Galaxy — Top-Down N-Body Simulation

A real gravitational N-body simulation of a spiral galaxy seen from above: Barnes-Hut self-gravity, differential rotation from a bulge-plus-halo rotation curve, and spiral arms that wind and evolve on their own — computed, not drawn.

Space & Astronomy2DModerate60 FPS📱 Mobile-adapted⇄ 3D version
2d-galaxy ↗ Open standalone

A real gravitational N-body simulation of a spiral galaxy viewed from above: a Barnes-Hut quadtree computes genuine star-on-star gravity, a bulge-plus-halo rotation curve drives differential rotation, and spiral arms wind and evolve on their own — computed every frame, never drawn.

🔬 What It Demonstrates

Stars orbit under real Newtonian gravity from a Barnes-Hut tree, seeded on a rotation curve built from a central bulge plus a flat-rotation dark-matter halo. Because inner stars have far higher angular speed than outer ones, an initial spiral perturbation shears and self-reinforces into trailing arms.

🎮 How to Use

Drag to pan, scroll to zoom. Adjust star count, bulge and halo mass, velocity dispersion, arm seed, gravity G and softening. Raise the halo slider live and watch the outer rotation speed flatten out — the classic dark-matter signature.

💡 Did You Know?

Differential rotation would wind a fixed set of spiral-arm stars into a tight coil within a few galactic rotations. Real arms survive because they behave as density waves — a slow-moving jam that different stars pass through and light up, over and over.

About this simulation

This is a top-down twin of Spiral Galaxy built on a different mechanic: instead of scattering points along a fixed procedural spiral, every star here starts on a physically motivated near-circular orbit and is then evolved by real N-body gravity. A Barnes-Hut quadtree sums the mutual pull of the bulge and every star each frame in O(N log N) time, a static isothermal dark-matter halo keeps the rotation curve flat at large radius, and differential rotation — inner stars sweeping around far faster than outer ones — shears an initial two-armed seed perturbation into a genuine, evolving spiral.

🔬 What it shows

Real gravitational dynamics: rotation-curve initialisation from bulge + halo mass, Barnes-Hut N-body integration, and spiral structure that emerges from differential rotation acting on a density perturbation rather than from a hard-coded spiral formula.

🎮 How to use

Sliders set star count, bulge mass, halo (dark matter) mass, velocity dispersion, the initial arm seed fraction, gravitational constant G and softening ε. G, softening, bulge and halo mass apply live; star count, dispersion and arm seed take effect on Restart. Drag to pan, scroll to zoom.

💡 Why differential rotation matters

Because orbital angular speed falls with radius, any straight line of stars drawn through the disk winds into a spiral within a few rotations — the same shearing that keeps re-shaping the arms here, frame by frame, as physics rather than animation.

Frequently asked questions

How is this different from the 3D Spiral Galaxy simulation?

The 3D version places up to 200,000 points directly onto a procedural logarithmic-spiral formula and rotates the whole disk rigidly — a geometric model, not a gravity solver. This 2D version instead integrates real Newtonian gravity between the bulge and every star with a Barnes-Hut quadtree, so orbits, differential rotation and spiral winding are all computed outcomes, not preset shapes.

What is differential rotation and why does it create spiral arms?

In a galaxy, stars closer to the centre complete an orbit much faster (in angle per unit time) than stars further out. If you start with a straight or lightly perturbed line of stars, that speed difference shears it into a trailing spiral within a few rotations — exactly what the "Arm seed" stars do here once the simulation starts running.

What does the dark-matter halo slider actually change?

It adds a smooth, non-responsive background mass whose gravity flattens the rotation curve at large radius, following v² = GM_halo/(r+a). Raise it while the simulation runs and the outer stars visibly speed up and stay roughly constant with radius instead of slowing down — the same flat-rotation-curve evidence that convinced astronomers most galactic mass is invisible.

Is the gravity in this simulation real physics or just for show?

It is real: every star's acceleration each frame comes from summing Newtonian gravity (softened at short range) from the bulge and every other star, sped up with a Barnes-Hut quadtree instead of being approximated away. You can even toggle Barnes-Hut off to fall back to exact O(N²) pairwise summation and confirm the results are consistent.

⚙ Under the hood

A real gravitational N-body simulation of a spiral galaxy seen from above: Barnes-Hut self-gravity, differential rotation from a bulge-plus-halo rotation curve, and spiral arms that wind and evolve on their own — computed, not drawn.

CanvasPhysicsAstronomyN-body

2D · HTML5 Canvas 2D · 60 FPS target · runs fully client-side, no install

What did you find?

Add reproduction steps (optional)