Godot 4 equivelent to Basis.xform_inv()?

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

I have used Godot for over a year at this point. It’s my favorite engine and I’m eagerly waiting for Godot 4 like most!

My issue is that I’m trying to get linear_velocity in a RigidBody3D, and get the local z component of that. Without it being local, the z component will not represent the forward force. I just want to get the forward force of the rigidbody. In Godot 3.X, we’d use transform.basis.xform_inv(linear_velocty), but it appears this method has been removed entirely from Basis in Godot 4. Here is my code:

extends RigidBody3D

var real_speed : float = 0

func set_inputs(vertical : float, horizontal : float):

func _physics_process(delta):
    linear_velocity = transform.basis.z * -20

func _integrate_forces(state):
    real_speed = linear_velocity.z

Is there a method that I don’t know about in the new API? Thanks in advance!

Edit: There seems to be a Transform2D method for basis_xform() and basis_xform_inv(). I wonder why a 3D version doesn’t exist…

Second Edit: Hmm. In the Transform3D header file, there is an implementation of this method. So why isn’t it in GDScript?

:bust_in_silhouette: Reply From: waimus

According to stable/3.4(current) documentation of xform, it’s:

Returns a vector transformed (multiplied) by the matrix.

So I try to multiply the linear_velocity with transform.basis then I got the desired effect.

var current_velocity : Vector3 = linear_velocity * transform.basis

# Use Z to get the forward speed
real_speed = current_velocity.z

Then I think, xform_inv() is the same as multiplying with transform.basis.transposed() as the documentation of xform_inv() says “vector transformed (multiplied) by the transposed basis matrix.”

But in this case I got the desired effect without transposing it.