修改乘法计算以使用增量时间
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
编辑
对于您的新更新:
为了展示我是如何到达那里的:
设x0为初始值,xn为n/30秒后的值。还令 T=目标,F=因子。然后:
继续 x3,x4,... 将显示:
现在将公式代入等比数列之和将得到上面的结果。这实际上只证明了整数 n 的结果,但它应该适用于所有值。
Edit
For your new update:
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:
Continuing with x3,x4,... will show:
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.x = x * powf(0.9, deltaTime / (1.0f / 30.0f))
x = x * powf(0.9, deltaTime / (1.0f / 30.0f))