首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >利用atan2实现延迟旋转

利用atan2实现延迟旋转
EN

Stack Overflow用户
提问于 2016-06-10 12:32:43
回答 2查看 309关注 0票数 2

我一直试图创造一个延迟的旋转效果与正确的模拟棒。下面的代码根据正确的模拟棒的输入获取角度,并使物体稳定地接近。由于atan2是-pi到pi的一个范围,变化的旋转总是倾向于通过0半径,而不是π。有没有办法使角度向相反的方向移动?

代码语言:javascript
复制
    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之间的转换问题。这让我尝试了其他的东西。我不知道为什么不起作用。

代码语言:javascript
复制
    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度。这将消除扭转方向的需要。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-06-11 00:58:15

经过一些试验,我终于成功了。感谢InBetween的CorrectedAtan2方法。for循环用于遍历需要添加pi以避免不稳定转换的所有实例。

代码语言:javascript
复制
    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;
    }
票数 0
EN

Stack Overflow用户

发布于 2016-06-10 13:06:28

只需修正角度以适应[0, 2·pi]范围:

代码语言:javascript
复制
public static double CorrectedAtan2(double y, double x)
{
    var angle = Math.Atan2(y, x);
    return angle < 0 ? angle + 2 * Math.PI : angle;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37748396

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档