Hi everybody,
I have recently started Box2D and I have created simple program to see how library works.
I put two b2_dynamicBody connected by a b2RevoluteJoint and a static body like obstable. B2_dynamicBody both fall together and when a body touches the obstable, it sticks! The b2_dynamicBody takes 10 seconds to finish falling and the other b2_dynamicBody has a strange behievor. And I can't enable motor.
I tried on Linux (gcc) and on Windows (MinGW). I tried version 2.1.2 and version SVN. With the version SVN, bodies fall correctly but I can't enable motor.
Maybe is my code :
and for the "step"
Thanks to help me,
Regards,
I have recently started Box2D and I have created simple program to see how library works.
I put two b2_dynamicBody connected by a b2RevoluteJoint and a static body like obstable. B2_dynamicBody both fall together and when a body touches the obstable, it sticks! The b2_dynamicBody takes 10 seconds to finish falling and the other b2_dynamicBody has a strange behievor. And I can't enable motor.
I tried on Linux (gcc) and on Windows (MinGW). I tried version 2.1.2 and version SVN. With the version SVN, bodies fall correctly but I can't enable motor.
Maybe is my code :
Code:
//world
b2Vec2 gravity(0.0f, 100.0f);
bool doSleep = true;
world=new b2World(gravity, doSleep);
//ground
b2BodyDef groundBodyDef;
groundBodyDef.position.Set(320.0f, 500.0f);
b2Body* groundBody = world->CreateBody(&groundBodyDef);
b2PolygonShape groundBox;
groundBox.SetAsBox(1280.0f, 20.0f);
b2Fixture* b2fixtureGround = groundBody->CreateFixture(&groundBox,0.0f);
b2fixtureGround->SetFriction(1.0f);
//obstacle
b2BodyDef obsBodyDef;
obsBodyDef.position.Set(460.0f, 220.0f);
b2PolygonShape obsShape;
obsShape.SetAsEdge(b2Vec2(0.0f,0.0f),b2Vec2(100.0f,-20.0f));
b2Body* obs1Body = world->CreateBody(&obsBodyDef);
obs1Body->CreateFixture(&obsShape,0.0f);
//dynamic
{
b2BodyDef bodyDef;
bodyDef.type = b2_dynamicBody;
bodyDef.position.Set(200.0f, 100.0f);
brick[0] = world->CreateBody(&bodyDef);
dynamicBox[0].SetAsBox(30.0f, 10.0f);
b2FixtureDef fixtureDef;
fixtureDef.density = 1.0f;
fixtureDef.friction = 1.0f;
fixtureDef.shape = &dynamicBox[0];
brick[0]->CreateFixture(&fixtureDef);
}
{
b2BodyDef bodyDef;
bodyDef.type = b2_dynamicBody;
bodyDef.position.Set(220.0f, 100.0f);
brick[1] = world->CreateBody(&bodyDef);
dynamicBox[1].SetAsBox(30.0f, 10.0f);
b2FixtureDef fixtureDef;
fixtureDef.density = 1.0f;
fixtureDef.friction = 1.0f;
fixtureDef.shape = &dynamicBox[1];
fixtureDef.filter.categoryBits=0x0004;
fixtureDef.filter.maskBits =0x0005;
brick[1]->CreateFixture(&fixtureDef);
}
//Joint
{
b2RevoluteJointDef jointDef;
b2Vec2 anc1=brick[0]->GetWorldCenter();
anc1.x+=20.0f;
jointDef.enableLimit = false;
jointDef.enableMotor = true;
jointDef.motorSpeed = -2;
jointDef.maxMotorTorque = 100;
jointDef.Initialize(brick[0], brick[1], anc1);
myMotor = (b2RevoluteJoint*)world->CreateJoint(&jointDef);
}
b2Vec2 gravity(0.0f, 100.0f);
bool doSleep = true;
world=new b2World(gravity, doSleep);
//ground
b2BodyDef groundBodyDef;
groundBodyDef.position.Set(320.0f, 500.0f);
b2Body* groundBody = world->CreateBody(&groundBodyDef);
b2PolygonShape groundBox;
groundBox.SetAsBox(1280.0f, 20.0f);
b2Fixture* b2fixtureGround = groundBody->CreateFixture(&groundBox,0.0f);
b2fixtureGround->SetFriction(1.0f);
//obstacle
b2BodyDef obsBodyDef;
obsBodyDef.position.Set(460.0f, 220.0f);
b2PolygonShape obsShape;
obsShape.SetAsEdge(b2Vec2(0.0f,0.0f),b2Vec2(100.0f,-20.0f));
b2Body* obs1Body = world->CreateBody(&obsBodyDef);
obs1Body->CreateFixture(&obsShape,0.0f);
//dynamic
{
b2BodyDef bodyDef;
bodyDef.type = b2_dynamicBody;
bodyDef.position.Set(200.0f, 100.0f);
brick[0] = world->CreateBody(&bodyDef);
dynamicBox[0].SetAsBox(30.0f, 10.0f);
b2FixtureDef fixtureDef;
fixtureDef.density = 1.0f;
fixtureDef.friction = 1.0f;
fixtureDef.shape = &dynamicBox[0];
brick[0]->CreateFixture(&fixtureDef);
}
{
b2BodyDef bodyDef;
bodyDef.type = b2_dynamicBody;
bodyDef.position.Set(220.0f, 100.0f);
brick[1] = world->CreateBody(&bodyDef);
dynamicBox[1].SetAsBox(30.0f, 10.0f);
b2FixtureDef fixtureDef;
fixtureDef.density = 1.0f;
fixtureDef.friction = 1.0f;
fixtureDef.shape = &dynamicBox[1];
fixtureDef.filter.categoryBits=0x0004;
fixtureDef.filter.maskBits =0x0005;
brick[1]->CreateFixture(&fixtureDef);
}
//Joint
{
b2RevoluteJointDef jointDef;
b2Vec2 anc1=brick[0]->GetWorldCenter();
anc1.x+=20.0f;
jointDef.enableLimit = false;
jointDef.enableMotor = true;
jointDef.motorSpeed = -2;
jointDef.maxMotorTorque = 100;
jointDef.Initialize(brick[0], brick[1], anc1);
myMotor = (b2RevoluteJoint*)world->CreateJoint(&jointDef);
}
and for the "step"
Code:
for(int i=0;i<3;i++){
world->Step(0.083, 6, 2);
world->ClearForces();
}
world->Step(0.083, 6, 2);
world->ClearForces();
}
Thanks to help me,
Regards,