This is an interactive 2D gravity simulator built with Python and Pygame. The simulation showcases gravitational forces between particles, with options for real-time interaction and visualization.
-
Gravity Simulation:
- Simulates gravitational attraction between particles based on their mass and distance.
- Particles influence each other dynamically in real-time.
-
Collision Detection:
- Particles merge when they collide, conserving mass and radius.
-
Interactive Controls:
- Right-click to add particles with velocity set by mouse direction.
- Middle-click to remove particles by selecting them.
- Adjustable particle properties like position, mass, radius, and velocity through the sidebar.
-
Vector Field Visualization:
- Displays a vector field representing gravitational forces at grid points.
- Color-coded arrows indicate the particle exerting the strongest force at each point.
-
Pause/Resume:
- Pause and resume the simulation with the spacebar.
-
Particle Trails:
- Visualize particle movement with trailing effects.
-
Screen Wrapping:
- Particles wrap around the screen edges, maintaining simulation continuity.
- Python 3.x
- Pygame library
-
Clone this repository:
git clone <repository-url>
-
Navigate to the project directory:
cd gravity-simulator -
Install the required dependencies:
pip install pygame
-
Run the simulation:
python main.py
- Arrow Keys: Select input fields in the sidebar.
- Enter: Confirm changes to the selected input.
- Backspace: Clear the current input field.
- Spacebar: Pause/Resume the simulation.
- Mouse Scroll: Adjust particle radius.
- Left Click: Drag particles or interact with sliders.
- Right Click: Add a new particle at the cursor's position.
- Middle Click: Remove particles by clicking on them.
-
Adjust Particle Properties:
- Modify position (X, Y), mass, radius, and initial velocity.
-
Gravitational Constant (G):
- Use the slider to adjust the gravitational strength.
-
Vector Field Density:
- Modify the spacing of the vector field grid.
-
Vector Field Toggle:
- Checkbox to enable or disable the vector field visualization.
-
Performance Optimization:
- Implement spatial partitioning (e.g., quadtrees) for faster collision detection and force calculations.
-
Enhanced Visuals:
- Add smooth animations for particle merging.
- Provide more customization options for particle trails and colors.
-
Advanced Features:
- Add preset systems (e.g., solar system, galaxies).
- Allow users to reset the simulation.
This project is licensed under the MIT License. See LICENSE for details.
- Built using Pygame.
- Inspired by physics simulations and gravitational systems.