# Ammojs apply force

**URL:** <https://forum.babylonjs.com/t/ammojs-apply-force/26492>\
**Category:** Questions\
**Created:** [January 5, 2022, 9:53pm UTC](https://forum.babylonjs.com/t/ammojs-apply-force/26492 "2022-01-05T21:53:27Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![kri100s](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/kri100s/32/21430_2.png) [@kri100s](https://forum.babylonjs.com/u/kri100s)\
**Post date:** [January 5, 2022, 9:53pm UTC](https://forum.babylonjs.com/t/ammojs-apply-force/26492/1 "2022-01-05T21:53:27Z")

</div>

Hello,  
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:

```auto
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?

---

<div class="post-metadata">

**Author:** ![Deltakosh](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/deltakosh/32/26635_2.png) [@Deltakosh](https://forum.babylonjs.com/u/Deltakosh)\
**Post date:** [January 5, 2022, 10:55pm UTC](https://forum.babylonjs.com/t/ammojs-apply-force/26492/2 "2022-01-05T22:55:03Z")

</div>

cc @Cedric

---

<div class="post-metadata">

**Author:** ![Cedric](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/cedric/32/2742_2.png) [@Cedric](https://forum.babylonjs.com/u/Cedric)\
**Post date:** [January 6, 2022, 8:54am UTC](https://forum.babylonjs.com/t/ammojs-apply-force/26492/3 "2022-01-06T08:54:44Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![kri100s](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/kri100s/32/21430_2.png) [@kri100s](https://forum.babylonjs.com/u/kri100s)\
**Post date:** [January 6, 2022, 10:43am UTC](https://forum.babylonjs.com/t/ammojs-apply-force/26492/4 "2022-01-06T10:43:05Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![kri100s](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/kri100s/32/21430_2.png) [@kri100s](https://forum.babylonjs.com/u/kri100s)\
**Post date:** [January 7, 2022, 9:00pm UTC](https://forum.babylonjs.com/t/ammojs-apply-force/26492/5 "2022-01-07T21:00:31Z")

</div>

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](https://playground.babylonjs.com/#RHBQY9#571)

---

<div class="post-metadata">

**Author:** ![sebavan](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sebavan/32/57_2.png) [@sebavan](https://forum.babylonjs.com/u/sebavan)\
**Post date:** [January 7, 2022, 9:23pm UTC](https://forum.babylonjs.com/t/ammojs-apply-force/26492/6 "2022-01-07T21:23:50Z")

</div>



---

<div class="post-metadata">

**Author:** ![kri100s](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/kri100s/32/21430_2.png) [@kri100s](https://forum.babylonjs.com/u/kri100s)\
**Post date:** [January 7, 2022, 10:43pm UTC](https://forum.babylonjs.com/t/ammojs-apply-force/26492/7 "2022-01-07T22:43:40Z")

</div>

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.

```auto
    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.

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

```

---

<div class="post-metadata">

**Author:** ![Cedric](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/cedric/32/2742_2.png) [@Cedric](https://forum.babylonjs.com/u/Cedric)\
**Post date:** [January 12, 2022, 10:34am UTC](https://forum.babylonjs.com/t/ammojs-apply-force/26492/8 "2022-01-12T10:34:18Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![kri100s](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/kri100s/32/21430_2.png) [@kri100s](https://forum.babylonjs.com/u/kri100s)\
**Post date:** [January 12, 2022, 10:50am UTC](https://forum.babylonjs.com/t/ammojs-apply-force/26492/9 "2022-01-12T10:50:39Z")

</div>

Thanks @Cedric

---

<div class="post-metadata">

**Author:** ![Cedric](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/cedric/32/2742_2.png) [@Cedric](https://forum.babylonjs.com/u/Cedric)\
**Post date:** [January 14, 2022, 1:23pm UTC](https://forum.babylonjs.com/t/ammojs-apply-force/26492/10 "2022-01-14T13:23:41Z")

</div>

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 (babylonjs.com)](https://playground.babylonjs.com/#7BNSQ8#1)

Edit: Same with Ammo, Oimo and Cannon

---

<div class="post-metadata">

**Author:** ![kri100s](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/kri100s/32/21430_2.png) [@kri100s](https://forum.babylonjs.com/u/kri100s)\
**Post date:** [January 14, 2022, 1:33pm UTC](https://forum.babylonjs.com/t/ammojs-apply-force/26492/11 "2022-01-14T13:33:16Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Cedric](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/cedric/32/2742_2.png) [@Cedric](https://forum.babylonjs.com/u/Cedric)\
**Post date:** [January 14, 2022, 1:34pm UTC](https://forum.babylonjs.com/t/ammojs-apply-force/26492/12 "2022-01-14T13:34:41Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![kri100s](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/kri100s/32/21430_2.png) [@kri100s](https://forum.babylonjs.com/u/kri100s)\
**Post date:** [January 14, 2022, 1:35pm UTC](https://forum.babylonjs.com/t/ammojs-apply-force/26492/13 "2022-01-14T13:35:41Z")

</div>

Cool, thanks.

---

<div class="post-metadata">

**Author:** ![Cedric](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/cedric/32/2742_2.png) [@Cedric](https://forum.babylonjs.com/u/Cedric)\
**Post date:** [January 14, 2022, 3:19pm UTC](https://forum.babylonjs.com/t/ammojs-apply-force/26492/14 "2022-01-14T15:19:44Z")

</div>

PR [ammo relative force computation by CedricGuillemet · Pull Request #11782 · BabylonJS/Babylon.js · GitHub](https://github.com/BabylonJS/Babylon.js/pull/11782)

---

<div class="post-metadata">

**Author:** ![Cedric](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/cedric/32/2742_2.png) [@Cedric](https://forum.babylonjs.com/u/Cedric)\
**Post date:** [April 14, 2022, 9:08am UTC](https://forum.babylonjs.com/t/ammojs-apply-force/26492/15 "2022-04-14T09:08:15Z")

</div>


