0 votes

I am making a game where the player is in control of a rocketship. The rocket is a RigidBody2D node that I apply torque and a central impulse to in order to move. However, the thrust vector should align with the rocket engine (instead of just propelling the ship directly upward) so that you can steer the rocket. Basically, how can I make the impulse apply in the direction the rocket is facing?
Thanks in advance! :)

in Engine by (67 points)

1 Answer

+2 votes

The apply_central_impulse() method takes a vector that's used as the direction of the applied force. So, really, you just want that vector to represent the direction your ship is facing. You can get that from the ship's rotation property.

So, assuming your ship is facing to the right at it's zero orientation, something like this should work (wired to a button in this case):

func _on_ButtonForward_pressed():
    var impulse_strength = 100
    var angle = $RigidBody2D.rotation
    $RigidBody2D.apply_central_impulse(Vector2(cos(angle), sin(angle)) * impuse_strength)

Just season to taste...

by (19,268 points)

Holy cow this worked perfectly, thank you so much! I had some janky crap with like 3 different constants I was trying to tweak but it never worked. I'm taking trig this week so hopefully this method will make sense to me soon. For now, it works great and that's what matters :)

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.