【问题标题】:Damping velocity with deltaTime?用 deltaTime 阻尼速度?
【发布时间】:2012-12-23 17:53:31
【问题描述】:

我正在尝试使用“阻尼”属性逐渐降低物体的速度。之后,我想使用速度移动对象的位置。它看起来像这样:

velocity.x *= velocity.damping;
velocity.y *= velocity.damping;
x += velocity.x;
y += velocity.y;

没有比这更简单的了,它工作正常,但这是我的问题:我正在使用一个 deltaTime 变量,它包含我的游戏循环的最后一次更新的时间量(以秒为单位)拿。应用速度很容易:

x += velocity.x * deltaTime;
y += velocity.y * deltaTime;

但是,当我乘以阻尼属性时,如何计算 deltaTime?我的想法是,找到 x 或 y 中的位移并将 deltaTime 乘以该位移,如下所示:

velocity.x += (velocity.x * velocity.damping - velocity.x) * deltaTime;
velocity.y += (velocity.y * velocity.damping - velocity.y) * deltaTime;

事实证明这是行不通的。我真的不明白为什么,但是当我测试它时,我不断得到不同的结果。如果我只是忽略阻尼或将其设置为 1.0,一切正常,所以问题一定出在最后两行。

【问题讨论】:

  • 为什么要减去最后一段代码中的速度?
  • (velocity.x * velocity.damping) 是新的velocity.x。我从中减去原始的velocity.x,以获得差异。

标签: actionscript-3 math game-physics


【解决方案1】:
velocity.x += (velocity.x * velocity.damping - velocity.x) * deltaTime;

这意味着你有一个恒定的加速度,而不是阻尼。

velocity.x * (1 - velocity.damping)

是您在一个时间单位内从当前值减少速度的量。它与当前速度成正比,因此在下一个时间单位中,您将使用阻尼因子将速度减少较小的量。但是与deltaTime 相乘,减去相同的数量,从所有deltaTime 时间单位的起始值计算得出。

让我们假设阻尼因子为0.9,因此您在每个时间单位中将速度减少十分之一。如果使用线性公式(乘以deltaTime),在 10 个时间单位后,您的速度将变为 0,在 11 个时间单位后,它会切换方向。如果你逐步进行,从v_0 = 1 的初始速度开始,你会得到

v_1 = v_0*0.9 = 0.9
v_2 = v_1*0.9 = 0.81
v_3 = v_2*0.9 = 0.729
v_4 = v_3*0.9 = 0.6561

等,速度下降较慢。如果你展开v_4 的公式,你会得到

v_4 = v_3*0.9 = v_2*0.9*0.9 = v_1*0.9*0.9*0.9 = v_0*0.9*0.9*0.9*0.9 = v_0 * 0.9^4

所以,概括起来,你看到公式应该是

velocity.x *= Math.pow(velocity.damping, deltaTime);

(假设 Action Script 与 ECMA Script 在调用函数方面没有区别。)

当然velocity.y 也是如此。

【讨论】:

  • 感谢您的解释。完全有道理! :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多