How will you check whether you have touched a body in Box2D?

By | February 3, 2011

You can iterate through all bodies in the world and check whether you have touched a particular body.
You can get all the userdata from the current body
The userdata contains the width, height, sprite etc.
Just Log the userdata in the current body to see all these.

 for (b2Body* b = world->GetBodyList(); b; b = b->GetNext())
  b2Fixture *bf = b->GetFixtureList(); // Get the fixture of current body.
  CCSprite *temp_sprite =(CCSprite *) b->GetUserData();// Got the sprite in that body.
  if(temp_sprite!=NULL){  // Checking if the body has a sprite.

   if (bf->TestPoint(locationWorld))
    NSLog(@"Hooray I touched the body");

    if (temp_sprite.tag==10)
      //do something…..



Leave a Reply

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