Implement Wind on Ammo.js Soft Body Flag
This article explains how to implement realistic aerodynamic wind forces on a soft body flag using the Ammo.js physics engine. You will learn how to configure the soft body’s aerodynamic properties, enable the correct simulation model, and apply dynamic wind velocity vectors to create a natural fluttering effect.
1. Enable Aerodynamics in the Soft Body Configuration
By default, Ammo.js soft bodies do not react to air resistance or
wind. To enable this behavior, you must modify the soft body’s
configuration (m_cfg) and set the aerodynamic model.
// Access the configuration of your soft body flag
var sbConfig = softBody.get_m_cfg();
// Set the aerodynamic model
// 2 corresponds to btSoftBody::eAeroModel::V_TwoSided (vertex-two-sided interaction)
sbConfig.set_pI(2); The value 2 represents the two-sided vertex aerodynamic
model, which calculation-wise is ideal for thin surfaces like flags,
ensuring wind forces are applied regardless of which side of the face
the wind hits.
2. Set Lift and Drag Coefficients
Once the aerodynamic model is active, you need to define how much the
flag resists the wind (drag) and how much it is lifted by it (lift). Use
the set_kLF (lift coefficient) and set_kDG
(drag coefficient) methods:
// Set lift coefficient (controls how much the flag rises in the wind)
sbConfig.set_kLF(0.05);
// Set drag coefficient (controls air resistance/flutter intensity)
sbConfig.set_kDG(0.01);Adjust these values to change the material feel of the flag. Higher drag makes the flag heavier and more resistant, while higher lift causes it to flap more aggressively.
3. Apply the Wind Velocity Vector
With aerodynamics configured, you must specify the direction and
strength of the wind. This is achieved by passing a
btVector3 representing wind velocity to the
setWindVelocity method of the soft body.
// Define wind velocity (X, Y, Z coordinates in meters per second)
var windVelocity = new Ammo.btVector3(15, 0, 5);
// Apply the wind to the soft body
softBody.setWindVelocity(windVelocity);4. Create Realistic Fluttering (Dynamic Wind)
Constant wind velocity results in a stiff, unnatural pose. To simulate natural turbulence and gusts, update the wind velocity vector inside your application’s render/animation loop using trigonometric functions or noise.
var clock = new THREE.Clock(); // Assuming Three.js is used for time tracking
var baseWind = new Ammo.btVector3(0, 0, 0);
function updatePhysics() {
var elapsedTime = clock.getElapsedTime();
// Calculate a fluctuating wind force
var windX = 12 + Math.sin(elapsedTime * 2.0) * 4;
var windY = Math.cos(elapsedTime * 1.5) * 2;
var windZ = Math.sin(elapsedTime * 1.0) * 3;
baseWind.setValue(windX, windY, windZ);
softBody.setWindVelocity(baseWind);
// Step the physics world
physicsWorld.stepSimulation(deltaTime, 10);
}This variation in wind speed and direction ensures the soft body flag behaves dynamically, generating the chaotic waves and ripples characteristic of real fabric.