How can I identify when the ground body collides with another b2Body? I can use b2ContactListener
to be notified of collision events, but how do I tell which bodies collided?
-
\$\begingroup\$ possible duplicate of Basic Box2D collision detection \$\endgroup\$– HouseCommented Jul 22, 2012 at 20:53
-
\$\begingroup\$ @Byte56, nope..see here I don't know how to find collision with groundBody and there its different. Please write answer if you know how to identify collision when object hits ground floor. \$\endgroup\$– Save WaterCommented Jul 23, 2012 at 3:52
-
\$\begingroup\$ even I used b2ContactListener, that helped me to identify collision with other b2body but not with groundBody..so your link is not helped. \$\endgroup\$– Save WaterCommented Jul 23, 2012 at 3:53
-
1\$\begingroup\$ @Byte56 Joe Wreschnig, I don't mind if you people vote up or down...now I got solution from Joe Wreschnig. Really 'Helping hands are better than praying Lips'..thanks alot for your answer. Now resolved all problem in game.. \$\endgroup\$– Save WaterCommented Jul 23, 2012 at 14:20
-
1\$\begingroup\$ @SaveWater Indeed they are. I'm glad you got the answer you wanted. Do try to be a bit clearer next time. I'm sure you can see the difference between your original question and Joe's edits. \$\endgroup\$– HouseCommented Jul 23, 2012 at 14:33
1 Answer
This is the point of b2Body's UserData
member. It's a pointer you can set to anything you want. During the collision you can check this pointer and change how you react to the collision depending on its value.
For a trivial case, you can make the user data a NSString. For example,
groundBody->SetUserData(@"Ground Body");
Then inside your b2ContactListener
subclass, or when you loop over bodies checking their contact points, you can use:
if ([bodyA->GetUserData() isEqual:@"Ground Body"]) {
// Do something special if the body is the ground body.
}
(n.b. This is not compatible with ARC. I wouldn't recommend using ARC with games yet if you're an Objective-C beginner. The libraries aren't ready and the best idioms have not gelled).