I have this funcion to rotate the character in the direction of my mouse, but for some reason when my mouse is pointing down my charactet points up and when my mouse is pointing up my character points down. How can I solve it?
public static float RotateTowards(Vector2 Pos, Vector2 focus)
{
float h, sineTheta, angle;
if (Pos.Y - focus.Y != 0)
{
h = (float)Math.Sqrt(Math.Pow(Pos.X - focus.X, 2) + Math.Pow(Pos.Y - focus.Y, 2));
sineTheta = (float)(Math.Abs(Pos.Y - focus.Y) / h); //* ((item.Pos.Y-focus.Y)/(Math.Abs(item.Pos.Y-focus.Y))));
}
else
{
h = Pos.X - focus.X;
sineTheta = 0;
}
angle = (float)Math.Asin(sineTheta);
// Drawing diagonial lines here.
//Quadrant 2
if (Pos.X - focus.X > 0 && Pos.Y - focus.Y > 0)
{
angle = (float)(Math.PI * 3 / 2 + angle);
}
//Quadrant 3
else if (Pos.X - focus.X > 0 && Pos.Y - focus.Y < 0)
{
angle = (float)(Math.PI * 3 / 2 - angle);
}
//Quadrant 1
else if (Pos.X - focus.X < 0 && Pos.Y - focus.Y > 0)
{
angle = (float)(Math.PI / 2 - angle);
}
else if (Pos.X - focus.X < 0 && Pos.Y - focus.Y < 0)
{
angle = (float)(Math.PI / 2 + angle);
}
else if (Pos.X - focus.X > 0 && Pos.Y - focus.Y == 0)
{
angle = (float)Math.PI * 3 / 2;
}
else if (Pos.X - focus.X < 0 && Pos.Y - focus.Y == 0)
{
angle = (float)Math.PI / 2;
}
else if (Pos.X - focus.X == 0 && Pos.Y - focus.Y > 0)
{
angle = (float)0;
}
else if (Pos.X - focus.X == 0 && Pos.Y - focus.Y < 0)
{
angle = (float)Math.PI;
}
return angle;
}
This is how I am using the function
SetRotation(-Globals.RotateTowards(GetPosition(), new Vector2(Globals.mouseControl.newMousePos.X, Globals.mouseControl.newMousePos.Y) - _offset));