Dear All,
I have four object connected by soft distance joint, and i have found out that collision detection fails sometime and those object penetrate each other. The frequency of distance joint is 0.7 and damping ratio is 1.5. Please point out if i have done something wrong, thank you.
I have attached two screenshot to illustrate the problem, And my testing code is a modified 'web.h' in testbed, the code is as following:
#ifndef CBTEST_H
#define CBTEST_H
// This tests distance joints, body destruction, and joint destruction.
class CbTest : public Test
{
public:
CbTest()
{
//m_world->SetGravity(b2Vec2(0.0f, 0.0f));
b2Body* ground = NULL;
{
b2BodyDef bd;
ground = m_world->CreateBody(&bd);
b2EdgeShape shape;
shape.Set(b2Vec2(-40.0f, 0.0f), b2Vec2(40.0f, 0.0f));
ground->CreateFixture(&shape, 0.0f);
//b2Body * sky;
//sky = m_world->CreateBody(&bd);
//shape.Set(b2Vec2(-40.0f, 1.0f), b2Vec2(40.0f, 1.0f));
//sky->CreateFixture(&shape, 0.0f);
b2Body * left;
left = m_world->CreateBody(&bd);
shape.Set(b2Vec2(-40.0f, 0.0f), b2Vec2(-40.0f, 5.0f));
left->CreateFixture(&shape, 0.0f);
b2Body * right;
right = m_world->CreateBody(&bd);
shape.Set(b2Vec2(40.0f, 0.0f), b2Vec2(40.0f, 5.0f));
right->CreateFixture(&shape, 0.0f);
}
{
b2PolygonShape shape;
shape.SetAsBox(2.5f, 2.5f);
b2BodyDef bd;
bd.bullet = true;
bd.type = b2_dynamicBody;
b2FixtureDef fd;
fd.shape = &shape;
fd.density = 5.0f;
fd.friction = 0.1f;
bd.position.Set(2.5f, 5.0f);
m_bodies[0] = m_world->CreateBody(&bd);
m_bodies[0]->CreateFixture(&fd);
bd.position.Set(7.5f, 5.0f);
m_bodies[1] = m_world->CreateBody(&bd);
m_bodies[1]->CreateFixture(&fd);
bd.position.Set(12.5f, 5.0f);
m_bodies[2] = m_world->CreateBody(&bd);
m_bodies[2]->CreateFixture(&fd);
bd.position.Set(17.5f, 5.0f);
m_bodies[3] = m_world->CreateBody(&bd);
m_bodies[3]->CreateFixture(&fd);
b2DistanceJointDef jd;
b2Vec2 p1, p2, d;
jd.frequencyHz = 0.7f;
jd.dampingRatio = 1.5f;
m_frequency = 0.7f;
m_dampingRatio = 1.5f;
jd.bodyA = m_bodies[0];
jd.bodyB = m_bodies[1];
jd.localAnchorA.Set(0.0f, 0.0f);
jd.localAnchorB.Set(0.0f, 0.0f);
p1 = jd.bodyA->GetWorldPoint(jd.localAnchorA);
p2 = jd.bodyB->GetWorldPoint(jd.localAnchorB);
d = p2 - p1;
jd.length = d.Length();
m_joints[0] = m_world->CreateJoint(&jd);
jd.bodyA = m_bodies[1];
jd.bodyB = m_bodies[2];
jd.localAnchorA.Set(0.0f, 0.0f);
jd.localAnchorB.Set(0.0f, 0.0f);
p1 = jd.bodyA->GetWorldPoint(jd.localAnchorA);
p2 = jd.bodyB->GetWorldPoint(jd.localAnchorB);
d = p2 - p1;
jd.length = d.Length();
m_joints[1] = m_world->CreateJoint(&jd);
jd.bodyA = m_bodies[2];
jd.bodyB = m_bodies[3];
jd.localAnchorA.Set(0.0f, 0.0f);
jd.localAnchorB.Set(0.0f, 0.0f);
p1 = jd.bodyA->GetWorldPoint(jd.localAnchorA);
p2 = jd.bodyB->GetWorldPoint(jd.localAnchorB);
d = p2 - p1;
jd.length = d.Length();
m_joints[2] = m_world->CreateJoint(&jd);
}
}
void Keyboard(unsigned char key)
{
switch (key)
{
case 'b':
{
b2Vec2 f = m_bodies[0]->GetWorldVector(b2Vec2(-200.0f, 0.0f));
b2Vec2 p = m_bodies[0]->GetWorldPoint(b2Vec2(0.0f, 0.0f));
m_bodies[0]->ApplyLinearImpulse(f, p);
}
break;
case 'j':
{
b2Vec2 f = m_bodies[3]->GetWorldVector(b2Vec2(200.0f, 0.0f));
b2Vec2 p = m_bodies[3]->GetWorldPoint(b2Vec2(0.0f, 0.0f));
m_bodies[3]->ApplyLinearImpulse(f, p);
}
break;
case 'q':
m_frequency += 0.1f;
setUpFrequency(m_frequency);
break;
case 'a':
m_frequency -= 0.1f;
setUpFrequency(m_frequency);
break;
case 'v':
m_dampingRatio += 0.1f;
setUpDampingRatio(m_dampingRatio);
break;
case 'f':
m_dampingRatio -= 0.1f;
setUpDampingRatio(m_dampingRatio);
break;
}
}
void setUpFrequency(float frequency)
{
for (int32 i = 0; i < sizeof(m_joints)/sizeof(m_joints[0]); ++i)
{
if (m_joints[i])
{
((b2DistanceJoint *)m_joints[i])->SetFrequency(frequency);
}
}
}
void setUpDampingRatio(float dampingRatio)
{
for (int32 i = 0; i < sizeof(m_joints)/sizeof(m_joints[0]); ++i)
{
if (m_joints[i])
{
((b2DistanceJoint *)m_joints[i])->SetDampingRatio(dampingRatio);
}
}
}
float getFrequency()
{
for (int32 i = 0; i < sizeof(m_joints)/sizeof(m_joints[0]); ++i)
{
if (m_joints[i])
{
return ((b2DistanceJoint *)m_joints[i])->GetFrequency();
}
}
return 1000000.01f;
}
float getDampingRatio()
{
for (int32 i = 0; i < sizeof(m_joints)/sizeof(m_joints[0]); ++i)
{
if (m_joints[i])
{
return ((b2DistanceJoint *)m_joints[i])->GetDampingRatio();
}
}
return 1000000.01f;
}
void Step(Settings* settings)
{
Test::Step(settings);
m_debugDraw.DrawString(5, m_textLine, "This demonstrates a soft distance joint.");
m_textLine += 15;
m_debugDraw.DrawString(5, m_textLine, "Press: (b) to delete a body, (j) to delete a joint");
m_textLine += 15;
m_debugDraw.DrawString(5, m_textLine,
"joint frequency %g(q to increase, a to decrease), damping %g(v to increase, f to decrease)",
getFrequency(), getDampingRatio());
m_textLine += 15;
}
void JointDestroyed(b2Joint* joint)
{
for (int32 i = 0; i < sizeof(m_joints)/sizeof(m_joints[0]); ++i)
{
if (m_joints[i] == joint)
{
m_joints[i] = NULL;
break;
}
}
}
static Test* Create()
{
return new CbTest;
}
b2Body* m_bodies[4];
b2Joint* m_joints[3];
float m_frequency;
float m_dampingRatio;
};
#endif
I have four object connected by soft distance joint, and i have found out that collision detection fails sometime and those object penetrate each other. The frequency of distance joint is 0.7 and damping ratio is 1.5. Please point out if i have done something wrong, thank you.
I have attached two screenshot to illustrate the problem, And my testing code is a modified 'web.h' in testbed, the code is as following:
#ifndef CBTEST_H
#define CBTEST_H
// This tests distance joints, body destruction, and joint destruction.
class CbTest : public Test
{
public:
CbTest()
{
//m_world->SetGravity(b2Vec2(0.0f, 0.0f));
b2Body* ground = NULL;
{
b2BodyDef bd;
ground = m_world->CreateBody(&bd);
b2EdgeShape shape;
shape.Set(b2Vec2(-40.0f, 0.0f), b2Vec2(40.0f, 0.0f));
ground->CreateFixture(&shape, 0.0f);
//b2Body * sky;
//sky = m_world->CreateBody(&bd);
//shape.Set(b2Vec2(-40.0f, 1.0f), b2Vec2(40.0f, 1.0f));
//sky->CreateFixture(&shape, 0.0f);
b2Body * left;
left = m_world->CreateBody(&bd);
shape.Set(b2Vec2(-40.0f, 0.0f), b2Vec2(-40.0f, 5.0f));
left->CreateFixture(&shape, 0.0f);
b2Body * right;
right = m_world->CreateBody(&bd);
shape.Set(b2Vec2(40.0f, 0.0f), b2Vec2(40.0f, 5.0f));
right->CreateFixture(&shape, 0.0f);
}
{
b2PolygonShape shape;
shape.SetAsBox(2.5f, 2.5f);
b2BodyDef bd;
bd.bullet = true;
bd.type = b2_dynamicBody;
b2FixtureDef fd;
fd.shape = &shape;
fd.density = 5.0f;
fd.friction = 0.1f;
bd.position.Set(2.5f, 5.0f);
m_bodies[0] = m_world->CreateBody(&bd);
m_bodies[0]->CreateFixture(&fd);
bd.position.Set(7.5f, 5.0f);
m_bodies[1] = m_world->CreateBody(&bd);
m_bodies[1]->CreateFixture(&fd);
bd.position.Set(12.5f, 5.0f);
m_bodies[2] = m_world->CreateBody(&bd);
m_bodies[2]->CreateFixture(&fd);
bd.position.Set(17.5f, 5.0f);
m_bodies[3] = m_world->CreateBody(&bd);
m_bodies[3]->CreateFixture(&fd);
b2DistanceJointDef jd;
b2Vec2 p1, p2, d;
jd.frequencyHz = 0.7f;
jd.dampingRatio = 1.5f;
m_frequency = 0.7f;
m_dampingRatio = 1.5f;
jd.bodyA = m_bodies[0];
jd.bodyB = m_bodies[1];
jd.localAnchorA.Set(0.0f, 0.0f);
jd.localAnchorB.Set(0.0f, 0.0f);
p1 = jd.bodyA->GetWorldPoint(jd.localAnchorA);
p2 = jd.bodyB->GetWorldPoint(jd.localAnchorB);
d = p2 - p1;
jd.length = d.Length();
m_joints[0] = m_world->CreateJoint(&jd);
jd.bodyA = m_bodies[1];
jd.bodyB = m_bodies[2];
jd.localAnchorA.Set(0.0f, 0.0f);
jd.localAnchorB.Set(0.0f, 0.0f);
p1 = jd.bodyA->GetWorldPoint(jd.localAnchorA);
p2 = jd.bodyB->GetWorldPoint(jd.localAnchorB);
d = p2 - p1;
jd.length = d.Length();
m_joints[1] = m_world->CreateJoint(&jd);
jd.bodyA = m_bodies[2];
jd.bodyB = m_bodies[3];
jd.localAnchorA.Set(0.0f, 0.0f);
jd.localAnchorB.Set(0.0f, 0.0f);
p1 = jd.bodyA->GetWorldPoint(jd.localAnchorA);
p2 = jd.bodyB->GetWorldPoint(jd.localAnchorB);
d = p2 - p1;
jd.length = d.Length();
m_joints[2] = m_world->CreateJoint(&jd);
}
}
void Keyboard(unsigned char key)
{
switch (key)
{
case 'b':
{
b2Vec2 f = m_bodies[0]->GetWorldVector(b2Vec2(-200.0f, 0.0f));
b2Vec2 p = m_bodies[0]->GetWorldPoint(b2Vec2(0.0f, 0.0f));
m_bodies[0]->ApplyLinearImpulse(f, p);
}
break;
case 'j':
{
b2Vec2 f = m_bodies[3]->GetWorldVector(b2Vec2(200.0f, 0.0f));
b2Vec2 p = m_bodies[3]->GetWorldPoint(b2Vec2(0.0f, 0.0f));
m_bodies[3]->ApplyLinearImpulse(f, p);
}
break;
case 'q':
m_frequency += 0.1f;
setUpFrequency(m_frequency);
break;
case 'a':
m_frequency -= 0.1f;
setUpFrequency(m_frequency);
break;
case 'v':
m_dampingRatio += 0.1f;
setUpDampingRatio(m_dampingRatio);
break;
case 'f':
m_dampingRatio -= 0.1f;
setUpDampingRatio(m_dampingRatio);
break;
}
}
void setUpFrequency(float frequency)
{
for (int32 i = 0; i < sizeof(m_joints)/sizeof(m_joints[0]); ++i)
{
if (m_joints[i])
{
((b2DistanceJoint *)m_joints[i])->SetFrequency(frequency);
}
}
}
void setUpDampingRatio(float dampingRatio)
{
for (int32 i = 0; i < sizeof(m_joints)/sizeof(m_joints[0]); ++i)
{
if (m_joints[i])
{
((b2DistanceJoint *)m_joints[i])->SetDampingRatio(dampingRatio);
}
}
}
float getFrequency()
{
for (int32 i = 0; i < sizeof(m_joints)/sizeof(m_joints[0]); ++i)
{
if (m_joints[i])
{
return ((b2DistanceJoint *)m_joints[i])->GetFrequency();
}
}
return 1000000.01f;
}
float getDampingRatio()
{
for (int32 i = 0; i < sizeof(m_joints)/sizeof(m_joints[0]); ++i)
{
if (m_joints[i])
{
return ((b2DistanceJoint *)m_joints[i])->GetDampingRatio();
}
}
return 1000000.01f;
}
void Step(Settings* settings)
{
Test::Step(settings);
m_debugDraw.DrawString(5, m_textLine, "This demonstrates a soft distance joint.");
m_textLine += 15;
m_debugDraw.DrawString(5, m_textLine, "Press: (b) to delete a body, (j) to delete a joint");
m_textLine += 15;
m_debugDraw.DrawString(5, m_textLine,
"joint frequency %g(q to increase, a to decrease), damping %g(v to increase, f to decrease)",
getFrequency(), getDampingRatio());
m_textLine += 15;
}
void JointDestroyed(b2Joint* joint)
{
for (int32 i = 0; i < sizeof(m_joints)/sizeof(m_joints[0]); ++i)
{
if (m_joints[i] == joint)
{
m_joints[i] = NULL;
break;
}
}
}
static Test* Create()
{
return new CbTest;
}
b2Body* m_bodies[4];
b2Joint* m_joints[3];
float m_frequency;
float m_dampingRatio;
};
#endif