Skip to main content

Joints

One of the most appealing features of a physics engine is to simulate articulations. Articulations, aka. joints, allow the restriction of the motion of one body relative to another body. For example, one well-known joint is the ball-in-socket joint also known as the spherical joint: it allows one object to rotate freely with regard to the other but not to translate. This is typically used to simulate shoulders of a ragdoll.

Basic concepts​

Joints can be modeled in various ways but let's talk about the concept of Degrees Of Freedom (DOF) first. In 3D, a rigid-body is capable of translating along the 3 coordinates axes x\mathbf{x}, y\mathbf{y} and z\mathbf{z}, and to rotate along those three axes as well. Therefore, a rigid-body is said to have 3 translational DOF and 3 rotational DOF. We can also say a 3D rigid-body has a total of 6 DOF. The 2D case is similar but with less possibilities of movements: a 2D rigid-body has 2 translational DOF and only 1 rotational DOF (which forms a total of 3 DOF). The number of relative DOF of a body wrt. another body is the number of possible relative translations and rotations.

The goal of a joint is to reduce the number of DOF a body has. For example, the aforementioned spherical joint removes all relative translations between two bodies. Therefore, it allows only the 3 rotational DOF in 3D simulations or the 1 rotational DOF in 2D simulations. Other joints exist allowing other combinations of relative DOF. Note that because there are less possible motions in 2D, some joints are only defined in 3D. This is illustrated by empty cells in the following table for joints that are not defined in 2D:

JointAllowed DOF in 2DAllowed DOF in 3DRapier support
Fixed jointNoneNoneYes
Free jointAllAllThrough a generic joint
Prismatic joint1 Translation1 TranslationYes
Revolute joint1 Rotation1 RotationYes
Spherical joint1 Rotation3 RotationsYes
Cartesian joint2 Translations3 TranslationsThrough a generic joint
Planar joint2 Translations + 1 RotationThrough a generic joint
Cylindrical joint1 Translation + 1 Rotation (along the same axis)Through a generic joint
Pin-slot joint1 Translation + 1 Rotation (along different axes)Through a generic joint
Rectangular joint2 TranslationsThrough a generic joint
Universal joint2 RotationsThrough a generic joint

A joint is described by an R3JointDesc which must be initialized by one of the constructors of the joint types detailed in this page: r3FixedJointDesc, r3SphericalJointDesc (3D only), r3RevoluteJointDesc, and r3PrismaticJointDesc. A few other joint types have their own constructors too: r3RopeJointDesc, r3SpringJointDesc, and r2PinSlotJointDesc (2D only), and any other combination of free DOF can be described by a generic joint. Then any of the fields of the description can be modified before the joint is inserted into the world either as an impulse joint with r3InsertImpulseJoint, or as a multibody joint with r3InsertMultibodyJoint, given the handles of the two rigid-bodies it attaches. They return an R3ImpulseJointHandle and an R3MultibodyJointHandle respectively. The difference between those is explained in next section.

Fixed joint​

A fixed joint ensures that two rigid-bodies don't move relative to each other. Fixed joints are characterized by one local frame (represented by a R3Pose) on each rigid-body. The fixed-joint makes these frames coincide in world-space.

info

Attaching multiple colliders to a single rigid-body will have the same effect as using one rigid-body per collider and attaching them with a fixed joint. However the multi-collider approach will be much more efficient and numerically efficient than the joint approach. So a fixed-joint should only be used when the multi-collider doesn't fit your use-case (for example if you want to read the force applied by the joint in order to break it dynamically).

The local frames are the localFrame1 and localFrame2 fields of the joint description, expressed in the local-space of the first and second rigid-body respectively. Their translation parts are the local anchors of the joint, which can also be set with r3JointDesc_SetLocalAnchor1 and r3JointDesc_SetLocalAnchor2:

// NOTE: the local anchors are the translation parts of the local frames.
R2JointDesc joint = r2FixedJointDesc();
joint.localFrame1.translation = r2Vector(0.0, 1.0);
joint.localFrame2.translation = r2Vector(0.0, -3.0);
r2InsertImpulseJoint(body_handle1, body_handle2, &joint);

Spherical joint​

