Hello i'm developing a game in c# monogame and having issues with resetting the camera back to behind the player.
When the player holds left click he's able to orbit the camera around his character. When he moves forward the camera resets back behind him slowly. The rotation should decide whether it should rotate left or right around the player to behind the player (the shortest path). At the moment it sometimes orbits the long way around the player and worst of all the currentY will be stuck in-between 1 and -1 and keep moving back and forth really quick like its stuck.
double targetY; //this is the radians Y our camera is going to slowly rotate to
double currentY; //this is our current camera radians Y
float rotateSpeed = 5; //this value lets us slow down how fast we rotate towards targetY
double direction = Math.Cos(targetY) * Math.Sin(currentY) - Math.Cos(currentY) * Math.Sin(targetY);
double distance = Math.Min(currentY - targetY, (MathHelper.Pi * 2) - currentY - targetY);
if (direction > 0)
{
//rotating left towards targetY
currentY -= distance / rotateSpeed;
}
else
{
//rotating right towards targetY
currentY += distance / rotateSpeed;
}
I figure its a problem with not using Math.PI to figure out if it should rotate left or right, but I haven't been able to solve it.
Thank you