+1 vote

I've created a sample project here:

https://github.com/imekon/shipthruster

The script I use to move the ship (like in an asteroids game) wobbles when you hit thrust (up):

extends RigidBody2D

const ROTATE_SPEED = 250
const ACCELERATION = 5

var size

func _ready():
    size = get_viewport().get_rect().size
    set_fixed_process(true)

func _fixed_process(delta):
    if (Input.is_action_pressed("ui_spin_left")):
        rotate(deg2rad(delta * ROTATE_SPEED))

    if (Input.is_action_pressed("ui_spin_right")):
        rotate(-deg2rad(delta * ROTATE_SPEED))

    if (Input.is_action_pressed("ui_thrust")):
        apply_impulse(get_pos(), Vector2(0, -ACCELERATION).rotated(get_rot()))

    var pos = get_pos()
    if (pos.x < 0):
        pos.x = size.x - 1
    if (pos.x > size.x):
    pos.x = 1
    if (pos.y < 0):
    pos.y = size.y - 1
    if (pos.y > size.y):
        pos.y = 1
    set_pos(pos)

I'm guessing it's my use of apply_impulse?

in Engine by (259 points)

not understanding much in rigidBody2D but try replacing apply_impulse with apply_force or whatever

addforce is even worse than applyimpulse. The ship goes into a mad spin.

1 Answer

+2 votes
Best answer

You're applying an offset to the impulse which causes the wobbling / spinning. Try:

apply_impulse(Vector2(), Vector2(0, -ACCELERATION).rotated(get_rot()))
by (1,560 points)
selected by
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.