How can you apply force or set the velocity of a rigidbidy on local axis?

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

In Godot is it possible to apply force or set the velocity of a rigidbody or a kinematicbody on local axis? If so, how can you do it? Thank you for your time.

:bust_in_silhouette: Reply From: wombatstampede

You need to use the transform of the rigidbody to convert the local axis to a global axis:

var forcemult=20
var direction= Vector3(0,0,-1) #example: direction in -z
var global_direction = global_transform.basis.xform(relOffset)
apply_impulse(Vector3(0,0,0), global_direction * forcemult) #apply to bodies center

I used global_transform because this also directly transforms the axis when the body is child of another body.
If you want to transform a global value back to a local then use global_transform.basis.xform_inv(vector)

(Doesn’t have to be a RigidBody, any Object derived from Spatial has a transform)

Thanks for the great answer. It worked!

MmTtDeveloper | 2019-03-17 18:43