Physics Integration

My goal for this weekend was to get a collision and physics library into C2.

We used ODE for PubCrawl and it didn’t leave me with much confidence. This time I chose to use the Bullet Physics Library because I’ve heard good things about it. Bullet is geared more towards game engines and is much simpler than ODE. I was able to get a quick test program up using Bullet in about a day. After another day I had integrated Bullet physics into the engine itself very nicely.

In C2 physics can be added to any SceneNodes by creating and attaching a PhysicsObject. A PhysicsObject does two things:

1) provides a description of an object for simulating physics
2) updates the transformation of the SceneNode after simulation each frame

For example a PhysicsObject can be created with a mass of 10 and a sphere collision shape of radius 2. The simulation is run: objects are moved, collisions are found and resolved. Then the PhysicsObject copies it’s simulated transformation to the SceneNode’s transformation. Then the scene is rendered.

The PhysicsObject class is actually a very simple wrapper around Bullet rigid bodies. It is mainly meant to keep the engine from using Bullet directly. This hides library and header dependencies. Once compiled into a library only the C2 engine headers are necessary to use the engine. If this were not the case using the engine would be a nightmare of dependencies.

I was able to add physics information to the scene file format as well. Any SceneNode with physics definitions has a PhysicsObject created and attached to itself on load. It’s very gratifying to write a quick scene file and load it up and watch objects fall and collide. It is surprising that good physics can be had at such a low investment of time. I highly recommend the Bullet Physics Library to anyone interested in adding physics to their game engine.

The next step in the physics system is to add constraints. Constraints would allow large static objects made up of multiple PhysicsObjects that act as one. Dynamic constraints would allow objects to move relative to each other in interesting ways. There also needs to be a way to fire scripts events on collision.

Portal was awesome and the ending is fantastic.
“The cake is a lie…”

Leave a Reply