Identify b2Contact among b2ContactListener callbacks

Here's the place to get help and discuss features. The focus is on the C++ version, but generic questions are welcome.
eugeneko
Posts: 2
Joined: Sat Mar 11, 2017 7:39 am

Identify b2Contact among b2ContactListener callbacks

Postby eugeneko » Sat Mar 11, 2017 7:48 am

Hello!

In docs it's highly recommended not to store pointers on contacts:

http://box2d.org/manual.pdf
Page 52
Caution
Do not keep a reference to the pointers sent to b2ContactListener. Instead make a
deep copy of the contact point data into your own buffer. The example below
shows one way of doing this.


I need some kind of contact list.
Now I store pointers on interesting contacts in BeginContact and remove in EndContact.
If I don't keep pointers, how can I identify contacts?

Thanks.

Louis Langholtz
Posts: 22
Joined: Tue Dec 20, 2016 10:53 am

Re: Identify b2Contact among b2ContactListener callbacks

Postby Louis Langholtz » Wed Mar 15, 2017 3:49 pm

You can use the fixtures and child IDs for this. There should only ever be zero or one Contact structures for a given pair of fixtures and child indexes; so it uniquely identifies the contact. Note that fixture A and fixture B may get interchanged but the child index for fixture will always stay with its fixture.

Here's some sample code for this...

Code: Select all

struct ProxyKey
{
   b2Fixture* fixture;
   int32 childIndex;
}

struct ContactKey
{
   ProxyKey first;
   ProxyKey second;
};

static bool IsMatch(const b2Contact& c, const ContactKey& key)
{
   const b2Fixture* fA = c.GetFixtureA();
   const int32 iA = c.GetChildIndexA();
   const b2Fixture* fB = c.GetFixtureB();
   const int32 iB = c.GetChildIndexB();

   if (fA == key.first.fixture && iA == key.first.childIndex &&
       fB == key.second.fixture && iB == key.second.childIndex)
   {
      return true;
   }
   if (fB == key.first.fixture && iB == key.first.childIndex &&
       fA == key.second.fixture && iA == key.second.childIndex)
   {
      return true;
   }
   return false;
}

eugeneko
Posts: 2
Joined: Sat Mar 11, 2017 7:39 am

Re: Identify b2Contact among b2ContactListener callbacks

Postby eugeneko » Mon Mar 20, 2017 11:15 pm

Thanks! Then I'll try this way.


Return to “General Discussion”



Who is online

Users browsing this forum: No registered users and 4 guests