I wasn't sure if this was a better place than Stack Overflow to ask this question. On a basic level, I have an algorithm I've written in Java to compute a number, but I feel like there's a better way to write it without a loop that just involves use of
floor and things like that.
So here's my algorithm:
float startAngle = getRotation(); double dAngle = (startAngle < 0 ? -Math.PI : Math.PI); double endAngle = (float)((destination % 2 == 0) ? 0 : dAngle/2); while (Math.abs(endAngle - startAngle) > Math.PI/2) endAngle += dAngle;
In simpler terms it does the following:
- Get the current angle of the object
- Get a delta angle based on whether the object has been rotated left or right (further left or further right)
- Define an angle to rotate the object to, based on whether the destination should be rotated parallel or perpendicular to 0
- As long as the difference between the end angle and start angle is greater than 90 degrees, change the end angle by the delta from step 2.
The reason behind all this is I've got playing cards on the screen at random angles, which any of four players can take (where destination 0 is the bottom of the screen, 1 is left, 2 is top, and 3 is right). I want to rotate the playing cards the shortest distance to face that player as they take them. This algorithm works correctly, but like I said before, it seems like there should be a way to do this without a loop. Is there?