Contact Listener

Okay as of right now my contact listener tells me when certain game objects collide like so:

Code:
void ContactListener::Add(const b2ContactPoint *point)
{
   if(point->shape1->GetBody()->GetUserData() == "Character" && point->shape2->GetBody()->GetUserData() == "Element")
   {
      //////Do stuff//////////
   }

      
}


So Basically I am checking specific user data that I designated when I create my classes. So essentially I would have to put a bunch of if statements in order to know if my character is colliding with certain objects. So I guess my question is, is there a better way to check a bunch of different object without creating a bunch of if statements. Also how would I go about deleting an object, I read through some of the documentation and it said the contact listener cant delete objects.
To start off, it's a lot easier to create an "entity" or "gameObject" class for each object in the playfield instead of assigning a string to the void* userData. The entity class has a virtual method, say "onCollisionStart(...)" which has the arguments of what it's colliding with or what ever you need the entity object to know for a collision. Then you make several sub classes that inherits from the entity class, such as a character class and an Element class.

In the contact listener you then write something like:
Code:
void ContactListener::Add(const b2ContactPoint *point)
{
   Entity* entityA = (Entity*) point->shape1->GetBody()->GetUserData();
   Entity* entityB = (Entity*) point->shape2->GetBody()->GetUserData();
   
   entityA->onCollisionStart(...);
   entityB->onCollisionStart(...);
}


Make sure you assign your class to all bodies you create.
If you're unfamiliar with inheritance and polymorphism in C++, I recommend reading about it here: http://cplusplus.com/doc/tutorial/inheritance/

As for the deletion, keep a list outside of the contact listener and add your objects that are to be deleted, then access it after Box2D simulation in your main loop and delete.
Well that kind of brings me into another question. I have a base class called Gameobject and everything derives from that. Now the problem I am having a little trouble with is how would I make it so that when my character collides with lets the say an "element" I run a specific function from my character class like? I know the contact listener knows when two objects collide but where am I suppose to put the game logic code? If I put it in my classes then how would I tell the contact listener to call that function?
Okay sorry about the last question after messing with it for awhile I got it to work the example you gave me but if I do it this way am I going to have to do a bunch of checks to know what shape one is and shape 2 for example. With the code you gave as an example:

Code:
void ContactListener::Add(const b2ContactPoint *point)
{
   Entity* entityA = (Entity*) point->shape1->GetBody()->GetUserData();
   Entity* entityB = (Entity*) point->shape2->GetBody()->GetUserData();
   
   entityA->onCollisionStart(...);
   entityB->onCollisionStart(...);
}


Lets say I want to run a character specific function when I collide withe a certain object. Now in order to that I would have to type cast like this:

Character* Main = (Character*) EntityA;

The problem is I won't know if the character is going to be the entity A or B. Now I could run a bunch of if statements and that would work but I feel like that is a "dirty" way of doing it. Is there a better way to check to see if my character is entity a or b or will I just have to do a bunch of if checks?
You should try to move the character specific calls into the virtual OnCollision() method for the specific class rather than call it externally. Any time you are dealing with a base and need to start switching or if-ing to find out the derived type, it is probably a suggestion that you are applying the logic in the wrong place.

If you could provide a more specific example of where you need to know the derived type, I'm sure we can examine other ways to achieve this without resorting to querying derived type.
I use an enumerator to tag classes derived from my base GameObject. The GameObject has a value that I simply call ObjectType or what ever which will hold the type id.


Code:
class GameObject{
public:
    enum ObjectType { unknown, player, enemy, bullet, wall } type;
    GameObject();
    virtual OnCollision( GameObject* crashedWith ); // You may just enter the contact as argument or which ever way you think is neat.
};

...
class Player : public GameObject{
    Player();
    OnCollision( GameObject* crashedWith ); // This will be called even if the Player object is cast as a GameObject.
};
Player::Player(){
    type = player;
}
Player::OnCollision( GameObject* crashedWith ){
    if( crashedWith->type == enemy ){
         // Damage player or what ever.
    }
}


Try to generalize your classes as much as possible through the GameObject class so you avoid casting as much as possible. Use the virtual methods to access higher level methods in the derived classes. Virtual functions are key!
Hope I made some sense!
I tend to go more down the:

Code:
class Entity
{
public:
    virtual bool KillsPlayer() const=0;
    virtual bool IsStickable() const=0;
};

void Player::OnCollide(const Entity &E)
{
    if(E.KillsPlayer()){ /* etc */ }
}


It's not ideal but C++ is not the best language for doing double-dispatch. I do prefer to query the object for required information than switch on its type though, even within the OnCollide. The solution posted by Zoomulator is better than we originally had, but still requires updating of all of the OnCollide methods when a new type is added or removed.

My approach does tend to bloat the interface with these query methods, but rather than make them pure in the interface as above, I tend to put the default values in there and override only the ones I need. Since each is just a return true/false, they just sit in the derived class definition as a single line.

Not saying this is great design from a theory perspective but it has proven best of a bad lot for me without over-engineering.
Hey guys thanks for the responses it helped me understand it a little more and now I got it working 😀 I followed what you guys said and changed my code around so my contact listener does this:

Code:
void ContactListener::Add(const b2ContactPoint *point)
{
   /////Type cast the two colliding objects to a GameObject////////////////
      GameObject* Object1 = (GameObject*)point->shape1->GetBody()->GetUserData();
      GameObject* Object2 = (GameObject*)point->shape2->GetBody()->GetUserData();   

      Object1->HandleCollision(Object2);
      Object2->HandleCollision(Object1);
      
}


Then lets say in my character class I go and basically check to see what class I am colliding with like this:

Code:
void Character::HandleCollision(GameObject* CollidesWith)
{
   
      if(CollidesWith->GetClass() == "Element")
      {
           /////////Run Element collision code///////////
      }
   
}


So this worked for me and I appreciate all the help.
I'm still using the older box2d right now and I am trying to make it so some objects don't collide with one another but when ever I try using the b2Fixturedef and b2Fixture class it doesn't know what they are. Do I have to upgrade to the new one in order to do this? I thought I remember in the older documentation something about fixtures. Can anyone shed some light onto this for me I have searched through my entire project and b2Fixture isn't there.

EDIT:

Sorry never mind found it

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