【问题标题】:Find minimum velocity needed to reach a given coordinate?找到到达给定坐标所需的最小速度?
【发布时间】:2021-09-10 19:58:48
【问题描述】:

我正在编写一个模拟球运动的代码。我有一个 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;
}

【问题讨论】:

    标签: math game-physics


    【解决方案1】:

    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 是与friction 成比例的加速度,相应于基本间隔dt (100 ms)。

    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
    

    【讨论】:

    • 感谢您的回答。是的,对 x 和 y 使用两个单独的速度是错误的,我现在已经编辑为使用单个速度。据我了解, v0 = sqrt(2*distance*a) 是公式,而 v0 的值是我需要的,对吗?我如何获得加速度的值,a=?
    • 是的,你想要 v0。我添加了加速度 a 的公式(注意它是比摩擦更通用的参数 - 如果你改变时间间隔,移动保持不变)
    猜你喜欢
    • 1970-01-01
    • 2020-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-28
    相关资源
    最近更新 更多