B2ContactListener

Does anyone have a little more in depth explanation on how to implement the contact listener in C++. I mean I have seen some tutorials in flash and read the documentation for box2d but nothing really goes to in depth on how to implement it with c++. I have tried a few things in my project and maybe someone can explain it to me a little more. Here is the basics of what I have no logic put into the functions just yet which is what I hope you guys can help me with:

Code:
void MyContactListener::Add(b2ContactPoint *point)
{

   if(point->shape1->GetBody()->GetUserData() && point->shape2->GetBody()->GetUserData())
   {
      
      
   }
   
}

   /// Called when a contact point persists. This includes the geometry
   /// and the forces.
void MyContactListener::Persist( b2ContactPoint* point)
{

}

   /// Called when a contact point is removed. This includes the last
   /// computed geometry and forces.
   void MyContactListener::Remove( b2ContactPoint* point)
{
   
}

   /// Called after a contact point is solved.
   void MyContactListener::Result( b2ContactResult* point)
{
   if(point->shape1->GetBody()->GetUserData() && point->shape2->GetBody()->GetUserData())
   {
      
   }
}


As you can tell I am still using the older box2d but, what needs to go inside those functions? I know in flash after you put all the logic inside those functions you just implement it like so:

Code:
ContanctListener* m_contactListener=new ContanctListener();
         m_world.SetContactListener(m_contactListener);


That's C++ but its the basic premise of what they did in the flash tut.

Now pretty much all they did with the code inside the Add and Remove was set the alpha of the box to 50% if it was colliding. So how would I be able to tell if a specific object is colliding rather than every object in the world? Is there a better way to check collision for one object or am I going to have to use the contact listener in order to achieve this? If so then how exactly would I achieve this because it seems like the world just checks to see if an object is colliding with another object and if it is run the Add function code in the contact listener. How do I ask a specify object if it is colliding with another specific object?

IE if the character is colliding with the item then add the item to the inventory.

If anyone is curious I am using Dark GDK to code in.
Poof Master wrote:
how would I be able to tell if a specific object is colliding rather than every object in the world?


You have to use the UserData for that. The simplest is to have a structure with attributes, so that you can do things like:

Code:
if(point->shape1->GetBody()->GetUserData() && point->shape2->GetBody()->GetUserData())
{
  YourClass *userdata = reinterpret_cast<YourClass *>(point->shape1->GetBody()->GetUserData());
  if (userdata->m_type == PLAYER)
  {
    // its the player colliding, do something special
    ...
  }     
}


You can also probably do fancy things with virtual functions instead of an attribute.
The thing is, all your UserData should be of the same type (here "YourClass"), because at the time you do the cast, you don't know yet what is in there.
Is it possible to do the contact listener with my own struct. What I mean by that is I would make a struct that holds pointers to my game objects like so:

Code:
struct MyGameObjects
{
   /////All my game objects in here like characters, items and enemies
};


Now in my base class called GameObject I have a function that gets the body of the object so I will be able to get the body and then call GetUserData() on specific game objects. So basically instead of checking if shapes collide like how I showed in my earlier post I could just check my character to an item and then if that is true run whatever code I have inside kind of like so:

Code:
void ContactListener::Add(MyGameObjects* point)
{
  ///instead of just point->shape->getBody()->GetUserData() it would be
if(point->character->GetBody()->GetUserData() && point->Item->GetBody()->GetUserData() )
{
///run logic
}


Now is add called every time there is a collision that happens in the World?
No, it works the other way around: Box2D tells you when it's bodies/shapes are colliding, and you have to translate this into something of your own by getting the user data. Box2D does not handle your classes, it can only give you back the UserData pointers that you set previously.
Once you see how it works, it's really simple. You seem to be trying too complicate things right now!
Haha yeah I tend to over think things when I program but anyway I have been messing with the contact listener and just trying to get text to show up on the screen when something collides. Now this is what I am trying but nothing is showing up:

Code:
void ContactListener::Add(const b2ContactPoint *point)
{
   if(point->shape1->GetBody()->GetUserData() && point->shape2->GetBody()->GetUserData())
   {
      dbText(200,200,"Hello I am colliding");
   }
}


Now in my main I am doing this:

Code:
world->SetContactListener(m_ContactListener);


Iv'e searched these forums and google but most the projects just set he contact listener and it works. Is there another line of code that I am missing? Or do I need to do more in my actually contact listener?
- check that your Add method prototype exactly matches the one in your box2D version for the contact listener interface (including const-ness)
- try printing outside of the "if"
- do you set some user data on your bodies ? (else the "if" will not evaluate to true)
Ahh I think the last one is my problem. I will just go through to my game object class and set the user data for my objects.
Well I wasn't calling the setUserData on my objects but even if I put the print outside of the if block it doesn't call that function. Shouldn't it call add every time there is a collision when I set my contact Listener? I guess I'm back at square 1 🙁 . Is there anything else that should be acalled in my main program that I might be missing?
Well I got it to work and print a message when my objects are colliding 😀. My next question is, does this effect the performance of my game if I use it? Like is there anything I should be careful of when using a contact listener? If anyone has some good tips when working with it I would greatly appreciate it.
Poof Master wrote:
My next question is, does this effect the performance of my game if I use it.

Don't worry about performance - just get it working the way you want it to work, and then if there are performance problems, you can address them at that time. (But no, I would not expect that using a contact listener would be likely to significantly affect performance.)

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