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
}
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.