How can one apply_impulse in an objects local space?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Löwenhügel
:warning: Old Version Published before Godot 3 was released.

I want a rigidbody to move forward along its own local axis, so when it rotates, it will still go forward from its own point of view. But this should be done using physics, via apply_impulse.

:bust_in_silhouette: Reply From: Zylann

Although physics are executed in world space, you can get the forward vector of a spatial node like this:

var forward = -get_transform().basis.z

Or, if you are unsure:

var forward = -get_global_transform().basis.z

Then you can use it to apply a force.

Note: I wrote a - in front of the Z axis, because the engine uses the OpenGL axis convention, so for example a camera looks at -Z instead of +Z, just FYI

Thanks a lot

Löwenhügel | 2017-08-30 12:48

What about 2D? transform2d doesn’t have basis.

Anutrix | 2018-04-30 10:23

Transform2D is also a mix of basis and translation. You have the up and right vectors right away, with transform2d.x and transform2d.y.

Zylann | 2018-05-01 23:25