The spherical joint ensures that two points on the local-spaces of two rigid-bodies always coincide (it prevents any relative translational motion at this points). This is typically used to simulate ragdolls arms, pendulums, etc. They are characterized by one local anchor on each rigid-body. Each anchor represents the location of the points that need to coincide on the local-space of each rigid-body.

R3JointDesc joint = r3SphericalJointDesc();
joint.localFrame1.translation = r3Vector(0.0, 0.0, 1.0);
joint.localFrame2.translation = r3Vector(0.0, 0.0, -3.0);
r3InsertImpulseJoint(body_handle1, body_handle2, &joint);
note

In 2D, revolute joints and spherical joints are the same thing. Therefore, there is no r2SphericalJointDesc constructor in 2D (use the r2RevoluteJointDesc constructor instead).

Revolute joint​

The revolute joint prevents any relative movement between two rigid-bodies, except for relative rotations along one axis. This is typically used to simulate wheels, fans, etc. They are characterized by one local anchor as well as one local axis on each rigid-body.

In 3D, the local axis is given to r3RevoluteJointDesc: it sets the rotation of both local frames so that their x\mathbf{x} axis is aligned with this axis. Different axes on each rigid-body can then be set with r3JointDesc_SetLocalAxis1 and r3JointDesc_SetLocalAxis2. In 2D, the rotation axis is implicit, so r2RevoluteJointDesc takes no argument:

R2JointDesc joint = r2RevoluteJointDesc();
joint.localFrame1.translation = r2Vector(0.0, 1.0);
joint.localFrame2.translation = r2Vector(0.0, -3.0);
r2InsertImpulseJoint(body_handle1, body_handle2, &joint);

Prismatic joint​

The prismatic joint prevents any relative movement between two rigid-bodies, except for relative translations along one axis. It is characterized by one local anchor as well as one local axis on each rigid-body.

The local axis is the x\mathbf{x} axis of the local frames. In 3D, the rotation of the local frames around this axis lets you control the fixed relative orientation of the rigid-bodies.

The prismatic joint supports the application of joint limits. This will restrict the relative distance between the rigid-body anchors (along the free joint axis) to remain in the specified range set with r3JointDesc_SetLimits. The signed distance is computed as (anchor2 - anchor1).dot(axis1).

The limits are set along one of the axes of the joint, identified by its index: R3_AXIS_LIN_X, R3_AXIS_LIN_Y, and R3_AXIS_LIN_Z for the translations, then R3_AXIS_ANG_X, R3_AXIS_ANG_Y, and R3_AXIS_ANG_Z for the rotations (in 2D, the only rotation is R2_AXIS_ANG_X, i.e., the rotation around z\mathbf{z}). For a prismatic joint, the free axis is R3_AXIS_LIN_X. The function r3JointDesc_SetLimits also enables the limits of this axis in the limitAxes bitmask of the description. Limits apply to any free axis of any joint: angular limits of a revolute joint are set on its R3_AXIS_ANG_X axis (in radians).

R2Vector x = r2Vector(1.0, 0.0);
R2JointDesc joint = r2PrismaticJointDesc(x);
joint.localFrame1.translation = r2Vector(0.0, 1.0);
joint.localFrame2.translation = r2Vector(0.0, -3.0);
// The free axis of a prismatic joint is the X axis of its local frames.
r2JointDesc_SetLimits(&joint, R2_AXIS_LIN_X, -2.0, 5.0);
r2InsertImpulseJoint(body_handle1, body_handle2, &joint);

Joint motors​

Spherical, revolute, and prismatic joints support joint motors.

