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

Setup

Before we jump to the really useful stuff, there are some steps to setup a project so we can focus entirely on Chipmunk after.

Begin by opening XCode and creating a new project using the iPhone "View-Based Application" template. This will create a basic application with one view being displayed right after you start the application.

We'll also need to import all the needed Chipmunk files, this task can be accomplished in several ways including creating a static library and then linking our project to it. In our case, its easier to simply copy the needed files to our project's folder. Download the latest Chipmunk code from here and unpack it. The folder should contain several files, demos and docs, but we're only interested only in the contents of the src folder.

Before importing all the files, I suggest you create a new group under the "Other Sources" group in your project. Then use the "Add Existing Files..." on your new group's context menu and select all the files inside the src folder of your Chipmunk copy. Make sure you check the "Copy items into the destination group's folder (if needed)" during the import wizard. From this point forward, the project should build without errors and you won't need to worry about those files anymore (see a XCode screenshot of how the files should be).

Now that we've all we need to use Chipmunk we need to worry about the setup of some code that will help us visualize what's really happening. To do this without a specific game engine, we'll create a few images that will represent our objects. Though you can replace this with any images and alternative code you like, I advise you to use my images and code to avoid some misunderstandings related to object dimensions and coordinates. Add the 2 following images to your resources group:

ball

floor

Now, open your view controller (it should be named project_nameViewController and be inside the Classes group), and add this to the header file:

UIImageView *floor; // Holds our floor image
UIImageView *ball; // Holds our ball image

Then on the implementation file un comment the viewDidLoad message and edit or copy paste this code into it, after the [super viewDidLoad] call:


floor = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"floor.png"]];

floor.center = CGPointMake(160, 350);

ball = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ball.png"]];

ball.center = CGPointMake(160, 230);

[self.view addSubview:floor];

[self.view addSubview:ball];

And now you've your application launching a view and displaying a ball and a floor that we'll be working with from now on.



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.