A Python/Pygame simulation implementing Newtonian gravity with Barnes-Hut optimization for efficient N-body force calculation.
The simulation visualizes the interactions between multiple bodies using gravitational attraction, while additionally supporting collisions, merging, camera movement, and randomly generated orbital systems.
More detailed information about the project and its technicalities is provided in the report: Project Report
- Barnes-Hut quadtree optimization
- Newtonian gravitational physics
- Elastic collisions with optional simple positioning fixing
- Optional anchoring and body merging
- Random orbital system generation
- Camera movement
- Real-time FPS, body count and node count metrics
- Support for 1000 bodies with optimized settings
Performance testing data was collected on an Apple M4 Max Mac Studio, using the standard orbital system configuration.
The graph compares the performance with and without the Barnes-Hut optimization to see the effect of the algorithm on simulation performance.
(The Barnes-Hut configuration used:
- BARNES_HUT_THETA = 0.5
- SKIP_REBUILD_STEPS = 1)
The y-axis represents the average FPS, the x-axis represents the number of random bodies being simulated.
(PERFORMANCE OF THE SIMULATION MAY VARY ON DIFFERENT HARDWARE, SOFTWARE AND WITH DIFFERENT SIMULATION SETTINGS)
(Requires Python 3.x.)
Clone the repository:
git clone <repository-url>
cd <repository-name>Install dependencies:
pip install -r requirements.txtRun the simulation:
python main.py| Key | Action |
|---|---|
| W | Move camera up |
| A | Move camera left |
| S | Move camera down |
| D | Move camera right |
Dmitry Wilder
Created as a personal project to learn, investigate and explore physics simulation, quadtree systems, optimization techniques and real-time visualization in Python using Pygame.



