Reputation: 139
I am new to Box2D and I am trying to to detect if an object (Ball) has managed to pass through another object (Goal) by using b2Contact.
For my Goal object, I have implemented the b2ContactListener in the declaration, as well as overriding the BeginContact and EndContact function. In the code, I have tried to cout some random words to see if neither BeginContact or EndContact function is called, but nothing came out. The code is as below:
void Goooal::BeginContact(b2Contact* contact)
{
std::cout << "AAAAAAAAAAAAA";
if (contact && contact->IsTouching())
{
std::cout << "BBBBBBBBBBBBBBB";
}
}
void Goooal::EndContact(b2Contact* contact)
{
std::cout << "CCCCCCCCCCCCCCCC";
}
From the code above, none of the output(AAA/BBB/CCC) are coming out and I am very sure my Ball and Goal has passed through each other.
The sensor is declared as follow:
bodyFixtureD.isSensor = true;
May I know where am I doing wrong? Or do I need to implement the b2ContactListener for my ball as well?
Upvotes: 1
Views: 138
Reputation: 365
Try setting the Goal object you created to be an object of ContactListener class in your world. (I assume you have not done it since you did not mention it.)
You can do something like this:
world->SetContactListener(&ContactListenerInstance);
Hope it helps.
Upvotes: 1