我一直试图创造一个延迟的旋转效果与正确的模拟棒。下面的代码根据正确的模拟棒的输入获取角度,并使物体稳定地接近。由于atan2是-pi到pi的一个范围,变化的旋转总是倾向于通过0半径,而不是π。有没有办法使角度向相反的方向移动?
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度。这将消除扭转方向的需要。
发布于 2016-06-11 00:58:15
经过一些试验,我终于成功了。感谢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;
}发布于 2016-06-10 13:06:28
只需修正角度以适应[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;
}https://stackoverflow.com/questions/37748396
复制相似问题