Is GetFixtureA() of b2Contact guaranteed to return the body on which GetContactList() was called?

| | August 4, 2015

For example, in the code below, will the assert ever fail?

for (const b2ContactEdge *contactEdge = playerPhysicsBody->GetContactList(); contactEdge; contactEdge = contactEdge->next) {
    assert(contactEdge->contact->GetFixtureA()->GetBody() == playerPhysicsBody);
}

I wasn’t able to determine this from the source code. From some brief debugging, it seems to be the case, but I want to avoid any subtle bugs if this assumption is wrong.

The reason that I want to do this is to find the particular fixture that belongs to the player that was involved in the contact.

One Response to “Is GetFixtureA() of b2Contact guaranteed to return the body on which GetContactList() was called?”

  1. Shortly after posting I tested this out a bit more and found that the assert will fail. GetFixtureA() may or may not return a fixture belonging to playerPhysicsBody.

    I’m now using this convenience function to safely find the fixture that I want:

    inline const b2Fixture *contactFixtureFor(const b2Body *body, const b2ContactEdge *contactEdge) {
        return contactEdge->contact->GetFixtureA()->GetBody() == body
                ? contactEdge->contact->GetFixtureA()
                : contactEdge->contact->GetFixtureB();
    }
    

Leave a Reply