How do I get local linear velocity?

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

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.

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.

sash-rc | 2021-12-21 07:28

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…

QGames | 2021-12-21 07:35

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

sash-rc | 2021-12-21 16:30

:bust_in_silhouette: Reply From: QGames

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
:bust_in_silhouette: Reply From: TroyM

If v represents the linear speed of a rotating object, r its radius, and ω its angular velocity in units of radians per unit of time, then v = rω.