Mar 312012
 

Prerequisites

This is the second part to a four part series about simulating a soft body physics in Box2d. Before continuing, you should read and understand the following two tutorials.

1. Quick Tip: Mixing OpenGL and Cocos2d (Triangle Fan)
2. Soft Body Physics with Box2d and Cocos2d Part 1/4

We will be using OpenGL to map a circle to the Box2d wheel object, using a triangle fan.

Making the Wheel Bounce

We will also be making the wheel bounce when you touch the screen. In HelloWorldLayer, we will be calling the bounce method in MyNode, which will apply an impulse force to the wheel. Here is the code to do this. If you are unfamiliar with how Box2d works, please refer to the Box2d tutorials on this site.

- (void) bounce {
    b2Vec2 impulse = b2Vec2(innerCircleBody->GetMass() * 0, innerCircleBody->GetMass() * 150);
    b2Vec2 impulsePoint = innerCircleBody->GetPosition();
    innerCircleBody->ApplyLinearImpulse(impulse, impulsePoint);	        
}

Drawing the Circle

Inside the draw method is where we can place custom OpenGL calls in Cocos2d, so this is where we will be drawing the circle. What we are doing is using the Box2d objects we defined as the vertices for the triangle fan. It’s a simple matter of calculating the vertices to send to OpenGL. Here is the draw method of MyNode.

- (void) draw {
    // Using the wheel defined by the box2d objects, we'll be mapping a circle on
    // top of it using a triangle fan. First, we calculate the center. The center
    // needs to be mulitplied by the PTM_RATIO (to get the pixel coordinate from box2d coordinate)
    // and also must be offset by the current position (remember, in HelloWorldLayer, we set
    // the position to the center of the screen (myNode.position = ccp(240, 160).
    triangleFanPos[0] = Vertex2DMake(innerCircleBody->GetPosition().x * PTM_RATIO - self.position.x, 
                                     innerCircleBody->GetPosition().y * PTM_RATIO - self.position.y);
    

    // Use each box2d body as a vertex and calculate coordinate for the triangle fan
    for (int i = 0; i < NUM_SEGMENTS; i++) {
        b2Body *currentBody = (b2Body*)[[bodies objectAtIndex:i] pointerValue];
        Vertex2D pos = Vertex2DMake(currentBody->GetPosition().x * PTM_RATIO - self.position.x, 
                                    currentBody->GetPosition().y * PTM_RATIO - self.position.y);
        triangleFanPos[i+1] = Vertex2DMake(pos.x, pos.y);
    }
    
    // Loop back to close off the triangle fan
    triangleFanPos[NUM_SEGMENTS+1] = triangleFanPos[1];

    // Disable the states. We're not texturing mapping just yet.
    glDisable(GL_TEXTURE_2D);
    glDisableClientState(GL_TEXTURE_COORD_ARRAY);
    glDisableClientState(GL_COLOR_ARRAY);

    // Set the color to red
    glColor4f(1.f, 0.f, 0.f, 1.f);

    glVertexPointer(2, GL_FLOAT, 0, triangleFanPos);
    // Number of vertices is NUM_SEGMENTS+2 because we have the origin
    // plus the loop back to close off the triangle fan
    glDrawArrays(GL_TRIANGLE_FAN, 0, NUM_SEGMENTS+2);

    // Re-enable states
    glEnableClientState(GL_COLOR_ARRAY);
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);
    glEnable(GL_TEXTURE_2D);
}

Source and Video

You can download the full source for this tutorial here.

Soft Body Physics with Box2d and Cocos2d Part 1/4
Soft Body Physics with Box2d and Cocos2d Part 3/4
Soft Body Physics with Box2d and Cocos2d Part 4/4

 Posted by at 7:50 am

  4 Responses to “Soft Body Physics With Box2d (and Cocos2d) Part 2/4”

  1. […] 1. Quick Tip: Mixing OpenGL and Cocos2d (Triangle Fan) 2. Soft Body Physics with Box2d and Cocos2d Part 1/4 3. Soft Body Physics with Box2d and Cocos2d Part 2/4 […]

  2. […] Mixing OpenGL and Cocos2d (Triangle Fan) 2. Soft Body Physics with Box2d and Cocos2d Part 1/4 3. Soft Body Physics with Box2d and Cocos2d Part 2/4 4. Soft Body Physics with Box2d and Cocos2d Part […]

  3. […] Soft Body Physics with Box2d and Cocos2d Part 2/4 Soft Body Physics with Box2d and Cocos2d Part 3/4 […]

  4. Hello thanks for the tutorial..
    I am new to open gl.
    Now i am making it in cocos2d-x and in that glVertexPointer is not working..
    Instead there is glVertexAttribPointer
    Do you know how to use glVertexAttribPointer for your code..

 Leave a Reply

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=""> <s> <strike> <strong>

(required)

(required)