0 votes

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?

Screenshot of tranform_3d.h in Godot source code

Godot version 4.0.dev (daily nightly build from Calinou)
in Engine by (123 points)
edited by

1 Answer

0 votes

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.

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.