Wednesday, September 7, 2016

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



Answer



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.


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...