# How do I calculate the angle difference?

0 favourites
• 5 posts
• Hi!

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

• ## Try Construct 3

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

Construct 3 users don't see these ads
• 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:

• 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

• 5 posts