RepositoryC++
MarcVivas

MarcVivas/N-body

N-body simulation made with C++ and OpenGL

56stars
12forks
0issues
0views

Featured achievements

The project's latest awards

1
TINIX TRENDING·Topic: nbody-simulation

#1 Repository of the month

2
TINIX TRENDING·Topic: particles

#2 Repository of the month

2
TINIX TRENDING·Topic: gravity-simulation

#2 Repository of the month

N-body Simulation

License: MIT

A 3D N-body simulation project developed to explore C++, OpenGL graphics, CPU and GPU parallel programming techniques (OpenMP & Compute Shaders). It simulates particle interactions (e.g., gravitational) using various algorithms from brute-force to Barnes-Hut, allowing for performance comparisons.

Simulation Showcase Demonstration of a simulation run with 37.000 particles

Overview

Overview Overview Overview

Table of contents

Controls

Key/InputAction
EscClose the program
SpacePause/start the simulation
Mouse scrollZoom in/out
Click and dragMove the camera
BActivate/Deactivate bloom
IIncrease bloom intensity
DDecrease bloom intensity
QEnable/disable point size

How to run the project

  1. Linux
  2. Windows

Linux

Previous requirements

You need to have installed:

  • git
  • xorg-dev
  • cmake
  • make

Run the program

This command will create a new directory titled build and change the location to it.

mkdir build && cd build

Build a Makefile using cmake.

cmake ..

Compile the program using the Makefile.

make

Run the compiled program.

./N-body

Windows

Previous requirements

  • git
  • cmake
  • OpenMP

Compile and run the program

These commands will create a new directory titled build and change the location to it.

mkdir build 
cd build

Generate the build files.

cmake ..

Compile the program.

cmake --build . --config Release

Run the executable.

.\Release\N-body.exe

Program arguments

  • -v (number) Configure which version you want to use.
  • -i (number) Configure which initialization you want (How the bodies are initialized).
  • -n (number) Configure how many bodies you want to simulate.
  • -t (decimalNumber) Configure the step size. (Smaller the more precise)
  • -s (decimalNumber) Configure the squared softening.
  • -f (path/to/file) Provide a particle system file.

The default arguments are:

./N-body -v 1 -i 2 -n 100 -t 0.0035 -s 40.0

Alternative usage:

./N-body -version 1 -init 2 -n 100 -time-step 0.0035 -softening 40.0

Available Versions

These are the available versions you can try:

VersionDescriptionTime Complexity
-v 1Naïve particle-particle interaction computed sequentially on the CPU. Every particle interacts with every other particle directly.O(n^2)
-v 2Parallelized version of -v 1 using OpenMP for CPU multithreading, improving performance.O(n^2)
-v 3GPU-accelerated particle-particle interaction using compute shaders. Leverages massive parallelism but still follows a brute-force approach.O(n^2)
-v 4Optimized version of -v 3 based on NVIDIA's Fast N-body Simulation, reducing memory bottlenecks and improving GPU efficiency.O(n^2)
-v 5Iterative, stackless Octree Barnes-Hut algorithm computed sequentially on the CPU. Approximates distant particle groups as single bodies to reduce complexity.O(n log n)
-v 6Fully parallel CPU Barnes-Hut implementation. Both tree construction and force calculations are parallelized using OpenMP.O(n log n)
-v 7Fully parallel GPU Barnes-Hut implementation. Both tree construction and force calculations are performed on the GPU for maximum parallelism.O(n log n)

Important Note on Barnes-Hut Versions (-v 5, -v 6, -v 7):

These implementations have known stability issues and bugs. They are prone to crashing, especially with large particle counts (e.g., >700k) or when particles approach each other very closely. The performance may also be suboptimal compared to theoretical expectations. Due to the significant difficulty in debugging these complex parallel algorithms, these issues are unlikely to be fixed. For stable simulations, please use the brute-force versions (-v 1 to -v 4).

If you want to learn how it works read BarnesHutExplained.md

Available initializations

You can try the next initializations:

  • -i 1 Particles form a cube.
  • -i 2 Particles form a disk.
  • -i 3 Particles form an equilateral triangle. (Only 3 particles)
  • -i 4 Particles form a sphere. (Only the surface)
  • -i 5 Particles form a ball.
  • -i 6 Particles form a cube. (Only the surface)

Particle system file

The files you give to the program must follow this format:

Particle System with 3 particles:
Particle ID: 0
Position: (5.8337, 4.6008, 3.35599)
Velocity: (-1.15428, 1.8317, 0)
Acceleration: (0, 0, 0)
Mass: 0.625
Particle ID: 1
Position: (2.13544, 2.37607, 3.12727)
Velocity: (1.01254, -1.94254, 0)
Acceleration: (0.4, 3.0, 0)
Mass: 0.875
Particle ID: 2
Position: (2.74389, 2.46503, 3.42229)
Velocity: (0.0960527, 2.17343, 0)
Acceleration: (0, 0, -8.0)
Mass: 0.875
Particle ID: 3
Position: (3.38766, 0.977181, 2.78489)
Velocity: (1.10174, 2.40912, 0)
Acceleration: (-8.0, 0, 0)
Mass: 0.625

Note: The world dimensions are (5, 5, 5)

This is how you can use the -f argument:

./N-body -f path/to/file

Program structure

Here you can see the class diagram. It should be relativley easy to add new initializations and versions.

Class diagram

An example of how a new simulation version can be added is by creating a new class that implements the ParticleSolver interface and then including the new version in the list of available version options in the ArgumentsParser class. This way, the user can select the new version through the input arguments.

Similarly, to add a new initialization, a new class that implements the ParticleSystemInitializer interface can be created and the new option can be added to the list of initialization options in the ArgumentsParser class.

Finally, you would have to use the new initalization or version you created in main.cpp.

Similar Projects

Discover other similar projects and AI models.

RepositoryC++
RPCS3/rpcs3

PlayStation 3 emulator and debugger

20.5k
Nov 29, 2026
RepositoryC++
olive-editor/olive

Free open-source non-linear video editor

9.2k
Dec 5, 2024
RepositoryC++
dfranx/SHADERed

Lightweight, cross-platform & full-featured shader IDE

4.8k
Sep 22, 2023
RepositoryC++
BrutPitt/glChAoS.P

3D GPUs Strange Attractors and Hypercomplex Fractals explorer - up to 256 Million particles in RealTime

861
Feb 10, 2025
RepositoryC++
PWhiddy/Nbody-Gravity

A small but powerful nbody gravity simulator with a built-in renderer

113
Oct 10, 2022
RepositoryC
fogleman/Craft

A simple Minecraft clone written in C using modern OpenGL (shaders).

11.2k
Apr 3, 2024
RepositoryJava
LWJGL/lwjgl3

LWJGL is a Java library that enables cross-platform access to popular native APIs useful in the development of graphics (OpenGL, Vulkan, bgfx), audio (OpenAL, Opus), parallel computing (OpenCL, CUDA) and XR (OpenVR, LibOVR, OpenXR) applications.

5.5k
Sep 10, 2026
RepositoryC
moderngl/moderngl

Thin Python layer over OpenGL. No dependencies, headless rendering, compute shaders. Subset runs in the browser via Pyodide + WebGL2.

2.4k
Nov 29, 2026
RepositoryJavaScript
anvaka/fieldplay

A vector field explorer

1.3k
Nov 22, 2026