Tutorial: cocos2d Xcode Proj.: Integrating Box2d physics Comments Feed" href="http://www.learn-cocos2d.com/knowledge-base/tutorial-professional-cocos2d-xcode-project-template/feed/"/>

Tutorial: cocos2d Xcode Proj.: Integrating Box2d physics

This Tutorial is superceded by Kobold2D

This tutorial was designed for Xcode 3 and most of the steps do not apply to Xcode 4.

All the experiences described in this tutorial have been utilized to help create the Kobold2D game engine.

Kobold2D is designed to make Cocos2D developers more productive. If you use Kobold2D you can bypass this tutorial and instead rely on a working solution with many improvements not described in this tutorial.

Search my cocos2d for iPhone FAQs & Tutorials

Please note that the blog search in the upper right corner doesn’t search my FAQs and Tutorials.
 

  • Note: please do not share direct download links to PDF files, the download links expire after a couple minutes!

Integrating Box2d requires a few extra steps in order to build programs that use Box2d physics.

Download Lesson PDF

Specify correct Header Path to Box2d files

Open the Get Info dialog of the project and make sure that "All Configurations" and "All Settings" are selected. Look for the Build Setting "Header Search Paths". Do not add it to the one that reads "User Header Search Paths", use the setting without "User" in its name, it won't work otherwise. Doubleclick this setting to enter the path.

Enter the Box2d Path

Add a new entry. The Path to the Box2d header files is:

$(COCOS2D_SOURCE)/external/box2d

Avoid Compile Errors with Box2d

Since Box2d is written in C++ the compiler must regard all files that #import "box2d.h" as C++ files. There are two ways you can ensure that.

1.) Either: create or rename all .m files which use Box2d as.mm files
2.) Or: change the file type of any .m file that uses Box2d as shown above tosourcecode.cpp.objcpp

Do not add box2d header to PCH file

One word of caution: although adding #import "box2d.h" to the *_Prefix.pch file would usually be a good and reasonable thing to do, it will most likely lead to a lot of compile errors. You could probably fix those by changing all the files in your project to be .mm or sourcecode.cpp.objcpp but it may not be worth the hassle.

Comments (0)

The content sidebar has been activated on this page/post but doesn't have any widgets added to it. Add some widgets to this sidebar in appearance > widgets in the admin.

183 Responses to “Tutorial: cocos2d Xcode Proj.: Integrating Box2d physics”

  1. Carl says:

    I get “cocos2d.h:No such file or directory” when trying to #import “cocos2d.h”

    what am I missing?

  2. vik says:

    This tutorial is toooo long.

    Is there a short version ? I spent two hours on this and as I expected nothing works.
    For begininers its not going to work

  3. Norm says:

    Great info! Thanks! However, the script is building the .ipa file before it does the codesign step. Have things changed in Xcode? How do I get the script to run after the codesign step?

  4. zutroi says:

    I tried twice to follow your tutorial step by step using cocos2d 0.99.5-rc1, Xcode 3.2.5 and iOS 4.2.1, but I keep having problems when building and running on device.
    The weird thing is I have no problem at all running the template on simulator!
    To be more clear:
    I’m trying to build and run following the final step of “Getting our Project Template to build minimal cocos2d code” section.
    Running on simulator works perfectly, running on device (iphone4) crashes after splash screen with this message on console:
    “Program received signal: “EXC_BAD_ACCESS”.
    warning: Unable to read symbols for /Developer/Platforms/iPhoneOS.platform/DeviceSupport/4.2.1 (8C148)/Symbols/Developer/usr/lib/libXcodeDebuggerSupport.dylib (file not found).
    Previous frame inner to this frame (gdb could not unwind past this frame)”
    By stepping with debugger I found the app crashes on line 98 of AppDelegate.m:
    “// make the View Controller a child of the main window
    [window addSubview: viewController.view];”

    I see there are little differences from your (great) tutorial and Xcode/cocos2d new templates, mainly for the use of RootViewController, may this crash be related? (I already added RootViewController.* to my template)
    Has anyone found a fix to this problem?
    Please please please help me, this thing is driving me crazy =/
    Thanks

    • It might be related. Have you tried the version on github? https://github.com/GamingHorror/cocos2d-project
      That version is tested with 0.99.5

      But I recently started getting those “could not unwind” errors … I was able to fix them by changing the compiler in all referenced projects back to GCC.

      • zutroi says:

        Bingo!
        Thanks a lot, you point me to a fix.
        I had already tried with your latest github version, without luck.
        I followed your suggestion, changing the compiler to GCC 4.2 on all projects (both cocos2d-project and cocos2d-ios) and it worked!
        I suppose the problem was in compiler setting for cocos2d-ios, it was set to LLVM compiler 1.6.
        If I set compiler to different version for the two projects, I always get that crash, when using same gcc version, everything is ok.
        I tried also with LLVM GCC4.2 on both projects and it works too.

        Probably I never met this problem since using cocos2d built-in templates everything get compiled using gcc without further setting.
        Thanks a lot for your tutorials… and for fast replying too ;)

        • Stephen Beeman says:

          I think your problem was optimization level, believe it or not. If you’re using the LLVM compiler, and you set optimization level to “none” (-O0), then a call to glColor4ub() in CCLabelAtlas will kernel fault on the device, but not in the simulator. Since adding the view controller to the window is what kicks off the render loop, and since every app has a CCLabelAtlas in it (the framerate counter), how this manifests itself is that you do the addSubview call and suddenly your app crashes without a stack trace. Deuced hard to track down.

          The fix (apart from using the old GCC compiler, which is really not a good idea anymore-Apple is close to deprecating it) is simply to set optimization to -Os. I realize it’s bizarre for the *lower* optimization level to introduce bugs.

          Oh, but be sure you delete the target-level build settings before you do this, as Steffen described above. The “Debug” configuration of the Cocos2D project sets optimization to -O0 on each target individually.

  5. Paris says:

    Hi Steffen

    To upgrade a project created with your template from e.g. 0.99.5 rc1 to the final 0.99.5 I just need to copy over the cocos2d-IPhone folder?

    I would assume yes but thought of asking since it does not clearly covered in the main points/links at the top of this tutorial

  6. Mehdi says:

    I receive a lot of EAGLView may not respond to …xxx warnings, and I believe these are the undeclared selectors you are mentioning in your comment. How can I fix it? Only with turning off the treat warning as error option or also by changing the code?

    I didn’t understand where to implement the code from your comment on this tutorial.

    • Mehdi says:

      BTW, the entire tutorial is really great!!

    • If you get these errors then it’s because the EAGLView has changed in the latest cocos2d version. It should work with the cocos2d-project that you can get from github: https://github.com/GamingHorror/cocos2d-project

      • Mehdi says:

        Hi Steffen!

        I am trying to implement the Spacemanager for quite some time now, but it doesn’t work.

        I can compile the example project of the Spacemanager package without any problem, but in this template I receive following error:

        “SpaceManager.mm:749: error: could not convert ‘cpSpaceSegmentQuery((*(cpSpace**)(((char*)self) + OBJC_IVAR_$_SpaceManager._space)), start, end, layers, group, ((void (*)(cpShape*, cpFloat, cpVect, void*))collectAllSegmentQueryShapes), ((void*)array))’ to ‘bool’

        Do you know a solution?

        Thanks!
        Mehdi

  7. rastermax says:

    Hi
    1st: GREAT job on this PDF. Realllly appreciated.

    I just did the first part up to Helloworld with the 99.5 version. (up to page 43 of pdf)

    You might want to update a bit, the part where we get the AppDelegate from Helloworld sample : since in 99.5 Cocos changed the samples so that for exemple main.m does not exists main() is in the delegate.m file.

    Continue the great work.

  8. JP-P says:

    Dude, you ruuuuuuuuuuuuuuuuuuule! Thanx a lot for this great tutorial!

  9. Sree says:

    My goodness, this is a very precious article, every line of the PDF is important and will save hours of repletion in your build cycles.

    Thanks Steffan.

    Regards Sree

  10. Yeah says:

    Can you update this for XCode 4?

  11. paris says:

    Kobold2D is going to replace this? if so can how can anyone get access to it since i understand its still a work in progress?

    Thanks

  12. Bananni says:

    best guide u will find on the whole internet!

  13. Anselm says:

    Hi Steffen,

    Thank you for the fantastic Tutorials,.
    I saw your facebookHelper file, and it would be fantastic to get a tutorial to make a similar File for the actual API :-) )

    Greetings

    Anselm

  14. Alberto says:

    hi! i have problems with xcode 4 and the cross-project references.
    When i “Add Target Dependencies” (in xcode4 is : Select Target: Build Phases: Target Dependencies: “+”:cocos2d) i have this error:

    ASSERTION FAILURE in /SourceCache/IDEXcode3ProjectSupport/IDEXcode3ProjectSupport-269/Xcode3Sources/XcodeIDE/Frameworks/DevToolsBase/pbxcore/PBXContainerItemProxy.m:33
    Details: Assertion failed: portal != ((void*)0)
    Object:
    Method: -initWithType:portal:remoteGlobalIDString:remoteInfo:
    Thread: {name = (null), num = 1}
    Hints: None
    Backtrace:
    [removed for clarity]

    Any ideas for fix it?

Leave a Reply