Problem with ContactListener

I'm trying to check if two bodies are touching each other. I'm using following ContactListener:

Code: Select all

class MyContactListener : public b2ContactListener
{
private:
    b2Body* b1;
    b2Body* b2;
    int num;

public:
    MyContactListener (b2Body* _b1, b2Body* _b2){
        b1 = _b1;
        b2 = _b2;
        num = 0;
    }
    bool check(){
        return num>0;
    }
    void Add(const b2ContactPoint* p){
        b2Body* pb1=p->shape1->GetBody();
        b2Body* pb2=p->shape2->GetBody();
        if( (pb1==b1 && pb2==b2) ||
        (pb1==b2 && pb2==b1) ){
            num++;
        }
    }   
    void Remove(const b2ContactPoint* p){
        b2Body* pb1=p->shape1->GetBody();
        b2Body* pb2=p->shape2->GetBody();
        if( (pb1==b1 && pb2==b2) ||
        (pb1==b2 && pb2==b1) ){
            num--;
        }
    }
};


It's working but there seems to be a problem. Somtimes check() is true, even if there's no contact. Could it be that some removes are not reported?
...and by the way when exactly is "Result" called?
Try num >= 0.
Erin Catto wrote:Try num >= 0.


I know this is a too old thread, but why do you say >=0 instead of >0?
No idea.

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