I'm coding simple airplane physics, and I need to get the linear velocity of the RigidBody in a local space, in other words, relative to self. (So that I know the velocity on each individual local axis.)

I've tried the to_local() function, but this doesn't work as intended, probably because it is meant to be used for position and not direction.

I know that for converting it from local to global space, one multiplies the vector with transform.basis; but how do I go the other way around?

It looks like that nobody has ever asked this question, so here I'm asking it now.

EDIT: My phrasing seems to be a bit confusing, because "a velocity relative to self" doesn't exist. I mean that I need to get a rotated version of the velocity vector, so that the axes of it are identical with the axes of the RigidBody, but the global direction and magnitude remain the same.

Godot version 3.4
in Engine
edited

velocity of the RigidBody in a local space, in other words, relative to self.

Excuse me, but "velocity relative to self" is not applicable term, by definition.
Probably you have meant some other situation, but you should properly describe your task.

I see... Yes, you're right. I meant the direction to be relative to self.
So basically just a rotated version of the global linear velocity.
Let me edit the original question then...

Normally they use `transform.basis.xform` or `xform_inv` to convert rotations.

I have found the solution! Maybe there is some simpler way, but here is my code:

``````var b = transform.basis
var v_len = linear_velocity.length()
var v_nor = linear_velocity.normalized()

var vel : Vector3
vel.x = b.x.dot(v_nor) * v_len
vel.y = b.y.dot(v_nor) * v_len
vel.z = b.z.dot(v_nor) * v_len
``````
by (16 points)