0 votes

Hi everyone,

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

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

in Engine by (1,032 points)

2 Answers

0 votes
Best answer

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)
by (1,032 points)
edited by
0 votes

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

by (3,216 points)

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.

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:

I know, this is why, if you look at my addforcelocal 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.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.