gpt4 book ai didi

c# - 使用 atan2 进行延迟旋转

转载 作者:太空宇宙 更新时间:2023-11-03 15:19:44 27 4
gpt4 key购买 nike

我一直在尝试使用正确的模拟摇杆创建延迟旋转效果。下面的代码根据右模拟摇杆的输入获取角度,并使物体稳定地靠近。由于 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, 2·pi] 范围:

public static double CorrectedAtan2(double y, double x)
{
var angle = Math.Atan2(y, x);
return angle < 0 ? angle + 2 * Math.PI : angle;
}

关于c# - 使用 atan2 进行延迟旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37748396/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com