Back Original

Show HN: Browser-based interactive 3D Three-Body problem simulator

About the N-Body Simulator

What is the Three-Body Problem?

The three-body problem is one of the most famous challenges in classical physics and celestial mechanics. It asks: given the initial positions, masses, and velocities of three bodies in space, can we predict their future motion under mutual gravitational attraction?

Unlike the two-body problem (which has an exact analytical solution), the three-body problem has no general closed-form solution. This makes numerical simulation the primary tool for studying these complex gravitational systems.

N-Body Gravitational Simulation

This simulator uses Newton's law of universal gravitation to model the gravitational forces between every pair of bodies:

F = G × m₁ × m₂ / (r² + ε²)

Each body experiences the sum of all pairwise gravitational forces from every other body. For N bodies, this requires calculating N(N-1)/2 force pairs each timestep. The ε² term is a softening parameter that prevents numerical singularities when bodies pass very close together.

The simulation supports multiple integration methods. By default, it uses the Velocity Verlet integration method, a symplectic integrator that provides superior energy conservation compared to simpler methods like Euler integration. This makes it ideal for long-term orbital mechanics simulations.

Users can switch to the 4th-order Runge-Kutta (RK4) method in the Advanced Settings, which offers higher accuracy per timestep and typically shows lower energy drift in short simulations. However, RK4 is not symplectic and accumulates systematic phase errors over long simulation times, causing orbits to gradually decay or expand. This makes RK4 better suited for short to medium duration simulations where minimizing instantaneous error is the priority, while Verlet excels at maintaining correct orbital shapes over extended periods.

Preset Configurations

The simulator includes several famous periodic three-body orbits discovered through numerical searches:

N-Body Presets Showcase

2D Orbits

  • Figure-8 choreography: Discovered by Cris Moore in 1993, where three equal masses chase each other along a figure-eight shaped path
  • Lagrange triangular configuration: Equilateral triangle configuration with circular orbits.
  • Butterfly, Broucke, Hénon, and Yarn: Periodic orbits from the Šuvakov-Dmitrašinović database of three-body choreographies, discovered through systematic numerical exploration of initial conditions

3D Orbits

Three-dimensional periodic orbits from Li and Liao (2025), which discovered 10,059 new periodic solutions including 21 choreographic orbits and 273 "piano-trio" orbits (where two equal-mass bodies share one orbit while a third body follows another). Paper | GitHub

Features & Applications

  • Real-time Physics: Experience gravitational dynamics in 3D with interactive controls
  • Multiple Integration Methods: Choose between Velocity Verlet (energy-conserving) and RK4 (high accuracy).
  • Exploration Platform: Experiment with different initial conditions and masses
  • Timeline Playback: Scrub through simulation history to analyze orbital behavior

How to Use

Getting Started: Use the preset configurations (Figure-8 or Lagrange) to see stable three-body orbits, or generate random initial conditions to explore chaotic dynamics.

Controls: Adjust body masses, simulation speed, and physics parameters. Use the timeline to review and analyze orbital patterns. Drag bodies while paused to create custom configurations.

Sharing: Click "Share Configuration" to generate a URL that preserves your exact simulation initial state.

Energy Conservation & Simulation Accuracy

The simulator displays two important energy metrics in the Advanced Settings panel:

  • Total Energy: The sum of kinetic energy (½mv²) and gravitational potential energy (-Gm₁m₂/r) of all bodies. In an ideal gravitational system, this value should remain constant over time.
  • Energy Drift: The percentage change in total energy from the initial state. This measures the numerical accuracy of the simulation.

In real physics, energy is conserved in isolated systems. However, numerical integration methods introduce small errors at each timestep. The energy drift indicator helps you evaluate simulation quality:

  • Green (<1%): Excellent energy conservation - the simulation is highly accurate
  • Yellow (1-5%): Moderate drift - acceptable for most purposes but consider reducing timestep
  • Red (>5%): Significant drift - simulation may be unreliable, reduce timestep or try other integration methods

The Velocity Verlet integration method is "symplectic," meaning it preserves the phase-space structure of Hamiltonian systems. While RK4 typically shows lower energy drift in short-term simulations (better local accuracy), Verlet prevents systematic phase errors that accumulate over extended simulations. This makes Verlet ideal for long-term orbital mechanics where maintaining orbital stability over thousands of periods is more important than minimizing instantaneous error.

Why is Total Energy Negative? In gravitational systems, total energy is often negative, and this is perfectly normal! Gravitational potential energy is defined as zero at infinite separation and becomes increasingly negative as bodies move closer together (PE = -Gm₁m₂/r). When total energy is negative, it means the system is gravitationally bound - the bodies don't have enough kinetic energy to escape to infinity, so they remain in orbit. This is exactly what you see in stable orbital systems like planets around stars or the choreographed orbits in this simulator. A negative total energy that remains constant indicates a stable, bound orbital system.

Technical Details

Built with Three.js for WebGL-accelerated 3D graphics and modern JavaScript. The physics engine implements N-body gravitational calculations with a configurable softening parameter to prevent numerical singularities during close encounters.

The simulator tracks up to 10,000 frames of history, allowing you to review and analyze the evolution of complex orbital systems. All simulations are deterministic and reproducible.

Feedback

Have suggestions, found a bug, or want to share your thoughts? Give feedback and help improve this simulator!