Stacks on Stacks

A long while back, I realized my scenes would be better served and more interesting if there was a more dynamic component to them.  Outside of the very basics, implementing  a proper physics engine with accurate collision detection and response was quite foreign to me.  Therefore, I picked up Ian Millington’s book Game Physics Engine Development and got to work.  I enjoyed the author’s approachable writing style and well-explained information on both particle and rigid body dynamics.  Within about a week or so, I was able to integrate a fairly robust adaptation of the engine presented in the book into my own engine’s architecture.

While the information presented on physical body simulation is quite good, the book’s main shortcoming is in collision detection and resolution.  In fairness, the author calls this out and tries to realistically set the reader’s expectations, but there’s a lot left to be desired when two boxes can’t reliably be stacked on top of one another due to non-converging solutions for contact generation and impulse resolutions.  Regardless, this is the approach that had lived in my engine for well over a year and still remains in the code base, although I consider it to be deprecated for anything beyond very simple simulations.

After a lot of research and a short back and forth email exchange with Randy Gaul, I tried my hand at implementing a more complex collision detection routine.  The new routine generated an entire contact manifold, as opposed to the old one, which only ever recorded one contact between two objects for any given point in time.  The contact manifold contained up to 4 points per collision pair.  This data, combined with a few other tricks I picked up here and there, finally allowed a small stack of boxes to sit on top of each other without shaking and falling over.

Eventually, I decided I wanted an overall more robust solution for both physics simulation and collision detection and resolution, so I spent a weekend integrating the Bullet Physics library into my engine.  Bullet’s API has proven to be reasonably straightforward, and I was able to get a stable stack of boxes set up in a very short amount of time.

The video below shows the dramatic difference in the old collision resolution method, and the newly implemented engine backed by Bullet.

With the old setup, I would place objects in the world with a sleep state and a tiny amount of space between each to give the appearance of a stack, but as soon as I interacted with anything in the stack, all bets were off.  With the new implementation, I can safely let objects fall into place and rest on top of each other at the start of the simulation without worrying too much about the whole thing going haywire.

(Regarding the ugly shadow artifacts in the video, those will be addressed in a follow-up post specific to the topic.)

References

http://www.randygaul.net/

http://allenchou.net/

https://code.google.com/p/box2d/downloads/list

https://github.com/bulletphysics/bullet3/releases