Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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 :)

share|improve this question
    
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

1 Answer 1

up vote 3 down vote accepted

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.

share|improve this answer
    
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
2  
@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. –  eBusiness Jan 9 '12 at 14:24

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.