Using joints

In this lesson we learn to use joints, called the constraint system in P2 Physics to simulate a joint physics system.

A rope or chain is an example of a joint system.

Creating a constraint
The simplest constraint we can use is the DistanceConstraint. To create it, we do:

This will make sure block1 and block2 are always 200 pixels away from each other. It will pull them together if the distance is greater, or push them away if the distance is closer.

The result looks like you've tied them together by a rope.

Other types of constraints
Check out the other types of constraints you can use:

http://phaser.io/docs#p2physics

A fun one to experiment with is the spring joint. Check out this Phaser example using it to let you pull an object using the mouse:

http://phaser.io/examples/v2/p2-physics/mouse-spring

Challenges

 * Can you use a constraint to make an object follow the mouse?
 * Can you make it follow the mouse when it's held down, and release when the mouse is released?