+1 vote

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?

asked Dec 22, 2017 in Engine by imekon (229 points)

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

1 Answer

+1 vote

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)
answered Dec 24, 2017 by imekon (229 points)

I've reported an issue on this: https://github.com/godotengine/godot/issues/15001

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 How to use this Q&A? before posting your first questions.