Intrepid Universe Logo

N-Body Gravitational Simulator | Orbital Mechanics

Published 18 March 2026

IU Home > Projects > n-body-simulator
Explore gravitational physics with our interactive N-Body Simulator. Simulate orbital mechanics using symplectic leapfrog integration in your browser.

Introduction

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.

Live Demo

Adjust the number of particles and watch gravitational physics unfold. The simulation uses symplectic integration for numerically stable long-term orbital predictions.

0%

How It Works

The N-Body Problem

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:

F = G · m 1 · m 2 r 2

Where:

  • F is the gravitational force
  • G is the gravitational constant (scaled for this simulation)
  • m1 and m2 are the masses of the two particles
  • r is the distance between the particles

Symplectic Integration (Leapfrog Method)

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:

  1. Half-step velocity update: V(n+1/2)=V(n)+F(n)·DT2·m
  2. Full-step position update: X(n+1)=X(n)+V(n+1/2)·DT
  3. Calculate new forces: F(n+1) based on X(n+1)
  4. Final half-step velocity update: V(n+1)=V(n+1/2)+F(n+1)·DT2·m

Softening Parameter

A softening parameter is added to prevent singularities when particles get very close together. Without softening, the gravitational force would approach infinity as r approaches zero. The formula becomes:

F = G · m 1 · m 2 r 2 + ε 2

Where ε is the softening length.

Torus Initialization

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:

v = G · M sun r

The particles then interact with each other, creating complex gravitational dynamics including spiral arm formation and density waves.

The Code

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.

Applications

This type of n-body simulation has applications in:

  • Astrophysics: Modeling galaxy formation, star clusters, and planetary systems
  • Molecular dynamics: Simulating particle interactions in materials
  • Fluid dynamics: SPH (Smoothed Particle Hydrodynamics) methods
  • Computer graphics: Procedural animation and visual effects

Further Reading