Physic Joints

Learn how to use physic joints

What's a Joint

A Joint is a mechanism used to connect two Physic Objects. For example if you want to build a car, you would need a chassis and two wheels. In order to connect the two wheels to the chassis you can use two Joints, one for each wheel. Once two Physic Objects are connected by a Joint, the way they can move relatively to each other becomes limited, this is why a Joint is often called a constrain. Joints constrains or forces two Physic Objects to interact (or move) relatively to each other in a strict way. There are many types of Joints, for example for our car, we use a Wheel Joint. This joint type will constrains the car wheel to stay attached to the chassis at a specific anchor point and the only movement the car wheel will be allows to perform will be to rotate around that specific anchor point.

Type of Joints

The Engine offers Eleven (11) types of Joints. Here is the full list

  • Distance Joint
  • Prismatic Joint
  • Rope Joint
  • Pulley Joint
  • Wheel Joint
  • Weld Joint
  • Revolute Joint
  • Gear Joint
  • Motor Joint
  • Friction Joint
  • Mouse Joint

Create Joints

A Joint is created using the method createJoint(..) of the class nero::Scene::ObjectManager. The method need three (3) parameters. The two first parameter are the two Physic Objects you want to connect with the Joint and the last parameter is the properties of the Joint. For each of the eleven (11) Joint types there is Joint Property object. For example for a Distance Joint you have nero::DistanceJointProperty, for a Wheel Joint you have nero::WheelJointProperty etc.

getObjectManager()->createJoint(objectA, objectB, jointProperty);

The code below shows how to create a Distance Joint. You can can create a Joint in the your Scene init() method or any time during the game

//retrieve objects
nero::Object::Ptr objectA =  getObjectManager()->findObject("object_a");
nero::Object::Ptr objectB =  getObjectManager()->findObject("object_b");
//create a joint property object (Example with a Distance Joint)
nero::DistanceJointProperty distanceJoint;                = "joint_name";
distanceJoint.collideConnected    = false;
distanceJoint.localAnchorA        = sf::Vector2f(0.f, 0.f);
distanceJoint.localAnchorB        = sf::Vector2f(0.f, 0.f);
distanceJoint.length              = 300.f;
distanceJoint.frequencyHz         = 5.f;
distanceJoint.dampingRatio        = 0.5f;
//create joint
getObjectManager()->createJoint(objectA, objectB, distanceJoint);
//retrieve joint (optional)
nero::DistanceJoint::Ptr myJoint = nero::DistanceJoint::Cast(getObjectManager()->findJoint("joint_name"));

Common Joint Property attribrutes

Each types of Joint Property (nero::DistanceJointProperty, nero::WheelJointProperty, etc.) has its own different set of attributes.  But their all share four (4) attributes

  • name (string) : the name of the Joint. It serves as the Joint Id and can be used to retrieve the Joint at any moment.
  • collideConnected (boolean) : indicated if the two physic object A and B can collide with each other
  • localAnchorA (sf::Vector2f()) : the joint anchor point on the object A. The anchor is local which mean that the value of (0.f, 0.f) correspond to Object A center of mass
  • localAnchorB (sf::Vector2f()) : the joint anchor point on the object B. The anchor is local which mean that the value of (0.f, 0.f) correspond to Object B center of mass