Box2D joints: Revolute Joint

After introducing Distance Joints, it’s time to learn something about Revolute Joints.

A Revolute Joint attaches two objects by “pinning” them together at an anchor point which they may revolve around.

The most interesting thing, that I will cover in another post, is the capibility of making motors out of Revolute Joints. Yes… motors… engines… easy to do and ecologic since they don’t need fuel, just some code.

But at the moment let’s see the basic Revolute Joint.

Here it is:

Now let’s see how to make this possible:

Line 16: Declaration of the_rev_joint variable, a b2RevoluteJointDef type

Lines 40-46: Creating a circle in the middle of the stage that will act as a pivot point. In most examples around the web you will find pivot points as tiny, almost invisible boxes. I am making a big one because I think it will be easier to skin it once I will need to do it. Notice at line 41 that the object is static because it has density at zero.

Lines 48-56: Creation of the box that will be tied to the joint. You can virtually place it anywhere, but for an interesting result, it should be in a position where the joint can act as a fulcrum.

Line 57: Initializing the joint: I am passing the joint pivot, the body tied to the joint and the point of intersection. You can see the (8.5,6.5) is the same as pivot’s origin (line 43).

Line 58: Adding the joint to the world.

And that’s it. Believe me, Box2D is very easy once you know the basics.

And this is the source code for you to download.