# How do I identify a collision with a specific body?

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?

-
possible duplicate of Basic Box2D collision detection –  Byte56 Jul 22 '12 at 20:53
@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. –  Save Water Jul 23 '12 at 3:52
even I used b2ContactListener, that helped me to identify collision with other b2body but not with groundBody..so your link is not helped. –  Save Water Jul 23 '12 at 3:53
@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.. –  Save Water Jul 23 '12 at 14:20
@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. –  Byte56 Jul 23 '12 at 14:33

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).

-
Thanks alot Sir..its working. –  Save Water Jul 23 '12 at 14:17