N-Body Gravitational Simulator Back
Space Physics / Orbital Mechanics

N-Body Gravitational Simulator

Place stars, planets and asteroids and watch gravitational orbital dynamics, collisions and mergers in real time. Figure-8, solar system, binary star, and galaxy presets included.

Statistics
Presets
Body Type
⭐ Star
🪐 Planet
🌑 Asteroid
Physics Parameters
Display

Theory Notes

Force on body i:

F_i = Σⱼ G·mᵢ·mⱼ/(r²+ε²) · r̂ᵢⱼ

ε: softening length (prevents divergence)
Integration: Velocity Verlet
Collision: merge with momentum conservation

Figure-8 solution (Chenciner-Montgomery 2000): three equal-mass bodies trace a stable figure-8 curve — a rare oasis of order within chaotic three-body dynamics.

CAE link: Molecular dynamics, SPH particle method, DEM discrete element method

Results
0
Bodies
0
Time (years)
Total Energy
Max Mass
Sim
Bodies: 0
Time: 0.00 yr
Total Energy:
Max Mass Body:
Click: place body  /  Drag: place body with velocity
Theory & Key Formulas

$$\mathbf{F}_{ij} = G\frac{m_i m_j}{|\mathbf{r}_{ij}|^2}\hat{\mathbf{r}}_{ij}$$

万有引力:\(G = 6.674\times10^{-11}\) N·m²/kg²、\(\mathbf{r}_{ij} = \mathbf{r}_j - \mathbf{r}_i\)

$$\ddot{\mathbf{r}}_i = \sum_{j \neq i} \frac{G m_j}{|\mathbf{r}_{ij}|^2 + \epsilon^2}\hat{\mathbf{r}}_{ij}$$

ソフトニングパラメータ \(\epsilon\):近接時の発散を防止

$$E = \frac{1}{2}\sum_i m_i v_i^2 - \sum_{i<j}\frac{G m_i m_j}{r_{ij}} = \text{const}$$

力学的エネルギー保存:数値精度の検証に使用

What is N-Body Gravitational Simulation?

🙋
What exactly is an "N-body" simulation? Is it just a fancy way of saying "planets moving around"?
🎓
Basically, yes, but with a crucial twist. It's a simulation where every body (planet, star, moon) feels the gravitational pull from every other body at the same time. In practice, this means the motion is chaotic and incredibly complex. For instance, in our solar system, Jupiter doesn't just orbit the Sun; it also tugs on Saturn, Earth, and everything else. Try moving the "Orbital Trails" slider on and off in the simulator above—you'll see the beautiful, non-repeating paths that emerge from this mutual attraction.
🙋
Wait, really? So it's not just one central sun? How do you even calculate where everything goes next?
🎓
Exactly! There's no single center. The computer solves Newton's law of gravity for every single pair of objects, sums up all the forces, and then calculates new positions and velocities—over and over, frame by frame. A common case is a binary star system, where two stars orbit their common center of mass. Turn on the "Force Vectors" parameter to see the arrows representing the net gravitational force on each body. You'll notice they're rarely pointing to the center of the screen!
🙋
That sounds intense. What's this "figure-8 solution" mentioned in the description? Is that even stable?
🎓
Great question! It's one of the most fascinating results in celestial mechanics. It's a stable orbital configuration for three bodies of equal mass, where they chase each other along a single figure-8 path. It's a perfect example of the complex, balanced solutions that only emerge from full N-body physics. In the simulator, try creating three bodies with equal mass and just the right starting conditions—then turn on "Glow Effects" and "Velocity Vectors" to appreciate the symmetry and stability of their dance.

Physical Model & Key Equations

The simulation is governed by Newton's Law of Universal Gravitation and his Second Law of Motion. The gravitational force between any two bodies i and j is:

$$ \vec{F}_{g, i \rightarrow j}= G \frac{m_i m_j}{r_{ij}^2}\hat{r}_{ij}$$

Here, $G$ is the gravitational constant, $m_i$ and $m_j$ are the masses, $r_{ij}$ is the distance between them, and $\hat{r}_{ij}$ is the unit vector pointing from body i to body j. The force is always attractive.

To find the motion of a specific body, we sum the gravitational forces from ALL other (N-1) bodies to get the net force. Then, we use Newton's Second Law to find the acceleration:

$$ \vec{F}_{net, j}= \sum_{i \neq j}^N \vec{F}_{g, i \rightarrow j}= m_j \vec{a}_j $$

This gives us $\vec{a}_j$. The simulator then numerically integrates this acceleration over tiny time steps to update the body's velocity and position, a process repeated for all N bodies every frame. This is the core computational challenge of the N-body problem.