Motors allow you to make the linked rigid-bodies move relative to one another, along the free degrees of freedom left by the joint, as if a motor was pushing them. The joint motor is simulated with a PD controller (Proportional Derivative controller) where you can set a target relative velocity along the free degrees of freedom as well as a target position. The stiffness of the PD controller controls the strength of the force that will be applied to make the bodies reach the target relative positions along the free DOFs. The damping of the PD controller controls the strength of the force that will be applied to make the bodies reach the target relative velocities along the free DOFs. The motor of each free axis is configured by the following functions (where axis is the index of the axis, e.g., R3_AXIS_LIN_X):

  • r3JointDesc_SetMotorPosition(desc, axis, target_pos, stiffness, damping): this tells the joint motor that we want the relative position of the rigid-bodies along the free DOFs to be equal to target_pos, and that the relative velocity when it reaches that position should be zero.
  • r3JointDesc_SetMotorVelocity(desc, axis, target_vel, damping): this tells the joint motor that we want the relative relative velocity of the rigid-bodies along the free DOFs to be equal to target_vel. There is no restriction on the relative position along the free DOF.
  • r3JointDesc_SetMotor(desc, axis, target_pos, target_vel, stiffness, damping): this lets you control all the parameters of the motor's spring-like equation. This should be used only if the other configuration methods are not flexible enough.
  • r3JointDesc_SetMotorModel(desc, axis, model): this selects the mathematical model for the motor's controller. All the available models use a spring-like equation. With R3_MOTOR_ACCELERATION_BASED (the default), the stiffness and damping are scaled by the mass of the rigid-bodies (which makes them easier to tune). With R3_MOTOR_FORCE_BASED, they produce absolute forces.

It is also possible to configure the maximum impulse applied by the motor with r3JointDesc_SetMotorMaxForce: this limits the maximum force/torque the motor is able to deliver. The following examples show the configuration of the joint motor for a prismatic joint:

R2Vector x = r2Vector(1.0, 0.0);
R2JointDesc joint = r2PrismaticJointDesc(x);
joint.localFrame1.translation = r2Vector(0.0, 1.0);
joint.localFrame2.translation = r2Vector(0.0, -3.0);
r2JointDesc_SetMotorVelocity(&joint, R2_AXIS_LIN_X, 1.0, 0.5);
R2ImpulseJointHandle joint_handle = r2InsertImpulseJoint(body_handle1, body_handle2, &joint);

Each of these functions also enables the motor of the given axis in the motorAxes bitmask of the description.

Generic joints​

All the joint constructors only initialize the fields of the same R3JointDesc structure, so any other joint can be described by modifying these fields directly. The main one is the lockedAxes bitmask: it contains one bit per axis of the joint (1 << R3_AXIS_LIN_X for the translation along x\mathbf{x}, etc.), each set bit removing the corresponding relative DOF. For example, lockedAxes is R3_JOINT_FIXED_AXES for a fixed joint, and R3_JOINT_REVOLUTE_AXES for a revolute joint. A description initialized by r3DefaultJointDesc locks nothing, which is a Free joint until its lockedAxes are set. The following fields of the description can be modified for any joint:

  • lockedAxes, limitAxes, and motorAxes: the bitmasks of the locked axes, and of the axes with enabled limits and motors. The limits and motors themselves are stored in the limits and motors arrays (one entry per axis).
  • contactsEnabled: whether the colliders of the two rigid-bodies attached by the joint can collide with each other.
  • softness: the natural frequency and damping ratio of the joint constraints. Lowering them makes the joint springy instead of rigid. Note that this has no effect on the locked axes of a multibody joint, which can't be violated.
  • enabled: whether the joint is enabled. A disabled joint stays attached to its rigid-bodies but is ignored by the constraints solver.
  • userData: 128 bits of data freely available to the application.
// A cartesian joint: only the rotation is locked.
R2JointDesc joint = r2DefaultJointDesc();
joint.lockedAxes = 1 << R2_AXIS_ANG_X;
joint.localFrame1.translation = r2Vector(0.0, 1.0);
joint.localFrame2.translation = r2Vector(0.0, -3.0);
// Limit the relative translation along the X axis.
r2JointDesc_SetLimits(&joint, R2_AXIS_LIN_X, -2.0, 5.0);
// Allow contacts between the colliders of the two rigid-bodies.
// Default: 1
joint.contactsEnabled = 1;
// Make the locked axes springy instead of rigid.
// Default: a natural frequency of 1.0e6 Hz and a damping ratio of 1.0.
joint.softness.natural_frequency = 10.0;
joint.softness.damping_ratio = 1.0;
r2InsertImpulseJoint(body_handle1, body_handle2, &joint);

