A real-time simulation of gravitational interactions between particles in 2D and 3D space, implemented in Python using Matplotlib.
- Simulates gravitational interactions in 2D and 3D
- Newtonian gravity with softening to prevent singularities
- Forward Euler integration for numerical updates
- Real-time animation with trajectory visualization
- Interactive controls for play, pause, and reset
- Demonstrates various three-body problem scenarios
- Built entirely with native Python libraries
- Python 3.6+
matplotlibnumpy
Install dependencies via pip:
pip install matplotlib numpyRun the main menu:
python Menu.pyAlternatively, you can run individual simulation scripts directly.
Classical Newtonian gravity is used, with computations updated in real-time per frame:
For each particle pair:
F = G * (m1 * m2) / (r² + ε²)
Where:
Gis the gravitational constantεis the softening factor to avoid singularities
v_new = v_old + a * Δt
x_new = x_old + v * Δt
Particle positions and trajectory trails are redrawn each frame to reflect current system state.
You can modify the following simulation settings in the source code:
| Parameter | Description |
|---|---|
t |
Time step size |
v |
Initial velocity magnitude |
L |
Initial spacing between particles |
soft |
Gravity softening factor (reference) |
These parameters allow you to model both periodic and chaotic solutions of the three-body problem.
To modify the particle system, edit the create_particles() function:
- Adjust mass, position, and velocity per particle
- Change particle appearance (color, size)
For an extended project overview and more physics-based simulations, visit: