IOS, Box2D and OpenGL.

Looking through the top selling IOS apps (well, the games section), it seems like having realistic physics is the place to be. (Angry Birds of course, but also others like Cover Orange, Cut The Rope, and the new number one app Tiny Wings).

Tiny Wings (excellent by the way) credits Box2D on one of it’s screens – this got me thinking how easy it would be to get something up and running that uses the Box2D library.

There’s a lot of stuff out there regarding Box2D on IOS, but most of it is related to cocos2d, and that’s not really my thing. Fortunately for me, I found an excellent blog post on Cocoanetics which walks you through how to quickly get Box2D working in a simple app.

My code is based largely on their excellent work (and of course the equally impressive Box2D library itself) – the changes I’ve made are purely to get something working using Open GL.

A big pile of blocks...

The code here generates a universal app (ipad or iphone/ipod touch). All the blocks are generated at random (size, colour, linear and angular velocity). Touching the screen restarts the process (or waiting for the blocks to all stop moving…).

Change the MAX_RECTS define in the ViewController.h file to change how many blocks are generated – size and colour info is done in Rectangle.m. Here’s the project:

nibfree

Side note – the project is called nibfree as I was originally trying to get a universal opengl app working with no nib file (done!).

I’m not sure why, but it’s quite mesmerising watching how the blocks fall.

Big thanks to the Cocoanetics guys for their original blog.

As usual, any feedback at all is welcomed.

You may also like...

16 Responses

  1. Rob says:

    Hi,
    This is great. Thanks for sharing the source code.

    Is there any chance you would share a source project based on the Cocoanetics tutorial. I can’t get that tutorial to compile without a bunch of errors because I am not sure how to tell the compiler how to process c++, it is quite foreign to me.

    I appreciae your contribution to all of us learning developers out there.

    Cheers,
    Rob

  2. John says:

    Hi Rob. I’m afraid I can’t really add any more help than the original blog that I linked to. I basically followed their lead and then changed it to use OpenGL.

    I’m assuming you changed the source type to sourcecode.cpp.objcpp?

    The only other problem I had was spaces in the path of Box2D, so had to put quotes around the ${PROJECT_DIR} stuff (“${PROJECT_DIR}”).

    Hope you manage to get it sorted.

  3. Jason says:

    John, Are you using Xcode 4? I seem to get box2D compile issues when trying the demo using xcode 4. Just curious if you got it working using xcode4.

  4. John says:

    Hi Jason, using xcode 4 here too. Did you import my project into xcode 4 and try it? The project was originally an xcode 3.x project, and when I moved to xcode4 I just loaded it up and it ran fine.
    I can zip up the current project (i.e. the xcode4 project) and give it to you to try?
    Let me know.

  5. Jason says:

    Ah, figured it out. You had actually already answered it in the previous post, I needed to have “‘s around ${PROJECT_DIR}.

    Thanks so much John, for the demo and the quick response. It is very much appreciated.

  6. John says:

    No worries – glad you got it sorted.

  7. Thibaut says:

    Hi John,
    Thanks for sharing this – I’m getting started in iOS programming and it’s good to see a simple implementation using OpenGL.

    I manage to get your xcode project to compile and run from Xcode4.
    However I am having a hard time with my attempt on the Cocoanetics tutorial (based on a the View-Based Application template from Xcode4). I tried everything I could think of to indicate the header search path but the compiler keeps on saying it can’t find the Box2D header files. it works with #include “…” directives but not with #include directives.
    No space in my path… I tried to compare your project with mine but found nothing.
    Any additional hints ?
    Thanks,

    Thibaut

  8. JoshM says:

    Hi John,

    Thanks for the fantastic Opengl Project here. I have a question, do you have any idea of how to do this in Opengl ES 2.0.. As I have been struggling to try to get this working on that version for days! It’s so annoying but I can’t get it to work there, though I am unable to use Opengl ES 1.1, strange as it sounds I am far more adept at Opengl ES 2.0… So do you think you are able to point me in the right direction of how I’d go about doing so..???

  9. John says:

    @Thibaut – I don’t think you’re the only one struggling with this. I’m going to try and build a new xcode4 project (without using the existing code) and see how I get on. I’ll post my findings when I’ve done it.

    @JoshM – Not done much with ES2.0 I’m afraid. This is a useful link (and a great book actually) – the author deals with both cases (1.1 and 2.0) so it may help?
    http://iphone-3d-programming.labs.oreilly.com/index.html

  10. JoshM says:

    Thank you very much John ordered it last week actually :)

  11. apes says:

    i want it (i don’t write english)
    but.. good project thank u^^

  12. KomodoDave says:

    Thanks a lot for sharing this project, it’s a great time saver. I was about to start creating my own OpenGL initialisation functions from online code samples and integrate Box2D from scratch, but you’ve done all this for us :) Very much appreciated, thanks again.

  13. John says:

    Apologies KomodoDave – just found your message in the WordPress spam folder. Thanks for the feedback, glad you found it useful :)

  14. Matt says:

    Thanks for posting this John! It’s been a huge help to see an example of Box2D integration with OpenGL. The code complied and worked perfectly the first time.

    :)

  15. John says:

    Thanks Matt – glad it worked. I’ve got a feeling it might need an update for IOS5, as it runs a bit strangely on my iPhone 4S…if I get chance I’ll dig the code out and have a look.

  16. George says:

    Thanks! Your code works well and it helps me a lot!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>