determining what you're colliding with

Alright, newb question here but... I have been looking all over. Googled b2ContactListener and box2d collision and all that and I'm still fuzzy on this. How do you determine what you're colliding with? For example, say you have a player, a wall, and a bullet. I implemented my own b2ContactListener and I output some text when I have contact, but this goes off when *anything* has contact with anything. How do I tell specifically if the player is colliding with the wall, or the player is colliding with the bullet, or the bullet is colliding with the wall? Do I need to SetUserData to a pointer to the base actor class and then I could do like (pseudo-y code):

Code:
Actor* a1 = (Actor*)contact->GetFixtureA()->GetUserData();
Actor* a2 = (Actor*)contact->GetFixtureA()->GetUserData();


and then the actor class will have like... string name or unsigned int id? Then that way I could do:

Code:
if(a1->name == "player" && a2->name == "bullet")
{
     a1->Damage(a2->GetStrength());
     a2->Kill();
}



This is the only way I can think of but it seems like it'll get messy fast. Sorry if this has been answered a billion times... I just was failing at finding anything more specific.
I've made some progress doing it this way and I just have a few questions on it. Here's what I'm doing:

Code:
void ContactListener::BeginContact(b2Contact* contact)
{
   std::cout << "begin contact" << std::endl;

   Entity* e1 = (Entity*)contact->GetFixtureA()->GetBody()->GetUserData();
   Entity* e2 = (Entity*)contact->GetFixtureB()->GetBody()->GetUserData();

   if(e1 && e2)
   {
      if(e1->GetName() == Entity::PLAYER && e2->GetName() == Entity::MOVING_WALL)
      {
         std::cout << "player hits wall" << std::endl;
      }
   }
}

void ContactListener::EndContact(b2Contact* contact)
{
   std::cout << "end contact" << std::endl;

   Entity* e1 = (Entity*)contact->GetFixtureA()->GetBody()->GetUserData();
   Entity* e2 = (Entity*)contact->GetFixtureB()->GetBody()->GetUserData();

   if(e1 && e2)
   {
      if(e1->GetName() == Entity::GROUND && e2->GetName() == Entity::PLAYER)
      {
         std::cout << "player dies" << std::endl;
      }
   }
}



Now... this works how I want it to, but I'm confused about something. What if I switch the if statement:
if(e1->GetName() == Entity::MOVING_WALL && e2->GetName() == Entity::MOVING_PLAYER)
instead of
if(e1->GetName() == Entity::PLAYER && e2->GetName() == Entity::MOVING_WALL)

If I switch it around like this, it doesn't work. How do I guarantee that fixture A and fixture B have the bodies I want? Do you just have to test it and figure it out w/trial and error? Maybe I'm missing something here...
The way I've been doing it is assigning a tag to each body/sprite and then checking for contacts based on the tags:
Code:
b2Body *bodyA = contact->GetFixtureA()->GetBody();
b2Body *bodyB = contact->GetFixtureB()->GetBody();
CCSprite *spriteA = (CCSprite *) bodyA->GetUserData();
CCSprite *spriteB = (CCSprite *) bodyB->GetUserData();

if (spriteA.tag == 1 || spriteB.tag == 1) {
   // Anything that can contact with tag 1 below
   if (spriteA.tag == 2 || spriteB.tag == 2) {
   }
   if (spriteA.tag == 3 || spriteB.tag == 3) {
   }
   if (spriteA.tag == 4 || spriteB.tag == 4) {
   }
}


if (spriteA.tag == 2 || spriteB.tag == 2) {
   // Anything that can contact with tag 2 below (anything less than 2 already dealt with)
   if (spriteA.tag == 3 || spriteB.tag == 3) {
   }
   if (spriteA.tag == 4 || spriteB.tag == 4) {
   }
}

if (spriteA.tag == 3 || spriteB.tag == 3) {
   // Anything that can contact with tag 3 below (anything less than 3 already dealt with)
   if (spriteA.tag == 4 || spriteB.tag == 4) {
   }
}

//etc.....


Messy yes, but it works and if you layout all the combinations first it's not too bad. Good luck if you've got lots of bodies! In yours, if you have a bullet that could hit many different things you could always check which of the two actors (a1 or a2) is the bullet based on its tag and then use a switch statement for the bullet's tag with the cases being the tags of the other bodies that the bullet can hit.

Original thread: https://box2d.org/forum/viewtopic.php?t=6763