Frame rate independent exponential interpolation

Viewed 25

I am creating a game. I usually start with quick-and-dirty get-the-job-done code, and improve it later. And precisely, I am trying to improve that.

Let's present the problem: the player controls an aircraft. When pressing a key, the aircraft rotates (a "pitch") and goes down (or up, depending on the key pressed). When the player releases the key, the aircraft goes back to horizontal.

The maximum angle for the aircraft should be reached quickly, it's not a simulation. Think of Starfox.

The quick and dirty approach is as follows: each frame, I check if a relevant key is pressed. The output of this step is a variable that contains either 0, -1 or +1 whether the aircraft should be horizontal, going down or going up.

Now, I do the following formula:

pitch = pitch*0.9 + maxAngle * turn * 0.1

turn is the variable obtained containing 0, -1 or +1.

This produces a nice effect. It's an interpolation, but not a linear one, which makes it more "fun" to watch.

Here is the problem: this formula doesn't contain the length of a frame. It's frame rate dependent.

I tried to extract the general formula. First, 0.9 and 0.1 are obviously what they are because they sum up to 1 (I didn't try to have one of them lesser than 0 and or one bigger than 2).

If I put that

a1=a0*x + b*(1-x)

I arrive at the general formula of

an = a0*(x^n) + b*(1-x)*(x^(n-1) + x^(n-2) + x^(n-3) + ... + 1 )

By assuming a certain frame length I could inject it into that, maybe, somehow, but I still don't know how to properly turn that into a function (especially this sum of x^n, I don't know how to factorize it).

The second problem is that, currently, the user can press and release the key as they like. By that, I mean by that the b in the equation can change (and the resolution I attempted does not take that into account).

So, this is the problem. In short, how to reverse engineer my own solution to inject the frame length in it so it becomes frame rate independent?

Please note that you can assume that the frame rate in the current program IS stable.

I am quite sure I am not the first one encountering such a problem. If you don't have a solution, hints are also welcome, of course.

Thanks

0 Answers
Related