帧率独立加速
所以我明白,为了获得与帧速率无关的运动,我必须将基本速度乘以(1000/delta)。它适用于代表速度的值,但是当我尝试使用加速度变量(例如重力)时,它不起作用,它甚至与帧速率无关。
加速度变量是否有不同的公式,或者我忘记了什么?
示例:
var Multiplier:Number = (GetDeltaTime()) / (1000 / 30);
jumpVelocity = -21 * Multiplier //works
gravity = 1.5 * Multiplier //dosn't work
这在每帧开始时调用
编辑:找到解决方案,我必须对增量时间进行平方。不能 100% 确定它为什么有效,但它确实有效。
所以:
jumpVelocity = -21 * Multiplier //unchanged
gravity = 1.5 * Math.pow(Multiplier,2) //works now
So I understand that, to get frame rate independent movement I have to multiply the base speed by (1000/delta). It works on values that represent speed, but when I try this with an acceleration variable, e.g. gravity, it won't work, it isn't even frame rate independent.
Is there a different formula for acceleration variables, or am I forgetting something else?
Example:
var Multiplier:Number = (GetDeltaTime()) / (1000 / 30);
jumpVelocity = -21 * Multiplier //works
gravity = 1.5 * Multiplier //dosn't work
This is called at the start of every frame
Edit: Found a solution, I had to square delta time. Not 100% sure why it works, but it does.
So:
jumpVelocity = -21 * Multiplier //unchanged
gravity = 1.5 * Math.pow(Multiplier,2) //works now
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
基本上,您想要做的是测量自上一帧以来经过的时间(“帧时间”),并将其添加到累加器中。
然后,您可以按“固定时间步长”(例如 1/30 秒)转发游戏状态,并从累加器中减去模拟时间,直到它小于一个时间步长。
举个例子:
固定时间步长设置为 33ms
1 帧以 20fps 的速度通过,帧时间为 1000ms/20 = 50ms
继续到下一帧
1 帧以 10fps 的速度传递,帧时间为 1000ms/10 = 100ms
Glenn Fiedler 有一篇关于这个问题的优秀文章。您可以在此处找到它。
Basically what you want to do is measure the time that has passed since the last frame (the "frame time"), and add that to an accumulator.
You then forward your game's state by "fixed time steps" (e.g. 1/30th of a second) and subtract that simulated time from the accumulator until it is less than one time step.
So as an example:
fixed time step set to 33ms
1 frame passes at 20fps, the frame time was 1000ms/20 = 50ms
continue to next frame
1 frame passes at 10fps, the frame time was 1000ms/10 = 100ms
There is an excellent article by Glenn Fiedler about just this issue. You can find it here.