How to add cocos2d Box2D sources to your XCode project?
cocos2d version 0.8.1 comes with Box2D physics engine; however, it’s not quite easy to add Box2D sources to your XCode project.
If you simply drag external/Box2d/Box2D directory into your project, you will encounter lots of compile errors. Probably over 3000!
I spent some time figuring out this problem. I am going to share a way to use Box2D physics engine for you cocos2d project.
I assume that you created your XCode project by choosing cocos2d project template. In case you don’t know how to install cocos2d project template, read the release note:
Go to http://www.cocos2d-iphone.org/wiki/doku.php/release_notes:0_8_1 and search for Template.
Here is how to add Box2D to the XCode project:
1. Copy Box2D directory from the unzipped cocos2d-iphone-0.8.1/external/Box2d directory into your project directory where build, Classes, etc directories reside.
2. Now, you need to make a flat file structure. In other words, copy all the files within Collision, Common, and Dynamics directories to Box2D directory. You could delete Collision, Common, and Dynamics directories after copying.
3. Open your XCode project file.
4. Drag Box2D directory from Finder to XCode project. You could add to any location, but I added to “cocos2d sources” group.
5. Here is a very important step that made me spend quite some time figuring out. Change the extension of your source file that uses Box2D from .m to .mm. Without this change, you will get No such file or directory compiler errors for C++ libraries like cassert and cmath.
The Apple compilers are based on the compilers of the GNU Compiler Collection. Objective-C syntax is a superset of GNU C/C++ syntax, and the Objective-C compiler works for C, C++ and Objective-C source code. The compiler recognizes Objective-C source files by the filename extension
.m, just as it recognizes files containing only standard C syntax by filename extension.c. Similarly, the compiler recognizes C++ files that use Objective-C by the extension.mm.<From Introduction to The Objective-C 2.0 Programming Language>
*** In case you want to keep the original tree structure of Box2D directory, skip Step 2 and do this after Step 5.
Click Project Menu -> Edit Active Target “Your Project Name”
Make “Always Search User Path” checked.
Add “enternal/Box2d” to “User Header Search Path”
Enjoy Box2D physics engine!
Thanks for the tip with .mm files. Saved myself a whole lot of trouble and time!
Thanks so much.Your mind it is very helpful for me now.