The coupledAxes bitmask changes the interpretation of the limits and motors of the coupled axes: instead of applying to each axis independently, they apply to the combined displacement along all the coupled linear axes (resp. angular axes). In this case, only the limit and motor of the first coupled linear axis (resp. angular axis) are used. This is, for example, how the rope joint limits the distance between its anchors:

// The relative translation along all the axes is free, but its length is limited to 2.0 (like a rope).
R2JointDesc joint = r2DefaultJointDesc();
joint.coupledAxes = (1 << R2_AXIS_LIN_X) | (1 << R2_AXIS_LIN_Y);
// Only the limits of the first coupled axis are used.
r2JointDesc_SetLimits(&joint, R2_AXIS_LIN_X, 0.0, 2.0);
r2InsertImpulseJoint(body_handle1, body_handle2, &joint);

The rope joint and the spring joint are both built this way, and have their own constructors. In 2D, the pin-slot joint (aka. groove joint) is also available: it allows the relative rotation, and the relative translation along one axis:

// The distance between the anchors can't exceed 2.0.
R2JointDesc rope = r2RopeJointDesc(2.0);
r2InsertImpulseJoint(body_handle1, body_handle2, &rope);
// A spring with a rest length of 2.0, a stiffness of 10.0, and a damping of 0.5.
R2JointDesc spring = r2SpringJointDesc(2.0, 10.0, 0.5);
r2InsertImpulseJoint(body_handle1, body_handle2, &spring);
// A pin-slot joint: free rotation, and free translation along the X axis.
R2JointDesc pin_slot = r2PinSlotJointDesc(r2Vector(1.0, 0.0));
r2InsertImpulseJoint(body_handle1, body_handle2, &pin_slot);

Modifying joints​

After its insertion, the configuration of an impulse joint can be read with r3ImpulseJoint_Desc, and replaced as a whole with r3ImpulseJoint_SetDesc. Each setter of R3JointDesc also has an r3ImpulseJoint_ counterpart modifying the inserted joint directly, e.g., r3ImpulseJoint_SetMotorVelocity, or r3ImpulseJoint_SetLimits. The last argument of these functions indicates whether the rigid-bodies attached by the joint must be woken up. The rigid-bodies attached by an impulse joint are given by r3ImpulseJoint_Bodies, its user data by r3ImpulseJoint_UserData, and a joint is removed with r3RemoveImpulseJoint (resp. r3RemoveMultibodyJoint for a multibody joint). The number of joints of the world is given by r3ImpulseJointCount and r3MultibodyJointCount, and their handles can be listed with r3ImpulseJointHandles and r3MultibodyJointHandles.

// Change the motor of an existing joint (the last argument wakes up its rigid-bodies).
r2ImpulseJoint_SetMotorVelocity(joint_handle, R2_AXIS_LIN_X, 2.0, 0.5, 1);
// Read a copy of the whole joint description, modify it, then apply it back.
R2JointDesc desc = r2ImpulseJoint_Desc(joint_handle);
desc.contactsEnabled = 0;
r2ImpulseJoint_SetDesc(joint_handle, &desc, 1);
// Disable the joint: it stays attached to its rigid-bodies but is ignored by the solver.
r2ImpulseJoint_SetEnabled(joint_handle, 0, 1);
// The rigid-bodies attached by the joint.
R2JointBodies bodies = r2ImpulseJoint_Bodies(joint_handle);
// Remove the joint, waking up its rigid-bodies.
r2RemoveImpulseJoint(joint_handle, 1);

The impulses applied by an impulse joint during the last simulation step are given by r3ImpulseJoint_Impulses: the linear and angular impulses applied along the locked axes, as well as the impulses applied by the limit and the motor of each axis (the limits and motors arrays). They are expressed along the axes of the joint frame, and are zero before the first simulation step of the joint. This can be used to break a joint dynamically, by removing it once these impulses exceed some threshold:

const R2Real max_impulse = 10.0;
r2Step(world, NULL, NULL);
// The impulses applied by the joint during the last step.
R2JointImpulses impulses = r2ImpulseJoint_Impulses(joint_handle);
// Break the joint if it has to pull its rigid-bodies too strongly to keep them together.
if (r2VectorLength(impulses.linear) > max_impulse) {
r2RemoveImpulseJoint(joint_handle, 1);
}