找到到达给定坐标所需的最小速度?

Find minimum velocity needed to reach a given coordinate?

我正在编写球运动的模拟代码。我有一个 updateBall 函数,它每 100 毫秒运行一次以更新球的位置。

如何找出到达给定目标坐标所需的最小速度? 下面是相关代码,

ball.x = 0;
ball.y = 0;

targetX = 100;
targetY = 200;

friction = 0.03;

dx = targetX - ball.x;
dy = targetY - ball.y;
distance = sqrt(dx*dx + dy*dy);
velocity = ?;

// runs every 100ms
updateBall()
{
  ball.x += velocity;
  ball.y += velocity;

  velocity -= friction;
}

您将 friction 分别应用于两个组件似乎是错误的 - 在这种情况下,球可以垂直停止但水平移动 - 看起来很奇怪,不是吗?

值得将加速度应用于速度矢量。似乎你有直线移动 - 所以你可以预先计算两个组件的系数。

关于所需速度:

distance = sqrt(dx*dx + dy*dy)
v_final = v0 - a * t = 0        so   t = v0 / a
distance = v0 * t - a * t^2 / 2      substitute t and get
distance = v0^2 / (2a)        
  and finally initial velocity to provide moving at distance
v0 = sqrt(2*distance*a)

其中 a 是与基本间隔 dt(100 毫秒)相应的与您的 friction 成正比的加速度。

friction = a * dt
a = friction / dt

v0.x = v0 * dx / distance = v0 * coefX
v0.y = v0 * dy / distance = v0 * coefY

在每个阶段更新 v 值并获取组件

v = v - friction
v.x = v * coefX
v.y = v * coefY