Python implementation of RK4 with variable timestep

06 Jul 2012

In previous dabbles with physics in video games I discovered that simply adding velocity to position each frame sometimes ran into issues with certain simulations, like a rope or force-based graph. The helpful crew at Stack Exchange explained why this was so and pointed me to some better solutions.

As a result I ended up reading an article by Glenn Fiedler on implementing RK4 integration for a more accurate simulation, and he posted sample code demonstrating his implementation in C. As I’ve been messing with Python lately I decided to try my hand at porting his code to Python.

The process was surprisingly easy. Most of the code is directly copied over, though due to lack of structs in Python I just used dictionaries. Pygame simplifies a lot of things that were required in GLUT for the C version, so I ended up using just 100 lines instead of 150.

Unfortunately it runs very slowly, and I realised this is because pygame uses software rendering by default. I might see if I can easily fix that next.


Yesterday I attended my second gamejam, an eight hour event where teams form to work on small games together around some loose themes, which this time were Black & White, Rockets, and Masks.


It’s early to say this, and maybe a little crazy, but I can’t help but put Twenty, a simple game from Stephen French, in the same category as Tetris. Not just in the way it plays - though it shares that methodical feeling of clearing a space that transitions to crowded panic - but in how it takes just a single mechanic and a simple interface to create something truly elegant and timeless. Twenty deserves to be played for decades to come, just like Tetris.