Ammojs apply force

I am trying to upgrade to the bjs v5 and the ammo physics seems to behave different. I have a mesh moving in the space. When I apply for example thrust force I want it to be applied always from its relative local back side. In the v4.2 I did that using these simple functions:

var vmult = function(v,q){
    var target =  new BABYLON.Vector3();
    var x = v.x,
        y = v.y,
        z = v.z;
    var qx = q.x,
        qy = q.y,
        qz = q.z,
        qw = q.w;
    // q*v
    var ix =  qw * x + qy * z - qz * y,
    iy =  qw * y + qz * x - qx * z,
    iz =  qw * z + qx * y - qy * x,
    iw = -qx * x - qy * y - qz * z;
    target.x = ix * qw + iw * -qx + iy * -qz - iz * -qy;
    target.y = iy * qw + iw * -qy + iz * -qx - ix * -qz;
    target.z = iz * qw + iw * -qz + ix * -qy - iy * -qx;
    return target;
var vectorToWorldFrame = function(localVector, quaternion){
    var result = new BABYLON.Vector3();
    result = vmult(localVector, quaternion);
    return result;
var pointToWorldFrame = function(localPoint,quaternion, position){
    var result = new BABYLON.Vector3();
    result = vmult(localPoint,quaternion);
    result = result.add(position);
    return result;

var applyLocalForce = function(localForce, localPoint, mesh){
    var worldForce = new BABYLON.Vector3();
    var worldPoint = new BABYLON.Vector3();
    // Transform the force vector to world space
    worldForce = vectorToWorldFrame(localForce, mesh.rotationQuaternion);
    worldPoint = pointToWorldFrame(localPoint, mesh.rotationQuaternion, mesh.getAbsolutePosition());
    mesh.physicsImpostor.applyForce(worldForce, worldPoint);

It worked in v4.2 but its spinning like crazy in the v5. I dont have a playground yet as it is in a larger repo. I will try to create something simple to demonstrate the issue. Maybe there is something obvious to spot?

cc @Cedric

I don’t see anything weird. Maybe something changed in ammojs. To be sure, I need your PG to compare behavior between different versions of bjs.

Thanks, I will put together some simple PG for testing. I didnt update the ammojs lib. There is some mesh hierarchy so maybe there is some issue. Anyway, hopefully with simple PG will be easier to solve.

1 Like

Ok, I think I have a playground that shows the problem. I apply the force form below the box. The physics selected is ammojs. When I run it in 4.2 it behaves as expected which is lifting the box up. When switching to the v5 the box lifts but spins.
ammojs problem PG

I was able to narrow it down a bit more. It seems there is a problem in converting the local into world point by adding absolute mesh position. If you remove that line in the playground it works fine for v5.

    var Pulse = function() {
        box.physicsImpostor.applyForce(forceDirection.scale(forceMagnitude), contactLocalRefPoint);

Also in my code I removed it from the function mentioned int the first comment above and it works fine in v5 as well.

var pointToWorldFrame = function(localPoint,quaternion, position){
    var result = new BABYLON.Vector3();
    result = vmult(localPoint,quaternion);
    //result = result.add(position);
    return result;

It’s on todo list. I’ll take a look later this week.

Thanks @Cedric

It looks like from this test PG, force is applied at local position whereas impulse is applied on world position.

physics apply impulse | Babylon.js Playground (

Edit: Same with Ammo, Oimo and Cannon

I think that applies only to translation. In my code for v5 I still need to convert the rotation (angle of the force) to the world frame.
Is that a feature update for v5 or do we go back to old behaviour?

That’s something I’m checking. As it works the same for 3 engines, I guess it’s been fixed to be that way.

Cool, thanks.

PR ammo relative force computation by CedricGuillemet · Pull Request #11782 · BabylonJS/Babylon.js · GitHub

1 Like