如何找到两个角之间的最小过渡。

huangapple go评论66阅读模式
英文:

How to find smallest Transition between two angles

问题

我有一个Java程序,在其中一个对象需要旋转到另一个对象。为此,我计算了两个向量的角度,然后计算它们之间的差异。现在我需要找出它们之间的最小旋转过渡。例如,如果第一个向量的角度是 -179°,而到第二个向量的角度是 179°,最小的旋转角度将是 2°,而旋转不需要是整个圆周旋转:-179° - 2° = -181° = 179°。
我希望解释没有问题。
那么我如何可以在Java中轻松计算这个呢?
我在思维上有一点障碍,无法想出解决方法。

编辑:角度以弧度表示

angle_Knight = atan2(knightNode.getLocalTranslation().y, knightNode.getLocalTranslation().x);
angle_follow = atan2(followKnight.getLocalTranslation().y, followKnight.getLocalTranslation().x);
angle = angle_follow - angle_Knight;

谢谢

英文:

I have a Java program in which one object needs to rotate to another object. For this I calculate the angles of the two Vectors and then the difference between them. Now I need to find out which will be the smallest transition between them. For example if the angle of the first vector -179° and the angle to the second vector is 179° the smallest rotation angle would be 2° and the rotation don't needs to be a whole round rotation : -179° -2° = - 181° = 179.
I hope the explanation is ok.
So how can I easy calculate this in Java?
I have a little bloackade in my head and can't figure this out.

Edit: the angles are in radians

angle_Knight = atan2(knightNode.getLocalTranslation().y,knightNode.getLocalTranslation().x));
angle_follow = atan2(followKnight.getLocalTranslation().y,followKnight.getLocalTranslation().x));
angle = angle_follow - angle_Knight;

Thanks

答案1

得分: 1

你离目标很近了。当得到的角度在 (-π, π) 范围之外时,只需添加或减去一个完整的圆周角(360度或2π弧度)。

角度 = 跟随角度 - 骑士角度;
如果 (角度 < -Math.PI) {
    角度 = 角度 + 2*Math.PI;
} else 如果 (角度 > Math.PI) {
    角度 = 角度 - 2*Math.PI;
}
英文:

You're almost there. Just add or subtract a full turn (360 degrees or 2pi radians) when the angle resulting angle is outside the (-pi, pi) range.

angle = angle_follow - angle_Knight;
if (angle &lt; -Math.PI) {
    angle = angle + 2*Math.PI;
} else if (angle &gt; Math.PI) {
    angle = angle - 2*Math.PI;
}

huangapple
  • 本文由 发表于 2020年4月7日 22:44:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/61082720.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定