Box2D

Thanks to Steve Dekorte’s blog, I just ran across Box2D , an open-source 2D physics engine for games. In other words, it simulates the motion over time of convex polygons, taking into account inertia, gravity, collisions, friction, angular momentum, torque — all the things I once painstakingly learned in college and then completely forgot. Now they’ve suddenly become fascinating again, since Box2D does all the hard work. The app just has to describe the objects, then call Box2D in a loop to find out how their coordinates change over time.

Box2D comes with some demos that are bare-bones graphically, but amazingly realistic in motion, including a swinging chain, a web of springs, and a pyramid of blocks that you can undermine and collapse:

A far more sophisticated usage of Box2D is in the indie game Crayon Physics Deluxe, “in which you get to experience what it would be like if your drawings would be magically transformed into real physical objects.” You must watch the amazing video on that site. (Then cry, because it’s only for Windows.)

Speaking of Windows, the official Box2D package only builds on that platform, so far. But the core library is platform-independent C++, and the demos use OpenGL, so porting isn’t a big deal. Building on some work of some others who’d written makefiles, I put together an Xcode project. Now you can download the demo app if you want to play with it. If you want to experiment, you can copy the Box2D dylib itself out of the app bundle, or download my patch and apply it to revision 49 from the Subversion repository.

There’s a killer opportunity here to plug Box2D into Core Animation. Then you’d have the gorgeous high-speed compositing of the latter, coupled with far more sophisticated animation capabilities. (The animation functionality in CA is fully subclassable, so this should be straightforward to do.) I’ve been fooling around with Core Animation lately, and having a lot of fun making pretty pictures that slide around smoothly. I’ve never before felt the need for a game-physics engine, but the prospect of making my pretty pictures move with that kind of realism is suddenly very enticing!

Previously: Review: ZackAndWiki
Next Post: Facebook and Decentralized Identifiers