N-Body Simulator – Interactive 3 Body Problem & Gravitational Physics Simulation

About 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 simulations 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 each pair of bodies:

F = G × M₁ × M₂ / (R² + ε²)

Every body experiences it sum of all paired gravitational forces From every other body. For n bodies, each timestep requires the calculation of n(n-1)/2 force pairs. The ε² term is a soft 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 better energy conservation than simpler methods such as Euler integration. This makes it ideal for long-term orbital mechanics simulations.

Users can switch to the fourth-order Runge–Kutta (RK4) method in advanced settings, which provides higher accuracy per timestep and typically shows lower energy drift in shorter simulations. However, RK4 is not symplectic and accumulates systematic phase errors over long simulation times, causing orbits to slowly decay or expand. This makes RK4 better suited for short to medium term simulations where minimizing instantaneous error is the priority, while Verlet excels at maintaining the correct orbital shape over extended periods.

preset configuration

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

N-Body preset showcase

2d classes

  • Picture-8 Choreography: Discovered by Chris Moore in 1993, where three identical masses pursue each other on a figure eight shaped path
  • Lagrange triangular configuration: Equilateral triangle configuration with circular orbits.
  • Butterfly, Broke, Hennon and Yarn:
    Periodic orbits from the Shuvakov–Dmitrasinovich database of three-body choreographies discovered through systematic numerical exploration of initial conditions.

3d classes

Three-dimensional periodic orbits from Li & Liao (2025), who discovered 10,059 new periodic solutions, including 21 choreographic orbits and 273 “piano-trio” orbits (where two equal mass bodies share an orbit while a third body follows the other). Paper GitHub

Features and Applications

  • Real Time Physics: Experience gravity 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: Explore simulation history to analyze orbital behavior

how to use

launch: Use preset configurations (Figure-8 or Lagrange) to look at stable three-body orbits, or generate random initial conditions to explore chaotic dynamics.

Control: Adjust body mass, simulation speed and physics parameters. Use the timeline to review and analyze orbital patterns. Drag frozen bodies to create custom configurations.

Sharing: Click “Share Configuration” to generate a URL that preserves your exact simulation starting state.

Energy conservation and simulation accuracy

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

  • Total Energy: The sum of the 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 Flow: Percentage change in total energy from the initial state. It 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 the simulation quality:

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

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

Why is total energy negative? In gravitational systems, the total energy is often negative, and this is absolutely normal! Gravitational potential energy is defined as zero at infinite separation and becomes increasingly negative as the bodies get closer together (PE = -Gm₁m₂/r). When the total energy is negative, it means that the system is negative
bound by gravity – Bodies do not have enough kinetic energy to escape to infinity, so they remain in orbit. This is exactly what you see in stable orbital systems such as 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 gravity 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

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



Leave a Comment