A real-time 2D N-Body gravitational simulator that models the Sun-Earth-Moon system with interactive asteroid impacts, powered by a custom RK4 numerical integrator.
- Custom RK4 Integrator: A hand-coded 4th-order Runge-Kutta solver for coupled ODEs, avoiding black-box solvers to provide full transparency over the physics engine.
- Real-Time Energy Conservation: Live tracking of total mechanical energy error (ΔE/E₀) to monitor numerical integration accuracy and stability over time.
- Dynamic Asteroid Events: Spawn asteroids on hyperbolic trajectories that interact gravitationally with planets and generate animated explosion effects upon collision.
- Interactive Controls: Adjustable sliders for Earth/Moon velocities and integration time step (dt), with buttons to reset, apply stable conditions, or inject asteroids.
- Polished Visualization: Deep-space dark theme with static starfields, glowing halos, atmosphere layers, motion trails, and particle-like explosion animations.
- N-Body Gravity Engine: Full pairwise gravitational interaction between all bodies using Newtonian gravity with softening to prevent singularities.
This project does not contain multiple UI screens. The entire simulation runs in a single Matplotlib window divided into three panels:
| View | Description |
|---|---|
| Space View | Main simulation area displaying the Sun, Earth, Moon, asteroids, orbital trails, and explosion effects on a dark starfield background. |
| Energy Panel | Real-time plot of the relative total mechanical energy error (ΔE/E₀) over simulation time. |
| Info Panel | Displays physical parameters, orbital constants, integrator settings, and live simulation metrics (time and energy error). |
-
Clone the repository:
git clone https://github.com/CamiloAT/space-orbit-dynamics.git cd space-orbit-dynamics -
Install dependencies:
Make sure you have Python 3.8 or later installed, then run:
pip install numpy matplotlib
-
Run the simulation:
python space.py
-
Interact with the simulation:
- Use the bottom sliders to adjust Earth velocity, Moon velocity, and the integration time step (dt).
- Click Aplicar Cambios to apply new slider values and restart the simulation.
- Click Condiciones Estables to reset to default stable orbital parameters.
- Click Añadir Asteroide to inject an asteroid on a hyperbolic trajectory into the system.
Note: The simulation window must be kept open for the animation to run. Close the window to terminate the program.
space-orbit-dynamics/
├── space.py ← Main simulation: physics engine, RK4 integrator, visualization, and interactive controls
└── README.md ← Project documentation and usage guide
| Layer | Technology |
|---|---|
| Language | Python 3.8+ |
| Numerical Computation | NumPy (vector math, array manipulation) |
| Visualization | Matplotlib (Pyplot, Animation, Widgets, Patches) |
| Integration Method | Manual 4-stage Runge-Kutta (RK4) |
| Physics Model | Newtonian N-Body Gravity with softening |
| Name | GitHub |
|---|---|
| Camilo Andres Arias Tenjo | @CamiloAT |
| Jose Luis Ortega Castillo | @JoseOrtegaUPTC |
Computer Simulation