How do I calculate the angle difference?

0 favourites
  • 5 posts
  • Hi!

    I can't get it - please help me.

    I want to calculate the angle between a orientated player and a destination - say the minimal angle the player has to turn to point to the destination point (if he has to turn left 0..-180° or turn right 0..180°).

  • player.angle will give you the angle of the player object

    angle(player.x,player.y,destination.x,destination.y) will give you the angle between the player and the destination.

    facing right the angle will be 0

    facing left the angle will be 180

    the difference between the angles will be abs(player.angle-angle(player.x,player.y,destination.x,destination.y)) and abs(angle(player.x,player.y,destination.x,destination.y)-player.angle)

    one of these two will be bigger than 180, the other one smaller..

  • Thanx LittleStain

    i normalized it to -180°..0°..180° to get the best rotation direction

  • Perhaps this is simpler?

    This will give a signed angle diff between two angles.

    angle(0,0,cos(a-b),sin(a-b))

    It's in the range of -180 to 180. It always picks the shortest distance but it's negative if CCW and positive if CW.

    from:

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Perhaps this is simpler?

    > This will give a signed angle diff between two angles.

    > angle(0,0,cos(a-b),sin(a-b))

    >

    > It's in the range of -180 to 180. It always picks the shortest distance but it's negative if CCW and positive if CW.

    >

    >

    from:

    Yes - this is the most elegant way

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