通過從B中減去A創建一個從A點到B點的2D向量。通過將X和Y向量除以向量的長度進行歸一化。乘以速度乘以X和Y分量。將其乘以時間片(即20/1000達20毫秒)。將這些X和Y值添加到您的位置。
實施例:
A is (2, 5) B is (1, 8)
Speed is 2 (move 2 units in one second)
Time slice is 20 milliseconds since last update
C will be (-1, 3)
Normalize C by dividing by distance (sqrt(-1 * -1 + 3 * 3) = 3.1622777)
Normalized is (-0.316277, 0.9486833) (to move 1 unit that direction)
Multiply by speed (2): (-0.632554, 1.8973666)
For movement this time period, multiply by 20 and divide by 1000:
(-0.01265108, 0.037947332), move sprite this far this frame
我總是使用代數爲沿着在2D的線移動到比使用TRIG更容易找到。這只是我個人的偏好。 – corsiKa 2011-03-01 20:35:55
你不必考慮負速度;它與在相反方向(alpha +180º)行進相同。 – Justin 2011-03-01 20:36:37