turn in the right direction

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

if the current degrees of rotation are, for example, 90 and I need to rotate to 0 degrees of rotation, I need the object to rotate to the left, because this is faster than if it rotated to the right. How to determine which direction it is spinning in order to reach the desired degree measure as quickly as possible?

:bust_in_silhouette: Reply From: exuin

So what you’re asking is more of a math problem. I’m not really good at visualizing stuff like this, but maybe an answer here could help. I don’t think you need to know the direction the object is currently spinning in order to find the fastest path there.

:bust_in_silhouette: Reply From: Magso

There’s a few ways of doing this but the easiest way for a single axis is to check if the end rotation is above or below the starting rotation by 180 degrees and add or subtracting 360 degrees.

if wanted_angle > rotation_degrees+180:
    wanted_angle -= 360
elif wanted_angle < rotation_degrees-180:
    wanted_angle += 360
:bust_in_silhouette: Reply From: ArthurER

the answer really depends on application one way to calculate whether you need a positive or negative rotation direction is

if self.get_angle_to(target.global_position) != 0:
   turn = abs(self.get_angle_to(target.global_position)) / self.get_angle_to(target.global_position)

the abs() value divided by the actual value will result in -1 if the angle is negative or 1 if the angle is positive. you have to test if the angle is zero to avoid divide by zero error.

:bust_in_silhouette: Reply From: Serafij

There is a lerp_angle() function.

It will do what You are asking for.