This is in GD4.0, working in 2D

I want to turn a physics object (or really anything with rotation) towards a certain angle, and because angle-related variables such as rotation and the output of Vector2.angle() can be both positive and negative, as well as the fact that TAU is closer to 0 than TAU/2, it's very painful to try to figure out which way I should turn for the fastest way to reach a desired orientation.

By the way, I know about look_at, but it doesn't help when trying to turn a physics object by applying a torque (rotational force), because it doesn't tell me which way I should be applying that torque.

So, if given a desired direction and current rotation of an object, how can you find out which direction the torque should be applied? Presumably the function looks like this:

func get_turn_direction(current_angle, desired_angle) -> int: #output is -1, 1 or 0
   ...
  • xyz replied to this.

    If you convert the angle to between +180 and -180, and then add 360 (Euler angles) then it should be simple to find the shortest distance just by subtracting and taking the absolute value (save the sign first).

      cybereality convert the angle to between +180 and -180, and then add 360 (Euler angles)

      Do you mean convert the angle to the range [0, 360] degrees?

        GE100 Don't work with angles directly, use orientation vectors instead, and Vector2::angle_to()

        sign(Vector2.from_angle(current_angle).angle_to(Vector2.from_angle(desired_angle)))

        DaveTheCoder Do you mean convert the angle to the range [0, 360] degrees?

        No. If you use 0 - 360 then you have to deal with weirdness around the divider. For example 359 - 1 = 358 but it's really 2 degrees.

        So using the range [180,540] degrees avoids that issue?