我正在撰寫一個模擬球運動的代碼。我有一個updateBall函式,每100毫秒運行一次,更新球的位置。
我怎樣才能找出到達一個給定的目標坐標所需的最小速度? 下面是相關的代碼,
ball.x = 0;
ball.y = 0;
targetX = 100;
targetY = 200。
摩擦力=0.03。
dx = targetX - ball.x;
dy = targetY - ball.y;
距離 = sqrt(dx*dx dy*dy)。
速度 = ?
//每100ms運行一次
updateBall()
{
ball.x = velocity;
ball.y = velocity;
速度 -= 摩擦力。
}
uj5u.com熱心網友回復:
你將friction分別應用于兩個部分似乎是錯誤的--在這種情況下,球可以垂直停止,但水平移動--看起來很奇怪,不是嗎?
是否值得對速度矢量應用加速度。似乎你有直線運動--所以你可以預先計算兩個分量的系數。
關于需要的速度:
distance = sqrt(dx*dx dy*dy)
v_final = v0 - a * t = 0 所以t = v0 / a
距離 = v0 * t - a * t^2 / 2 代入t,得到
距離 = v0^2 / (2a)
最后是初始速度,以提供移動的距離
v0 = sqrt(2*distance*a)
其中a是與你的friction成比例的加速度,相應的基本間隔dt(100ms)。
摩擦力=a * dt
a = 摩擦力/dt
v0.x = v0 * dx / distance = v0 * coefX
v0.y = v0 * dy / 距離 = v0 * coefY
在每個階段你都要更新v值,并獲得組件
v = v - friction
v.x = v * coefX
v.y = v * coefY
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/319975.html
標籤:
