【问题标题】:Rotation Interpolation Over Time随时间的旋转插值
【发布时间】:2017-02-15 12:22:50
【问题描述】:

为了简单起见,我将使用欧拉角和度数。

假设我的角度是 45 度。我想随着时间的推移将角度更改为 315。这应该采取最短路径来获得目标角度。这种最短路径上方的情况将涉及经过 0 度到达 315。我已经有了下面函数的基础。但是,这不适用于角度。

public static final float approach(float current, float goal, float delta) {

    final float difference = goal - current;

    if (difference > delta) return current + delta;
    if (difference < -delta) return current - delta;

    return goal;

}

这里有一张图片可以让问题看起来更清楚。 https://drive.google.com/file/d/0B4AZGzr3HML0cThORExNMHFEYlE/view

【问题讨论】:

    标签: java angle euler-angles


    【解决方案1】:

    我想从头开始,以确保我理解您的要求。
    所以对于

     approach() 
    函数参数“current”表示当前角度。参数“goal”表示所需的角度。参数“delta”表示角度必须达到“目标”的时间(假设秒)。

    现在,要计算到该角度的最短路径,您可以简单地执行以下操作:

    if(current + (360 - goal) <= 180) // If goal is 180° away it will rotate to the right automatically.
       // ... Rotate to the right
    else
       // ... Rotate to the left
    

    我们可以测试它是否真的有效,因为如果我们采用您要求的值(当前 = 45,目标 = 315)并执行上面的计算,我们应该向右转。

    45 + (360 - 315) <= 180
    45 + 45 <= 180
    90 <= 180, true.
    We turn it right.
    

    现在只需根据 delta 计算旋转量。这个相当简单。
    我将“totalAngle”声明为一个变量,以跟踪当前角度和目标角度之间的差异。我将在 if/else 代码块中分配这个变量。我还将声明一个布尔值来帮助我跟踪需要旋转的方向。

    boolean rotateRight;
    float totalAngle;
    
    if(current + (360 - goal) <= 180) {
       totalAngle = current + (360 - goal);
       rotateRight = true;
    }
    else {
       totalAngle = goal - current;
       rotateRight = false;
    }
    

    现在假设它每秒旋转一次,我将取 totalAngle 并将其除以传递的秒数(也称为“delta”参数)。然后我修改“current”参数,并返回它。

    current -= rotateRight ? (totalAngle / delta) : -(total / delta);
    

    结束函数应该是这样的:

    public static float approach(float current, float goal, float delta) {
       boolean rotateRight;
       float totalAngle;
    
       if(current + (360 - goal) <= 180) {
          totalAngle = current + (360 - goal);
          rotateRight = true;
       }
       else {
          totalAngle = goal - current;
          rotateRight = false;
       }
    
       current -= rotateRight ? (totalAngle / delta) : -(totalAngle / delta);
       return current;
    }
    

    编辑:如果最短路径实际上是最长的,假设“当前 - (360 - 目标)

    【讨论】:

    • 谢谢!这在大多数情况下都有效!抱歉,我没有把方法()函数说得很清楚。函数中的 delta 参数实际上是我想要向目标角度旋转的度数。但是,您的实现应该很容易为此进行调整。
    • 没问题!很高兴我能帮忙:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-09
    • 2016-10-01
    相关资源
    最近更新 更多