[V3.0-beta2] How to make apply_impulse work with rotation

: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.

The following used to work with V2.0 but no longer in V3.0:

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

It does the acceleration OK but the rotation seems to be locked straight up. I can no longer rotate and point where I want to go.

Here’s the full code:

extends RigidBody2D

const ROTATE_SPEED = 250
const ACCELERATION = 5

func _physics_process(delta):
    if Input.is_action_pressed("ui_left"):
        rotate(-deg2rad(delta * ROTATE_SPEED))
	
    if Input.is_action_pressed("ui_right"):
        rotate(deg2rad(delta * ROTATE_SPEED))

    if Input.is_action_pressed("ui_up"):
        apply_impulse(Vector2(), Vector2(0, -ACCELERATION).rotated(rotation))

If I don’t press UP, the LEFT and RIGHT will rotate the ship as expected. However, press UP and it locks going straight up, jiggling slightly if I press LEFT or RIGHT.

How do I make this work in V3.0?

It looks as though apply_impulse is resetting rotation. I changed the code to this:

if Input.is_action_pressed("ui_up"):
	print("Ship rotation: " + str(rotation))
	apply_impulse(Vector2(), Vector2(0, ACCELERATION).rotated(rotation))
	print("Ship rotation: " + str(rotation))

I get this printout:

Ship rotation: -0.727221
Ship rotation: -0.727221
Ship rotation: 0
Ship rotation: 0
Ship rotation: 0
Ship rotation: 0
Ship rotation: 0
Ship rotation: 0
Ship rotation: 0
Ship rotation: 0
Ship rotation: 0
Ship rotation: 0
Ship rotation: 0
Ship rotation: 0

imekon | 2017-12-24 09:51

:bust_in_silhouette: Reply From: imekon

I think I’ve found a bug in V3.0

By storing the angle elsewhere and jamming it into roation, I get what I want:

func _physics_process(delta):
    if Input.is_action_pressed("ui_left"):
        angle -= delta * ROTATE_SPEED

    if Input.is_action_pressed("ui_right"):
        angle += delta * ROTATE_SPEED

    if Input.is_action_pressed("ui_up"):
        apply_impulse(Vector2(), Vector2(0, -ACCELERATION).rotated(deg2rad(angle)))

    // At this point rotation has been set to zero,
    // so set it to the angle I've computed
    rotation = deg2rad(angle)

I’ve reported an issue on this: apply_impulse resets rotation · Issue #15001 · godotengine/godot · GitHub

imekon | 2017-12-24 10:09