我一直试图使用正确的模拟摇杆创建延迟旋转效果。 下面的代码采用基于右模拟杆输入的角度,使对象稳定地靠近。 由于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度以上。这样就不需要改变方向了。
答案 0 :(得分:0)
只需更正适合[0, 2·pi]
范围的角度:
public static double CorrectedAtan2(double y, double x)
{
var angle = Math.Atan2(y, x);
return angle < 0 ? angle + 2 * Math.PI : angle;
}
答案 1 :(得分:0)
经过一些实验,我设法让它发挥作用。 感谢InBetween为CorrectedAtan2方法。 for循环用于遍历所有需要添加pi的实例,以避免震动过渡。
private float Angle(float y, float x)
{
//Angle to go to
RotationReference = -(float)(CorrectedAtan2(y, x));
for (int i = 0; i < 60; i++)
{
if (Math.Abs(RotationReference - Rotation) > Math.PI)
RotationReference = -(float)(CorrectedAtan2(y, x) +
(Math.PI * 2 * i));
}
//Adds on top of rotation to steadily bring it closer
//to the direction the analog stick is facing
return Rotation += (RotationReference - Rotation) * Seconds * 15;
}
public static double CorrectedAtan2(double y, double x)
{
var angle = Math.Atan2(y, x);
return angle < 0 ? angle + 2 * Math.PI: angle;
}