What is the Function of a Wheel Joint in Planck.js?

Planck.js, a 2D physics engine for JavaScript, utilizes a specialized Wheel Joint to simulate the suspension and rotational behavior of vehicle wheels. This article explains how the wheel joint combines translational and rotational freedom to mimic real-world physics, details its core components like springs and motors, and provides a quick implementation guide for vehicle creation.


Understanding the Wheel Joint Mechanics

The wheel joint is uniquely designed for vehicle simulation because it restricts movement along a specified axis while allowing free rotation. It essentially combines the properties of a Prismatic Joint (line of motion) and a Revolute Joint (rotation).

When you attach a wheel to a vehicle chassis using this joint, you define a translation axis. The wheel can move up and down along this axis to simulate shock absorption, and it can spin freely around its center to propel the vehicle.


Key Features and Components

To make vehicles behave realistically, the wheel joint relies on two critical built-in mechanisms:


Basic Code Implementation

Setting up a wheel joint in Planck.js requires defining the two connecting bodies (the chassis and the wheel), an anchor point, and an axis of motion.

// Define the wheel joint configuration
const jointDef = {
  anchorA: chassis.getWorldPoint(Vec2(1.0, -0.5)), // Position on the car
  localAxisA: Vec2(0.0, 1.0),                     // Vertical suspension axis
  enableMotor: true,                              // Enable for drive wheels
  maxMotorTorque: 20.0,
  motorSpeed: 0.0,
  frequencyHz: 4.0,                               // Suspension stiffness
  dampingRatio: 0.7                               // Suspension damping
};

// Create the joint in the physics world
const wheelJoint = world.createJoint(pl.WheelJoint(jointDef, chassis, wheel));

Summary of Benefits

Using a wheel joint instead of forcing separate generic joints together offers several distinct advantages for developers: