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

Ruby, SDL and OpenGL

August 2nd, 2006

NOTE: This is an old post which I am merely republishing. The earlier version was lost due to a RAID failure on my former web host’s server.

I had a bit of trouble tracking down decent documentation for Ruby/SDL and Ruby/OpenGL, so I thought I’d put together some sample code in case anybody is interested in it.

require 'sdl'
require 'opengl'

# initialize SDL and OpenGL
SDL.init(SDL::INIT_VIDEO | SDL::INIT_AUDIO | SDL::INIT_TIMER)
screen = SDL.set_video_mode(800, 600, 16, SDL::HWSURFACE | SDL::OPENGL)
GL.ClearColor(0.0, 0.0, 1.0)
GL.ClearDepth(1.0)

finished = false
while not finished
  # pull events from the input queue
  while event = SDL::Event2.poll
    # watch for quit events
    if event.kind_of?(SDL::Event2::Quit)
      finished = true
      break
    end
  end

  # clear the screen
  GL.Clear(GL::COLOR_BUFFER_BIT | GL::DEPTH_BUFFER_BIT)

  # TODO: rendering code goes here!

  # display the back buffer
  SDL.GL_swap_buffers
end

# terminate SDL
SDL.quit

Obviously, the convention is underscored lower case method names for Ruby/SDL (although camelCase aliases are provided too), while Ruby/OpenGL’s API uses UpperCamelCase and breaks Ruby convention by using a capital letter for the first character.

Update: Here’s documentation for Ruby/SDL .

Categories: Graphics Programming, Ruby, Software Development | No Comments