A real-time double pendulum physics simulator with a Tron-inspired visual aesthetic. Built with C, SDL2, and custom RK4 integration for accurate chaotic motion simulation.
- Real-time double pendulum simulation using 4th-order Runge-Kutta (RK4) integration
- Neon trail rendering with additive blending for visual effect
- Energy conservation display and tracking
- Configurable simulation speed
- Interactive controls for pausing, resetting, and randomizing initial conditions
- Grid overlay for reference
- HUD displaying system parameters and total energy
The simulator implements the double pendulum equations of motion derived from Lagrangian mechanics. The system exhibits chaotic behavior, where small changes in initial conditions lead to drastically different trajectories over time.
The simulation uses:
- RK4 integration for numerical stability
- Optional damping factor to reduce numerical drift
- Energy calculation (kinetic + potential) for validation
- SDL2
- SDL2_ttf
- Standard C library (math.h, stdlib.h, stdio.h)
sudo pacman -S sdl2 sdl2_ttfsudo apt-get install libsdl2-dev libsdl2-ttf-devsudo dnf install SDL2-devel SDL2_ttf-develbrew install sdl2 sdl2_ttfThe project uses a Makefile for compilation:
makeThis will compile the source files and create the executable bin/double_pendulum_tron.
To clean build artifacts:
make cleanAfter building, run the simulator:
./bin/double_pendulum_tronOr use make to build and run:
make run| Key | Action |
|---|---|
| SPACE | Pause/unpause simulation |
| R | Reset to initial conditions and clear trail |
| X | Randomize initial angles and clear trail |
| Z | Decrease simulation speed (multiply by 0.6) |
| C | Increase simulation speed (multiply by 1.6) |
| T | Toggle trail rendering on/off |
| G | Toggle grid overlay on/off |
| H | Toggle HUD display on/off |
| ESC | Exit application |
double-pendulum
βββ assets
β βββ JetBrainsMonoNerdFontMono-Regular.ttf
βββ bin
β βββ double_pendulum_tron
βββ Makefile
βββ README.md
βββ src
βββ main.c # Main loop and event handling
βββ physics.c # Pendulum dynamics and RK4 integration
βββ physics.h
βββ render.c # SDL2 rendering and visualization
βββ render.h
βββ trails.c # Trail buffer management
βββ trails.h
Edit physics.c in the pendulum_init() function to modify:
m1,m2: Masses of the pendulum bobs (default: 1.0 each)l1,l2: Lengths of the pendulum rods (default: 170.0 pixels each)theta1,theta2: Initial angles in radians (default: 0.6Ο and 0.4Ο)damping: Numerical damping factor (default: 0.0)
Edit render.c to modify:
- Color scheme (tron_blue, tron_accent, bg_dark)
- Grid spacing (default: 40 pixels)
- Trail length (set in main.c trail_create, default: 4000 points)
- Glow intensity and radius
Edit main.c to modify:
- Window size (default: 1280x720)
- Physics timestep
dt(default: 0.008 seconds) - Initial simulation speed (default: 2.5x)
The simulator uses the 4th-order Runge-Kutta (RK4) method for time integration. This provides a good balance between accuracy and computational efficiency for the chaotic double pendulum system.
Trails are rendered using a circular buffer that stores the position history of the second pendulum bob. The trail uses additive blending with alpha gradients to create a glowing neon effect.
The HUD displays the total mechanical energy (kinetic + potential). In an ideal simulation, this value should remain constant. Small variations indicate numerical drift, which can be reduced by:
- Decreasing the timestep
dt - Adding small damping (trade-off: physical accuracy)
- Using double precision (already implemented)
- No collision detection between pendulum segments
- Trails are rendered as 1-pixel lines regardless of width parameter
- Font fallback is limited to common system paths
- No configuration file support (requires recompilation for parameter changes)
This project is provided as-is for educational and research purposes.
Inspired by the chaotic beauty of double pendulum systems and Tron's visual aesthetic.