Box2D Bicycle Wheels Motor Problem - Flash 2.1a
- by Craig
I have made a bicycle with Box2D using several polygons for the frame at different angles connected using weld joints, and I have revolute joints on the wheels with a motor.
I have made some basic terrain (straight ground and a small ramp) and added keyboard input to control the bicycle with torque to balance it. All of this is done in with Box2D's Debug Draw.
When the bicycle is on its back wheel but diagonally forward (kinda like this position - /) the motors just cause it go spinning backwards over when in reality it should either stay on its back wheel or go down onto both wheels.
Here's my code the revolute joints:
//Front Wheel Joint
var frontWheelJointDef:b2RevoluteJointDef = new b2RevoluteJointDef();
frontWheelJointDef.Initialize(frontWheelBody, secondFrameBody, frontWheelBody.GetWorldCenter());
frontWheelJointDef.enableMotor=true;
frontWheelJointDef.maxMotorTorque=10000;
frontWheelJoint = _world.CreateJoint(frontWheelJointDef) as b2RevoluteJoint;
//Rear Wheel Joint
var rearWheelJointDef:b2RevoluteJointDef = new b2RevoluteJointDef();
rearWheelJointDef.Initialize(rearWheelBody, firstFrameBody, rearWheelBody.GetWorldCenter());
rearWheelJointDef.enableMotor=true;
rearWheelJointDef.maxMotorTorque=10000;
rearWheelJoint = _world.CreateJoint(rearWheelJointDef) as b2RevoluteJoint;
And here's the relevant part of my update function:
// up and down control wheels motor
if (up) {
motorSpeed-=0.5;
}
if (down) {
motorSpeed += 0.5;
}
// left and right control cart torque
if (left) {
middleCentreFrameBody.ApplyTorque( -3);
gearBody.ApplyTorque( -3);
firstFrameBody.ApplyTorque( -3);
secondFrameBody.ApplyTorque( -3);
rearWheelToChainBody.ApplyTorque( -3);
chainToFrontFrameBody.ApplyTorque( -3);
topMiddleFrameBody.ApplyTorque( -3);
}
if (right) {
middleCentreFrameBody.ApplyTorque( 3);
gearBody.ApplyTorque( 3);
firstFrameBody.ApplyTorque( 3);
secondFrameBody.ApplyTorque( 3);
rearWheelToChainBody.ApplyTorque( 3);
chainToFrontFrameBody.ApplyTorque( 3);
topMiddleFrameBody.ApplyTorque( 3);
}
// motor friction
motorSpeed*=0.99;
// motor max speed
if (motorSpeed>100) {
motorSpeed=100;
}
rearWheelJoint.SetMotorSpeed(motorSpeed);
frontWheelJoint.SetMotorSpeed(motorSpeed);
Any ideas what might be causing this?
Thanks