I am trying to ease (gradually move) the rotation of an object to an arbitrary position. The angle of rotation is determined by a Virtual Thumbstick class which returns X/Y coordinates between -1 and 1. If there is no movement on the thumbstick, I am rotating back to point to 0, except I am compensating for the angle of the sprite's image.
The problem I am having is that this code will only allow approximately 1.5 rotations (anywhere between -3*PI and 3*PI) instead of continuous rotation. Using Math.Atan2 with the X/Y coords of the thumbsticks, the returned angle is constrained between -PI and PI but allows continuous rotation. Also, if I rotate the object in one direction and release the thumbstick, it will rotate back to top from the direction it came. I want it to rotate back to the top on the shortest route.
if (VirtualThumbsticks.LeftThumbstick.Length() > .2f)
{
double rotateTo = Math.Atan2(VirtualThumbsticks.LeftThumbstick.Y, VirtualThumbsticks.LeftThumbstick.X);
if (rotateTo > Rotation + Math.PI) rotateTo -= (Math.PI * 2);
if (rotateTo < Rotation - Math.PI) rotateTo += (Math.PI * 2);
Rotation += (rotateTo - Rotation) * 0.2;
}
else
{
Rotation += (-1.57079 - Rotation) *0.2;
}
If there are any Flash/ActionScript game developers that know what I'm talking about, please chime in as I can apply that to C#.
Thanks in advance, everyone!
EDIT:
This chunk of code works flawlessly in AS3:
function enterFrameHandler(e:Event):void
{
var curMouseX = Math.round(-(arrow.x - stage.mouseX));//(stage.stageWidth/2)-(stage.mouseX/2);
var curMouseY = Math.round(-(arrow.y - stage.mouseY));//(stage.stageHeight/2)-(stage.mouseY/2);
var angleTo:Number = Math.atan2(curMouseX, -curMouseY) * TO_DEGREES;
if (angleTo > arrow.rotation+180) angleTo -= 360;
if (angleTo < arrow.rotation-180) angleTo += 360;
tf_angle.text = angleTo.toString();
tf_mouseX.text = curMouseX.toString();
tf_mouseY.text = curMouseY.toString();
arrow.rotation += (angleTo - arrow.rotation) * 0.2;
}
I'm beginning to wonder if there is an issue with my types or typecasting that is causing the problem. If anyone has any ideas, your input is greatly appreciated.