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….
- Create a new XCode project. I’m creating a View-Based Application called MyGame on my desktop.
-
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 - Right click on MyGame under Groups & Files and choose Add > Existing Files…
- Browse to the your project directory, select the Box2D-2.0.2 folder and click Add.
- Make sure the Copy items into destination group’s folder checkbox is unchecked and click Add.
- 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.
- Expand the Source group and delete the Makefile.
- 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).
- 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 - 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 - Build the project again (Command-B). It will build cleanly, but we’re not quite out of the woods yet.
- Open MyGameViewController.m and add the following line at the top of the file:
#import "Box2D.h" - Build the project again (Command-B). Yay, more errors:
error: initializer element is not constant - Select all of the
.mfiles in your project (yes, all of them). - Click the Info button in the toolbar, go to the General tab and find the File Type dropdown.
- Change the filetype from
sourcecode.c.objctosourcecode.cpp.objcpp. This tells XCode to compile the files as Objective-C++ (mixed Objective-C and C++ code) instead of plain Objective-C code. - 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.
















May 14th, 2009 at 10:38 am
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”?
August 6th, 2009 at 5:25 am
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
August 11th, 2009 at 10:34 pm
Can you provide a ZIP file of the final project directory? I don’t have an “Other C Flags” option in the project settings.
August 29th, 2009 at 5:21 am
hey
i added your code
screen is just white can you show how to add box and circle
Thanks
September 2nd, 2009 at 2:24 am
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]];
}
September 18th, 2009 at 10:08 am
hi
i have seen your demo .. its amazing
can you tell me how to scroll Bg using the same tutorial
Thanks
September 25th, 2009 at 2:04 pm
Nice work, now i can work with Box2D, thanks a lot :)