0 votes

I have been following the tutorial here:

Flappy Bird

However, it is done in 2.1 and I'm using 3.0. Although I'm a beginner I like trying to teach myself why certain errors are present. Unfortunately, I'm a little stumped on getrot() in 3.0. I have discovered getrotation() but it seems to be cumulative on key input as opposed to get_rot(), which appears to set a maximal value.

Can anyone shed any light?

in Engine by (35 points)

4 Answers

0 votes
Best answer

In 3.0, you just read or set the rotation property:

mynode.rotation = PI
mynode.rotation_degrees = 90
var rot = mynode.rotation

See http://docs.godotengine.org/en/latest/classes/class_node2d.html

by (698 points)
selected by

Thanks for your response. I implemented the code using 'rotation', however it rotates to a value of 0.5xxxx, when my desired value is 30 degrees. It is also still cumulative upon multiple presses of the input key.

extends RigidBody2D

func _ready():
    set_physics_process(true)
    set_process_input(true)

func _physics_process(delta):
    pass

func flap():
    set_angular_velocity(-3)
    rotation = deg2rad(30)
    print(rotation)

func _input(event):
    if event.is_action_pressed("flap"):
        flap()

First of all, 0.523599 radians == 30 degrees, so that's not the problem.

The problem is you can't just set the rotation (or position, etc.) of a RigidBody2D, because a rigid body is controlled by the physics engine. Because of this you might even see an instant during which your body snaps to 30 deg, but then immediately goes back to rotating based on its angular velocity.

You can't control a physics body directly, you can only apply forces or impulses to it, and then the physics engine takes over.

To quote the documentation for RigidBody2D:

Note: You should not change a RigidBody2D’s position or linear_velocity every frame or even very often. If you need to directly affect the body’s state, use _integrate_forces, which allows you to directly access the physics state.

I've not looked at the tutorial you're referencing, but this is definitely not recommended use of RigidBody2D.

Okay, thanks again. I think I will have to dial it back and do some simpler tutorials to get a better understanding of fundamental mechanics.

Appreciate you taking your time.

0 votes

There have been a number of changes between 2.1 and 3.0:

  • Member variables

You can access node properties directly:

# in 2.1
print(get_rot())
# in 3.0
print(rotation)

# in 2.1
set_rot(get_rot() + angle)
# in 3.0
rotation += angle
  • Orientation

In 3.0, setting rotation to 0 points to the right (along the positive x-axis), not upwards (along the negative y-axis) and a positive rotation is clockwise. This may be the source of your problem.

I'm not sure what you mean by "cumulative on key input" as rotation doesn't have anything directly to do with input. Perhaps if you share the snippet of code you are using?

by (20,397 points)

Thanks for taking the time to reply. My code snippet is in the comment to the reply above.

Good day, so I have actually tried this tutorial just now and here is my code below. I think what he meant by cumulative, is the fact that if you keep on pressing space bar to to rotate the bird anti clockwise it will be set to -0.523599 but if you keep on pressing space bar further and print the rotation() you can clearly see that it keeps on subtracting until it goes positive the rads which means it has moved just more then 180 degrees. Then the bird goes from 30 degrees to 210 degrees. He finds it weird because in his code he did state that if the bird is rotated more then 30 degrees he should stay at 30 degrees and should keep on subtracting since its new rotation is set to -0.523599. I'm also wondering why it keeps on adding especially after you've set a limit to it.

extends RigidBody2D

func _ready():
    set_linear_velocity(Vector2(50, get_linear_velocity().y))
    pass

func _integrate_forces(delta):
    print(get_rotation())
    if rad2deg(get_rotation()) < -30:
        set_rotation(deg2rad(-30))

    if get_linear_velocity().y > 0:
        set_angular_velocity(1.5)
    pass

func flap():
    set_linear_velocity(Vector2(get_linear_velocity().x, -150))
    set_angular_velocity(-3)
    pass


func _input(event):
    if event.is_action_pressed("ui_spacebar"):
        flap()
    pass

Going further than "-0.5.."
Rotates 180 degrees when going positive

0 votes

Regarding the rotation being "cumulative", are you possibly referring to the difference in rotate and set_rotation.

rotate(x)
# Will rotate object x radians relative to its CURRENT rotation

# versus

set_rotation(x)
# Will set its rotation relative to its ORIGINAL rotation
by (310 points)
+1 vote

=== Solution ==
Okay so I actually took a look at the next part of the tutorial and to fix this actually you need to add setangularvelocity(0), this will prevent it to go further.

This should work

extends RigidBody2D

func _ready():
    set_linear_velocity(Vector2(50, get_linear_velocity().y))
    pass

func _integrate_forces(delta):
    print(get_rotation())
    if rad2deg(get_rotation()) < -30:
        set_rotation(deg2rad(-30))
        set_angular_velocity(0)

    if get_linear_velocity().y > 0:
        set_angular_velocity(1.5)
    pass

func flap():
    set_linear_velocity(Vector2(get_linear_velocity().x, -150))
    set_angular_velocity(-3)
    pass


func _input(event):
    if event.is_action_pressed("ui_spacebar"):
        flap()
    pass
by (23 points)
edited by

Thanks VivAz.
its worked for me set_angular_velocity(0)

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.
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 webmaster@godotengine.org with your username.