Contacts been created before collision

Hi all,
I'm developing a game with a massive number of collisions.
My game is quite complex so I'm getting the contacts within box2d bodies and I'm facing a quite weird issue, that is, the contacts are created far before the bodies collide.
To fix that, I'm currently using b2TestOverlap checking that the bodies are really colliding and if not I just ignore the contact.
As you can imagine this causes performance problems that I'd like to avoid.

Is there any reason why contacts have been constantly created even if there is no collision? And how can I avoid it?

This is my code:

Code:
   for (b2ContactEdge* ce = _body->GetContactList(); ce; ce = ce->next) {
      b2Contact* c = ce->contact;
      
      b2Fixture *fixtureA = c->GetFixtureA();
      b2Fixture *fixtureB = c->GetFixtureB();

      //checking if they overlap
      if (!b2TestOverlap(fixtureA->GetShape(), fixtureB->GetShape(),
                    fixtureA->GetBody()->GetTransform(), fixtureB->GetBody()->GetTransform())) {
         continue; //they don't => ignore contact
      }
      
      ...
   }


Thanks a lot!
Pre-emptive contact creation improves performance. Think of them as potential overlapping pairs.
Thanks Eric.
I was wondering if this is the correct way of dealing with a huge amount of contacts between different bodies.
I mean, is it OK to go to each contact within each body or should I implement a ContactListener?

I was thinking about something like this (there's a bit of obj-c, hope you don't mind):
Code:
void MyContactListener::BeginContact(b2Contact* contact) {
    b2Body *bodyA = contact->GetFixtureA()->GetBody();
    b2Body *bodyB = contact->GetFixtureB()->GetBody();

    if (bodyA->GetUserData() != NULL) {
        id userData = bodyA->GetUserData();

        //if userData implements the beginContactWithBody method I'll call it
        if ([userData respondsToSelector:@selector(beginContactWithBody:)]) {
             [userData beginContactWithBody:bodyB];
        }
    }

    //the same for bodyB
}

//something similar for EndContact


and in beginContactWithBody set some properties without changing the physics so that the update method of the object can inspect them.

Hope this is clear.

Thanks again, much appreciated.
That seems fine. Why do you have so many contacts? Are you creating all your bodies at the origin?
My bodies are animals and lots of objects like stones etc., they all move and have to interact with each other and often in different ways.
An animal has 1 main body, 3 movement sensors e 4 bodies (with 4 joints) for the movement (the legs), other objects only have 1 body and 1 sensor.
There might be up to 15 bodies on the screen and they have to process the contacts to avoid obstacles and collisions.
I'm currently processing the contacts within the bodies because each one interacts with the others (sensors, bodies etc.) in different ways and it works just fine but on older devices the fps sometimes drops to 20 for half a second, not really a big deal but I want to try implementing a ContactListener and see if it gets better.

Ah and I'm not creating all my bodies at the origin. 😉

If you have a better idea, please let me know but I guess a ContactListener like the one I shown you should be OK (I hope :S ).
Thanks a lot!

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