旋转插值 [英] Rotation Interpolation
问题描述
注意:为了简单起见,我将用度数提出这个问题,弧度,度数,不同的零轴承,问题本质上是相同的.
NB: I'll present this question in degrees purely for simplicity, radians, degrees, different zero-bearing, the problem is essentially the same.
有人对旋转插值背后的代码有任何想法吗?给定一个线性插值函数:Lerp(from, to, amount),其中 amount 为 0...1,它按数量返回 from 和 to 之间的值.如何将相同的函数应用于 0 到 360 度之间的旋转插值?鉴于度数不应返回 0 和 360 度之外.
Does anyone have any ideas on the code behind rotational interpolation? Given a linear interpolation function: Lerp(from, to, amount), where amount is 0...1 which returns a value between from and to, by amount. How could I apply this same function to a rotational interpolation between 0 and 360 degrees? Given that degrees should not be returned outside 0 and 360.
给定度数的单位圆:
从 = 45 到 = 315,算法应该采用最短路径到达角度,即它应该经过零,到 360,然后到 315 - 而不是一直绕到 90、180、270 到 315.
where from = 45 and to = 315, the algorithm should take the shortest path to the angle, i.e. it should go through zero, to 360 and then to 315 - and not all the way round 90, 180, 270 to 315.
有没有很好的方法来实现这一点?还是只是 if() 块的可怕混乱?我错过了一些很好理解的标准方法吗?任何帮助将不胜感激.
Is there a nice way to achieve this? Or is it going to just be a horrid mess of if() blocks? Am I missing some well understood standard way of doing this? Any help would be appreciated.
推荐答案
我知道这已经有 2 年历史了,但我最近一直在寻找同样的问题,如果没有发布 ifs,我看不到一个优雅的解决方案来了,就这样吧:
I know this is 2 years old, but I've recently been looking around for the same problem and I don't see an elegant solution without ifs posted in here, so here it goes:
shortest_angle=((((end - start) % 360) + 540) % 360) - 180;
return shortest_angle * amount;
就是这样
ps:当然,% 表示取模,shortest_angle 是保存整个插值角度的变量
ps: of course, % is meaning modulo and shortest_angle is the variable that holds the whole interpolation angle
这篇关于旋转插值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!