This project has been tested with:
- Python 3.13.14
- Pygame 2.6.1
Note: Python 3.14 may currently encounter installation issues due to upstream Pygame compatibility.
- Python 3.x installed
git clone https://github.com/NeilNNP45-dev/orbit_simulator.gitcd orbit_simulatorWindows
python -m venv .venvActivate it:
.venv\Scripts\activatemacOS / Linux
python3 -m venv .venvActivate it:
source .venv/bin/activatepip install -r requirements.txtpython main.pyA physics-based 2D orbital simulator built using Python and Pygame, where celestial bodies interact through gravity and orbit each other in real time.
What started as a small gravity experiment slowly evolved into a full simulation sandbox featuring stable orbital mechanics, camera controls, simulation controls, and real-time diagnostics.
๐ธ IMAGE 1 โ Main Simulation Sandbox
- ๐ True n-body gravity simulation (every body attracts every other body)
- ๐ Leapfrog integration for improved long-term orbital stability
- ๐ช Stable multi-body orbital system
- ๐ Orbit trail rendering
- ๐งน Automatic cleanup for escaped bodies
๐ธ IMAGE 2 โ Time Acceleration / Deceleration (Simulation Speed Control)
- ๐ฅ Camera panning
- ๐ Zoom controls
- โป๏ธ Full universe reset
- โธ๏ธ Pause / resume simulation
- โก Adjustable simulation speed
๐ธ IMAGE 3 โ Time Stop (Pause / Resume Simulation) (Yes, it's a JoJo reference :3)
- ๐ชจ Spawn new bodies with mouse clicks
- ๐ฅ๏ธ Real-time HUD with simulation statistics
- ๐ Timed notification system for simulation events
๐ธ IMAGE 4 โ User-Spawned Asteroids
This simulator uses Newton's Law of Universal Gravitation to compute gravitational forces between bodies.
Gravitational Force
F = G ร (mโ ร mโ) / rยฒ
Each body experiences gravitational attraction from every other body.
Acceleration
a = F / m
To improve orbital stability, the simulator uses Leapfrog Integration (Velocity Verlet style) instead of basic Euler integration.
This significantly reduces:
- Orbital drift
- Numerical instability
- Energy loss / gain over long simulations
๐ธ IMAGE 5 โ Universe Reset (Restart Simulation from the Beginning โ another JoJo reference :3)
orbit_simulator/
โ
โโโ main.py # Main simulation loop and rendering
โโโ body.py # Body class and default solar system creation
โโโ physics.py # Gravity calculations and Leapfrog integration
โโโ camera.py # Camera movement, zoom, and coordinate conversion
โโโ requirements.txt # Project dependencies
โโโ README.md # Project documentation
The project follows a modular structure, separating physics, rendering, camera logic, and simulation management into individual modules to keep the codebase easier to maintain and extend.
The current simulation contains:
- โ๏ธ 1 Sun
- ๐ช 5 orbiting planets
Each body stores:
- Position (
x,y) - Velocity (
vx,vy) - Acceleration (
ax,ay) - Mass
- Radius
- Color
- Trail history
W / A / S / Dโ Move cameraIโ Zoom inOโ Zoom out
Spaceโ Pause / Resume simulationRโ Reset universeโโ Increase simulation speedโโ Decrease simulation speed
- Left Click โ Spawn asteroid / new body
The HUD displays:
- FPS
- Number of active bodies
- Zoom level
- Simulation speed
- Simulation state
- Physics integrator
- Python ๐
- Pygame ๐ฎ
- Physics
- Math
- Lots of debugging ๐ตโ๐ซ
Possible future additions:
- Follow-body camera
- Collision / merger physics
- Save & load simulation states
- Named celestial bodies
- BarnesโHut optimization for large-scale simulations
This project has been a major learning experience in:
- Physics simulation
- Numerical integration
- Software design
- Debugging complex systems
- Writing modular Python code
Still evolving, but already a fun little universe to experiment with ๐