• Advertisement
Sign in to follow this  
  • entries
    53
  • comments
    225
  • views
    69493

The Physics of Doors

Sign in to follow this  

298 views

As any physics programmer worth his salt will tell you, one of the keys to fast and stable physics simulation is auto-disable. You want to auto-disable everything you can. Aside from mass-destruction sequences, most of the time, there are actually very few physical bodies in motion - and so it's worth taking the time to determine all the bodies you can leave disabled, and only wake them up when either they start moving, something constrained to them starts moving, or something in motion hits them.

A while ago I added some debug rendering to my engine that shows me all the physics bodies in the scene, and colours them based on their state: blue represent totally static bodies (scenery), green represent bodies in motion (being solved that frame), and red representing bodies that are currently sleeping (auto-disabled). A quick look at the bodies representing my doors showed them green all the time - meaning that the physics solver is recalculating them every frame even though they're just sitting there (because there's no animation playing).

While my first thought was that I could just leave them asleep all the time given they didn't actually need any simulation, I soon realised it's important to make sure they're made active when they're moving, or any auto-disabled bodies that are touching them won't react when they start moving (imagine a character hanging in the air when a trap door drops open beneath him - not good).

The first thing I had to do was let the physics engine know that these doors don't need any motion simulation - they're pure animation (i.e. no gravity, friction, momentum, etc). My physics engine allows you to lock specific axes on any given body (so for example on a see-saw, you want to lock translation in x y and z so it doesn't slide anywhere, then lock rotation around y and z, leaving it free to pivot around the x axis) - so for any animated scenery (like a door, or a lift) I just lock all the axes. The physics engine now knows this body is only there to interact with other bodies - it doesn't actually need any simulation itself.

Next, I just added some auto-disable code to wake the body up when it receives any motion from a controller (e.g. the animation) and put it to sleep whenever there's no motion present.

Here's the result - the first shot shows a door sitting full closed (notice that it's red, so it's not taking up any cycles), and the second shows it automagically waking up to open (in green). I can't tell you how many times this coloured physics feedback has saved me hours debugging stuff!

Disabled

Enabled
Sign in to follow this  


4 Comments


Recommended Comments

Funnily enough, that's actually not the font I wanted - on Windows I get a funky comic speech bubble style font. The one you see here is the OSX default "the font you asked for wasn't found" font. I've left it there because for development, it lets me see more characters per line =)

Share this comment


Link to comment
Oh, I didn't know the game ran on OS X too. I am even more enraptured by your excellent cow avatar and Head Over Heels interpretation now, and wish to subscribe to your newsletter.

Share this comment


Link to comment
Hehehe - OSX indies of the world unite! Thanks for your kind words. Does GameDev support newsletters somehow? I must admit, I didn't really poke around the subscription control panel too much.

Share this comment


Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Advertisement