PlayN Game Library Tutorial #4 – Box2D Joints

Box2D allows multiple ways to define physical objects. A body can be composed of multiple shapes or it is possible to create composite object, made of multiple bodies connected using joints. There are several types of joints in Box2d, distance joint, revolute joint, gear joint and others. Joints can be used to model interaction between physical objects to simulate chains, ropes, wheels or hinges. Using joints effectively is important to create an engaging scene.

In the code bellow a new chain is created ever 100 frames. The chain consists of a random number of circles of random radius, all the circles are linked using distance joints. The distance joint can be thought of as a spring but without any physical characteristics or collision handling. It has a number of parameters, which affect it's behavior:

  1. length indicates the smallest length of the joint, in the spring example, this is the length of a fully relaxed spring.
  2. frequencyHz indicates the oscillating frequency of the spring in our example 0 means that the joint is rigid and does not oscillate.
  3. dampingRatio indicates the rate at which oscillations lose energy.

Because the joint has no physical properties, objects can pass through the joint and no collision detection is performed on the joint. You can see this when the chain passes through the middle platform. If you wish the joint to have physical properties you need to build the object in such a way that there is no empty space between shapes. In our example we would place a rectangle between each circle and join the rectangle to the circles on both sides with a short distance joint.





Leave a Reply

Your email address will not be published. Required fields are marked *