我一直在尝试使用正确的模拟摇杆创建延迟旋转效果。下面的代码根据右侧模拟摇杆的输入获取角度,并使对象稳定地靠近。由于 atan2 是 -pi 到 pi 的范围,因此不断变化的旋转总是倾向于通过 0 弧度而不是 pi。有没有办法使角度朝相反的方向移动?
private void Angle()
{
//Angle to go to
RotationReference = -(float)(Math.Atan2(YR, XR));
//Adds on top of rotation to steadily bring it closer
//to the direction the analog stick is facing
Rotation += (RotationReference - Rotation) * Seconds *15;
Console.WriteLine(RotationReference);
}
编辑:
我尝试使用 InBetween 建议的方法,导致 2pi 到 0 之间的转换出现问题。这让我尝试了别的东西。我不知道为什么它不起作用。
private void Angle()
{
//Angle to go to
RotationReference = -(float)(CorrectedAtan2(YR, XR));
//Adds on top of rotation to steadily bring it closer
//to the direction the analog stick is facing
if (Math.Abs(RotationReference - Rotation) > Math.PI)
Rotation += ((float)(RotationReference + Math.PI * 2) - Rotation) * Seconds * 15;
else Rotation += (RotationReference - Rotation) * Seconds *15;
Console.WriteLine(RotationReference);
}
public static double CorrectedAtan2(double y, double x)
{
var angle = Math.Atan2(y, x);
return angle < 0 ? angle + 2 * Math.PI: angle;
}
这背后的想法是,如果您需要行进超过 180 度,您将使行进角度大于 360 度。这应该消除反转方向的需要。