开发者

Framerate independent acceleration

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


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:

  • accumulator starts at 0
  • fixed time step set to 33ms

  • 1 frame passes at 20fps, the frame time was 1000ms/20 = 50ms

  • 50ms added to accumulator
  • game state is forwarded by 1 fixed time step (33ms), that time is subtracted from accumulator
  • accumulator is now 17ms, which is less than one fixed time step -> game state is done updating
  • continue to next frame

  • 1 frame passes at 10fps, the frame time was 1000ms/10 = 100ms

  • 100ms added to accumulator
  • accumulator is now 117ms
  • game state is forwarded by 1 fixed time step (33ms), that time is subtracted from accumulator
  • accumulator is now 84ms, which is more than one fixed time step -> game state is not done updating
  • game state is forwarded by 1 fixed time step (33ms), that time is subtracted from accumulator
  • accumulator is now 51ms, which is more than one fixed time step -> game state is not done updating
  • game state is forwarded by 1 fixed time step (33ms), that time is subtracted from accumulator
  • accumulator is now 18ms, which is less than one fixed time step -> game state is done updating
  • continue to next frame

There is an excellent article by Glenn Fiedler about just this issue. You can find it here.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