This project is a continuation of fluid simulation concepts, extended to simulate gravitational interactions between particles. The simulation calculates gravitational forces between particles and visualizes their dynamic interactions in a 2D space using OpenGL and GLFW.
- Introduction
- Features
- Installation
- Build
- Usage
- Continuous Integration
- Project Layout
- Acknowledgments
- License
This project builds on the principles of fluid simulation by focusing on gravitational interactions between particles. The primary goal is to simulate how particles attract each other based on gravitational forces, similar to celestial bodies.
The project uses Newton's law of universal gravitation, where the force between two particles is inversely proportional to the square of the distance between them. The resulting simulation demonstrates complex and dynamic behavior as particles interact in real time.
- Simulates gravitational forces between particles based on their mass and distance.
- Real-time visualization using OpenGL and GLFW.
- Wrap-around behavior to keep particles within the visible simulation area.
- Customizable particle attributes, such as mass and initial position.
To build and run the simulation, ensure you have the necessary dependencies installed:
-
OpenGL
-
GLEW
-
GLFW
-
OpenMPI
On Ubuntu, you can install these with:
sudo apt-get update
sudo apt-get install build-essential libgl1-mesa-dev libglew-dev libglfw3-dev libopenmpi-devAlternatively, you can install all dependencies via Make:
make depsTo set up and compile the project on your local machine, follow these steps:
-
Clone the Repository: Start by cloning the project repository from GitHub. This will create a local copy of the code on your machine.
git clone https://github.com/shamykyzer/gravity-simulation.git
-
Navigate to the project directory:
cd gravity-simulation -
Compile the project using the provided `Makefile`:
make
This command will compile the source files and produce an executable named `gravity_simulation` in the root directory.
-
Run the executable to start the simulation:
./gravity_simulation
This project uses a GitHub Actions workflow with four sequential jobs:
Installing Dependencies, Test, Build, and Make. Each job
installs the required packages with apt-get, and compiled object files are
cached between jobs to speed up subsequent runs. The build steps run
make clean before make to ensure a fresh compilation each time.
To run the simulation:
-
Execute the compiled binary:
./gravity_simulation
-
A window will open displaying particles interacting under gravitational forces. The particles are initially placed randomly within the simulation area and will attract each other according to Newton's law of universal gravitation.
- Mouse Interaction: Click and hold the left mouse button to attract particles towards the mouse position. This simulates an additional gravitational pull originating from the mouse location.
- Wrap-around: Particles that move beyond the visible boundaries of the simulation will reappear on the opposite side, creating a continuous space effect.
Watch as the particles dynamically interact, forming clusters and complex orbital paths due to gravitational attraction. The simulation demonstrates both local interactions and larger structures emerging over time.
The repository is organized as follows:
.
├── Makefile
├── README.md
├── gravity_simulation (prebuilt binary)
├── include/ (header files)
├── src/ (C source files)
├── shaders/ (GLSL shader programs)
├── bin/ (older binary build)
└── obj/ (object files)
- Headers (
include/) –particle.hdefines the mainParticlesstructure and associated routines.quadtree.himplements the Barnes–Hut node and helpers. Additional headers provide constants, input handling and shader utilities. - Sources (
src/) –main.cinitializes GLFW and GLEW, then enters the main loop.particle.ccontains particle initialization, force computation and drawing.controls.cprocesses user input. Modules such asquadtree.c,shader_utils.candintegral_image.cimplement supporting algorithms. - Shaders (
shaders/) – GLSL programs for particle rendering and update logic. - Makefile – Builds the project into the
gravity_simulationexecutable.
- Particle Storage – Particles are stored in a structure-of-arrays layout for efficient updates.
- Force Calculation – A Barnes–Hut quadtree approximates gravitational forces when many particles are present.
- Rendering – Particles are drawn as points via OpenGL.
- Interaction – Mouse movement adjusts an attractor position; pressing
Rresets the system.
- Review the LICENSE file to understand usage terms.
- Inspect
quadtree.candshader_utils.cto understand the Barnes–Hut algorithm and shader compilation. - Experiment with additional forces or improved rendering techniques.
This project was inspired by Brendan Galea's YouTube video on gravitational systems. Special thanks to Brendan Galea for sharing his insights and sparking new ideas.
This project is licensed under the MIT License - see the LICENSE file for details.