N-Body Simulation

Gravity Makes
Everything

The Milky Way and Andromeda are on a collision course

At 110 km/s, Andromeda is approaching — one of the few galaxies in the universe moving toward us. In roughly 4 billion years, two spiral galaxies will become one elliptical giant.

Scroll to begin

One Force, Infinite Complexity

This simulation uses a single force: gravity. No dark energy, no magnetic fields, no gas dynamics. Just F = Gm₁m₂/r². Every tidal tail, every bridge of stars, every distortion you see emerges from this one equation applied 50,000 times per timestep.

The Barnes-Hut algorithm makes it possible. Instead of calculating 2.5 billion force pairs directly, distant particle groups are approximated by their center of mass. Complexity drops from O(N²) to O(N log N) — the difference between days and minutes of computation.

What you're watching isn't an animation. It's the actual output of gravitational physics, computed particle by particle.

Loading galaxy simulation...
0% - 50,000 particles

Stars Never Collide

Despite the violence of the merger, individual stars almost never collide. The average distance between stars is 160 billion kilometers — galaxies are mostly empty space. When these galaxies "collide," their stars pass through each other like two clouds of gnats.

Gas is different. When interstellar gas clouds collide, they compress and heat up, triggering massive bursts of star formation. The merger will light up with new stars — blue, hot, and short-lived. This "starburst" phase is visible in real merging galaxies like the Antennae Galaxies (NGC 4038/4039).

Our Solar System's fate? Earth will likely survive. We'll be flung to the outskirts of the new galaxy — perhaps 100,000 light-years from the core. The Sun still has 5 billion years of fuel. But our descendants will see a completely different night sky.

The Simulation

Barnes-Hut Tree

Space is recursively subdivided into octants. Distant particle groups are approximated by their total mass and center of mass. With opening angle θ = 0.5, this is accurate to within 1% while being 100x faster than direct summation.

Gravitational Softening

Each particle represents millions of stars. Close two-body encounters are numerical artifacts — real collisionless galaxies don't have them. Plummer softening (Φ = -Gm/√(r² + ε²)) keeps forces finite and suppresses artificial scattering.

Leapfrog Integration

The symplectic leapfrog integrator updates positions and velocities in a staggered pattern, naturally conserving energy over billions of years. It's the standard choice for collisionless stellar dynamics.

Initial Conditions

Each galaxy starts as an equilibrium exponential disk — particle positions and velocities sampled from a distribution function that satisfies the collisionless Boltzmann equation. The Milky Way has 25,000 particles, Andromeda has 25,000 at 3x the mass.

Will It Actually Happen?

Probably. Recent research (Sawala et al. 2025) revised the merger probability. A head-on collision in 4-5 billion years has only 2% probability. Including the Large Magellanic Cloud's gravitational influence, the merger probability within 10 billion years is about 50%.

The key revision factor: the LMC is more massive than previously thought (2 × 10¹¹ solar masses), and its gravity pulls the Milky Way off its orbital plane relative to Andromeda. If we include the Triangulum Galaxy (M33), the probability rises to 63%.

Galaxy mergers are fundamental to cosmic structure formation. Most massive elliptical galaxies formed through mergers. The Milky Way itself has absorbed dozens of smaller galaxies — the Sagittarius Dwarf is being torn apart right now, its stars streaming across our sky.

Further Exploration

Related Explainers

External Resources

Key References

  • Barnes & Hut 1986 — A hierarchical O(N log N) force-calculation algorithm
  • Rein & Liu 2012 — REBOUND: An open-source multi-purpose N-body code
  • Sawala et al. 2025 — The Andromeda-Milky Way merger: Revised probability