How does Friction Joint work in planck.js?

In 2D physics simulations using planck.js, a Friction Joint is used to reduce both relative linear and angular motion between two rigid bodies by simulating friction forces. This article explores how this joint operates, its key properties like maximum force and torque, and how to implement it effectively to prevent objects from sliding or spinning indefinitely.

Understanding the Friction Joint

A Friction Joint (FrictionJoint) dampens the relative movement between two connected bodies. Unlike real-world friction, which depends on the normal force holding two surfaces together, planck.js allows you to set independent, explicit limits on the maximum frictional forces applied.

It operates on two fronts simultaneously:

Key Properties and Parameters

When configuring a Friction Joint in planck.js, two parameters dictate how aggressively it reduces motion:

If the external forces acting on the bodies are less than these maximum thresholds, the bodies will move together as if locked. If the external forces exceed these limits, the bodies will slip, but their relative motion will still be slowed down.

Implementing a Friction Joint

To create a Friction Joint, you define a joint definition, configure the target anchors on both bodies, and add it to your physics world.

// Step 1: Define the joint configuration
const jointDef = {
  bodyA: body1,
  bodyB: body2,
  localAnchorA: planck.Vec2(0, 0),
  localAnchorB: planck.Vec2(0, 0),
  maxForce: 10.0,  // Limits linear sliding
  maxTorque: 5.0   // Limits angular spinning
};

// Step 2: Create the joint in the world
const frictionJoint = world.createJoint(planck.FrictionJoint(jointDef));

Common Use Cases

Friction Joints are particularly useful in top-down 2D games or simulations where you need to model surface friction without utilizing complex contact physics.