get_rot() in Godot 3.0

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

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 get_rot() in 3.0. I have discovered get_rotation() 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?

:bust_in_silhouette: Reply From: timoschwarzer

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

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

See Node2D — Godot Engine (latest) documentation in English

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()

Icarus | 2017-12-04 07:20

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.

kidscancode | 2017-12-04 08:00

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.

Icarus | 2017-12-04 08:14

:bust_in_silhouette: Reply From: kidscancode

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?

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

Icarus | 2017-12-04 07:44

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
   


VivAZ | 2018-06-21 15:19

:bust_in_silhouette: Reply From: Michael Paul

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
:bust_in_silhouette: Reply From: VivAZ

=== Solution ==
Okay so I actually took a look at the next part of the tutorial and to fix this actually you need to add set_angular_velocity(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
	

Thanks VivAz.
its worked for me set_angular_velocity(0)

kuldeep Mourya | 2020-04-10 12:22