b2Fixture::SetSensor infinite loop and setting bad flag

Hi,

I think there are two bugs in b2Fixture::SetSensor, but maybe someone can clarify just in case I'm missing something. Or maybe this might help someone!

The portion of that function's code in question is:

Code:
   // Flag associated contacts for filtering.
   b2ContactEdge* edge = m_body->GetContactList();
   while (edge)
   {
      b2Contact* contact = edge->contact;
      b2Fixture* fixtureA = contact->GetFixtureA();
      b2Fixture* fixtureB = contact->GetFixtureB();
      if (fixtureA == this || fixtureB == this)
      {
         contact->SetAsSensor(m_isSensor);
      }
   }


First, if edge is not null, this will go into an infinite loop. I fixed it by adding

Code:
edge=edge->next;

at the end of the loop.

Second, if you call SetSensor(false) and it is currently colliding with something that is a sensor, then it will make the contact not a sensor even though it should still be a sensor (it is colliding with a sensor). I fixed this by adding:

Code:
contact->SetAsSensor(fixtureA->IsSensor()||fixtureB->IsSensor());


so the final code is:

Code:
   // Flag associated contacts for filtering.
   b2ContactEdge* edge = m_body->GetContactList();
   while (edge)
   {
      b2Contact* contact = edge->contact;
      b2Fixture* fixtureA = contact->GetFixtureA();
      b2Fixture* fixtureB = contact->GetFixtureB();
      if (fixtureA == this || fixtureB == this)
      {
         contact->SetAsSensor(fixtureA->IsSensor()||fixtureB->IsSensor());
      }
      edge=edge->next;
   }


I'm using code from the google svn repository and it is up to date as of this date.

-j
Thanks. I'm integrating your fixes.
Wow - i was literally JUST experiencing this right now - and googled "SetSensor" - and this page came right up first with the fix.
Amazing lol! - and thanks 🙂



http://littlephysics.com
Erin Catto wrote:
Thanks. I'm integrating your fixes.

I've seen in the new release, r25, that you have removed the "second fix", the row:
Code:
contact->SetAsSensor (fixtureA->IsSensor () || fixtureB->IsSensor ());

reverting it to the previous version.

I thought queasy was right, this change seems contradictory respect how b2Contact constructor initializes m_flags when a sensor is used.

Maybe have been an error or does I miss something? Can you please explain?
Right, I'll fix this.

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