Hi everyone,
this is my first project with box2d in c++. I need to make simple car physics. So far I have just a box with two wheels joint togheter with revolute joints.
My problem ist that I can't get a realistic acceleration, the car moves very slow and the controls are not very responsive. I based my values more or less on the second Actionscript example on this page: http://www.emanueleferonato.com/2009/04/06/two-ways-to-make-box2d-cars/. I also changed the values but I can't get a realistic behaviour. It seems like the wheels are heavily breaked as soon as they have contact with the groundbody.
Please can someone help me. How do I get realistic values, or am I doing something wrong?
Thanks, wooDo
btw: for the rendering and the input handling I use sfml, but I think you can just ignore this codelines. It's just a simple debug rendering to see what's going on.
this is my first project with box2d in c++. I need to make simple car physics. So far I have just a box with two wheels joint togheter with revolute joints.
My problem ist that I can't get a realistic acceleration, the car moves very slow and the controls are not very responsive. I based my values more or less on the second Actionscript example on this page: http://www.emanueleferonato.com/2009/04/06/two-ways-to-make-box2d-cars/. I also changed the values but I can't get a realistic behaviour. It seems like the wheels are heavily breaked as soon as they have contact with the groundbody.
Please can someone help me. How do I get realistic values, or am I doing something wrong?
Thanks, wooDo
btw: for the rendering and the input handling I use sfml, but I think you can just ignore this codelines. It's just a simple debug rendering to see what's going on.
Code:
int main(){
//sfml
sf::RenderWindow window(sf::VideoMode(WINDOW_WIDTH, WINDOW_HEIGHT, WINDOW_DEPTH), WINDOW_TITLE, sf::Style::Close);
sf::Clock clock;
sf::View view;
//debug draw
DebugDraw debugDraw(window);
debugDraw.SetFlags(b2DebugDraw::e_shapeBit | b2DebugDraw::e_jointBit);
//world
b2Vec2 gravity(0.0f, 9.8f);
bool doSleep = true;
b2World world(gravity, doSleep);
world.SetDebugDraw(&debugDraw);
//Groundbody
b2BodyDef groundDef;
groundDef.position.Set(0, 45);
b2Body* groundBody = world.CreateBody(&groundDef);
b2PolygonShape groundShape;
groundShape.SetAsBox(50, 0.5);
b2FixtureDef groundFixture;
groundFixture.density = 0;
groundFixture.friction = 1;
groundFixture.shape = &groundShape;
groundBody->CreateFixture(&groundFixture);
//cart
b2BodyDef cartDef;
cartDef.position.Set(20, 30);
cartDef.type = b2_dynamicBody;
b2Body* cart = world.CreateBody(&cartDef);
b2PolygonShape cartShape;
cartShape.SetAsBox(4, 1);
b2FixtureDef cartFixture;
cartFixture.density = 5;
cartFixture.friction = 0.5;
cartFixture.restitution = 0.2;
cartFixture.filter.groupIndex = -1;
cartFixture.shape = &cartShape;
cart->CreateFixture(&cartFixture);
//wheels
b2BodyDef wheelDef;
wheelDef.type = b2_dynamicBody;
wheelDef.position.Set(16, 30);
b2Body* wheel1 = world.CreateBody(&wheelDef);
wheelDef.position.Set(23, 30);
b2Body* wheel2 = world.CreateBody(&wheelDef);
b2CircleShape wheelShape;
wheelShape.m_radius = 2;
b2FixtureDef wheelFixture;
wheelFixture.density = 0.1;
wheelFixture.friction = 5;
wheelFixture.restitution = 0.2;
wheelFixture.filter.groupIndex = -1;
wheelFixture.shape = &wheelShape;
wheel1->CreateFixture(&wheelFixture);
wheel2->CreateFixture(&wheelFixture);
b2RevoluteJointDef wheelJointDef;
wheelJointDef.enableMotor = true;
wheelJointDef.maxMotorTorque = 100;
wheelJointDef.Initialize(wheel1, cart, wheel1->GetWorldCenter());
b2RevoluteJoint* motor1 = (b2RevoluteJoint*)(world.CreateJoint(&wheelJointDef));
wheelJointDef.Initialize(wheel2, cart, wheel2->GetWorldCenter());
b2RevoluteJoint* motor2 = (b2RevoluteJoint*)(world.CreateJoint(&wheelJointDef));
//main loop
while(window.IsOpened()){
sf::Event evt;
//event loop
while(window.GetEvent(evt)){
switch(evt.Type){
case sf::Event::Closed: //close event
window.Close();
break;
}
}
//input
const sf::Input& input = window.GetInput(); //get input
if(input.IsKeyDown(sf::Key::Up)){
motor1->SetMotorSpeed(45);
motor2->SetMotorSpeed(45);
motor1->SetMaxMotorTorque(200);
motor2->SetMaxMotorTorque(200);
}else if(input.IsKeyDown(sf::Key::Down)){
motor1->SetMotorSpeed(-45);
motor2->SetMotorSpeed(-45);
motor1->SetMaxMotorTorque(200);
motor2->SetMaxMotorTorque(200);
}else{
motor1->SetMotorSpeed(0);
motor2->SetMotorSpeed(0);
motor1->SetMaxMotorTorque(200);
motor2->SetMaxMotorTorque(200);
}
//clear
window.Clear();
world.Step(clock.GetElapsedTime(), 30, 30);
view.SetCenter(cart->GetWorldCenter().x * PIXELS_PER_METER, cart->GetWorldCenter().y * PIXELS_PER_METER);
world.DrawDebugData();
clock.Reset();
//display
window.Display();
}
return 0;
}
//sfml
sf::RenderWindow window(sf::VideoMode(WINDOW_WIDTH, WINDOW_HEIGHT, WINDOW_DEPTH), WINDOW_TITLE, sf::Style::Close);
sf::Clock clock;
sf::View view;
//debug draw
DebugDraw debugDraw(window);
debugDraw.SetFlags(b2DebugDraw::e_shapeBit | b2DebugDraw::e_jointBit);
//world
b2Vec2 gravity(0.0f, 9.8f);
bool doSleep = true;
b2World world(gravity, doSleep);
world.SetDebugDraw(&debugDraw);
//Groundbody
b2BodyDef groundDef;
groundDef.position.Set(0, 45);
b2Body* groundBody = world.CreateBody(&groundDef);
b2PolygonShape groundShape;
groundShape.SetAsBox(50, 0.5);
b2FixtureDef groundFixture;
groundFixture.density = 0;
groundFixture.friction = 1;
groundFixture.shape = &groundShape;
groundBody->CreateFixture(&groundFixture);
//cart
b2BodyDef cartDef;
cartDef.position.Set(20, 30);
cartDef.type = b2_dynamicBody;
b2Body* cart = world.CreateBody(&cartDef);
b2PolygonShape cartShape;
cartShape.SetAsBox(4, 1);
b2FixtureDef cartFixture;
cartFixture.density = 5;
cartFixture.friction = 0.5;
cartFixture.restitution = 0.2;
cartFixture.filter.groupIndex = -1;
cartFixture.shape = &cartShape;
cart->CreateFixture(&cartFixture);
//wheels
b2BodyDef wheelDef;
wheelDef.type = b2_dynamicBody;
wheelDef.position.Set(16, 30);
b2Body* wheel1 = world.CreateBody(&wheelDef);
wheelDef.position.Set(23, 30);
b2Body* wheel2 = world.CreateBody(&wheelDef);
b2CircleShape wheelShape;
wheelShape.m_radius = 2;
b2FixtureDef wheelFixture;
wheelFixture.density = 0.1;
wheelFixture.friction = 5;
wheelFixture.restitution = 0.2;
wheelFixture.filter.groupIndex = -1;
wheelFixture.shape = &wheelShape;
wheel1->CreateFixture(&wheelFixture);
wheel2->CreateFixture(&wheelFixture);
b2RevoluteJointDef wheelJointDef;
wheelJointDef.enableMotor = true;
wheelJointDef.maxMotorTorque = 100;
wheelJointDef.Initialize(wheel1, cart, wheel1->GetWorldCenter());
b2RevoluteJoint* motor1 = (b2RevoluteJoint*)(world.CreateJoint(&wheelJointDef));
wheelJointDef.Initialize(wheel2, cart, wheel2->GetWorldCenter());
b2RevoluteJoint* motor2 = (b2RevoluteJoint*)(world.CreateJoint(&wheelJointDef));
//main loop
while(window.IsOpened()){
sf::Event evt;
//event loop
while(window.GetEvent(evt)){
switch(evt.Type){
case sf::Event::Closed: //close event
window.Close();
break;
}
}
//input
const sf::Input& input = window.GetInput(); //get input
if(input.IsKeyDown(sf::Key::Up)){
motor1->SetMotorSpeed(45);
motor2->SetMotorSpeed(45);
motor1->SetMaxMotorTorque(200);
motor2->SetMaxMotorTorque(200);
}else if(input.IsKeyDown(sf::Key::Down)){
motor1->SetMotorSpeed(-45);
motor2->SetMotorSpeed(-45);
motor1->SetMaxMotorTorque(200);
motor2->SetMaxMotorTorque(200);
}else{
motor1->SetMotorSpeed(0);
motor2->SetMotorSpeed(0);
motor1->SetMaxMotorTorque(200);
motor2->SetMaxMotorTorque(200);
}
//clear
window.Clear();
world.Step(clock.GetElapsedTime(), 30, 30);
view.SetCenter(cart->GetWorldCenter().x * PIXELS_PER_METER, cart->GetWorldCenter().y * PIXELS_PER_METER);
world.DrawDebugData();
clock.Reset();
//display
window.Display();
}
return 0;
}