Verlet Integration: A Little Physics Demo in Python
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.
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)!
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!
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 …