Why does my 2d ship wobble?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By imekon
:warning: Old Version Published before Godot 3 was released.

I’ve created a sample project here:

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?

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

rustyStriker | 2017-11-03 16:27

add_force is even worse than apply_impulse. The ship goes into a mad spin.

imekon | 2017-11-03 16:47

:bust_in_silhouette: Reply From: mollusca

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

apply_impulse(Vector2(), Vector2(0, -ACCELERATION).rotated(get_rot()))