Jump to content
  • Advertisement
Sign in to follow this  
Federico Barlotti

Make This Motion Fps-Independent?

This topic is 779 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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). 

Edited by Irlan Robson

Share this post


Link to post
Share on other sites

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?

Edited by Banderi

Share this post


Link to post
Share on other sites

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.

Edited by Banderi

Share this post


Link to post
Share on other sites

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/

Edited by Irlan Robson

Share this post


Link to post
Share on other sites

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.

Edited by Banderi

Share this post


Link to post
Share on other sites

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

Edited by Irlan Robson

Share this post


Link to post
Share on other sites

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. :)

Share this post


Link to post
Share on other sites

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.

Edited by Lactose!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!