Cool Box2D stuff with Appcelerator Titanium

I happened upon the Appcelerator Open Mobile Marketplace the other day, and started looking through the modules.  One that caught my eye was the Box2D module.  Earlier this year, we’d been dabbling with Cocos2d which is a game engine for iPhone, which uses Box2D as one of the physics engine you could use inside it.

Box2D is an open source library originally written in C that has been ported to Flash, Java, C++, Objective C and is widely popular in the game community.  It’s not a huge stretch to see a Ti module adaptor being placed over the Objective C code, also nice because it should perform well.

I had a million and one things to do, so of course I dropped them all to see just what this shiny new bauble could do.  Right now the module is in beta and there’s zero documentation, so the best thing is to check out some examples like pinball or a Hello World (more or less), and peek at the Objective C source code for a couple proxy classes, like Body and World.

Running the example from the github repository, you get a square and a ball that basically start at the top of the screen and fall down (and off) the screen.

Objects fall from top of screen and then off bottom of screen

Here’s the code with no floor:


To get a floor for these objects to land on, I added this:

With Floor

Square now lands on top of ball on the floor


But then I wanted to see if the objects would tumble down a hill, so the floor needed to have an angle to it.  This is where examining the implementation of the World proxy object came in handy.  the world.addBody(aBody) function returns a handle to the body it added.  With that in hand, you can then apply methods from the Body proxy object implementation, and there happens to be one in there named setAngle(someDegree). That led to this code:

Angled Floor

ball bounces, then rolls down hill. Square tumbles a bit and then comes to rest


Lastly, I wanted to see if I could put up a wall that would let the ball bounce off the floor, roll down and then be stopped by the intersection of the wall and floor.  Here’s how:

And a Wall

ball has bounced off the wall and rolled back up the floor a bit


I’m looking forward to playing with Box2D module in more detail, maybe over Christmas break.  Hope you gained a little insight into its workings in Titanium Appcelerator.

13 thoughts on “Cool Box2D stuff with Appcelerator Titanium

  1. Nice writeup. Since the information on this module is pretty sparse when applied directly to titanium, it’s nice to see posts that show some basic examples.

    Have you done any additional work on this?

  2. A lot can be done with Box2D for Titanium, even with the limitations of the current module. I have a game in the App Store called Bead Frenzy, which was written with Titanium using the Box2D module.

    If I had one big tip it would be the collision event listener and getting access to data. The event listener is setup like the following:

    world.addEventListener(“collision”, function(e) {
    if(e.phase == ‘begin’){
    if(e.a.view.objectType == ‘ball’ && e.b.view.objectType == ‘holder’){
    //do stuff

    The collision event happens when a body collides with another body. The event will pass an A and B object, which are the two objects that collided. You can then get access to the view attached to that object as you see by .view

    This makes it easy to attach any amount of data you want pertaining to that object by using properties on the view. After I figured this out, it made it a lot easier determining which objects actually collided.

  3. Thanks

    i was just looking at the collision stuff, and i had wondered what a and b represented.

    Any further info you have or document resources would be great, im keen to help the community with box2d it seems like it has great potential

  4. Hey guys

    ive managed to make a simple game where you shoot objects and they dissapear.
    However i have a memory issue I’ve looked everywhere in forums and through box2D documentation but i cant seem to be able to remove the box2d body objects and views from memory
    i thought the destroyBody function would do it , but it doesnt . In the xCode profiler they stay in memory

    Any ideas?

    its driving me insane

    • My suspicion is that the memory is tied to the underlying view that the box2d body is built on. While destroyBody gets rid of the Box2d object, does it also get rid of the underlying view? If not, that’s where I bet the problem is.

      • I too think that however I can’t seem to use the remove(); I’ve contacted module support and they emailed me today ad said that it may be a bug and they will look into it.

  5. Pingback: A Simple Box2D Tutorial | Titaniummm

  6. Just to I form everyone about my memory issue

    I’ve spoken to module support they have confirmed there is an issue in the module and will be fixing it in an update

  7. Can you help me how to move object left or right using ti.Boz2D module ? I don’t want to use quicktigame2d module.

    • No, I’ve not looked at Box2d in awhile. This would probably be better directed at the general Ti Appcelerator forums.

  8. Pingback: This Week in Titanium Mobile Development: 27 Dec 2011 | TiDev

Comments are closed.