How to use add_force within local coordinates

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By BraindeadBZH

Hi everyone,

I’m trying to simulate a vehicle with a thruster in the back, so not centered at (0, 0, 0), but the add_force function drives me nuts. If I look at this sentence, from apply_impulse:

The position uses the rotation of the global coordinate system, but is centered at the object’s origin

I have tried a lot of thing, but I cannot wrap my around on how to make my force vector take into account the rotation of the vehicle.

If someone can help? Thanks

:bust_in_silhouette: Reply From: BraindeadBZH

Here what I came up with, where self is a RigidBody:

func add_force_local(force: Vector3, pos: Vector3):
    pos_local = self.transform.basis.xform(pos)
    force_local = self.transform.basis.xform(force)
    self.add_force(force_local, pos_local)
:bust_in_silhouette: Reply From: wombatstampede

You get the local axis vectors in global space from transform.basis or global_transform.basis. So a relative position 2 local units back on the z-axis in global space is global_transform.basis.z * -2.0

The thing is when applying a force, you don’t want to apply the translation, just global rotation, as the the force position is already in local coordinate.

BraindeadBZH | 2019-06-09 11:32

Local coordinates don’t help you with apply_force or apply_impulse.
If you don’t want to apply to apply force/impulse to the center (=offset (0,0,0)) then you have to specify the offset vector3 in global coordinate system.

‘centered at the object’s origin’ means that you don’t pass a global coordinate but an offset to the objects origin in global space.

Same is with the direction vector of the force/impulse this has to in global space (like i.e. the velocity vector).

I put something up for reading for this topic:

wombatstampede | 2019-06-09 13:44

I know, this is why, if you look at my add_force_local function (in the pinned answer), I rotate the force and the offset so they are in global coordinate relative to the body center.

Thanks for the link, I’ll bookmark that.

BraindeadBZH | 2019-06-09 14:41