This is an interactive simulation of a double pendulum written in C using Raylib. It uses Runge-Kutta 4 (RK4) to numerically integrate the differential equations given by the Euler-Lagrange equation. For now, it only works on Windows machines.
The system is comprised of two point mass bodies connected by fixed-length strings. The position of each body is parameterized in terms of the angle from the vertical
Based on this, the positions of the two bodies are given by
To get the differential equations needed to simulate the systems, we need the Lagrangian.
The kinetic energy
where
And the potential energy
where
The full Lagrangian, which is quite long, is
The differential equations are given by the Euler-Lagrange equation
which will give a system two equations that are each linear in terms of their
This system can be reparameterized to get
These equations are now in a form suitable for numerical integration via Runge-Kutta 4 or any other numerical integration technique. I've chosen RK4 because it's relatively easy to implement and performs quite well.
The simulation can be ran with Double_Pendulum.exe, which creates a fixed-size 1920x1080p window. You can exit the window by pressing escape. In the future, I might make the window resizable, but that would require overhauling the rendering and UI to make it responsive.
I've written the simulation in C using Raylib for getting input and rendering. You can start and stop the simulation by pressing the spacebar or clicking the Start/Stop button. You can also increase or decrease the speed using the left and right arrow keys. To change the initial configuration, you can use the sliders. Finally, I've included the initial and final energy as well as the percent change between the two. The energy values don't really correspond to real world values but are somewhat interesting nonetheless.
I've also included a single pendulum, though that one is more primitive. I started on an N-Body simulation, but it's very much incomplete.
The compilation is done in the command line via make.bat [sim], where sim is either single, double, body, or all. This will create an executable in the build folder, which can be ran via run.bat [sim]. Be warned that make.bat requires Visual Studio 2022 to be in the default C: directory and will not work otherwise. In the future, I might consider creating a CMake file to universalize the build process.
I initially wanted to simulate a system with N bodies, but that turned out to be a significantly more complex problem than I had anticipated. To support N bodies, I'd need to express everything as some sort of sum, and I'd also need to solve a variable system of linear equations. I found a paper that would prove useful for the first part, but I'd either need to solve the linear system by hand or incorporate some sort of scientific computation library to solve it for me.


