!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> » Chipmunk: 2D Physics on the iPhone -- Mobile Perspectives

Chipmunk: 2D Physics on the iPhone



By deans ~ July 2nd, 2009. Filed under: Resources.

We wanted to support much more interesting gameplay, along with a more accurate physics simulation, in the first significant update to iPuck.  Unfortunately, this forced us to swap out the simulation engine used in v1.0.  The old library worked fine, but it couldn’t be easily extended to meet the new requirements.  I looked at some of the 3D packages (One that I considered pretty carefully was Bullet, which is bundled with Oolong, the package that Paul used for iPunt).  However, the 3D libraries all seemed to be overkill for iPuck.  Besides, I would feel bad about wasting the Z axis.  One 2D package kept turning up in my searches—chipmunk-physics (another wiki page is also available, but I think that the code.google.com page is more up-to-date). chipmunk-physics logo.

Chipmunk is a

Fast and lightweight 2D rigid body physics library in C

That last bit is important for iPhone development, because, as we all know, ‘C’ plays very well with Objective-C.  Once I figured out how to call Objective-C methods from the Chipmunk related ‘C’ code, integration was very smooth.  It’s worth mentioning that the standard ‘C’ code in the Chipmunk distribution compiles fine in Xcode and runs with no problems on the iPhone and iPod touch.  I was worried that I might run into some issues, but it was perfect.  By the way, we plan to use Chipmunk for other projects, so I set it up as a “Source Tree” in Xcode, rather than just dumping the files directly into the iPuck project space.  Paul wrote an excellent post on doing this for Oolong.  I just followed his directions.

I won’t list all of Chipmunk’s features, they are nicely presented on the project page, but it is fast and straightforward to use.

The documentation is a bit sparse, especially for someone just getting started.  The demos are very helpful, but generally seem to assume that we already have a pretty good understanding of Chipmunk.  A set of demo programs is bundled with the ChipmunkLatest.tgz download.  Even nicer demo programs are available in the project repository

Fortunately, Alexandre Gomes has prepared a nice tutorial on getting started with Chipmunk.  I noticed a few typos in the sample code, but it gave me a great start towards learning the library and figuring out how to slide it on to the iPuck chassis.  Mr. Gomes does a good job of explaining the basic concepts.  I highly recommend taking a look at the tutorial if you’re considering using Chipmunk.

Once I got started actually porting iPuck, I only encountered a couple of small snags.  The first came when I was still working with permutations of Mr. Gomes’ sample.  I couldn’t figure out how to grab an object and drag it around on the screen.  After a bit of searching on the extremely useful Chipmunk forum, I found some sample code that provided most of what I needed.  I’m led to believe that there will soon be better ways to do this in Chipmunk.  However, after a bit of customization, cpMouse met the needs for iPuck v1.1.  I plan to look at some of the other methods to see whether I can continue to improve the code.  Look out v1.2.

The other problem was my fault for not reading the cpShape documentation more thoroughly.  I had originally modeled a number of my static objects as cpSegmentShapes.  This worked really well as long as my active objects were all cpCircleShapes.  Unfortunately, when I tried to model an active object using segments, I discovered that the current version of Chipmunk does not support cpSegment-to-cpSegment collisions.  After I went back and converted to cpPolyShapes, everything was perfect.  If I had carefully read the Chipmunk documentation, before I started coding, I would have seen:

Line segments: Meant mainly as a static shape. They can be attached to moving bodies, but they don’t currently generate collisions with other line segments.

I needed to develop against a stable library, so I chose to stick with the 4.1.0 version.  Judging by the activity in the project archive, there has been substantial progress since 4.1.0 and I’m looking forward to working with the latest revisions when they’ve settled into a steady state.

If you’re working with rigid bodies, and you need a fast, accurate, easy to use and, best of all, license fee free, 2D physics library, Chipmunk may be a perfect match for your needs.

 
——–
Note:  Instructions for using Subversion to check out the code and demos are here.
——–
Technorati Tags:  , , , ,

Comments are closed.