0 votes

I'm trying to simulate a ship in 3D space using a RigidBody, but I'm having a hard time figuring it out. My goal is to have all movement come as the result of physics calculations involving forces that have their source in the ship itself in the form of various thrusters. I need movement and rotation in all three axes as the result of player input, calculated as physics, so pure translations don't cut it.

The code below seems to work for applying thrust forces to the RigidBody right up until it rotates, at which point the forces applied no longer correspond to the orientation of the object in 3D space:

func _physics_process(delta):
    var thrust_total = Vector3(0, 0, 0)
    if Input.is_action_pressed("ui_up"):
        thrust_total += thrust_forward
    if Input.is_action_pressed("ui_down"):
        thrust_total += thrust_backward
    if Input.is_action_pressed("ui_left"):
        thrust_total += thrust_left
    if Input.is_action_pressed("ui_right"):
        thrust_total += thrust_right
    if Input.is_action_pressed("ui_page_up"):
        thrust_total += thrust_up
    if Input.is_action_pressed("ui_page_down"):
        thrust_total += thrust_down
    apply_impulse(Vector3(0, 0, 0), thrust_total * delta)

How do I translate between local forces (for example, the player applies the ship's main rear thrusters in an attempt to get the ship moving forward relative to it's own orientation in space) and global forces (the way Godot seems to currently handle 3D forces)?

in Engine by (12 points)

1 Answer

+1 vote
#Get your basis from your transform..
var basis = global_transform.basis
#Then transform your thrust total..
thrust_total = basis.xform(thrust_total)
#And finally use your thrust total as usual.
apply_impulse(Vector3(), thrust_total * delta)
by (3,910 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]e.org with your username.