Make This Motion Fps-Independent?

Started by
18 comments, last by _WeirdCat_ 7 years, 8 months ago

I have a position point that moves towards a destination point by a factor of:


position = position + distance * X;

where:


distance = destination - position;

and `X` is a factor chosen by the code between 0.0f and 1.0f (0 = stopped and 1 = instantaneous motion);

And it all works fine, except the motion speeds up and slows down depending on the framerate. I have access to the delta time of the frames, but I have no idea how to fit it in the formula. I've tried many combinations and failed.

Any help?

Advertisement

What you call distance is generally a velocity vector and assuming MKS has units of meters per second. The last term on the right of the equation is a displacement vector (velocity * dt). The distance, the lenght of the displacement, is a scalar and has units of meters. The speed, the lenght of a velocity, is a scalar as well.

This article is tipically recommended here for frame-independent position updates:

http://gafferongames.com/game-physics/fix-your-timestep/

Oh, roughly answering your question: X is tipically not allowed to vary during the game for deterministic updates. Usully it is set to X = 1 / 60 (60 Hz) or X = 1 / 30 (30 Hz).

You're confusing my `X` with the fixed delta time described in the article (which I did read, yet is unhelpful to my more specific problem). X is not the delta time, it's a "friction" factor. It doesn't change over time, but the approach should work for any X.

Also, quoting from the article:

"[...] This is usually significantly easier in practice than attempting to make your simulation bulletproof at a wide range of delta time values."

I'm not going for this approach. As I said, the formula should be able to correctly interpret virtually any framerate and factor values between 0.0f and 1.0f.

So asking again, where does my delta time fit in in the operation?

Are you talking about frame-independent linear interpolation?

I'm talking about my formula.


position = position + (destination - position) * X;

It lacks the delta time, and I don't know where to put it.

An equation that would probably work approximately would be:


Vec3 displacement = destination - origin;
float base = 1.0f - t; // 0 <= t <= 1 
float exponent = elapsedTime / dt; // 0 <= dt <= 1
float lerp = 1.0f - pow(base, exponent);

position += lerp * displacement;

An exponential decay should work as well here:

http://www.rorydriscoll.com/2016/03/07/frame-rate-independent-damping-using-lerp/

I'm assuming `elapsedTime` is my delta time, but what are `t` and `dt`? Is one of them my `X` factor?

There is no origin and no time management in my system, maybe I've not been clear enough. I only have these factors, run on a frame by frame basis, with no "origin" or "time until B" etc.

I just have to put my delta time in the equation I've already got for it to work.

elapsedTime = delta time of a frame

origin = position

t = a number between 0 and 1

dt = a fixed time step between 0 and 1

X = lerp

Thank you so much! This is much, much better than any of my results. It's still not perfect, probably due to my cheap framerate calculation, but it's barely noticeable. This is more than enough for my needs. :)

Another approach:

Store the original position, and increment x based on delta time.


float timeNeeded = 3.0f;
float increment = deltaTime / timeNeeded;
x += increment;

x = clamp(x, 0.0f, 1.0f);

position = originalPosition + (distance * x);

This will need a bit more logic to deal with cases where timeNeeded is 0.

Hello to all my stalkers.

This topic is closed to new replies.

Advertisement