Published 18 March 2026
The N-Body Simulator is an interactive gravitational simulation that demonstrates the N-body problem in physics. Using Newton's law of gravitation, it calculates the gravitational forces between multiple particles and simulates their orbital mechanics in real-time.
Whether you're interested in orbital mechanics, stellar dynamics, or galaxy formation, this simulator provides a visual way to explore these concepts.
Adjust the number of particles and watch gravitational physics unfold. The simulation uses symplectic integration for numerically stable long-term orbital predictions.
The N-body problem describes the motion of multiple bodies that interact gravitationally with each other. Unlike the two-body problem, which has analytical solutions, N-body systems with N greater than two generally require numerical methods to simulate.
This gravity simulation calculates the gravitational force on each particle using Newton's law:
Where:
The simulator uses leapfrog integration, also known as the Velocity Verlet method, which is a type of symplectic integrator.
Unlike standard numerical integration methods (like Euler or Runge-Kutta), symplectic methods preserve the energy of the system. This makes them ideal for orbital mechanics where you want stable, long-term predictions without the orbit slowly spiraling inward or outward due to numerical drift.
The leapfrog algorithm works as follows:
A softening parameter is added to prevent singularities when particles get very close together. Without softening, the gravitational force would approach infinity as approaches zero. The formula becomes:
Where ε is the softening length.
Particles are initialized in a torus (donut-shaped) region around the central mass (the "sun"). This creates a disk-like structure reminiscent of spiral galaxies. Each particle is given an initial orbital velocity calulated to place it in a roughly circular orbit:
The particles then interact with each other, creating complex gravitational dynamics including spiral arm formation and density waves.
The simulator is implemented in approximately 200 lines of vanilla JavaScript:
// Particle class holds position, velocity, mass, and force components
class Particle {
constructor(x, y, vx, vy, m) {
this.x = x;
this.y = y;
this.vx = vx;
this.vy = vy;
this.m = m;
this.fx = 0; // Force x
this.fy = 0; // Force y
}
}
// The Kick-Drift-Kick leapfrog integration step
function leapfrogStep() {
// Step 1: Half-kick (velocity update)
for (let p of particles) {
p.vx += p.fx / p.m * (DT / 2);
p.vy += p.fy / p.m * (DT / 2);
}
// Step 2: Full drift (position update)
for (let p of particles) {
p.x += p.vx * DT;
p.y += p.vy * DT;
}
// Step 3: Calculate new forces at new positions
calculateForces();
// Step 4: Final half-kick
for (let p of particles) {
p.vx += p.fx / p.m * (DT / 2);
p.vy += p.fy / p.m * (DT / 2);
}
}
The complete source code is embedded in this page and runs entirely in your browser using the HTML5 Canvas API. No plugins or server-side processing are required.
This type of n-body simulation has applications in:
javascript
HTML5 Canvas
Physics Simulation