Chipmunk Pro Autogeometry demo built on top of Cocos2D 2.0.
Objective-C C C++ Other


YouTube Video


A Chipmunk Pro autogeometry / Cocos2D 2.0 example project where you play as an grumpy storm cloud that shoots nuclear missiles at a bunch of happy boxes on a lovely rolling landscape. Trust me. It makes perfect sense!


This is a Chipmunk Pro autogeometry demo project that is built on top of Cocos2D 2.0. The focus for this demo is to show how simple it can be to mix Chipmunk Pro’s autogeometry to implement deformable terrain with Cocos2D. It implements a single screen game that uses the same Cocos2D rendering code that renders the screen to generate the Chipmunk geometry. While very simple and easy, transfering pixels from the GPU to the CPU is a little slow. Performance is not a major focus of this demo over simplicity. However, the cost of regenerating all the geometry only takes a couple dozen milliseconds. This method would work perfect for generating static level geometry at runtime or an occasional terrain deformation such as in a Scorched Earth clone.

How it works:

Chipmunk Pro allows you to trace images (or procedural functions) to turn them into geometry. For Objective-C, it comes with a few handy wrapper classes that make this easy to do. The included class, ChipmunkGLRenderBufferSampler, sets up an offscreen OpenGL rendering buffer that is used as a bitmap image source. You can then simply call Cocos2D code while the sampler is bound and the pixels are copied into sampler and are ready to be marched (extracting the contours)!

While it’s somewhat expensive to transfer the pixels from OpenGL back to the CPU where Chipmunk can use them. This is too slow to perform every frame, but works perfectly fine at load time and works pretty good if you only need to change the terrain once in a while.

The holes in the terrain are cut by sprites drawn over it with a multiply blending mode. When composited together in the render texture, they multiply the destination alpha to 0 in their centers where they are transparent, and darken around the edges of the hole where they are opaque and black. It’s a pretty easy compositing trick to do. Then all you have to do is blit the render texture to the screen (or the sampler).

What’s included:

A number of handy utility classes you can use in your own code:

  • ChipmunkSprite: Automatically synchronize Chipmunk bodies to Cocos2D sprites.
  • ChipmunkDebugNode: An simple node for rendering Chipmunk collision shapes and constraints.
  • HMVectorNode: A node that provides immediate mode rendering of high quality, anti-aliased primitives. ChipmunkDebugNode is built on top of it.
  • ChipmunkGLRenderBufferSampler: ChipmunkBitmapSampler subclass that allows you to easily render GL content into a sampler and then extract geometry from it.
  • imageconvert/retinaconvert: Utility programs I’ve made and use that allow you to generate .png files from Photoshop source files without needing to re-export them by hand every time you build.