Skip to content

Gravity Particles

Math Chaos

An interactive, real-time particle simulation built with RayLib that explores gravitational interactions between hundreds of particles in a 2D space.

The Gravity Particle simulation (also known as raylib-particle-toy) models the N-body problem where each particle exerts a gravitational force on every other particle. This leads to the emergence of complex orbital behaviors, clusters, and chaotic trajectories.

RayLib Integration

Leverages the high-performance RayLib library for hardware-accelerated 2D rendering.

C++ Core

Efficient particle physics calculations implemented in modern C++.

N-Body Physics

Calculates gravitational attraction using Newton’s law of universal gravitation.

Cross-Platform

Support for desktop (Windows/Linux) and potential for Android builds.

The simulation tracks each particle’s position, velocity, and mass. At every frame, it calculates the net force acting on each particle:

F = G * (m1 * m2) / r^2
  • particle.h: Defines the particle structure and physics update logic.
  • main.cpp: Manages the simulation loop, RayLib window, and user input.

The project uses CMake with presets for a streamlined build process.

  1. Configure CMake

    Terminal window
    cmake --preset=default
  2. Build the Project

    Terminal window
    cmake --build --preset=default
  3. Run the Simulation Execute raylib-particle-toy from the generated build folder.