【发布时间】:2011-10-31 11:56:40
【问题描述】:
我有两个向量:heading 和 target。如何通过某些因素将航向转向面对目标?每帧说 10% 什么的。
社区编辑: target 向量在不断变化。
谢谢!
【问题讨论】:
-
这个问题太模糊了。您想以恒定角速率(例如每秒一度)还是以其他方式转动?你的航向幅度有什么意义吗?等
标签: math language-agnostic vector rotation
我有两个向量:heading 和 target。如何通过某些因素将航向转向面对目标?每帧说 10% 什么的。
社区编辑: target 向量在不断变化。
谢谢!
【问题讨论】:
标签: math language-agnostic vector rotation
假设唯一重要的是heading 和targetHeading 的方向,我们将假设所有向量都是标准化的。你也说过你希望这是真的:
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
【讨论】:
使用点积求两个向量之间的角度:
heading . target = |heading|*|target|*cos(theta)
然后每一帧,使用旋转矩阵将heading 旋转0.10*theta。
【讨论】:
a x b 将是与a 和b 正交的某个向量)