Box2D Buoyancy Example Screenshot

Box2D: Buoyancy example for Cocos2D

I’m back with an update of my Box2D example project. As the previous post, I was inspired by an example written for Box2DAS3.

This time I decided to implement the Buoyancy example on iOS (iPhone/iPad). For more info about the buoyancy feature provides by Box2D you can check this link. This new example shows few boxes that float on water thanks to the buoyancy. Moreover it uses the accelerometer values to change the world gravity and adapt the water plane to stay always horizontal.

To use the buoyancy controller I had to download the last version of the Box2D project and integrate it into Cocos2D. I’ve also merged the b2Controllers file from the Enhancements contribution branch to the main folder.

But I encountered some issues while trying to code this example. The main issue came from the b2BuoyancyController::Draw method which not drew the correct water line while I rotated the device. So I have modified this method as shown below to draw affine line.

void b2BuoyancyController::Draw(b2Draw *debugDraw)
{
    float32 lineWidth = 1024.0f;
    b2Vec2 p1, p2;

    // If the normal is vertical
    if (normal.y == 0.0f)
    {
        // Define the vertical line
        b2Vec2 horizontal(normal.x == 0.0f ? 0.0 : offset / normal.x, normal.y == 0.0f ? 0.0 : offset / normal.y);
        p1 = horizontal + b2Cross(horizontal, lineWidth);
        p2 = horizontal - b2Cross(horizontal, lineWidth);
    } else
    {
        // Define the affine line
        // D : y = a * x + b
        float32 b = (offset / normal.y);
        float32 a = b / (offset / normal.x * -1);

        p1 = b2Vec2(0.0f, b);
        p2 = b2Vec2(lineWidth, a * lineWidth + b);
    }

    // Draw the buoyancy water limit line
    b2Color blue(0.0f, 0.0f ,0.8f);
    debugDraw->DrawSegment(p1, p2, blue);
}

There are things to fix, to improve and/or to add yet, so if you want contribute to the project you are welcome. To download the project go to my github.

I hope this example will be helpful.

1 Star2 Stars3 Stars4 Stars5 Stars (3 votes, average: 4.67 out of 5)
Loading...

4 comments

Cancel

Time limit is exhausted. Please reload CAPTCHA.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  1. Gude · July 8, 2011

    Hi,

    I’m trying to join your controller classes into my cocos2d 2.0, but I stuck, just keep popping some errors of path import, which I sure its ok.
    Have you update your cool b2BuoyancyController to new cocos2d?

    Thanks in advance

    • Yannick Loriot · July 8, 2011

      Hi,

      No you I didn’t update the project to Cocos2D 2.0 yet but this is into my in my plans. However I don’t know when I’ll have time to do that yet. :$

  2. Sandbird · July 8, 2011

    I am trying to make a limited width buoyancy in cocos2d like this guy did here:
    http://www.youtube.com/watch?v=imzZnae4OHg

    Do you have any pointers on how to achieve this with this code :/ ?

    Thanks

  3. Pingback: List of Open Source Cocos2d Projects, Extensions and Code Snippets | iUridium