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!
November 18th, 2007 at 8:22 PM
There’s also Chipmunk: http://www.slembcke.net/photos/v/programming/chipmunk/
Looks neat, and comes with xcode projects.
November 18th, 2007 at 8:23 PM
Actually, that link should have been: http://wiki.slembcke.net/main/published/Chipmunk
November 18th, 2007 at 10:11 PM
I’ve played with chipmunk a little and it is really really sweet.
The source code is nice and simple, pure C with no C++ cruft anywhere. It’s also a lot of fun. I’ll look at box2D when Ironcoder 7 is over
November 18th, 2007 at 10:58 PM
@Gus: Thanks! Daniel Jalkut just sent me the same link too. Now I’m wondering which one is faster / more accurate / more featureful.
@Jonathan: Heh, we have different opinions on what ‘cruft’ is. I read the Chipmunk docs and was all “aw man, zillions of functions, warnings about what struct fields I shouldn’t touch … I like Box2D’s clean C++ API better.”
November 19th, 2007 at 12:04 AM
[…] Jens (Stickies, iChat, Safari RSS, etc.) ponders using Core Animation subclasses to integrate a physics engine. […]
January 17th, 2008 at 8:20 AM
There’s someone working on their own interpretation of “Crayon Physics” using Chipmunk for the physics. Also looks pretty sweet. http://www.slembcke.net/forums/viewtopic.php?f=6&t=10&p=19&hilit=crayon#p19