After seeing how to create a revolute joint, it’s time to use them to create motors.
Box2D already has everything we need to create such motors.
The script is the same as the one I showed you in Box2D joints: Revolute Joint, and you just need to add the lines I am going to show between lines 57 and 58:
the_rev_joint.enableMotor = true;
the_rev_joint.motorSpeed = 2;
the_rev_joint.maxMotorTorque = 1;
enableMotor: A flag to enable the joint motor
motorSpeed: The DESIRED (see below) motor speed, radians per second.
maxMotorTorque: The maximum motor torque (moment) used to achieve the desired motor speed, in joules.
Normally one joule is the amount of energy required by a force of one newton traveling through a distance of one meter, that’s why around the web you can see torque expressed as N*m.
Anyway, it does not matter if I whether I am talking about joules or bananas, if your torque is too weak, the motor won’t have the necessary force to reach the desired speed, and sometimes it won’t even work, like in this case:
The motor does not have enough force to raise its own arm…
To make it work, just change line 60 with a higher value such as
the_rev_joint.maxMotorTorque = 10;
and it will work as expected
Next time we’ll se how to limit a motor and create passive forces