Skip to content

A bunch of simulations written in c. Made for Hackclub competition

Notifications You must be signed in to change notification settings

Sentry892/Csimulations

Folders and files

NameName
Last commit message
Last commit date

Latest commit

1624166 · · Feb 21, 2026

History

1 Commit
Feb 21, 2026
Feb 21, 2026

Repository files navigation

C simulations

Particle Collision Simulation

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.

What You'll See

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.


Prerequisites

Requirement Recommendation
C compiler GCC via MSYS2 or w64devkit
raylib pacman -S mingw-w64-x86_64-raylib (MSYS2) or download binaries

Build & Run Run

MSYS2 / MinGW

gcc collisions.c -o collisions.exe -lraylib -lopengl32 -lgdi32 -lwinmm -lm
./collisions.exee

GCC with manual raylib install (adjust paths as needed)

gcc collisions.c -o collisions.exe -Iraylib>gt;/include -Lraylib>gt;/lib -lraylib -lopengl32 -lgdi32 -lwinmm -lm
./collisions.exee

Configuration

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

How It Works

  1. Initialization — particles are spawned at random positions with random velocities and radii (5–10 px).
  2. 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.
  3. Rendering — each particle is drawn as a white filled circle on a black background.

About

A bunch of simulations written in c. Made for Hackclub competition

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages