开发者

Rotating one vector to face another but slowly?

I have t开发者_开发百科wo vectors: heading and target. How can I turn heading to face target by some factor? Say 10% every frame or something.

community edit: The target vector is constantly changing.

Thanks!


Find the angle between the two vectors using the dot product:

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

Then every frame, rotate heading by 0.10*theta using the rotation matrix.


Assuming that the only thing that matters is the direction of heading and targetHeading, we will assume all vectors are normalized. You have also said that you would like this to be true:

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

(At least that's how I interpret it, as opposed to "gets closer by 10% every frame but never reaches the destination")

To get an exact answer you'd need integration and some math. If you want to simulate it and get a precise answer, you probably want to decouple this from the "frames" and simulate it maybe 100 intervals per second, depending on the required accuracy.

Thus:

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