I am sure this is a simple task that eludes me.
I wish to create a hanging cage consisting of 3 solid sides (top, and l/r) and attached to the base is 2 swinging doors (half width of cage) hinged (revolute) at the base of the 2 sides,
I set a body for the cage and attached the 3 sides as fixtures. The 2 doors are 2 more bodies attached to the cage and held together by a central distance joint.
This all works fine if I make the body static, but once dynamic, the 3 fixtures (and attached doors) fall (I have not attached it to the world as yet) and the body appears to stay at the same coordinates and the fixtures deform.
I know I am missing something perhaps very simple here, but this is the first time I have tried to make a dynamic body from multiple fixtures.
If it helps, here is the code I am using,
(sorry for the length)
Any help would be wonderful...
I wish to create a hanging cage consisting of 3 solid sides (top, and l/r) and attached to the base is 2 swinging doors (half width of cage) hinged (revolute) at the base of the 2 sides,
I set a body for the cage and attached the 3 sides as fixtures. The 2 doors are 2 more bodies attached to the cage and held together by a central distance joint.
This all works fine if I make the body static, but once dynamic, the 3 fixtures (and attached doors) fall (I have not attached it to the world as yet) and the body appears to stay at the same coordinates and the fixtures deform.
I know I am missing something perhaps very simple here, but this is the first time I have tried to make a dynamic body from multiple fixtures.
If it helps, here is the code I am using,
Code:
BodyDef = new b2BodyDef(); // set the initial body!
BodyDef->position.Set(position.x, position.y);
BodyDef->type = b2_staticBody; // Make dynamic to fall!
BodyDef->fixedRotation = false;
BodyDef->allowSleep = false;
Body = g_world->CreateBody(BodyDef);
for(int i=0; i<CAGE_SHAPE_COUNT; i++)
{
FixtureDefArray[i] = new b2FixtureDef();
FixtureDefArray[i]->density = 0.0f; // kills it if non zero
FixtureDefArray[i]->friction = 1.0f;
FixtureDefArray[i]->restitution = 0.0f;
}
PolygonShapeArray[CAGE_TOP] = new b2PolygonShape(); // The Lid
PolygonShapeArray[CAGE_TOP]->SetAsBox(Size.width * 0.68f, Size.height / 15.0f, b2Vec2(-(Size.width / 32.0f),size.height / 2.07f) ,0.0f);
FixtureDefArray[CAGE_TOP]->shape = PolygonShapeArray[CAGE_TOP];
FixtureArray[CAGE_TOP] = Body->CreateFixture(FixtureDefArray[CAGE_TOP]);
PolygonShapeArray[CAGE_LEFT] = new b2PolygonShape(); // Left Side
PolygonShapeArray[CAGE_LEFT]->SetAsBox(Size.width / 25.0f, Size.height * 0.41f, b2Vec2(-(size.width * 0.52f),Size.height / 60.5f) ,0.0f);
FixtureDefArray[CAGE_LEFT]->shape = PolygonShapeArray[CAGE_LEFT];
FixtureDefArray[CAGE_LEFT]->friction = 0.0f;
FixtureArray[CAGE_LEFT] = Body->CreateFixture(FixtureDefArray[CAGE_LEFT]);
PolygonShapeArray[CAGE_RIGHT] = new b2PolygonShape(); // Right Side
PolygonShapeArray[CAGE_RIGHT]->SetAsBox(Size.width / 25.0f, Size.height * 0.41F, b2Vec2(size.width * 0.58f,(Size.height / 60.5f)) ,0.0f);
FixtureDefArray[CAGE_RIGHT]->shape = PolygonShapeArray[CAGE_RIGHT];
FixtureDefArray[CAGE_RIGHT]->friction = 0.0f;
FixtureArray[CAGE_RIGHT] = Body->CreateFixture(FixtureDefArray[CAGE_RIGHT]);
// Ok, the two doors should be another body really!
DoorLeftDef = new b2BodyDef();
DoorLeftDef->position.Set(position.x, position.y);
DoorLeftDef->type = b2_dynamicBody;
DoorLeft = g_world->CreateBody(DoorLeftDef);
PolygonShapeArray[CAGE_BOTTOM_LEFT] = new b2PolygonShape(); // Bottom Door Left
PolygonShapeArray[CAGE_BOTTOM_LEFT]->SetAsBox(Size.width / 3.5f, Size.height / 40.0f, b2Vec2(-(Size.width / 3.9f),-(size.height / 2.65f)) ,0.0f);
FixtureDefArray[CAGE_BOTTOM_LEFT]->shape = PolygonShapeArray[CAGE_BOTTOM_LEFT];
FixtureDefArray[CAGE_BOTTOM_LEFT]->density = 1.0f;
FixtureArray[CAGE_BOTTOM_LEFT] = DoorLeft->CreateFixture(FixtureDefArray[CAGE_BOTTOM_LEFT]);
b2Vec2 PointL = Body->GetWorldCenter();
PointL.y -= Size.height / 2.65;
PointL.x -= Size.width * 0.52f;
JointLeft.Type = JOINTTYPE_REVOLUTE;
JointLeft.RevoluteJointDef = new b2RevoluteJointDef();
JointLeft.RevoluteJointDef->collideConnected = false;
JointLeft.RevoluteJointDef->maxMotorTorque = 20.0f;
JointLeft.RevoluteJointDef->enableMotor = true;
JointLeft.RevoluteJointDef->enableLimit = true;
JointLeft.RevoluteJointDef->lowerAngle = DEGREES_TO_RADIANS(-250);
JointLeft.RevoluteJointDef->upperAngle = DEGREES_TO_RADIANS(80);
JointLeft.RevoluteJointDef->Initialize(Body, DoorLeft, PointL);
JointLeft.Joint = g_world->CreateJoint(JointLeft.RevoluteJointDef);
DoorRightDef = new b2BodyDef();
DoorRightDef->position.Set(position.x, position.y);
DoorRightDef->type = b2_dynamicBody;
DoorRight = g_world->CreateBody(DoorRightDef);
PolygonShapeArray[CAGE_BOTTOM_RIGHT] = new b2PolygonShape(); // Bottom Door Right
PolygonShapeArray[CAGE_BOTTOM_RIGHT]->SetAsBox(Size.width /3.5f, Size.height / 40.0f, b2Vec2((Size.width / 3.2f),-(size.height / 2.65f)) ,0.0f);
FixtureDefArray[CAGE_BOTTOM_RIGHT]->shape = PolygonShapeArray[CAGE_BOTTOM_RIGHT];
FixtureDefArray[CAGE_BOTTOM_RIGHT]->density = 1.0f;
FixtureArray[CAGE_BOTTOM_RIGHT] = DoorRight->CreateFixture(FixtureDefArray[CAGE_BOTTOM_RIGHT]);
b2Vec2 PointR = Body->GetWorldCenter();
PointR.y -= Size.height / 2.65f;
PointR.x += Size.width * 0.58f;
JointRight.Type = JOINTTYPE_REVOLUTE;
JointRight.RevoluteJointDef = new b2RevoluteJointDef();
JointRight.RevoluteJointDef->collideConnected = false;
JointRight.RevoluteJointDef->maxMotorTorque = 20.0f;
JointRight.RevoluteJointDef->enableMotor = true;
JointRight.RevoluteJointDef->enableLimit = true;
JointRight.RevoluteJointDef->lowerAngle = DEGREES_TO_RADIANS(-80);
JointRight.RevoluteJointDef->upperAngle = DEGREES_TO_RADIANS(250);
JointRight.RevoluteJointDef->Initialize(Body, DoorRight, PointR);
JointRight.Joint = g_world->CreateJoint(JointRight.RevoluteJointDef);
b2Vec2 PointLockL = DoorLeft->GetWorldCenter();
PointLockL.x += Size.width / 4.0f;
b2Vec2 PointLockR = DoorRight->GetWorldCenter();
PointLockR.x -= Size.width / 4.0f;
JointLock.Type = JOINTTYPE_DISTANCE;
JointLock.DistanceJointDef = new b2DistanceJointDef();
JointLock.DistanceJointDef->dampingRatio = 1.0f;
JointLock.DistanceJointDef->collideConnected = false;
JointLock.DistanceJointDef->Initialize(DoorLeft, DoorRight, PointLockL, PointLockR);
JointLock.Joint = g_world->CreateJoint(JointLock.DistanceJointDef);
BodyDef->position.Set(position.x, position.y);
BodyDef->type = b2_staticBody; // Make dynamic to fall!
BodyDef->fixedRotation = false;
BodyDef->allowSleep = false;
Body = g_world->CreateBody(BodyDef);
for(int i=0; i<CAGE_SHAPE_COUNT; i++)
{
FixtureDefArray[i] = new b2FixtureDef();
FixtureDefArray[i]->density = 0.0f; // kills it if non zero
FixtureDefArray[i]->friction = 1.0f;
FixtureDefArray[i]->restitution = 0.0f;
}
PolygonShapeArray[CAGE_TOP] = new b2PolygonShape(); // The Lid
PolygonShapeArray[CAGE_TOP]->SetAsBox(Size.width * 0.68f, Size.height / 15.0f, b2Vec2(-(Size.width / 32.0f),size.height / 2.07f) ,0.0f);
FixtureDefArray[CAGE_TOP]->shape = PolygonShapeArray[CAGE_TOP];
FixtureArray[CAGE_TOP] = Body->CreateFixture(FixtureDefArray[CAGE_TOP]);
PolygonShapeArray[CAGE_LEFT] = new b2PolygonShape(); // Left Side
PolygonShapeArray[CAGE_LEFT]->SetAsBox(Size.width / 25.0f, Size.height * 0.41f, b2Vec2(-(size.width * 0.52f),Size.height / 60.5f) ,0.0f);
FixtureDefArray[CAGE_LEFT]->shape = PolygonShapeArray[CAGE_LEFT];
FixtureDefArray[CAGE_LEFT]->friction = 0.0f;
FixtureArray[CAGE_LEFT] = Body->CreateFixture(FixtureDefArray[CAGE_LEFT]);
PolygonShapeArray[CAGE_RIGHT] = new b2PolygonShape(); // Right Side
PolygonShapeArray[CAGE_RIGHT]->SetAsBox(Size.width / 25.0f, Size.height * 0.41F, b2Vec2(size.width * 0.58f,(Size.height / 60.5f)) ,0.0f);
FixtureDefArray[CAGE_RIGHT]->shape = PolygonShapeArray[CAGE_RIGHT];
FixtureDefArray[CAGE_RIGHT]->friction = 0.0f;
FixtureArray[CAGE_RIGHT] = Body->CreateFixture(FixtureDefArray[CAGE_RIGHT]);
// Ok, the two doors should be another body really!
DoorLeftDef = new b2BodyDef();
DoorLeftDef->position.Set(position.x, position.y);
DoorLeftDef->type = b2_dynamicBody;
DoorLeft = g_world->CreateBody(DoorLeftDef);
PolygonShapeArray[CAGE_BOTTOM_LEFT] = new b2PolygonShape(); // Bottom Door Left
PolygonShapeArray[CAGE_BOTTOM_LEFT]->SetAsBox(Size.width / 3.5f, Size.height / 40.0f, b2Vec2(-(Size.width / 3.9f),-(size.height / 2.65f)) ,0.0f);
FixtureDefArray[CAGE_BOTTOM_LEFT]->shape = PolygonShapeArray[CAGE_BOTTOM_LEFT];
FixtureDefArray[CAGE_BOTTOM_LEFT]->density = 1.0f;
FixtureArray[CAGE_BOTTOM_LEFT] = DoorLeft->CreateFixture(FixtureDefArray[CAGE_BOTTOM_LEFT]);
b2Vec2 PointL = Body->GetWorldCenter();
PointL.y -= Size.height / 2.65;
PointL.x -= Size.width * 0.52f;
JointLeft.Type = JOINTTYPE_REVOLUTE;
JointLeft.RevoluteJointDef = new b2RevoluteJointDef();
JointLeft.RevoluteJointDef->collideConnected = false;
JointLeft.RevoluteJointDef->maxMotorTorque = 20.0f;
JointLeft.RevoluteJointDef->enableMotor = true;
JointLeft.RevoluteJointDef->enableLimit = true;
JointLeft.RevoluteJointDef->lowerAngle = DEGREES_TO_RADIANS(-250);
JointLeft.RevoluteJointDef->upperAngle = DEGREES_TO_RADIANS(80);
JointLeft.RevoluteJointDef->Initialize(Body, DoorLeft, PointL);
JointLeft.Joint = g_world->CreateJoint(JointLeft.RevoluteJointDef);
DoorRightDef = new b2BodyDef();
DoorRightDef->position.Set(position.x, position.y);
DoorRightDef->type = b2_dynamicBody;
DoorRight = g_world->CreateBody(DoorRightDef);
PolygonShapeArray[CAGE_BOTTOM_RIGHT] = new b2PolygonShape(); // Bottom Door Right
PolygonShapeArray[CAGE_BOTTOM_RIGHT]->SetAsBox(Size.width /3.5f, Size.height / 40.0f, b2Vec2((Size.width / 3.2f),-(size.height / 2.65f)) ,0.0f);
FixtureDefArray[CAGE_BOTTOM_RIGHT]->shape = PolygonShapeArray[CAGE_BOTTOM_RIGHT];
FixtureDefArray[CAGE_BOTTOM_RIGHT]->density = 1.0f;
FixtureArray[CAGE_BOTTOM_RIGHT] = DoorRight->CreateFixture(FixtureDefArray[CAGE_BOTTOM_RIGHT]);
b2Vec2 PointR = Body->GetWorldCenter();
PointR.y -= Size.height / 2.65f;
PointR.x += Size.width * 0.58f;
JointRight.Type = JOINTTYPE_REVOLUTE;
JointRight.RevoluteJointDef = new b2RevoluteJointDef();
JointRight.RevoluteJointDef->collideConnected = false;
JointRight.RevoluteJointDef->maxMotorTorque = 20.0f;
JointRight.RevoluteJointDef->enableMotor = true;
JointRight.RevoluteJointDef->enableLimit = true;
JointRight.RevoluteJointDef->lowerAngle = DEGREES_TO_RADIANS(-80);
JointRight.RevoluteJointDef->upperAngle = DEGREES_TO_RADIANS(250);
JointRight.RevoluteJointDef->Initialize(Body, DoorRight, PointR);
JointRight.Joint = g_world->CreateJoint(JointRight.RevoluteJointDef);
b2Vec2 PointLockL = DoorLeft->GetWorldCenter();
PointLockL.x += Size.width / 4.0f;
b2Vec2 PointLockR = DoorRight->GetWorldCenter();
PointLockR.x -= Size.width / 4.0f;
JointLock.Type = JOINTTYPE_DISTANCE;
JointLock.DistanceJointDef = new b2DistanceJointDef();
JointLock.DistanceJointDef->dampingRatio = 1.0f;
JointLock.DistanceJointDef->collideConnected = false;
JointLock.DistanceJointDef->Initialize(DoorLeft, DoorRight, PointLockL, PointLockR);
JointLock.Joint = g_world->CreateJoint(JointLock.DistanceJointDef);
(sorry for the length)
Any help would be wonderful...