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:
The code I'm using for calculating contact points is the following:
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?
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);
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)
}
}
}
{
//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?