2 Impulses, 1 Cube in space

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):

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.

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.

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:

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

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

I changed my pulse to be

    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));
    }

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.

That was an attempt at fixing your suggestion which is:

    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:

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.

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:

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

1 Like

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.

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!!!

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

1 Like

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