修改乘法计算以使用增量时间

发布于 2024-08-29 08:46:13 字数 383 浏览 3 评论 0原文

function(deltaTime) {
  x = x * FACTOR; // FACTOR = 0.9
}

该函数在游戏循环中调用。首先假设它以恒定的 30 FPS 运行,因此 deltaTime 始终为 1/30。

现在游戏发生了变化,所以 deltaTime 不再总是 1/30,而是变得可变。如何将 deltaTime 合并到 x 的计算中以保持“每秒效果”相同?


那么呢

function(deltaTime) {
  x += (target - x) * FACTOR; // FACTOR = 0.2
}
function(deltaTime) {
  x = x * FACTOR; // FACTOR = 0.9
}

This function is called in a game loop. First assume that it's running at a constant 30 FPS, so deltaTime is always 1/30.

Now the game is changed so deltaTime isn't always 1/30 but becomes variable. How can I incorporate deltaTime in the calculation of x to keep the "effect per second" the same?


And what about

function(deltaTime) {
  x += (target - x) * FACTOR; // FACTOR = 0.2
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

时光磨忆 2024-09-05 08:46:13
x = x * Math.pow(0.9, deltaTime*30)

编辑

对于您的新更新:

x = (x-target) * Math.pow(1-FACTOR, deltaTime*30) + target;

为了展示我是如何到达那里的:

设x0为初始值,xn为n/30秒后的值。还令 T=目标,F=因子。然后:

x1 = x0 + (T-x0)F = (1-F)x0 + TF
x2 = (1-F)x1 + TF = (1-F)^2 * x0 + (1-F)TF + TF

继续 x3,x4,... 将显示:

xn = (1-F)^n * x0 + TF * (1 + (1-F) + (1-F)^2 + ... + (1-F)^(n-1))

现在将公式代入等比数列之和将得到上面的结果。这实际上只证明了整数 n 的结果,但它应该适用于所有值。

x = x * Math.pow(0.9, deltaTime*30)

Edit

For your new update:

x = (x-target) * Math.pow(1-FACTOR, deltaTime*30) + target;

To show how I got there:

Let x0 be the initial value, and xn be the value after n/30 seconds. Also let T=target, F=factor. Then:

x1 = x0 + (T-x0)F = (1-F)x0 + TF
x2 = (1-F)x1 + TF = (1-F)^2 * x0 + (1-F)TF + TF

Continuing with x3,x4,... will show:

xn = (1-F)^n * x0 + TF * (1 + (1-F) + (1-F)^2 + ... + (1-F)^(n-1))

Now substituting the formula for the sum of a geometric sequence will give the result above. This really only proves the result for integer n, but it should work for all values.

那片花海 2024-09-05 08:46:13

x = x * powf(0.9, deltaTime / (1.0f / 30.0f))

x = x * powf(0.9, deltaTime / (1.0f / 30.0f))

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文