Hey, I have built a ragdoll out of boxes and i'm trying to attach the arms (or one of them for starters) to a chain that is just hanging from the ceeling.
I disconnected one of my revoluteJoints for my arm and put it on the position on the last body of my chain(have an array to be able to access the position of the last body) because it's supposed to hang from there. And it worked.
So I disconnected another body, my upperArm(that is connected to the torso, (disconnected that revoluteJoint aswell). And I tried to do the exact same thing, I use;
but my upper arm does not attach, it just falls down like a normal b2_dynamicBody.
what have I done wrong?
I disconnected one of my revoluteJoints for my arm and put it on the position on the last body of my chain(have an array to be able to access the position of the last body) because it's supposed to hang from there. And it worked.
So I disconnected another body, my upperArm(that is connected to the torso, (disconnected that revoluteJoint aswell). And I tried to do the exact same thing, I use;
Code:
float _x = upperArmL->GetPosition().x;
float _y = lowerArmL->GetPosition().y;
jd.initialize(upperArmL, lowerArmL , b2Vec2(_x, _y));
float _y = lowerArmL->GetPosition().y;
jd.initialize(upperArmL, lowerArmL , b2Vec2(_x, _y));
but my upper arm does not attach, it just falls down like a normal b2_dynamicBody.
what have I done wrong?
Code:
b2Body* link = paddleBody;
for (int i = 0; i <= 30; i++)
{
b2BodyDef bodyDef;
bodyDef.position.Set(400.0f, 100.0f + i * 10);
bodyDef.type = b2_dynamicBody;
body[i] = _world->CreateBody(&bodyDef);
b2PolygonShape shape;
shape.SetAsBox( 0.125f, 6.0f);
b2FixtureDef ballShapeDef;
ballShapeDef.shape = &shape;
ballShapeDef.density = 70.0f;
ballShapeDef.friction = 0.0f;
ballShapeDef.restitution=0.2f;
body[i]->CreateFixture(&ballShapeDef);
revoluteJointDef.bodyA = link;
revoluteJointDef.bodyB = body[i];
body[i]->SetBullet(true);
revoluteJointDef.Initialize(link, body[i], body[i]->GetPosition());
_world->CreateJoint(&revoluteJointDef);
//saving the reference of the last placed link
link = body[i];
}
/*shape.Set(b2Vec2( 40.0f, 0.0f), b2Vec2(-40.0f, 0.0f)) ;
fixtureDef.shape = &shape;
fixtureDef.density = 1.0f;
paddleBody->CreateFixture( &fixtureDef );*/
bd.type = b2_dynamicBody;
//head
{
//headDef.type = b2_staticBody;
headShape.m_radius = 12.5f;
testFixtureDef.shape = &headShape;
testFixtureDef.density = 1.0f;
testFixtureDef.friction = 0.0f;
testFixtureDef.restitution = 0.0f;
bd.position.Set(300.0f, 50.0f);
head = _world->CreateBody(&bd);
head->CreateFixture(&testFixtureDef);
/*head->ApplyLinearImpulse(b2Vec2(500, 0) , head->GetWorldCenter());*/
//b2Vec2(std::rand() % 100 + 50, std::rand() % 100 + 50), head->GetWorldCenter()
}
//torso1
{
//torsoDef.type = b2_dynamicBody;
box.SetAsBox(15.0f, 10.0f);
testFixtureDef.shape = &box;
testFixtureDef.density = 1.0f;
testFixtureDef.friction = 0.0f;
testFixtureDef.restitution = 0.0f;
bd.position.Set(300.0f, 50.0f +25.0f);
torso1 = _world->CreateBody(&bd);
torso1->CreateFixture(&testFixtureDef);
}
// Torso2 ----
box.SetAsBox(15.0f, 10.0f);
fixtureDef.shape = &box;
bd.position.Set(300, 50 + 43.0f);
b2Body *torso2 = _world->CreateBody(&bd);
torso2->CreateFixture(&fixtureDef);
// Torso3 ----
box.SetAsBox(15.0f, 10.0f);
fixtureDef.shape = &box;
bd.position.Set(300, 50 + 58.0f);
b2Body *torso3 = _world->CreateBody(&bd);
torso3->CreateFixture(&fixtureDef);
//upperArm
fixtureDef.density = 1.0f;
fixtureDef.friction = 0.0f;
fixtureDef.restitution = 0.0f;
//left
box.SetAsBox(18.0f, 6.5f);
fixtureDef.shape = &box;
bd.position.Set(420.0f - 30.0f, 415.0f + 20.0f);
b2Body *upperArmL = _world->CreateBody(&bd);
upperArmL->CreateFixture(&fixtureDef);
// Right
box.SetAsBox(18.0f, 6.5f);
fixtureDef.shape = &box;
bd.position.Set(300 + 30.0f, 50 + 20.0f);
b2Body *upperArmR = _world->CreateBody(&bd);
upperArmR->CreateFixture(&fixtureDef);
// -----------
// Lower Arm
fixtureDef.density = 1.0f;
fixtureDef.friction = 0.0f;
fixtureDef.restitution = 0.0f;
// Left
box.SetAsBox(17.0f, 6.0f);
fixtureDef.shape = &box;
bd.position.Set(420.0f /*- 57.0f*/, 415.0f /*+ 20.0f*/);
b2Body *lowerArmL = _world->CreateBody(&bd);
lowerArmL->CreateFixture(&fixtureDef);
// Right
box.SetAsBox(17.0f, 6.0f);
fixtureDef.shape = &box;
bd.position.Set(300 + 57.0f, 50 + 20.0f);
b2Body *lowerArmR = _world->CreateBody(&bd);
lowerArmR->CreateFixture(&fixtureDef);
// -----------
// UpperLeg --
fixtureDef.density = 1.0f;
fixtureDef.friction = 0.0f;
fixtureDef.restitution = 0.0f;
// Left
box.SetAsBox(7.5f, 22.0f);
fixtureDef.shape = &box;
bd.position.Set(300 - 8.0f, 50 + 85.0f);
b2Body *upperLegL = _world->CreateBody(&bd);
upperLegL->CreateFixture(&fixtureDef);
// Right
box.SetAsBox(7.5f, 22.0f);
fixtureDef.shape = &box;
bd.position.Set(300 + 8.0f, 50 + 85.0f);
b2Body *upperLegR = _world->CreateBody(&bd);
upperLegR->CreateFixture(&fixtureDef);
// -----------
// LowerLeg --
fixtureDef.density = 1.0f;
fixtureDef.friction = 0.4f;
fixtureDef.restitution = 0.0f;
// Left
box.SetAsBox(6.0f, 20.0f);
fixtureDef.shape = &box;
bd.position.Set(300 - 8.0f, 50 + 120.0f);
b2Body *lowerLegL = _world->CreateBody(&bd);
lowerLegL->CreateFixture(&fixtureDef);
// Right
box.SetAsBox(6.0f, 20.0f);
fixtureDef.shape = &box;
bd.position.Set(300 + 8.0f, 50 + 120.0f);
b2Body *lowerLegR = _world->CreateBody(&bd);
lowerLegR->CreateFixture(&fixtureDef);
// -------------------------
// Joints ------------------
// -------------------------
b2RevoluteJointDef jd;
jd.enableLimit = true;
// Head to shoulders
jd.lowerAngle = -40.0f / (180.0f / PI);
jd.upperAngle = 40.0f / (180.0f / PI);
jd.Initialize(torso1, head, b2Vec2(300, 50 + 15.0f));
_world->CreateJoint(&jd);
// Upper arm to shoulders --
// Left
//jd.lowerAngle = -85.0f / (180.0f / PI);
//jd.upperAngle = 130.0f / (180.0f / PI);
//jd.Initialize(torso1, upperArmL, b2Vec2(300 - 18.0f, 50 + 20.0f));
//_world->CreateJoint(&jd);
// Right
jd.lowerAngle = -130.0f / (180.0f / PI);
jd.upperAngle = 85.0f / (180.0f / PI );
jd.Initialize(torso1, upperArmR, b2Vec2(300 + 18.0f, 50 + 20.0f));
_world->CreateJoint(&jd);
// -------------------------
// Lower arm to shoulders --
// Left
jd.lowerAngle = -130.0f / (180.0f / PI);
jd.upperAngle = 10.0f / (180.0f / PI);
float _x = upperArmL->GetPosition().x;
float _y = upperArmL->GetPosition().y;
jd.Initialize(upperArmL, lowerArmL , b2Vec2(_x, _y));
float x = body[29]->GetPosition().x;
float y = body[29]->GetPosition().y;
jd.Initialize(lowerArmL, body[29], b2Vec2(x, y));
_world->CreateJoint(&jd);
// Right
jd.lowerAngle = -10.0f / (180.0f / PI);
jd.upperAngle = 130.0f / (180.0f / PI);
jd.Initialize(upperArmR, lowerArmR, b2Vec2(300 + 45.0f, 50 + 20.0f));
_world->CreateJoint(&jd);
// -------------------------
// Shoulders / stomach -----
jd.lowerAngle = -15.0f / (180.0f / PI);
jd.upperAngle = 15.0f / (180.0f / PI);
jd.Initialize(torso1, torso2, b2Vec2(300, 50 + 35.0f));
_world->CreateJoint(&jd);
// Stomach / hips
jd.lowerAngle = -40.0f / (180.0f / PI);
jd.upperAngle = 40.0f / (180.0f / PI);
jd.Initialize(torso2, torso3, b2Vec2(300, 50 + 50.0f));
_world->CreateJoint(&jd);
// -------------------------
// Torso to upper leg ------
// Left
jd.lowerAngle = -25.0f / (180.0f / PI);
jd.upperAngle = 45.0f / (180.0f / PI);
jd.Initialize(torso3, upperLegL, b2Vec2(300 - 8, 50 + 72.0f));
_world->CreateJoint(&jd);
// Right
jd.lowerAngle = -45.0f / (180.0f / PI);
jd.upperAngle = 25.0f / (180.0f / PI);
jd.Initialize(torso3, upperLegR, b2Vec2(300 + 8, 50 + 72.0f));
_world->CreateJoint(&jd);
// -------------------------
// Upper leg to lower leg --
// Left
jd.lowerAngle = -25.0f / (180.0f / PI);
jd.upperAngle = 115.0f / (180.0f / PI);
jd.Initialize(upperLegL, lowerLegL, b2Vec2(300 - 8, 50 + 105.0f));
_world->CreateJoint(&jd);
// Right
jd.lowerAngle = -115.0f / (180.0f / PI);
jd.upperAngle = 25.0f / (180.0f / PI);
jd.Initialize(upperLegR, lowerLegR, b2Vec2(300 + 8, 50 + 105.0f));
_world->CreateJoint(&jd);
for (int i = 0; i <= 30; i++)
{
b2BodyDef bodyDef;
bodyDef.position.Set(400.0f, 100.0f + i * 10);
bodyDef.type = b2_dynamicBody;
body[i] = _world->CreateBody(&bodyDef);
b2PolygonShape shape;
shape.SetAsBox( 0.125f, 6.0f);
b2FixtureDef ballShapeDef;
ballShapeDef.shape = &shape;
ballShapeDef.density = 70.0f;
ballShapeDef.friction = 0.0f;
ballShapeDef.restitution=0.2f;
body[i]->CreateFixture(&ballShapeDef);
revoluteJointDef.bodyA = link;
revoluteJointDef.bodyB = body[i];
body[i]->SetBullet(true);
revoluteJointDef.Initialize(link, body[i], body[i]->GetPosition());
_world->CreateJoint(&revoluteJointDef);
//saving the reference of the last placed link
link = body[i];
}
/*shape.Set(b2Vec2( 40.0f, 0.0f), b2Vec2(-40.0f, 0.0f)) ;
fixtureDef.shape = &shape;
fixtureDef.density = 1.0f;
paddleBody->CreateFixture( &fixtureDef );*/
bd.type = b2_dynamicBody;
//head
{
//headDef.type = b2_staticBody;
headShape.m_radius = 12.5f;
testFixtureDef.shape = &headShape;
testFixtureDef.density = 1.0f;
testFixtureDef.friction = 0.0f;
testFixtureDef.restitution = 0.0f;
bd.position.Set(300.0f, 50.0f);
head = _world->CreateBody(&bd);
head->CreateFixture(&testFixtureDef);
/*head->ApplyLinearImpulse(b2Vec2(500, 0) , head->GetWorldCenter());*/
//b2Vec2(std::rand() % 100 + 50, std::rand() % 100 + 50), head->GetWorldCenter()
}
//torso1
{
//torsoDef.type = b2_dynamicBody;
box.SetAsBox(15.0f, 10.0f);
testFixtureDef.shape = &box;
testFixtureDef.density = 1.0f;
testFixtureDef.friction = 0.0f;
testFixtureDef.restitution = 0.0f;
bd.position.Set(300.0f, 50.0f +25.0f);
torso1 = _world->CreateBody(&bd);
torso1->CreateFixture(&testFixtureDef);
}
// Torso2 ----
box.SetAsBox(15.0f, 10.0f);
fixtureDef.shape = &box;
bd.position.Set(300, 50 + 43.0f);
b2Body *torso2 = _world->CreateBody(&bd);
torso2->CreateFixture(&fixtureDef);
// Torso3 ----
box.SetAsBox(15.0f, 10.0f);
fixtureDef.shape = &box;
bd.position.Set(300, 50 + 58.0f);
b2Body *torso3 = _world->CreateBody(&bd);
torso3->CreateFixture(&fixtureDef);
//upperArm
fixtureDef.density = 1.0f;
fixtureDef.friction = 0.0f;
fixtureDef.restitution = 0.0f;
//left
box.SetAsBox(18.0f, 6.5f);
fixtureDef.shape = &box;
bd.position.Set(420.0f - 30.0f, 415.0f + 20.0f);
b2Body *upperArmL = _world->CreateBody(&bd);
upperArmL->CreateFixture(&fixtureDef);
// Right
box.SetAsBox(18.0f, 6.5f);
fixtureDef.shape = &box;
bd.position.Set(300 + 30.0f, 50 + 20.0f);
b2Body *upperArmR = _world->CreateBody(&bd);
upperArmR->CreateFixture(&fixtureDef);
// -----------
// Lower Arm
fixtureDef.density = 1.0f;
fixtureDef.friction = 0.0f;
fixtureDef.restitution = 0.0f;
// Left
box.SetAsBox(17.0f, 6.0f);
fixtureDef.shape = &box;
bd.position.Set(420.0f /*- 57.0f*/, 415.0f /*+ 20.0f*/);
b2Body *lowerArmL = _world->CreateBody(&bd);
lowerArmL->CreateFixture(&fixtureDef);
// Right
box.SetAsBox(17.0f, 6.0f);
fixtureDef.shape = &box;
bd.position.Set(300 + 57.0f, 50 + 20.0f);
b2Body *lowerArmR = _world->CreateBody(&bd);
lowerArmR->CreateFixture(&fixtureDef);
// -----------
// UpperLeg --
fixtureDef.density = 1.0f;
fixtureDef.friction = 0.0f;
fixtureDef.restitution = 0.0f;
// Left
box.SetAsBox(7.5f, 22.0f);
fixtureDef.shape = &box;
bd.position.Set(300 - 8.0f, 50 + 85.0f);
b2Body *upperLegL = _world->CreateBody(&bd);
upperLegL->CreateFixture(&fixtureDef);
// Right
box.SetAsBox(7.5f, 22.0f);
fixtureDef.shape = &box;
bd.position.Set(300 + 8.0f, 50 + 85.0f);
b2Body *upperLegR = _world->CreateBody(&bd);
upperLegR->CreateFixture(&fixtureDef);
// -----------
// LowerLeg --
fixtureDef.density = 1.0f;
fixtureDef.friction = 0.4f;
fixtureDef.restitution = 0.0f;
// Left
box.SetAsBox(6.0f, 20.0f);
fixtureDef.shape = &box;
bd.position.Set(300 - 8.0f, 50 + 120.0f);
b2Body *lowerLegL = _world->CreateBody(&bd);
lowerLegL->CreateFixture(&fixtureDef);
// Right
box.SetAsBox(6.0f, 20.0f);
fixtureDef.shape = &box;
bd.position.Set(300 + 8.0f, 50 + 120.0f);
b2Body *lowerLegR = _world->CreateBody(&bd);
lowerLegR->CreateFixture(&fixtureDef);
// -------------------------
// Joints ------------------
// -------------------------
b2RevoluteJointDef jd;
jd.enableLimit = true;
// Head to shoulders
jd.lowerAngle = -40.0f / (180.0f / PI);
jd.upperAngle = 40.0f / (180.0f / PI);
jd.Initialize(torso1, head, b2Vec2(300, 50 + 15.0f));
_world->CreateJoint(&jd);
// Upper arm to shoulders --
// Left
//jd.lowerAngle = -85.0f / (180.0f / PI);
//jd.upperAngle = 130.0f / (180.0f / PI);
//jd.Initialize(torso1, upperArmL, b2Vec2(300 - 18.0f, 50 + 20.0f));
//_world->CreateJoint(&jd);
// Right
jd.lowerAngle = -130.0f / (180.0f / PI);
jd.upperAngle = 85.0f / (180.0f / PI );
jd.Initialize(torso1, upperArmR, b2Vec2(300 + 18.0f, 50 + 20.0f));
_world->CreateJoint(&jd);
// -------------------------
// Lower arm to shoulders --
// Left
jd.lowerAngle = -130.0f / (180.0f / PI);
jd.upperAngle = 10.0f / (180.0f / PI);
float _x = upperArmL->GetPosition().x;
float _y = upperArmL->GetPosition().y;
jd.Initialize(upperArmL, lowerArmL , b2Vec2(_x, _y));
float x = body[29]->GetPosition().x;
float y = body[29]->GetPosition().y;
jd.Initialize(lowerArmL, body[29], b2Vec2(x, y));
_world->CreateJoint(&jd);
// Right
jd.lowerAngle = -10.0f / (180.0f / PI);
jd.upperAngle = 130.0f / (180.0f / PI);
jd.Initialize(upperArmR, lowerArmR, b2Vec2(300 + 45.0f, 50 + 20.0f));
_world->CreateJoint(&jd);
// -------------------------
// Shoulders / stomach -----
jd.lowerAngle = -15.0f / (180.0f / PI);
jd.upperAngle = 15.0f / (180.0f / PI);
jd.Initialize(torso1, torso2, b2Vec2(300, 50 + 35.0f));
_world->CreateJoint(&jd);
// Stomach / hips
jd.lowerAngle = -40.0f / (180.0f / PI);
jd.upperAngle = 40.0f / (180.0f / PI);
jd.Initialize(torso2, torso3, b2Vec2(300, 50 + 50.0f));
_world->CreateJoint(&jd);
// -------------------------
// Torso to upper leg ------
// Left
jd.lowerAngle = -25.0f / (180.0f / PI);
jd.upperAngle = 45.0f / (180.0f / PI);
jd.Initialize(torso3, upperLegL, b2Vec2(300 - 8, 50 + 72.0f));
_world->CreateJoint(&jd);
// Right
jd.lowerAngle = -45.0f / (180.0f / PI);
jd.upperAngle = 25.0f / (180.0f / PI);
jd.Initialize(torso3, upperLegR, b2Vec2(300 + 8, 50 + 72.0f));
_world->CreateJoint(&jd);
// -------------------------
// Upper leg to lower leg --
// Left
jd.lowerAngle = -25.0f / (180.0f / PI);
jd.upperAngle = 115.0f / (180.0f / PI);
jd.Initialize(upperLegL, lowerLegL, b2Vec2(300 - 8, 50 + 105.0f));
_world->CreateJoint(&jd);
// Right
jd.lowerAngle = -115.0f / (180.0f / PI);
jd.upperAngle = 25.0f / (180.0f / PI);
jd.Initialize(upperLegR, lowerLegR, b2Vec2(300 + 8, 50 + 105.0f));
_world->CreateJoint(&jd);