A realistic, real-time 2D simulation of the Sun-Earth-Moon system with interactive asteroid impacts.
Space with Explosion is a physics-based, interactive graphical simulation built in Python using Matplotlib. It accurately models an N-Body system (primarily focused on the Sun, Earth, and Moon) by implementing a custom numerical integrator (Runge-Kutta 4th order) to solve a system of coupled first-order Ordinary Differential Equations (ODEs). The project is designed for educational and visualization purposes, demonstrating orbital mechanics, real-time total mechanical energy conservation, and dynamic events like hyperbolic asteroid intrusions and explosive collisions.
- Real Physics Engine: Implements a custom RK4 (Runge-Kutta 4th order) integrator, avoiding generic black-box solvers to provide a fully transparent gravitational physics core.
-
Energy Conservation Tracking: Includes a real-time plot of the Total Mechanical Energy error (
$\Delta E / E_0$ ) to monitor the stability and accuracy of the numerical integration over time. - Dynamic Asteroid Events: Interactively spawn asteroids from outside the system. Watch them enter on fast hyperbolic trajectories, interact gravitationally with the planets, and generate animated visual explosions upon collision!
-
Interactive Controls: Use sliders to adjust simulation variables like the initial velocities of the Earth and Moon and the integration time step (
dt). It features a dashboard monitoring physical orbital parameters side-by-side with live metrics. - Polished Visualization: Features a deep-space dark theme with static starfields, scalable glowing halos, atmosphere layers, dynamic motion trails for celestial bodies, and dynamic particle-like explosion animations.
- Language: Python 3
- Core Libraries: NumPy (for vector math and array manipulation)
- Visualization: Matplotlib (Pyplot, Animation, Widgets, Patches)
- Integration Method: Manual 4-stage Runge-Kutta (RK4)
Follow these steps to start the simulation on your local machine:
-
Open your terminal in the project directory:
cd "space-orbit-dynamics"
-
Install all required dependencies: Make sure you have Python installed, then run:
pip install numpy matplotlib
-
Run the simulation:
python space.py
-
Interact: Once the Matplotlib window opens, you can observe the system. Use the bottom sliders to alter the speeds or the computational time step. Click the Añadir Asteroide button to spawn an independent body on a hyperbolic trajectory.