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

in Engine by (17 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

+5 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)
by (21,973 points)
selected by

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 Frequently asked questions and 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 web[email protected] with your username.