Articles

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 www.pledgie.com !

About this tutorial

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

Contents

  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

Basic Concepts

Now that we've our project setup and ready to go, we can start actually studying Chipmunk itself. To begin with, we need to learn the basic terminology of Chipmunk and how it's organized.

To begin simulating a world with physics, you need to determine some rules (i.e. gravity) for your new "universe" and you need to create a container for all your objects. Chipmunk calls this a Space. Besides those basic rules it also acts as a container for all the objects you'll want to add to the simulation as well as making them interact correctly.

To define any object, you'll need to define its mass and some other options. This is done using what Chipmunk calls a Body. A body doesn't produce collisions by itself because it has no "shape" when it comes to interact with other objects. Other than it doesn't have a shape some objects aren't homogeneous, you sometimes need to define different parameters to different parts of the object (i.e. the top side of an ice cream is softer than the cookie cone or the stick).

To define which areas of a body interact with the rest of the world and those different surfaces, you need to add one or more Shapes to a body. There are 3 types of shapes available with Chipmunk: circle, segment and poly. Combining those 3 shape types you can define almost any area of interaction imaginable for your bodies. Obviously, shapes added to the same body don't produce collisions between themselves. It is also worthy of a warning that it's not always better to completely "shape" your body to match your sprites, adding many shapes greatly increases the number of calculations Chipmunk needs to compute at each frame, it is better to simplify your shapes in a way that it only covers the important parts but it still feels correct when running the simulation.

While working with Chipmunk you can try to update things like the velocity or position of a body by yourself, but this is not recommended. The main method of changing things on Chipmunk, just like in the real world, is to use and apply Forces. A force is a vector , you tell the strength along the XX and YY axis. To help working with vectors, Chipmunk comes with a library called cpVect that allows you to do the most common operations like multiplying vectors or projecting two vectors.

Additionally Chipmunk has support for Joints which allows you to connect two bodies with pin points, slide points, pivot and groove points. Although I won't be using or covering joints in this tutorial, at the end you should be able to easily grasp and use them by yourself.



Donate

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.