Skip to content
ExtendedSource checked

N-Body Simulation

Simulates many bodies interacting through forces such as gravity.

Quick Facts

Compact metadata for implementation and discovery.

Category
Simulation
Difficulty
Intermediate
Family
Particle Simulation
Tier
Extended
Status
Verified
Review
Source Checked
Sources
1
Formulas
1
Relationships
4
Code example
Not ready
Visualization
Ready

Overview

N-Body Simulation repeatedly computes interactions between bodies and integrates their motion through time.

Core Concepts

simulated statephysical stateparticle interactionparticle simulationstate updatetime stepnumerical stability

Quick facts

Complexity

Runtime and memory behavior at a glance.

Best

O(n)

Average

O(n^2)

Worst

O(t n^2)

Space

O(n)

When to Use

  • Use N-Body Simulation when you need to compute pairwise or approximated forces.
  • Use it for astronomy simulations with inputs like bodies with mass and position.
  • Use it when approximate behavior over time is more useful than a closed-form answer.
  • Use it when the Particle Simulation tradeoff is acceptable for the problem size.

When Not to Use

  • Avoid it when the time step, grid size, or sample count cannot support stable results.
  • Avoid it when O(t n^2) worst-case behavior is too costly for the expected input size.
  • Avoid it when the required output is not body trajectories.

What It Does

  • Computes pairwise or approximated forces
  • Updates body velocities and positions

Use Cases

  • Astronomy simulations
  • Particle systems
  • Physics education

Inputs

  • Bodies with mass and position
  • Force law
  • Time step

Outputs

  • Body trajectories

Key Formulas (1)

Pair force

force = G · m1 · m2 / distance2
force = G * m1 * m2 / distance^2

Computes gravitational-style pairwise interaction strength.

Pseudocode

  1. 1.For each body, compute forces from other bodies
  2. 2.Sum the forces
  3. 3.Update acceleration, velocity, and position
  4. 4.Repeat for each time step

Canonical Example

Input
position = 0, velocity = 2, time step = 0.5
Output
next simulated state
Explanation
N-Body Simulation: The algorithm advances the system by applying its update rule over a small step.

Implementation Notes

  • Keep the input contract explicit: Bodies with mass and position; Force law; Time step.
  • Use this step as the implementation spine: For each body, compute forces from other bodies.
  • Then preserve the next state change: Sum the forces.
  • Track units and invariants such as mass, energy, or count where they matter.

Common Pitfalls

  • Large N-Body simulations often use approximation structures to avoid all pair checks.
  • Using a time step that is convenient for rendering but unstable for the simulated system.
  • Returning body trajectories without handling empty, invalid, or boundary inputs.

Interactive Mechanism Walkthrough

Iterative Convergence & State Trajectory

Show bodies pulling on each other and moving through space.

N-Body Simulation mechanism walkthrough step 1 of 4x₀x₁x₂x*
Step 1 of 4: For each body, compute forces from other bodies
orbiting-bodies

Notes

N-Body Simulation operates in O(n^2) time and O(n) space within the Particle Simulation family. Key governing relation: force = G * m1 * m2 / distance^2. Often compared with Verlet Integration and Smoothed Particle Hydrodynamics.

Sources (1)

What each reference supports for this entry.

Computer Animation: Algorithms and Techniques

Book

Simulation reference covering N-Body Simulation integration, particle update, constraint, or animation workflow.

Explore Next

Good next jumps from the relationship graph, ranked for useful discovery.

Alternative toBarnes-Hut Simulation

Barnes-Hut simulation approximates n-body forces to reduce all-pairs interaction cost.

Tags

SimulationPhysicsParticles