Reputation: 11
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
Upvotes: 1
Views: 1277
Reputation: 700
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.
Upvotes: 2