# rotating an object from sourceAngle to destAngle, both 0-359, clockwise or counter clockwise?

I've got a game object i need to rotate. It's current angle is player.rotation, the destination is targetAngle, both in degress, 0 to 359. I've got a function named rotateDirection(float sourceAngle, float destAngle) that needs to return -1 or 1 depending on whether the shortest way to reach the destination angle would be clockwise or counterclockwise.

For example - for sourceAngle 300 and destAngle 0 the function would return 1 for clockwise.

The solution is most probably going to leave me looking silly, and i usually don't like just asking for code, but my brain is completely broken so please help me with this one :)

-
possible duplicate of Calculating angle between 2 vectors – user744 May 4 '11 at 5:58
Also effectively a duplicate of gamedev.stackexchange.com/questions/4467/…, which was a question about the same thing with some particular hardware restrictions. – user744 May 4 '11 at 5:59

This should do:

``````(((source-dest+360) mod 360)>180)?1:-1
``````

The +360 is only necessary for languages with a fuzzy `mod` function. Note that most languages implement fuzzy `mod`.

-
hey, thanks for the answer. It seems to be returning -1 for source=3 dst = 38, which is an error... – Zaky German May 3 '11 at 23:38
Zaky: It returns 1 for source=3, dest=38. It reduces to 325>180 which is true, so it returns 1. – Olhovsky May 3 '11 at 23:47
@Olhovsky My bad! Thanks alot sirs, it works! – Zaky German May 4 '11 at 0:22
this does not work for me ! sorry for giving you a +1 – Vishnu Jan 9 '12 at 11:11
@Vish No hard feelings. The code is fine, but of course there are countless ways of incorporating it into a whole in a way that does not produce the desired result. – aaaaaaaaaaaa Jan 9 '12 at 14:24