Frequently Asked Questions

Yes, it is possible. You can freely place stars and planets on the canvas and set initial velocities (vectors) to simulate arbitrary orbits. If you want to reproduce stable orbits such as figure-8 orbits, it is recommended to refer to known initial conditions.
This is due to the time step being too large or calculation errors when celestial bodies approach extremely close. Reducing the time step in the simulation settings or enabling adaptive step size improves stability. Also, caution is needed when the mass ratio is extreme.
By default, colliding bodies merge into a single new body, conserving mass and momentum. The velocity after merging is calculated according to the law of conservation of momentum. It is also possible to disable merging upon collision in the settings and make them bounce off instead.
It is possible, but you need to input the exact masses, orbital radii, and initial velocities of the real planets. However, due to the accumulation of calculation errors, long-term stable reproduction is difficult, so we recommend using it for short-term orbital visualization and understanding of dynamics.

Real-World Applications

Solar System & Exoplanet Dynamics: Astronomers use N-body simulations to model the long-term stability of our solar system and to predict the orbital configurations of newly discovered exoplanet systems. They can test if a proposed system of planets would survive for billions of years or if one would be ejected.

Galaxy Formation & Evolution: On a colossal scale, cosmologists simulate the gravitational interaction of billions of stars and dark matter particles to understand how galaxies like the Milky Way form, merge, and evolve over cosmic time.

Space Mission Trajectory Planning: When planning a mission like the Voyager probes, engineers use precise N-body calculations (including the Sun, planets, and major moons) to plot gravity-assist flybys, saving immense amounts of fuel by using planetary gravity as a slingshot.

Star Cluster Dynamics: In dense globular clusters, stars can undergo close encounters, gravitational slingshots, and even collisions. N-body simulations help astrophysicists understand phenomena like "core collapse" and the creation of binary star systems through dynamical interactions.

Common Misconceptions and Points to Note

First, you might think "the results are accurate because it's running in real-time," but that is a major misconception. This simulator prioritizes calculation speed and clarity of visualization. For instance, if you set the time step Δt too large, "numerical instability" can occur, where energy is not conserved and orbits continuously expand or conversely fall into the center. In practical applications, careful settings suited to the phenomenon are necessary, such as using a time step smaller than 1/1000th of the planet's orbital period.

Next, do not underestimate the "chaotic" nature where tiny differences in initial conditions can drastically change the results. For example, if you run a simulation with Earth's position shifted by just 1 meter, its orbit could be completely different hundreds of years later. This is not a bug in the simulator but an inherent property of the three-body problem. For experiments requiring reproducibility, it is a golden rule to precisely record and manage the values of initial conditions.

Finally, regarding the handling of "collisions". In this tool, stars merge when they overlap, but real celestial collisions are far more complex. They involve not just simple mergers, but also fragmentation, vaporization, and scattering of debris. Interpreting the simulator's results directly as reality is dangerous. In fields specializing in collision phenomena like "Spaceguard," other simulations using more detailed material models are required.

How to Use

  1. Set the number of bodies using slMassNum (2–10 objects); each body requires mass input via sl-mass in kilograms (e.g., 1.989e30 kg for solar mass).
  2. Configure initial velocities with slVelNum sliders (m/s); typical Earth orbit velocity is 29,780 m/s; adjust sl-vel for stable elliptical or hyperbolic trajectories.
  3. Set gravitational constant via slGNum (default 6.674e-11 m³·kg⁻¹·s⁻²); run simulation to observe orbital dynamics, three-body chaos, or collision mergers in real time.

Worked Example

Simulate a binary star system: Body 1 (1.989e30 kg, velocity 15,000 m/s) and Body 2 (1.5e30 kg, velocity 20,000 m/s) separated by 1 AU (1.496e11 m). Using standard G=6.674e-11, the simulator computes orbital period ≈ 0.87 years. Add a third body (mass 0.1e24 kg) at 0.5 AU with velocity 8,000 m/s to observe gravitational perturbations and potential ejection from the binary system.

Practical Notes

  1. Binary star figure-8 solution: Set three equal masses (≈1e30 kg each) with initial velocities 8,000–12,000 m/s; requires precise positioning to avoid immediate collision.
  2. Stable planetary systems: Sun (1.989e30 kg, stationary) plus planets at 0.387 AU (Mercury, v=47,360 m/s), 0.723 AU (Venus, v=35,020 m/s), and 1 AU (Earth, v=29,780 m/s) reproduce realistic orbital mechanics.
  3. Collision detection: Merger occurs when centers separate by sum of effective radii; monitor energy conservation (total should remain constant within 0.1% for stable orbits).