旋转一个矢量面对另一个但慢慢地?

时间:2011-08-21 06:40:11

标签: math language-agnostic vector rotation

我有两个向量:headingtarget。如何通过某种因素转向标题?每帧或类似物说10%。

社区编辑: target向量不断变化。

谢谢!

2 个答案:

答案 0 :(得分:3)

使用点积找到两个向量之间的角度:

heading . target = |heading|*|target|*cos(theta)

然后每一帧,使用旋转矩阵将heading旋转0.10*theta

答案 1 :(得分:0)

假设唯一重要的是headingtargetHeading的方向,我们将假设所有向量都已归一化。你还说你希望这是真的:

dheadingDegrees/dt = angle(targetHeading,heading) degrees/sec in the direction of targetHeading

(至少我是如何解释它的,而不是“每帧接近10%但从未到达目的地”)

要获得确切的答案,您需要整合和一些数学。如果你想模拟它并得到一个精确的答案,你可能想要将它与“帧”分离,并根据所需的准确度模拟它每秒100个间隔。

因此:

every time interval dt:
 target = getCurrentTarget()
 rotationSpeed = angleBetween(target,currentHeading)/(1second)
 heading = {rotate heading by dt*rotationSpeed radians towards target}
            ^-------- for how to do this, see below ----------------^

to rotate a vector v1 to v2 from time t=0 to t=1, with constant angular velocity:
 v1normalized = normalized(v1)
 v2perpNormalized = normalized(v2 - v2*v1normalized)
 animated = cos(t*pi/2)*v1normalized + sin(t*pi/2)*v2perpNormalized