nbody.mp4 |
Real-time 3D N-body gravity simulation with Barnes-Hut octree optimization and OpenGL rendering. Built with C++17, OpenGL 4.6, GLFW, GLM, and GLAD. |
|---|
Key Features:
- Barnes-Hut octree for O(n log n) force calculations
- OpenGL 4.6 rendering with point sprites and alpha blending
- Interactive 3D camera (orbit, zoom, height, FOV)
- Velocity-based and mass-based coloring modes
- Dynamic particle sizing based on mass
- Single and double galaxy initialization
- Multi-threaded with OpenMP (optional)
- Pause/resume simulation
- FPS display in title bar
![]() |
![]() |
![]() |
|---|---|---|
| Full galaxy overview | Double galaxy option | Velocity and mass based color/size mapping |
Represents a single body with position, velocity, acceleration, mass, and radius. Provides velocity-based and mass-based coloring via getColor().
Implements the Barnes-Hut octree for spatial partitioning. Recursively subdivides 3D space into octants, approximating distant groups as single center-of-mass nodes. Computes gravitational acceleration for each body in O(n log n).
Core simulation manager. Initializes bodies (single or double galaxy), builds the octree each step, computes gravitational forces, and integrates positions/velocities with configurable timestep.
Orbital camera with mouse-driven rotation, scroll zoom, keyboard height/position control, and dynamic FOV adjustment. Outputs view and projection matrices for the renderer.
Galaxy initialization functions. uniform_disc() generates a single disc-shaped galaxy with a central massive body. uniform_disc_bin() generates a double galaxy system. Bodies are placed with radial distribution and Keplerian orbital velocities.
Application entry point. Initializes GLFW window with OpenGL 4.6 Core Profile, compiles vertex/fragment shaders, manages GPU buffers (VAO, VBO for positions, colors, sizes), runs the main loop (poll events → simulate → render), handles input callbacks, and cleans up resources.
Shaders:
- Vertex: Transforms positions via view/projection matrices, passes per-particle color and size
- Fragment: Draws smooth circles with
smoothstepalpha and additive blending
- CMake 3.16+
- C++17 compiler (MSVC recommended on Windows)
- OpenGL 4.6 capable GPU/driver
- GLFW 3.3 (via vcpkg or system install)
- Install GLFW via vcpkg:
vcpkg install glfw3:x64-windows
- Open the project folder in Visual Studio (File → Open → Folder).
- Visual Studio detects
CMakeLists.txtand configures the project automatically.- If using vcpkg, ensure the toolchain is set: in
CMakeSettings.jsonor via theVCPKG_ROOTenvironment variable.
- If using vcpkg, ensure the toolchain is set: in
- Select the
Nbodytarget, build and run.
cmake -S . -B build -DCMAKE_TOOLCHAIN_FILE=%VCPKG_ROOT%/scripts/buildsystems/vcpkg.cmake
cmake --build build --config Release
./build/Release/Nbody.exe| Key | Action |
|---|---|
W/A/S/D |
Move camera |
Q/E |
Move camera target down/up |
R |
Toggle mouse cursor |
P |
Pause/resume simulation |
ESC |
Exit |
| Mouse drag | Rotate view |
| Scroll | Zoom in/out |
Simulation settings (main.cpp):
const int N = 30000; // Number of bodies
const int type = 1; // 0 = single galaxy, 1 = double galaxy
const int typeColor = 0; // 0 = velocity-based, 1 = mass-basedCamera defaults (Camera.h):
#define SENSITIVITY 0.08f
#define ZOOM 70.0fSimulation parameters (Simulation.h):
float theta = 1.0f; // Barnes-Hut opening angle
float epsilon = 0.1f; // Softening factor
unsigned int leaf_capacity = 4; // Max bodies per octree leaf
float cutoffDistance = 1500.0f; // Force cutoff distance- Barnes-Hut octree with configurable theta and cutoff distance
- Two galaxy initialization modes (single and binary)
- Velocity-based and mass-based coloring with smooth gradients
- Mass-proportional particle sizing
- Interactive 3D orbital camera with full keyboard/mouse controls
- OpenMP parallelization for buffer updates
- Modern OpenGL 4.6 rendering with custom shaders
- Pause/resume functionality
- Dynamic window resizing with aspect ratio adaptation
- GUI panel (ImGui) for runtime parameter tuning
- GPU compute shaders for force calculation
- Collision detection and merging
- Save/load simulation state
- Trail/trajectory visualization
- Additional galaxy shapes (spiral arms, elliptical)


