Hello.
I have a problem with collision detection and behavior. I have model of plane, which consists of three bodies: hull, landing gear and wheel. Wheel is connected to leg with revolute joint without limits, leg is connected to hull with revolute joint with both limits set to 0 ("welded").
See attached picture for first 6 frames of simulation.
When i start simulation, the plane is 2 meters above the ground body - a huge box.When plane falls on the ground, wheel detaches from leg, and leg falls into. Further into simulation, one can see that ground box tries to push the leg out, and wheel tries to get back to it's position, but with little success.
Using box2d 2.0.1
What's going on? How to fix it?
This is how I create world and ground:
and plane:
I have a problem with collision detection and behavior. I have model of plane, which consists of three bodies: hull, landing gear and wheel. Wheel is connected to leg with revolute joint without limits, leg is connected to hull with revolute joint with both limits set to 0 ("welded").
See attached picture for first 6 frames of simulation.
When i start simulation, the plane is 2 meters above the ground body - a huge box.When plane falls on the ground, wheel detaches from leg, and leg falls into. Further into simulation, one can see that ground box tries to push the leg out, and wheel tries to get back to it's position, but with little success.
Using box2d 2.0.1
What's going on? How to fix it?
This is how I create world and ground:
Code:
// create world
b2AABB worldAABB;
worldAABB.lowerBound.Set(-11000, -11000);
worldAABB.upperBound.Set(11000, 11000); // 10x10 km box
// gravity
b2Vec2 gravity(0.0, -9.81);
_pWorld = new b2World( worldAABB, gravity, true );
// create ground
b2BodyDef groundBodyDef;
groundBodyDef.position.Set(0.0f, -2500.0f);
_pGround = _pWorld->CreateBody( & groundBodyDef );
b2PolygonDef groundShapeDef;
groundShapeDef.SetAsBox(5000, 2500);
groundShapeDef.friction = 0.1;
groundShapeDef.restitution = 0.1;
_pGround->CreateShape( & groundShapeDef );
b2AABB worldAABB;
worldAABB.lowerBound.Set(-11000, -11000);
worldAABB.upperBound.Set(11000, 11000); // 10x10 km box
// gravity
b2Vec2 gravity(0.0, -9.81);
_pWorld = new b2World( worldAABB, gravity, true );
// create ground
b2BodyDef groundBodyDef;
groundBodyDef.position.Set(0.0f, -2500.0f);
_pGround = _pWorld->CreateBody( & groundBodyDef );
b2PolygonDef groundShapeDef;
groundShapeDef.SetAsBox(5000, 2500);
groundShapeDef.friction = 0.1;
groundShapeDef.restitution = 0.1;
_pGround->CreateShape( & groundShapeDef );
and plane:
Code:
// main body
b2BodyDef mainDef;
mainDef.massData.mass = 1000; // 1000kg
mainDef.massData.center.SetZero();
mainDef.massData.I = 600;
mainDef.linearDamping = 0;
mainDef.angularDamping = 0.4;
// initial pos
mainDef.position.Set( pos.x(), pos.y() );
mainDef.angle = 0;
// shape
_shapeBody.append( QPointF( 1.5, 0.5 ) );
_shapeBody.append( QPointF(-0.1, 0.9 ) );
_shapeBody.append( QPointF(-4.0, 0.0 ) );
_shapeBody.append( QPointF( 0.0,-0.7 ) );
_shapeBody.append( QPointF( 1.5,-0.5 ) );
_shapeSkid.append( QPointF(-4.0,-0.3) );
_shapeSkid.append( QPointF(-3.7,-0.3) );
_shapeSkid.append( QPointF(-3.0, 0.0) );
_shapeSkid.append( QPointF(-3.7, 1.0) );
_shapeSkid.append( QPointF(-4.0, 1.0) );
b2PolygonDef mainShape = shapeToDef( _shapeBody );
mainShape.friction = 0.1;
mainShape.restitution = 0.3;
b2PolygonDef skidShape = shapeToDef( _shapeSkid );
skidShape.friction = 0.0;
skidShape.restitution = 0.1;
_pMainBody = pWorld->CreateBody( & mainDef );
_pMainBody->CreateShape( & mainShape );
_pMainBody->CreateShape( & skidShape );
// wheel
b2BodyDef wheelDef;
wheelDef.massData.mass = 10; // 10kg
wheelDef.massData.center.Set( WHEEL_POS.x(), WHEEL_POS.y() );
wheelDef.massData.I = 1;
wheelDef.linearDamping = 0;
wheelDef.angularDamping = 0.01;
wheelDef.position.Set( pos.x() + WHEEL_POS.x(), pos.y() + WHEEL_POS.y() ); // 1 meter below
wheelDef.angle = 0;
b2CircleDef wheelShape;
wheelShape.radius = WHEEL_RADIUS;
wheelShape.localPosition.SetZero();
wheelShape.isSensor = false;
wheelShape.restitution = 0.7;
wheelShape.density = 10;
_pMainWheel = pWorld->CreateBody( & wheelDef );
_pMainWheel->CreateShape( & wheelShape );
_pMainWheel->SetMassFromShapes();
// leg
b2BodyDef legDef;
legDef.massData.mass = 20; // 20kg
legDef.massData.center.Set( 0.15, -0.5 );
legDef.massData.I = 40;
legDef.position.Set( pos.x(), pos.y() );
legDef.angle = 0;
legDef.linearDamping = 0;
legDef.angularDamping = 0;
_shapeLeg.append( QPointF(-0.2, -0.2) );
_shapeLeg.append( QPointF( 0.1, -1.6) );
_shapeLeg.append( QPointF( 0.5, -1.6) );
_shapeLeg.append( QPointF( 0.2, -0.2) );
b2PolygonDef legShape = shapeToDef( _shapeLeg );
legShape.friction = 0;
legShape.restitution = 0.3;
_pMainLeg = pWorld->CreateBody( & legDef );
_pMainLeg->CreateShape( & legShape );
// joint between wheel and leg
b2RevoluteJointDef wheelJoint;
wheelJoint.Initialize( _pMainLeg, _pMainWheel, b2Vec2( pos.x()+WHEEL_POS.x(), pos.y()+WHEEL_POS.y() ) );
wheelJoint.enableMotor = false;
wheelJoint.enableLimit = false;
wheelJoint.collideConnected = false;
// joint between leg and body
b2RevoluteJointDef legJoint;
legJoint.Initialize( _pMainBody, _pMainLeg, b2Vec2( pos.x()+0.0, pos.y()-0.3 ) );
legJoint.enableMotor = false;
legJoint.enableLimit = true;
legJoint.upperAngle = 0;
legJoint.lowerAngle = 0;
legJoint.collideConnected = false;
_pMainWheelJoint = (b2RevoluteJoint*)pWorld->CreateJoint( & wheelJoint );
_pLegJoin = (b2RevoluteJoint*)pWorld->CreateJoint( & legJoint );
b2BodyDef mainDef;
mainDef.massData.mass = 1000; // 1000kg
mainDef.massData.center.SetZero();
mainDef.massData.I = 600;
mainDef.linearDamping = 0;
mainDef.angularDamping = 0.4;
// initial pos
mainDef.position.Set( pos.x(), pos.y() );
mainDef.angle = 0;
// shape
_shapeBody.append( QPointF( 1.5, 0.5 ) );
_shapeBody.append( QPointF(-0.1, 0.9 ) );
_shapeBody.append( QPointF(-4.0, 0.0 ) );
_shapeBody.append( QPointF( 0.0,-0.7 ) );
_shapeBody.append( QPointF( 1.5,-0.5 ) );
_shapeSkid.append( QPointF(-4.0,-0.3) );
_shapeSkid.append( QPointF(-3.7,-0.3) );
_shapeSkid.append( QPointF(-3.0, 0.0) );
_shapeSkid.append( QPointF(-3.7, 1.0) );
_shapeSkid.append( QPointF(-4.0, 1.0) );
b2PolygonDef mainShape = shapeToDef( _shapeBody );
mainShape.friction = 0.1;
mainShape.restitution = 0.3;
b2PolygonDef skidShape = shapeToDef( _shapeSkid );
skidShape.friction = 0.0;
skidShape.restitution = 0.1;
_pMainBody = pWorld->CreateBody( & mainDef );
_pMainBody->CreateShape( & mainShape );
_pMainBody->CreateShape( & skidShape );
// wheel
b2BodyDef wheelDef;
wheelDef.massData.mass = 10; // 10kg
wheelDef.massData.center.Set( WHEEL_POS.x(), WHEEL_POS.y() );
wheelDef.massData.I = 1;
wheelDef.linearDamping = 0;
wheelDef.angularDamping = 0.01;
wheelDef.position.Set( pos.x() + WHEEL_POS.x(), pos.y() + WHEEL_POS.y() ); // 1 meter below
wheelDef.angle = 0;
b2CircleDef wheelShape;
wheelShape.radius = WHEEL_RADIUS;
wheelShape.localPosition.SetZero();
wheelShape.isSensor = false;
wheelShape.restitution = 0.7;
wheelShape.density = 10;
_pMainWheel = pWorld->CreateBody( & wheelDef );
_pMainWheel->CreateShape( & wheelShape );
_pMainWheel->SetMassFromShapes();
// leg
b2BodyDef legDef;
legDef.massData.mass = 20; // 20kg
legDef.massData.center.Set( 0.15, -0.5 );
legDef.massData.I = 40;
legDef.position.Set( pos.x(), pos.y() );
legDef.angle = 0;
legDef.linearDamping = 0;
legDef.angularDamping = 0;
_shapeLeg.append( QPointF(-0.2, -0.2) );
_shapeLeg.append( QPointF( 0.1, -1.6) );
_shapeLeg.append( QPointF( 0.5, -1.6) );
_shapeLeg.append( QPointF( 0.2, -0.2) );
b2PolygonDef legShape = shapeToDef( _shapeLeg );
legShape.friction = 0;
legShape.restitution = 0.3;
_pMainLeg = pWorld->CreateBody( & legDef );
_pMainLeg->CreateShape( & legShape );
// joint between wheel and leg
b2RevoluteJointDef wheelJoint;
wheelJoint.Initialize( _pMainLeg, _pMainWheel, b2Vec2( pos.x()+WHEEL_POS.x(), pos.y()+WHEEL_POS.y() ) );
wheelJoint.enableMotor = false;
wheelJoint.enableLimit = false;
wheelJoint.collideConnected = false;
// joint between leg and body
b2RevoluteJointDef legJoint;
legJoint.Initialize( _pMainBody, _pMainLeg, b2Vec2( pos.x()+0.0, pos.y()-0.3 ) );
legJoint.enableMotor = false;
legJoint.enableLimit = true;
legJoint.upperAngle = 0;
legJoint.lowerAngle = 0;
legJoint.collideConnected = false;
_pMainWheelJoint = (b2RevoluteJoint*)pWorld->CreateJoint( & wheelJoint );
_pLegJoin = (b2RevoluteJoint*)pWorld->CreateJoint( & legJoint );