Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add Box2d to an existing Xcode/Cocos2d Project

I have a working Xcode project that includes Cocos2d in one of the views. I now need to add Box2d. I tried dragging the Box2D folder into the libs directory of my cocos2d-2.x-ARC-iOS folder and adding to that target, but I'm getting this when I compile:

(in b2BroadPhase.cpp)
'Box2D/Collision/b2BroadPhase.h' file not found

I'm sure this is just the first of many errors I will get. What step am I missing to get all the Box2d files compiled in my project?

All of the box2d files are listed in the Compile Sources for the cocos2d-library target.

And why is this so difficult to do? Why doesn't it work like any other library?

AND - do I have to rename every single .m in my project to .mm? That's pretty much impossible as this is a large xcode project with many files. I'm just trying to use Box2d in one view.


Additional info in response to the answer given by LearnCocos2D-

My folder structure (in Finder) looks like this:

Root = MyProject.xcodeproj, MyProject folder, cocos2d-2.x-ARC-iOS folder In the cocos2d-2.x-ARC-iOS folder there is a folder called libs. Inside that folder I have the Box2D folder, along with Cocos2d, CocosDenshion, etc.

In XCode I have a PROJECT (MyProject) And 2 TARGETS (cocos2d-library, MyProject) There are "Header Search Paths" for both the PROJECT, and the TARGETS. Which one do I set? And should it be recursive or non? And when I drag the Box2D folder into Xcode, which target do i set it to?

My project file is located at /Users/me/Dropbox/iOS/MyProject. So what exactly do I enter for the Header Search Path? /Users/me/Dropbox/iOS/MyProject/cocos2d-2.x-ARC-iOS/libs doesn't seem to work.


Another Update - /Users/me/Dropbox/iOS/MyProject/cocos2d-2.x-ARC-iOS/libs as the Header Search Path in the MyProject TARGET seems to get me past the Box2D errors. Then I had a 'cassert' file not found error. Got past that by changing the view controller, it's parent, and the AppDelegate to a File Type of "Objective-C++ Source" like you suggested. And now...

Now I've got a litany of errors in NSObjCRuntime.h NSZone.h, NSObject (Expected unqualified-id, Unknown type name 'NSString', etc.). So it seems like you either create an app with Box2d/Cocos2d, or a UIKit app. Trying to have both is a complete nightmare.

like image 611
soleil Avatar asked Mar 02 '13 20:03

soleil


1 Answers

Easiest solution: start a new project with Box2D already integrated. Then add your source files & resources to it. Trust me, it doesn't get easier.

A couple things from the top of my head that are required for Box2D:

As search path use the path to the Box2D.h file minus one level. Ok, this needs a better explanation:

If Box2D.h is in /Projects/MyProject/libs/Box2D

… then search path should be /Projects/MyProject/libs

This is because Box2D #include statements always include the Box2D path:

#include <Box2D/Common/b2Settings.h>

In summary, the Header Search Path plus the #include/#import path should give the full path to the file:

/Projects/MyProject/libs/Box2D/Common/b2Settings.h

Then it's easy to check whether you got the search path right or not.

Be sure to add the search path to Header Search Path and not User Header Search Path setting as for other libraries. This is because Box2D uses angle bracket <file.h> includes. Most other libraries use "file.h" instead, which use the User Header Search Path to find their header files.

Why is it difficult? Compiler settings. There are plenty of them. Errors and warnings aren't always obvious. Actually, this is the norm. And plenty of ways to write and add code. Oh so many different languages and language versions, too. It's mostly a matter of experience. At level 5 and higher it gets easier, at level 10 it's merely a nuisance. :)

You have to rename every .m file that includes Box2D either directly (#import "Box2D.h") or indirectly (including a header that includes Box2D). If you add the Box2D header to the Prefix.pch file, that would be the same as including it in every file - so I wouldn't do that in your situation.

You can avoid the rename by setting the file type of the .m files in Xcode to "Objective-C++". Though this will be just as tedious, if not more. Tip: there are good tools out there for mass-renaming files, for situations exactly like this.

like image 133
LearnCocos2D Avatar answered Sep 22 '22 19:09

LearnCocos2D