0 votes

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 by (16 points)
edited by

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.

2 Answers

0 votes

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)
0 votes

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ω.

by (14 points)
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 Frequently asked questions and 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 [email protected] with your username.