Contact Listener

Hi!

Can anyone explain in more detail how does the b2ContactListener works? By the documentation I didn't realy get it.

For example, what can we do if we only want to detect the sensor-shapes colisions? and how do we know what shapes are colliding?

tx!
A contact between two shapes lasts as long as the two shapes in are in contact. The contact listener, which inherits b2ContactListener, recieves a call to Add for each contact created, Persist for each frame after the first, and Remove when the shapes are no longer in contact.

It is passed a b2ContactPoint, which allows you to determine facts about the collision. E.g. to determine if you have a sensor to non-sensor contact, you would do something like:
Code:
public void MyListener::Add(b2ContactPoint* point){
  b2Shape * sensor,nonSensor;
  if(point->shape1->IsSensor()&&!point->shape2->IsSensor()){
    sensor=shape1;
    nonSensor=shape2;
  }else if(!point->shape1->IsSensor()&&point->shape2->IsSensor()){
    sensor=shape2;
    nonSensor=shape1;
  }else{
    return;
  }
  //Do sensor stuff here
}

That should get you started on the main points of this feature.
huhuu!

thanks alot! things are way clearer now.

and one more thing ... the listener gets a call every time there is any collision in the world?

tx!
Yep, every collision that doesn't get filtered.
still on the subject ...

when are the points received by the listener destroid?

I'm buffering the pointers in a vector<b2ContactPoint*> so I can take care of them after the Step is finished. When I try to acess those points (rigth after the Step() )... there is only garbage there ... -_-

any ideias of what might be happening?

tx!
The contact points are temporary, you must copy them into your own buffer as shown in the manual.

http://www.box2d.org/manual.html#d0e1300

You did read the manual, right?
sometimes one word makes a hell of a light in your head ...

I was buffering the points without COPY them

thanks anyway! *** feelling dumb 😳 ***


PS: yeah ... I read it 🙄

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