Shortest rotation angle?

0 favourites
  • 11 posts
  • If I have an object facing in a direction that needs to turn to match the angle of another object, is there a formula to determine the shortest type of rotation (clockwise vs. counterclockwise)?

    This is NOT the same thing as determining the shortest way to turn to FACE another object, which is the formula I ended up creating. I need my object to MATCH the angle of another object.

    If you're curious about the facing formula, here it is:

    f

    (Player.X - cos(Target.Angle)) * sin(Player.Angle) > (Player.Y - sin(Target.Angle)) * cos(Player.Angle)

    ...then Clockwise is the shortest.

    Otherwise, counterclockwise.

    For those who don't understand what the above formula does, it basically draws a line behind the player and then determines which side of that line the target is on. However, the above formula made sense to me because I am able to think of it in cartesian coordinates and not polar ones.

    I'm trying to think of a way to determine the shortest way to match the angle of another object, and I can't seem to find a cartesian way to think of it.

    EDIT: Current line of thinking is:

    bs() = absolute value

    If

    abs(Player.Angle - Target.Angle) > abs(Player.Angle + 180 - Target.Angle)

    ... then Counterclockwise

    However, this fails if Target.Angle = 0, so this can't be it, even though it seems to work for a lot of cases.

  • if abs(Target.Angle-Player.Angle)>180   .... CCW

    else                                    .... CW

    i think this could work

  • If Player.Angle = 10 and Target.Angle = 0, then the absolute value is 10, and by your formula the shortest direction would be clockwise.

    However, that is incorrect because clockwise means turning 350 degrees.

  • Every Tick
       -> Sprite: Rotate ROTATION_SPEED * dt degrees toward target.Angle

    isn't enough for what you need? (:

  • Did you even read the first post? I know how to rotate an object.

    However, there's a difference between rotating 10 degrees counterclockwise and 350 degrees clockwise. That's what I'm trying to solve.

  • In mathematics degrees increased in CCW. But in Construct 2 degrees increased in CW. I wrote the formula thinking in general mathematical way. Simply you can change it like this.

    if abs(Target.Angle-Player.Angle)>180   .... CW

    else   ... CCW

  • If Target.Angle = 350 and Player.Angle = 10, your formula suggests going clockwise, or 340 degrees. The correct answer is to go counterclockwise 20 degrees.

  • Sorry if I offended you with my answer Excal, but I tend to try to understand why the question was asked and come up with something simple just in case.

    Here is my answer to the question "is there a formula to determine the shortest type of rotation (clockwise vs. counterclockwise)?"

    rotationType.capx

    There might be a complex formula for that, but I would do it this way. It's simple to understand and debug. And the function themselves could be used for other stuff in your project.

    (also sine and cosine is known to be a slow operation)

  • if (360 + Target.Angle - Player.Angle) % 360 > 180   .... CCW
    else                                                 .... CW

    Should work for any valid angles.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Angle(x1,y1,x2,y2) will return either positive or negative 0-180 depending on the order of objects x, and y's used.

    So basically it doesn't care about the shortest route, just the order.

    I guess in theory you could use that as well to get the shortest angle by testing + or -.

  • if (360 + Target.Angle - Player.Angle) % 360 > 180   .... CCW
    else                                                 .... CW

    Should work for any valid angles.

    Thank you!

    Yann, you didn't offend me, I'm just extraordinarily harsh when I get replies that seem to 'probe' and be less related to answering the question.

    I think this might actually be a good add to the list of topics on the FAQ, but maybe better worded there because "Shortest rotation angle" can be kind of misleading.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)