0 votes

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

asked Sep 9, 2018 in Engine by AsuosOnurb (15 points)

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

This is part that handles the rotation of the object (WeaponContainer = weapon): https://imgur.com/w7pqhw7

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!

1 Answer

+2 votes
Best answer

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)

or

rotation = fmod(rotation, 2 * PI)
answered Sep 9, 2018 by kidscancode (17,702 points)
selected Sep 9, 2018 by AsuosOnurb

Thanks for the help man, works like a charm!

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

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.