Configure Ammo.js Vehicle Suspension

This article explains how to configure suspension stiffness and damping compression for a vehicle in the ammo.js physics engine. By adjusting these parameters on the vehicle’s wheel objects, you can control how the vehicle reacts to bumps, jumps, and weight distribution, allowing you to simulate everything from stiff racing cars to bouncy off-road trucks.

In ammo.js (a JavaScript port of the Bullet Physics engine), vehicle suspension is managed on a per-wheel basis using the btWheelInfo class. Once you have created your btRaycastVehicle and added wheels to it, you must retrieve each wheel’s configuration object to modify its suspension properties.

The following steps and code demonstrate how to access the wheels and configure both stiffness and damping compression.

Step 1: Access the Wheel Information

To modify suspension properties, loop through all the wheels added to your vehicle and retrieve their configuration reference using vehicle.getWheelInfo(index).

const numWheels = vehicle.getNumWheels();

for (let i = 0; i < numWheels; i++) {
    const wheel = vehicle.getWheelInfo(i);
    
    // Configuration properties will be applied here
}

Step 2: Configure Suspension Stiffness

Suspension stiffness determines how rigid the springs are. A higher value prevents the vehicle from bottoming out but makes the ride rougher, while a lower value makes the vehicle floaty and bouncy.

const stiffness = 30.0; // Medium stiffness
wheel.set_m_suspensionStiffness(stiffness);

Step 3: Configure Damping Compression

Damping compression controls the resistance when the suspension spring is being compressed (e.g., when the vehicle hits a bump or lands a jump). Without damping, the vehicle will bounce indefinitely.

const dampingCompression = 4.0; // Prevents fast, uncontrolled compression
wheel.set_m_wheelsDampingCompression(dampingCompression);

Step 4: Configure Damping Relaxation (Rebound)

For the suspension to behave realistically, you must also set the damping relaxation (rebound). This controls how quickly the spring expands back to its original length after being compressed. The relaxation value should always be higher than the compression value.

const dampingRelaxation = 6.0; // Prevents the vehicle from bouncing up too quickly
wheel.set_m_wheelsDampingRelaxation(dampingRelaxation);

Complete Implementation Example

Below is a complete helper function to apply these suspension settings to an existing ammo.js vehicle:

function configureVehicleSuspension(vehicle, stiffness, compression, relaxation) {
    const numWheels = vehicle.getNumWheels();
    
    for (let i = 0; i < numWheels; i++) {
        const wheel = vehicle.getWheelInfo(i);
        
        wheel.set_m_suspensionStiffness(stiffness);
        wheel.set_m_wheelsDampingCompression(compression);
        wheel.set_m_wheelsDampingRelaxation(relaxation);
    }
}

// Example usage:
// configureVehicleSuspension(myVehicle, 25.0, 3.5, 5.0);