A real-time 2D particle simulation built in C with raylib. Watch 500 particles bounce, collide, and settle under gravity with elastic collisions and energy damping.
A 1300×800 window filled with white particles that:
- Fall under gravity
- Bounce off walls and each other
- Gradually lose energy and settle to the bottom
- Display a live FPS counter
Press Escape or close the window to exit.
| Requirement | Recommendation |
|---|---|
| C compiler | GCC via MSYS2 or w64devkit |
| raylib | pacman -S mingw-w64-x86_64-raylib (MSYS2) or download binaries |
gcc collisions.c -o collisions.exe -lraylib -lopengl32 -lgdi32 -lwinmm -lm
./collisions.exeegcc collisions.c -o collisions.exe -Iraylib>gt;/include -Lraylib>gt;/lib -lraylib -lopengl32 -lgdi32 -lwinmm -lm
./collisions.exee All simulation parameters are #define constants at the top of collisions.c:
| Constant | Default | Description |
|---|---|---|
WIDTH |
1300 | Window width (px) |
HEIGHT |
800 | Window height (px) |
NUM_PARTICLES |
500 | Number of particles |
GRAVITY |
0.1 | Downward acceleration per frame |
DAMPING_FACTOR |
0.98 | Velocity retained after each collision (0–1) |
SPEED |
10 | Max initial velocity per axis |
- Initialization — particles are spawned at random positions with random velocities and radii (5–10 px).
- Physics loop (60 FPS):
- Gravity is added to each particle's vertical velocity.
- Wall collisions reflect velocity and apply damping.
- Particle collisions are detected via circle overlap (O(n²) brute force). Velocities are decomposed into normal/tangent components along the collision axis, normal components are swapped (elastic collision for equal masses), and overlapping particles are pushed apart.
- Rendering — each particle is drawn as a white filled circle on a black background.