Verlet Integration: A Little Physics Demo in Python

September 28th, 2007

Just before I crawl into bed, here’s a little demo based on the first fifth of Thomas Jakobsen’s Advanced Character Physics. I don’t really have the mathematical background to talk about verlet at any length other than to say it’s very interesting. :)

Verlet demo screenshot

The screenshot doesn’t really do the neat “physics” justice – it’s a cheap yet interesting effect.

In a nutshell, velocity verlet is an alternative to Euler (i.e. new_position = current_position + velocity * elapsed). In verlet, velocity is implicitly calculated based on the last position of an entity such that when elapsed is fixed:

new_position = 2 * current_position - last_position + acceleration * elapsed * elapsed

This equation tends to change the way you do pretty much everything related to your game physics. Rather than simply setting your game object’s velocity, you might push it instead via a force or hack the position/last position to achieve the same effect. I’m finding the leap a little daunting myself.

I actually touched on verlet briefly years ago using DirectX thanks to one of my more interesting lecturers at QANTM. Hope you don’t consider the Googling of your name to be excessively creepy, Christian. It’s been a few years. ;)

The supporting code is a little heavy for such a little demo – I plan to put it to wider use, so please excuse the mess for now.

You’ll need a reasonably recent version of Pygame and PyOpenGL installed in order to run this demo. Once it’s up and running, click your mouse on the window to watch some OpenGL triangles drop from the sky. Click, drag and release to fling triangles … almost like you were throwing them yourself.

Hopefully I’ll get a chance to expand on this among the thousand other things I want to do with my free time (not the least of which is writing more Ocaml tutorials)!

Download the Python verlet demo

Categories: Graphics Programming, OpenGL, Python, Software Development | 5 Comments