Skip Navigation

Ramblings from the team at zinc Roe

Metal Fish Eggs

How To Add Box2D To An iPhone XCode Project

Using Box2D is a great way to add physics to a 2D iPhone game. Since Box2D is written in C++ rather than Objective-C, adding it to your XCode project and getting it to build cleanly can be a bit tricky. If you just want to get aquainted with Box2D, there is an sample XCode project in the Box2D svn repository that includes the Box2D testbed and basic debug drawing support for the iPhone. Once you get past some quick experiments, you’ll probably want to add Box2D to a clean XCode project with a minimum of cruft. It’s not tough, but there are a couple of gotchas along the way. Below are instructions for adding Box2D to a new iPhone XCode project and getting that project to build cleanly on a device (note: the project doesn’t actually do anything, that’s where you come in…).

The instructions below assume you’re a registered iPhone developer and have:

  • Mac OS X 10.5
  • XCode 3.1.2
  • iPhone SDK 2.2.1
  • Box2D 2.0.2
  • A valid app id and development provisioning profile that you can use to build the project for a device.

So, for my future reference and yours….

  1. Create a new XCode project. I’m creating a View-Based Application called MyGame on my desktop.

    01

    02

  2. Open the terminal and export Box2D 2.0.2 from the Box2D svn repository into your project folder:
    svn export https://box2d.svn.sourceforge.net/svnroot/box2d/tags/Box2D-2.0.2 ~/Desktop/MyGame/Box2D-2.0.2
  3. Right click on MyGame under Groups & Files and choose Add > Existing Files…

    04

  4. Browse to the your project directory, select the Box2D-2.0.2 folder and click Add.

    05

  5. Make sure the Copy items into destination group’s folder checkbox is unchecked and click Add.

    06

  6. Expand the Box2D-2.0.2 group and delete everything except the Include and Source groups. Whether you move the files to the trash or just delete the references is up to you.

    07a

    07c

  7. Expand the Source group and delete the Makefile.

    08a

    08b

  8. If you haven’t already, set the app id (in Resources/Info.plist) and the Code Signing Identity in the project build settings (in Project > Edit Project Settings > Build). Make sure you have Device 2.2.1 and Debug selected in the menu at the top left corner of XCode (anyone know what this menu is called?). Finally, build the project (Command-B).
  9. Open up the build results (Command-Shift-B), and you’ll see a whole bunch of errors, along the lines of:

    error: 'finite' was not delcared in this scope

    09

  10. Open the project build settings (Project > Edit Project Settings > Build) and find the Other C Flags setting. Add the following flag, which tells Box2D that it’s being compiled for the iPhone so that it knows where to find the standard C++ libraries.

    -DTARGET_OS_IPHONE

    10

  11. Build the project again (Command-B). It will build cleanly, but we’re not quite out of the woods yet.
  12. Open MyGameViewController.m and add the following line at the top of the file:

    #import "Box2D.h"

    11

  13. Build the project again (Command-B). Yay, more errors:

    error: initializer element is not constant

    12

  14. Select all of the .m files in your project (yes, all of them).

    13a

  15. Click the Info button in the toolbar, go to the General tab and find the File Type dropdown.

    13b

  16. Change the filetype from sourcecode.c.objc to sourcecode.cpp.objcpp. This tells XCode to compile the files as Objective-C++ (mixed Objective-C and C++ code) instead of plain Objective-C code.

    13c

  17. Build the project again (Command-B). That’s it!

So there you go, you’re ready to start using Box2D on the iPhone. Just remember to change the file type from sourcecode.c.objc to sourcecode.cpp.objcpp for any .m files you add to the project.

8 comments on How To Add Box2D To An iPhone XCode Project
  1. Jason Says:

    Great overview. Mixing c++ and obj-c still looks bizarro, but I guess I’ll get used to it.
    re: top-left menu – “Active SDK”?

  2. Joe Says:

    An easier way is to change the extension of the file to .mm instead of .m and xcode will compile with the objective-c++ extension

  3. Jeff Says:

    Can you provide a ZIP file of the final project directory? I don’t have an “Other C Flags” option in the project settings.

  4. sam Says:

    hey
    i added your code
    screen is just white can you show how to add box and circle
    Thanks

  5. sam Says:

    in order to make new ball with different size do i need to write that code in same ball.m or i need
    to make a different class for that
    here is the code

    (id)init {
    if(self = [super init]) {

    [self setBodyDef:new b2BodyDef];

    [self setShapeDef:new b2CircleDef];

    [self shapeDef]->density = 2.0f;

    [self shapeDef]->restitution = 0.75f;

    [self setRadius:32.0f];
    [self setBallImageView:[[[UIImageView alloc]
    initWithImage:[UIImage imageNamed:@"Ball.png"]] autorelease]];

    }

  6. sam Says:

    hi
    i have seen your demo .. its amazing
    can you tell me how to scroll Bg using the same tutorial
    Thanks

  7. nitrorazor Says:

    Nice work, now i can work with Box2D, thanks a lot :)

  8. Linh Says:

    thanks it works fine with version 2.0.2.
    how do i make it work with version 2.1.2?

Your Comment…

You can use these tags: <a> <blockquote> <strong> <em> <strike> <code> <pre> Use <pre lang="LANGUAGE"> for syntax highlighted code blocks.