Disable Rear Wheel Steering in Ammo.js Vehicle

This article explains how to disable the steering functionality on the rear wheels of a 3D vehicle using the Ammo.js physics engine. You will learn how the raycast vehicle system manages steering angles for individual wheels and how to configure your vehicle update loop to restrict steering control exclusively to the front wheels.

In Ammo.js, vehicles are typically simulated using the btRaycastVehicle class. This class does not have a global “steer” function; instead, it requires you to apply steering angles to each wheel individually using its index. To disable steering on the rear wheels, you must manage how you apply values to these wheel indices in your physics update loop.

Step 1: Identify your wheel indices

When you build a vehicle in Ammo.js, wheels are added sequentially using the addWheel method. The order in which you add them determines their index:

Step 2: Configure the wheel properties

When adding your wheels to the vehicle, ensure that the boolean flag for isFrontWheel is set correctly. While this flag does not automatically block the setSteeringValue method, it is crucial for the internal physics calculations of the btRaycastVehicle.

// Adding Front Wheels (isFrontWheel = true)
vehicle.addWheel(connectionPointCS0, wheelDirectionCS0, wheelAxleCS, suspensionRestLength, wheelRadius, tuning, true);
vehicle.addWheel(connectionPointCS1, wheelDirectionCS0, wheelAxleCS, suspensionRestLength, wheelRadius, tuning, true);

// Adding Rear Wheels (isFrontWheel = false)
vehicle.addWheel(connectionPointCS2, wheelDirectionCS0, wheelAxleCS, suspensionRestLength, wheelRadius, tuning, false);
vehicle.addWheel(connectionPointCS3, wheelDirectionCS0, wheelAxleCS, suspensionRestLength, wheelRadius, tuning, false);

Step 3: Update your steering logic

To disable steering on the rear wheels, only pass your steering input variable to the front wheel indices (0 and 1). For the rear wheel indices (2 and 3), explicitly set the steering value to 0. This locks the rear wheels parallel to the vehicle chassis.

Run this logic inside your application’s physics update loop:

function updateVehiclePhysics(steeringInput) {
    // Apply steering input to the front wheels
    vehicle.setSteeringValue(steeringInput, 0); // Front-Left
    vehicle.setSteeringValue(steeringInput, 1); // Front-Right

    // Disable steering on the rear wheels by forcing the angle to 0
    vehicle.setSteeringValue(0, 2); // Rear-Left
    vehicle.setSteeringValue(0, 3); // Rear-Right
}

By explicitly setting the steering value of indices 2 and 3 to 0 on every frame, the rear wheels will remain rigid, allowing only the front wheels to turn the vehicle.