Using Chipmunk Physics (In All The Wrong Ways)

by Paul Hart

As I mentioned in passing in my previous post, I’ve started doing some iPhone development again. Last time I developed an application for playing soundboards (ignore the Flash-specificity) on the device, and creating them via a web application. This time, I’ve decided to create a game.

The cocos2d-iphone library seems to be the best option available for creating two-dimensional games. It’s been proven in production games many times, and several of the most successful iPhone games have been developed with it. The latest distributions include built-in support for a couple of physics libraries, Chipmunk and Box2D.

My game doesn’t use gravity (the core mechanic is a top-down tower defence thing), but physics libraries offer some useful characteristics that have made my game easier to develop.

What a Creep

Tower defence gameplay takes place on a grid. Two points in the grid are occupied at the start of the game – one is the location from which the creeps (bad guys) will start, and the other is the destination they want to reach. The player can place towers anywhere on the grid to shoot at the creeps and impede their movement, so long as the creeps can still reach the destination. Creeps will spawn from the start point at intervals.

The dynamic layout of towers in the grid means that the creeps must be able to figure out the optimal path to their destination. There is a standard algorithm for doing this called A* (and Alexander Bussman has done all the heavy lifting with some great sample code). This pathfinding algorithm tells me which direction the creep should move in next, but it doesn’t manage the movement itself.

I didn’t want to have to track the location of each creep all the time, so I decided to use chipmunk to manage the movement of the creeps. With chipmunk it is very simple to define a speed vector for each object in the game space. I can also define a number of other characteristics for the object, such as the shape of its body (the area I state it displaces in the game space), and a “collision type” that tells the engine what kind of object it is. I can also write functions than handle collisions between different types of objects, so they are handled in various manners.

Now that the creeps have a “physical presence” in the game world, I needed to be told when they got to interesting points in the map (i.e. places where they might have to change direction). I created phantom bodies in the game space that the creeps would “bump into”, and associated the two collision types of the creeps and the phantoms with a callback function. When the function is called, I make the creep recalculate its path to the destination square on the grid and change direction if needed. For the purposes of the physics engine, I ignore the collision.

Bang Bang, Shoot Shoot

I decided early on that I wanted the towers to target a single creep while it remained within range, and that the bullets should home in on that creep. Unfortunately I can’t use the physics engine to find the collision between the bullet and the one creep that its targeting, but chipmunk does make the homing very simple.

By defining a location of the bullet, I can calculate the current distance vector between the bullet and its target. I can then set the velocity of the bullet to have the same direction as the distance vector, and even apply an angle (just because an object is moving in a particular direction doesn’t mean it’s pointing in that direction).

With each tick of the game state I check all in-flight bullets to see if they’ve hit their targets. If so, I apply damage as appropriate and remove the bullet from the visible gameplay. If the target has disappeared, the bullet is also removed.

An Easier Life

While I may not have used the chipmunk physics engine as intended, having access to its large feature set has been valuable in simplifying my code development. Admittedly, sometimes the documentation is lacking, but there are many examples online explaining how it can be put to use.