CoderzHeaven

February 3, 2011

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

You can iterate through all bodies in the world and check whether you have touched a particular body.
Youcan getall 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 Cancel reply