I was wondering that suppose a ball hits 2 bodies simultaneously, how can we give the conditions for that? Suppose a ball is hitting an L shaped 2 lines and i want have some conditions specifically for that occasion, how do i go about it?
When a collision happens you get a callback containing pointers to the two fixtures colliding.
I store a pointer to my entity in the UserData of the fixture, so I now know my two entities.
In my ‘ball’ entity I have a ‘currentlytouching’ collection into which I push the ‘wall1’ entity I just collided with.
similarly on ‘uncollide’ I remove that entity from the collection.
So I have a collection of all entities this entity is touching.
So in your case you can examine that collection &, if you just hit wall1 , test of wall2 is in the collection, and act accordingly.
I have uploaded an image. I have different line colliders for all sides of the rectangle. I want to process the collisions when i hit the corners, that time the ball touches 2 physics bodies instead of one. So i was wondering how to solve it.
Sorry - I don’t have code that does exactly that - and no time to write it for you.
Essentially,
IN BeginContact I’d have something like …
void ContactListener::BeginContact(b2Contact* contact)
{
b2Fixture* fixtureA = contact->GetFixtureA();
b2Fixture* fixtureB = contact->GetFixtureB();
// I have a class PhysicalObject that represents an object that has a body
PhysicalObject* objectA = (PhysicalObject*) fixtureA->GetBody()->GetUserData();
PhysicalObject* objectB = (PhysicalObject*) fixtureB->GetBody()->GetUserData();
// Now I can call a method on each of the objects involved in the collision
if (objectA)
{
objectA->collidedWith(objectB);
}
if (objectB)
{
objectB->collidedWith(objectA);
}
In the collidedWith(PhysicalObject* object) method I’d do something like…
Check my Vector<PhysicalObject*> collection to see what is in it;
If there’s already an objectA, just ignore it - I’ve just triggered a double collision - happens sometimes.
If there’s not an objectA, but there is an objectX, then I know I have collided with both an A and an X - so I can act accordingly,
I obviously need to write the associated EndContact method in the listener, to call the unCollidedWith method that removes that object from the collection.