3
votes

I have an object with an x,y,width,height, centre of rotation for X and Y (originX, originY) and have successfully calculated the angle between this centre point and the touch point using

touchAngle = Math.toDegrees(Math.atan2(deltaY, deltaX));

and this works very well.

I am now trying to rotate the object towards this touch point by simply checking if the angle is larger than its current angle, add some rotation and if it's smaller then remove some rotation. I know that this is wrong as when the touch point crosses the 0/360 border the object begins to rotate the wrong way, even if it means doing a very large rotation.

What would be an elegant solution to make it rotate the shortest distance to the touch point?

(Using Java, specifically Libgdx)

Thanks

1
What's a "touch Point"? And is it a good touch or a bad touch? - Andrew Thompson
It's just the point where the mouse/finger is clicked/touched. It's always a good touch and the angle between this point and the the objects centre is all correct and checked - LiamJPeters
OK - I've had my fun. ;) Hope you get a good answer. :) - Andrew Thompson

1 Answers

3
votes

I've done a bunch of these calculations before (very different application, tho). Just take the difference and if it's negative add 2pi and then if it's greater than 2pi subtract 2pi. Do that for a-b and then for b-a. The lesser value will be the way to go.