Making things turn! With this joint you can make your b2Body turn around an axis but that’s not all. You can make them turn freely, make them turn on a range of degrees, and even apply a motor to power the turning! That’s basically everything you need to simulate any turning element from a complex car gear system to a simple fan. You could easily create a car brake system by adding a b2Frictionjoint in the mix for example. Also if you want to create a car game then this is the number one joint you need to look into. So let’s take a look at our first demo. We simply set a platform turning freely around an axis. Simple enough. Click anywhere to get started:


Relevant code:

var platform:b2Body = DrawBox(250, 250, 250, 10, true, 0, 1, 1, 1);

var revolute:b2RevoluteJointDef = new b2RevoluteJointDef();

revolute.Initialize(world.GetGroundBody(), platform, platform.GetWorldCenter());

var joint:b2RevoluteJoint = world.CreateJoint(revolute as b2JointDef) as b2RevoluteJoint;

We create our platform b2Body and we initialize our joint definition with the world.GetGroundBody() as the b2Body of reference. The second b2Body (platform) is the one we want this b2RevoluteJoint to apply to. The most important part is the last one, platform.GetWorldCenter(), since this sets the axis around which the platform will turn. Here I simply retrieve the platform center of mass. This b2Vec2 could actually be set to other values obviously to create a type of swing for example. Since our platform is set at x:250, y:250 we could simply pass a b2Vec2 at 250, 150 which will set the axis 100 pixel above:

revolute.Initialize(world.GetGroundBody(), platform, new b2Vec2( 250 / SCALE, 150 / SCALE));

and the result:


Now let’s try to limit the degree range:

revolute.lowerAngle = -30 * Math.PI / 180;

revolute.upperAngle = 30 * Math.PI / 180;

revolute.enableLimit = true;

We set the lowerAngle to 30 degrees (converted to radians), the upperAngle to 30 degrees and finally we enable the angle limit. Now our platform can only move in between -30 and 30 degrees:


Now let’s try to use a motor:

revolute.enableMotor = true;

revolute.maxMotorTorque = 10;

revolute.motorSpeed = 2;

We first enable the motor, we then set a small amount of torque and finally give the joint some speed. Now to stop a joint like that from turning or to change it’s speed you need to keep a reference to the created joint. With a b2RevoluteJoint you can use a method like SetMotorSpeed() and simply set it to zero to stop the motor. Here is our demo:


Because we have a very low amount of torque, other object can stop or modify the way the platform turns very easily. The platform could even stop moving if there are too many object in front of it. Let’s try again with a bigger amount of torque to see the difference:


We set the torque to 2500 and now even though the speed is the same it seems that no object can really alter the platform speed or even stop it anymore.

Alright we covered everything with this joint but just for the fun of it let’s finish with a car example. Use the arrow key on your keyboard.


We build a simple car body with a few b2Body and then attach two b2Body at each extremity. We then set two b2RevoluteJoint and keep a reference of them so we can set the motor speed on the fly. Also in this example we set the two inclined ground on the side with a friction of 0 which pretty much simulates icy ground. Use the left and right arrow to play with it.

var revolute:b2RevoluteJointDef = new b2RevoluteJointDef();

revolute.Initialize(suspensionholder, weelone, new b2Vec2(267 / SCALE, 415 / SCALE));

revolute.collideConnected = false;

revolute.enableMotor = true;

revolute.motorSpeed = 0;

revolute.maxMotorTorque = 100;

motorone = world.CreateJoint(revolute as b2JointDef) as b2RevoluteJoint;

In a keyboardEvent listener:

motorone.SetMotorSpeed(10);

That’s it for this b2RevoluteJoint.