Visualizations
097 / 500

097 · Astronomy

Galaxy Collision

Two spiral galaxies collide and fling out long tidal tails.

Each galaxy is a heavy core orbited by thousands of stars that feel the gravity of both cores but not of each other, the method Toomre and Toomre used in 1972 to explain tidal tails. As the galaxies swing past each other, tides pull stars into long arcing tails and bridges like the Antennae. Drag from the dark halos makes the cores spiral in and merge, then two new galaxies are launched on a fresh orbit. Stars are colored by population and drawn additively with glow and dust.

Try it. Drag to rotate the view. Click to launch a new collision.

  • Restricted N-body gravity
  • Leapfrog integration
  • Additive star rendering

View the source · one module, plus a small shared runtime for sizing, the animation loop and input

Build your own

Paste this into Claude Code, Codex or any coding agent to get a simple version running, then take it wherever you like.

Build a galaxy collision simulation with JavaScript and the HTML canvas element. Put everything in a single index.html file with no libraries or build step, so I can open it directly in a browser.

Start simple:
- Make a canvas that fills the window, stays sharp on high-DPI screens, and resizes with the window. Paint it near-black.
- Use the restricted three-body approach from Toomre and Toomre (1972): two heavy galaxy cores that attract each other, and thousands of massless stars that feel both cores but not each other.
- Use a softened gravity law, acceleration = M * d / (|d|² + eps²)^1.5, so stars passing close to a core do not explode.
- Build each galaxy as a flat disk of about 5,000 stars on circular orbits around its core, with speed sqrt(r * |a(r)|). Tilt the two disks differently.
- Start the cores far apart on a near-parabolic orbit so they swing past each other.
- Integrate with a leapfrog (drift, kick, drift) at a fixed time step, several steps per frame, and store positions and velocities in Float32Arrays.
- Project in 3D with a slowly orbiting perspective camera and draw each star as a single pixel.

Once that works, make it beautiful:
- Accumulate stars additively into a float buffer, tone map it with 1 - exp(-x), and add a soft glow by drawing a shrunken copy of the frame back on top with "lighter".
- Color stars by population: a warm yellow bulge, white disk stars, and blue young stars.
- Add a small drag on the cores when they overlap, standing in for dynamical friction, so they merge after a pass or two. Then launch a new encounter.
- Let the visitor drag to rotate the view and click to restart.

Explain the key ideas in short code comments. When you're done, tell me how to open it and suggest three directions I could take it next, such as density-wave spiral arms before the encounter, dust lanes that absorb light, or unequal masses and retrograde disks.
PreviousPetri FractalsBacterial colonies branch into fractals on starved agar, in a backlit petri dish. NextFPUT RecurrenceThe 1955 computer experiment where a chain of masses refused to thermalize.

Related visualizations

  • Dark Matter CurveAstronomy Switch off the dark halo and watch a spiral galaxy fly apart.
  • Spiral GalaxyAstronomy Tens of thousands of stars wheel through glowing spiral arms.
  • Lensing ClusterAstronomy Drag a galaxy cluster and its gravity bends a deep field into giant arcs.
  • Stellar VampireAstronomy A star overflows its Roche lobe and feeds a white dwarf's disk until it ignites a nova.
  • Planet ForgeAstronomy A dusty disk grows rocky worlds, ice giants and gas giants by N-body accretion.
  • Cosmic Light ConeAstronomy Our past light cone, horizons and galaxies in the expanding universe, drawn three ways.
  • CometAstronomy A comet rounds the Sun and grows a straight blue ion tail and a curved dust tail.
  • ChirpPhysics Two black holes spiral together and ring spacetime, from post-Newtonian formulas.
  • TwinkleAstronomy Turbulent air boils a star into speckles until adaptive optics snaps it sharp.

Use ← and → to move between demos. While the canvas has focus, keys go to the demo instead.

← More from Emergent Mind Labs