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

  1. OJ

    This stuff looks like fun. I’m going to start ramping up with a bit of Python work, so you might have a few questions coming your way.

    That character physics article looks like a good read too. Will check that out a bit later :)
    Cheers!

  2. tom

    No worries OJ, my nerdy Python brain is yours for the picking.

    And yes, the article is fantastic. It’s slow going for somebody who sucks at mathematics as much as I do, but it’s great fun trying to put it into practice all the same.

    Next on the agenda is collision detection & proper constraint handling …

  3. Hannes

    It is a very helpful article, it helped with my ragdoll project a lot.

    An easy way to think of verlet is that it takes the current position Vector (x), then moves it by as much as it moved in the last frame(x – oldx), and then adds acceleration (a), put this all together and you get x + (x-oldx) + a, or 2x – oldx + a

  4. tom

    Glad it was of use, Hannes. I really do need to revisit this stuff some day …

  5. tom

    Haha oh, I just realized you’re probably referring to the Advanced Character Physics article — yes, very helpful! ;)

Leave a comment