# 2 Impulses, 1 Cube in space

**URL:** <https://forum.babylonjs.com/t/2-impulses-1-cube-in-space/34448>\
**Category:** Questions\
**Created:** [October 3, 2022, 4:02pm UTC](https://forum.babylonjs.com/t/2-impulses-1-cube-in-space/34448 "2022-10-03T16:02:19Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michael\_Clark](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/michael_clark/32/27152_2.png) [@Michael\_Clark](https://forum.babylonjs.com/u/Michael_Clark)\
**Post date:** [October 3, 2022, 4:02pm UTC](https://forum.babylonjs.com/t/2-impulses-1-cube-in-space/34448/1 "2022-10-03T16:02:20Z")

</div>

_To have a cube maintain it’s position and rotation in space, while adding a rotation around it’s local axis by applying a pair of impulses._

Objects are moving in space with no gravity or friction.  
I want to add rotation, pitch, and yaw to my object by using applyImpulse (like a spaceship would).  
When I do, it is applied relative to world space, however not in the local vector’s direction when the object has previously been impulsed or rotated.

I took your forces example and modified it slightly to show the effect.  
This is the desired effect (shown on the world zero axis so it works, as expected):

> **[Babylon.js Playground](https://playground.babylonjs.com/#RHBQY9%23701)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL 3D scenes

This is what happens whenever the item has been rotated. (I start with a rotation here because it’s easier to discuss, but the outcome is the same from an impulse.) It now rotates in world space, without referencing the local space as desired.

> **[Babylon.js Playground](https://playground.babylonjs.com/#RHBQY9%23702)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL 3D scenes

I considered getDirection, see line 53, but this does worse than nothing:  
direction = box.getDirection(impulseDirection); // uncomment this to see world direction issue

To be clear. The desired outcome is to have the item rotate about it’s own Z axis when the impulses:  
Pulse(BABYLON.Vector3.Up(),impulseMagnitude, BABYLON.Vector3.Left());  
Pulse(BABYLON.Vector3.Down(),impulseMagnitude, BABYLON.Vector3.Right());  
are applied, Regardless of it’s location, velocity, and rotation in world space.

Thank you in advance.

---

<div class="post-metadata">

**Author:** ![Takemura](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/takemura/32/25836_2.png) [@Takemura](https://forum.babylonjs.com/u/Takemura)\
**Post date:** [October 3, 2022, 4:40pm UTC](https://forum.babylonjs.com/t/2-impulses-1-cube-in-space/34448/2 "2022-10-03T16:40:09Z")

</div>

Do you want to set direction-Vector of world space to mesh’s local coordinate system? Then this might be what you are looking for:

```auto
function vecToLocal(vector, mesh){
   var m = mesh.getWorldMatrix();
   var v = BABYLON.Vector3.TransformCoordinates(vector, m);
   return v;
}

```

---

<div class="post-metadata">

**Author:** ![Michael\_Clark](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/michael_clark/32/27152_2.png) [@Michael\_Clark](https://forum.babylonjs.com/u/Michael_Clark)\
**Post date:** [October 3, 2022, 4:46pm UTC](https://forum.babylonjs.com/t/2-impulses-1-cube-in-space/34448/3 "2022-10-03T16:46:42Z")

</div>

That doesn’t seem to quite do it, in fact I’m not sure what that does here.

> **[Babylon.js Playground](https://playground.babylonjs.com/#RHBQY9%23703)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL 3D scenes

I changed my pulse to be

```auto
    var Pulse = function(impulseDirection, impulseMagnitude, contactLocalRefPoint) {
        let direction = impulseDirection;
        var m = box.getWorldMatrix();
        var v = BABYLON.Vector3.TransformCoordinates(direction, m);
        direction = box.getDirection(v);
        box.physicsImpostor.applyImpulse(direction.scale(impulseMagnitude), box.getAbsolutePosition().add(contactLocalRefPoint));
    }

```

---

<div class="post-metadata">

**Author:** ![Takemura](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/takemura/32/25836_2.png) [@Takemura](https://forum.babylonjs.com/u/Takemura)\
**Post date:** [October 3, 2022, 4:53pm UTC](https://forum.babylonjs.com/t/2-impulses-1-cube-in-space/34448/4 "2022-10-03T16:53:23Z")

</div>

Why you use this line?

`direction = box.getDirection(v);`

Shouldn’t it rather be:

`direction = v;`

Or directly set direction instead of using new “v”-variable.

---

<div class="post-metadata">

**Author:** ![Michael\_Clark](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/michael_clark/32/27152_2.png) [@Michael\_Clark](https://forum.babylonjs.com/u/Michael_Clark)\
**Post date:** [October 3, 2022, 5:23pm UTC](https://forum.babylonjs.com/t/2-impulses-1-cube-in-space/34448/5 "2022-10-03T17:23:04Z")

</div>

That was an attempt at fixing your suggestion which is:

```auto
    var Pulse = function(impulseDirection, impulseMagnitude, contactLocalRefPoint) {
        // let direction = impulseDirection;
        var m = box.getWorldMatrix();
        let direction = BABYLON.Vector3.TransformCoordinates(impulseDirection, m);
        box.physicsImpostor.applyImpulse(direction.scale(impulseMagnitude), box.getAbsolutePosition().add(contactLocalRefPoint));
    }

```

Represented here:

> **[Babylon.js Playground](https://playground.babylonjs.com/#RHBQY9%23705)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL 3D scenes

Which does not work.

To be clear, the goal is:  
_To have a cube maintain it’s position and rotation in space, while adding a rotation around it’s local axis by applying a pair of impulses._

I would think this very easy, I’m missing something simple.

---

<div class="post-metadata">

**Author:** ![Takemura](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/takemura/32/25836_2.png) [@Takemura](https://forum.babylonjs.com/u/Takemura)\
**Post date:** [October 3, 2022, 5:55pm UTC](https://forum.babylonjs.com/t/2-impulses-1-cube-in-space/34448/6 "2022-10-03T17:55:05Z")

</div>

Mhh, maybe babylonjs physics guru @Cedric knows why it doesn’t work with initial rotation offset and got a good solution. I also tried rotateByQuaternionToRef, but it did not work either or even box.up-Vector does not give expected results.

The only other solution I know would be to use setAngularVelocity:

> **[Babylon.js Playground](https://playground.babylonjs.com/#T035UZ)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL 3D scenes

---

<div class="post-metadata">

**Author:** ![Blake](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/blake/32/21509_2.png) [@Blake](https://forum.babylonjs.com/u/Blake)\
**Post date:** [October 3, 2022, 6:08pm UTC](https://forum.babylonjs.com/t/2-impulses-1-cube-in-space/34448/7 "2022-10-03T18:08:35Z")

</div>

If you use TransformNormal for the direction and TransformCoordinates for the point then it works as expected?

> **[Babylon.js Playground](https://playground.babylonjs.com/#RHBQY9%23708)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL 3D scenes

---

<div class="post-metadata">

**Author:** ![Michael\_Clark](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/michael_clark/32/27152_2.png) [@Michael\_Clark](https://forum.babylonjs.com/u/Michael_Clark)\
**Post date:** [October 3, 2022, 9:54pm UTC](https://forum.babylonjs.com/t/2-impulses-1-cube-in-space/34448/8 "2022-10-03T21:54:13Z")

</div>

MUCH better. Much much better… however (sorry)…

Works great until we pitch and thrust again, especially after stopping.  
This is a paired down FlightFrame… use wsad and WS… x to stop.

> **[Babylon.js Playground](https://playground.babylonjs.com/#NJRRSA)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL 3D scenes

If you thrust (w) then pitch (W) then stop (x) then thrust, you will immediately see the problem.  
It MUST be something simple, an angle recalculation I’m missing somewhere…

Thanks so far to @Takemura and @Blake … give me a hand if you can please!!!

---

<div class="post-metadata">

**Author:** ![Blake](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/blake/32/21509_2.png) [@Blake](https://forum.babylonjs.com/u/Blake)\
**Post date:** [October 3, 2022, 10:09pm UTC](https://forum.babylonjs.com/t/2-impulses-1-cube-in-space/34448/9 "2022-10-03T22:09:51Z")

</div>

TransformCoordinates already accounts for the absolute position so you don’t need to add it… And then it’s working better? 🙂

> **[Babylon.js Playground](https://playground.babylonjs.com/#NJRRSA%232)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL 3D scenes

---

<div class="post-metadata">

**Author:** ![Michael\_Clark](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/michael_clark/32/27152_2.png) [@Michael\_Clark](https://forum.babylonjs.com/u/Michael_Clark)\
**Post date:** [October 3, 2022, 10:12pm UTC](https://forum.babylonjs.com/t/2-impulses-1-cube-in-space/34448/10 "2022-10-03T22:12:01Z")

</div>

There it is. Go @Blake !!! TYVVM. More fun coming soon!
