Weird behaviour with manually evaluated sensor contacts

I'm building a game in which objects can be picked up, dragged over top of other objects, and then dropped to weld them together. My implementation thus far has been to turn the object being dragged into a sensor, calculate the manifold using the contact's Evaluate function, undo the sensor status on the dragged body, and then create a weld joint between the two bodies at the contact point. This has worked wonderfully for me so far.

However, I've recently found a bizarre bug. If I create my environment's body (a static body with three fixtures with density 0) and add it to the world AFTER anything else, everything is fine. However, if I create it first, or create other bodies after it, then I have the following problem: the objects I'm trying to find contacts with aren't generating any useful contacts (IE, no joints are created), or if they are, the contact point is being detected as the origin of the world object REGARDLESS of where the contacts are supposed to be. In other words, the order in which I add bodies to the world affects whether or not contacts are being generated.

Stepping through the code didn't give me any particular insights. The best I can tell is that the contact manifold that I'm generating has absolutely zero useful data. I've tried adding mass to the environment's fixtures and even making the world a dynamic body instead, but so long as the environment body is added to the b2World before the body I'm trying to connect to it I get this bizarre behaviour with my contacts.

The code that creates my world body looks like this:
Code:
   b2BodyDef def;
   def.type = b2_dynamicBody;//b2_staticBody;
   def.position = b2Vec2(15,20);
   def.userData = this;

   body = world->addBody(&def);

   b2PolygonShape shape;

   shape.SetAsBox(30.0f, 0.5f);
   body->CreateFixture(&shape,1);
   shape.SetAsBox(0.5f, 15.0f, b2Vec2(-15,-15), 0.0f);
   body->CreateFixture(&shape,1);
   shape.SetAsBox(0.5f, 15.0f, b2Vec2(15,-15), 0.0f);
   body->CreateFixture(&shape,1);


The code I'm using for calculating contact points is the following:
Code:
      for(b2ContactEdge *contactEdge = body->GetContactList(); contactEdge != NULL; contactEdge = contactEdge->next)
      {         
         //For all TOUCHING contact edges....
         b2Contact *contact = contactEdge->contact;
         if(contactEdge->contact->IsTouching())
         {
            b2Body *A = body;
            b2Body *B = contactEdge->other;

            //SENSORS DO NOT CREATE OWN MANIFOLDS
            //Figure out the contact point(s).
            b2Manifold contactManifold;
            contact->Evaluate(&contactManifold, A->GetTransform(), B->GetTransform());
            b2WorldManifold contactWorldManifold;
            contactWorldManifold.Initialize(&contactManifold,
               A->GetTransform(), contact->GetFixtureA()->GetShape()->m_radius,
               B->GetTransform(), contact->GetFixtureB()->GetShape()->m_radius);

            //Create a new joint for each contact point
            for(int i = 0; i < contactManifold.pointCount; i++)
            {
               //When this point is reached, the contact points are ALWAYS the environment body's origin
               //Usually this point is never reached, even if it SHOULD be.               

               //(Joint creation code goes here)
            }
         }
      }


I've been banging my head against my keyboard for the last few hours trying to figure out why the order in which bodies are added to the world should matter here. Does anyone have any ideas?
I've been messing around, and I've found that the cause of this is that fixture A in the contact belongs to a static body. If I flip them around like so:
Code:
            if(contact->GetFixtureA()->GetBody()->GetType() == b2_staticBody)
            {
               //HACK: Creating contacts manually.
               contact = new b2PolygonContact(contact->GetFixtureB(), contact->GetFixtureA());
            }


Then I get my expected behaviour. Apparently, when evaluating my sensor contacts, it's not kosher if the first party to the contact is static, but creating a new contact with the fixtures flipped around fixes the problem.

Can anyone shed any light on why this is? I can hack around this problem now, but I'd still like an elegant solution if it is at all possible.
You cannot make any assumptions about which is which for fixtureA and B.
Ah, thank you. That fixes the problem elegantly. Using the following two lines solved the issue and did away with the need for that hack:
Code:
b2Body *A = contact->GetFixtureA()->GetBody();
b2Body *B = contact->GetFixtureB()->GetBody();

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