What's the correct way to apply force at fixed global direction, like gravity

Hi,

As title says, I’m trying to apply a force that always point to one direction in global space, no matter how the object rotate. Gravity is a good example, regardless the object’s rotation, it’s always pointing down.

My attempt is converting global direction into local direction then use it in applyForce function:

const globalDirection = new Vector3(0, 1, 0)
const localDirection = Vector3.TransformNormal(globalDirection, targetObject.getWorldMatrix().clone().invert())

Is this the right way? If I want to fight off the gravity with this implementation, the object hovers and stay still if the up force is applied at center of mass, but if applied slightly different from center of mass, the object will start to rotate then making unexpected movements.

I made this PG here as an example, where red box hovers and green box moves unexpectedly: https://playground.babylonjs.com/#MFIPKH#9

I’m not sure exactly what you’re wanting but the applyForce function takes a global direction vector, so there’s no need to transform it. See https://playground.babylonjs.com/#MFIPKH#10

As for the green box rotation, I don’t see a way to apply force to an off-center point without it rotating, but like the red box, you can just apply the force to the absolute position so it won’t rotate.

2 Likes

Ah thank you very much :heart:, it makes sense now. I thought the applyForce direction was in local space

1 Like