Help me replace my broken Mac Mini!

Click here to lend your support to: Help me replace my broken mac mini and make a donation at !

About this tutorial

Learn about Chipmunk Physics Engine and how to start developing games for the iPhone and other platforms.


  1. Introduction
  2. Setup
  3. Basic Concepts
  4. Initializing Chipmunk
  5. Defining the ball's body and shapes
  6. Tracking the ball's movements
  7. Defining the floor's body and shapes
  8. Evaluating the results & Conclusion
  9. Download the complete project

Defining the ball's body and shapes

Defining bodies is also a simple process although it might already require some of that physics knowledge. Chipmunk has a convenient cpBodyNew(mass, moment); function that handles almost all of the initial conditions any body requires. Usually all that's left is to set the position of the body.

So, the following code will define our ball's body, set it's position and add it to our space, place it at the end of the setupChipmuck method code:

// Create our ball's body with 100 mass
// and infinite moment

cpBody *ballBody = cpBodyNew(100.0, INFINITY);

// Set the initial position

ballBody->p = cpv(160, 250);

// Add the body to the space

cpSpaceAddBody(space, ballBody);

Next, we need to tell how the ball interacts with other objects by defining its shape and associated properties. This process is usually the most complex of all and sometimes requires some tuning along trial & error. Chipmunk provides you with 3 different functions to create shapes based on the type you desire:

  • cpCircleShapeNew(cpBody * body, cpFloat radius, cpVect offset)
  • cpSegmentShapeNew(cpBody * body, cpVect a, cpVect b, cpFloat radius)
  • cpPolyShapeNew(cpBody * body, int numVerts, cpVect * verts, cpVect offset)

The meaning of the arguments for those functions are as following:

  • body: the body associated with the shape you're creating
  • radius: the radius of the circle you're creating or the "width" of the line/segment
  • offset: where to place the shape relative to the body's center
  • a, b: the start/end points for segment shapes (it creates a line linking the two points)
  • numVerts, verts: for poly shapes you provide an array of the vertexes defining the shape (due to C constraints you need to pass numVerts which is the number of items/points on that array)

So, now that you've a brief overview of how to create shapes all that's left is to know how to define it's properties, this is self explanatory if you read the comments on the code that follows (add it after the last piece of code):

// Create our shape associated with the ball's body

cpShape *ballShape = cpCircleShapeNew(ballBody, 20.0, cpvzero);

ballShape->e = 0.5; // Elasticity

ballShape->u = 0.8; // Friction

ballShape->data = ball; // Associate with out ball's UIImageView

ballShape->collision_type = 1; // Collisions are grouped by types

// Add the shape to out space

cpSpaceAddShape(space, ballShape);

There are more properties you can define, check the docs to know more about them, for now, all we really need is to make sure the data and collision_type are set, you could discard the elasticity and friction or even change their values (sometimes with funny results).

Right now, the project should build correctly but you won't see anything moving just yet.


If you find this article useful and enjoy it, consider donating, even a small amount goes a long way to keep the articles up-to-date and to fund new ones.