# Box2D Bicycle Wheels Motor Problem - Flash 2.1a

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

• Would it be possible to share a demonstration online? Sep 30 '12 at 16:39
• This sounds a lot like your mass and friction values are off. Also I don't understand why you apply torque to all these different body parts? Applying a force to the motor alone should be sufficient to move the bike. Aug 29 '13 at 16:52

## 1 Answer

maxMotorTorque(10000) seems too much for me.

I don't think giving torque to each part separately will work.why don't you make one bike body and create parts as fixtures of it. Then give torque to the bike body.