2D rotation value (in degrees and radians) keeps on adding up

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By AsuosOnurb

I’m using .get_rotation() to get the rotation value in radians of a Node2D node.
The problem I’m having is that after every rotation of the node, it’s rotation value keeps on going up/down.

Basically, I need a way to get the rotation to range between [0, 2PI] only, and not their multiples.

Maybe I need to get the rotation and then apply some math functions to get the [0, 2PI] equivalent but I don’t know how…

Can you include some information about how you’re rotating the object?

kidscancode | 2018-09-09 16:13

This is part that handles the rotation of the object (WeaponContainer = weapon): Imgur: The magic of the Internet

Im trying to make the weapon face the direction of the player’s movement. That part seems to work fine. The problem is in the if statements. I need some way to transform the current_angle variable into an angle 0º <= x <= 360º.

Thanks for your time!

AsuosOnurb | 2018-09-09 17:07

:bust_in_silhouette: Reply From: kidscancode

BTW, you can access the rotation and rotation_degrees properties directly, you don’t have to use the get_ method. As a bonus, autocomplete works with the properties, but not the getter/setter.

To keep your rotation in the range, you can use the modulus operator, which is fmod() for floating points:

rotation_degrees = fmod(rotation_degrees, 360)


rotation = fmod(rotation, 2 * PI)

Thanks for the help man, works like a charm!

AsuosOnurb | 2018-09-09 17:28

Or shorter version fmod(rotation,TAU) as TAU = 2 * PI. :slight_smile:

kozaluss | 2018-09-10 07:50