simple physics | experiment | C | SDL2 | visualizationA simple pendulum experiment written in C using SDL2 for visualization.
This project was made to learn:
- basic physics simulation
- numerical integration (RK4)
- low-level graphics rendering with SDL2
Nothing fancy — just experimenting and learning step by step.
This simulation does not rely on fake movement or animation tricks. Everything is calculated using real physics equations.
A mass swinging under gravity. The motion is stable, smooth, and predictable (simple harmonic motion).
Two connected pendulums. This system is chaotic, meaning:
- extremely sensitive to initial conditions
- small changes produce very different motion
- beautiful and complex trajectories
Instead of the simple Euler method, this project uses:
Runge-Kutta 4th Order (RK4)Benefits:
- smoother motion
- better numerical stability
- closer-to-real physics behavior
The simulation draws the path of the pendulum over time, allowing you to visualize chaos, symmetry, and patterns.
pendulum-simulation/
├── src/
│ ├── main.c # Entry point, SDL initialization & input handling
│ ├── physics.c # RK4 physics logic (single & double pendulum)
│ ├── physics.h # Data structures & simulation state
│ ├── render.c # SDL2 drawing logic & motion trails
│ └── render.h # Rendering headers
├── Makefile # Build automation (GCC)
└── README.md # Project documentationThis project requires SDL2 and GCC to be installed on your system.
Install dependencies:
sudo apt update
sudo apt install build-essential libsdl2-devBuild and run:
make
./pendulumInstall dependencies:
sudo pacman -S gcc make sdl2Build and run:
make
./pendulumInstall Homebrew (if not installed):
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"Install dependencies:
brew install sdl2Build and run:
make
./pendulumRecommended options:
- MSYS2 (recommended)
- MinGW + SDL2
- WSL (Windows Subsystem for Linux)Using MSYS2:
pacman -S mingw-w64-x86_64-gcc mingw-w64-x86_64-SDL2 makeThen build:
makeIf the program opens a window and shows a swinging pendulum, your installation is successful.
[1] → Single Pendulum mode
[2] → Double Pendulum mode (chaotic)
[SPACE] → Pause / Resume simulation
[R] → Reset simulation
[Scroll]→ Zoom in / Zoom out
[ESC] → Exit program
Smooth and predictable harmonic motion
Chaotic, sensitive, and beautiful motion trails
Variables used in the code:
θ (theta) : Angle from vertical
ω (omega) : Angular velocity
L : Arm length
m : Mass
g : Gravity (9.81 m/s²)
dt : Time step (≈ 0.016 for 60 FPS)NewState = OldState + (dt / 6) × (k1 + 2k2 + 2k3 + k4)This keeps the simulation stable over long periods of time.
- learn numerical physics (RK4)
- practice memory & structure management in C
- experiment with SDL2 renderingThis project was inspired after watching the anime:
"Chi: On the Movements of the Earth"
(Chikyuu no Undou ni Tsuite)It made me realize how beautiful motion can be when described using mathematics.
MIT License — free to use, modify, remix, and experiment.
still learning, still experimenting