#### Archived

This topic is now archived and is closed to further replies.

# Frame-rate independent acceleration

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

## Recommended Posts

I want some sort of exponential decay. At the moment I''m using vx = vx * .999; but this is unreliable as frame rate changes. What do you suggest? - seb

##### Share on other sites
each frame before the anything is done grab the framerate. Then when you do the movement divide all your per second stuff by the framerate. That should produce an accurate effect.

##### Share on other sites
Yep, that''s how I do all of my other movement. Though I grab the milliseconds since last update and multiply speeds by this. But it''s all the same thing.

But that won''t work in this case.

- seb

##### Share on other sites
quote:
Original post by BSXrider
But that won''t work in this case.

It won''t?

##### Share on other sites
No. Look at the line of code I showed and explain how a similar effect can be done using framerate/time information.

- seb

##### Share on other sites
It will work, but you''re not using it properly.

Say you''ve got a velocity "v" in meters per second.

Say you''ve got a constant acceleration "a", in meters per second per second.

Every second:

v += a;

Therefore, every frame:

v += (a * frame_time);

If you think about what you''re doing (multiplying by .99 or whatever) you should see that the object will never come to a stop. This isn''t natural.

Instead, see if the object is moving, and if it''s not, apply a deceleration.

- Pete

##### Share on other sites
Hi,

http://www.gamedev.net/community/forums/topic.asp?topic_id=75233

i think this could solve your problem ...

##### Share on other sites
quote:
Original post by siaspete
If you think about what you''re doing (multiplying by .99 or whatever) you should see that the object will never come to a stop. This isn''t natural.

Nevertheless this is the behaviour I want. It will come to a stop because of rounding errors, and it''ll do so in a nice ease-out fashion.

- seb

##### Share on other sites
That''s possibly not the best approach. Fair enough it''s your software, but it''s best to code specific algorithms than to rely on what''s essentially a fault in the FPU.

##### Share on other sites
I''m not relying on it. It could never reach 0 and I wouldn''t be bothered. It''s just not important.

- seb

##### Share on other sites
The problem is that with acceleration, the time factor is squared, so if you scale the time, you have to scale the acceleration by a square of the same value. Your problem is that your acceleration is not based on physics, but on a percentage, so as the frame rate changes, the visual behaviour will change. To do acceleration independant of the frame rate, your acceleration must be based on time.

Your other option is to lock your frame rate to a certain minimum value and then your percentage will work the same all the time. If you do not do this, the guy running at 30FPS will see a shallower arc than the guy running at 100FPS.

quote:

I want some sort of exponential decay. At the moment I'm using

vx = vx * .999;

but this is unreliable as frame rate changes.

This is a fractional decay, not an exponentional one, because the decay gets smaller with time. Exponential gets faster with time.

To summarize, you have 2 options:
1. Lock the frame rate
2. Change your decay to a time based acceleration

I don't see any other options. Hope this helped.

---
Make it work.
Make it fast.

"Commmmpuuuuterrrr.." --Scotty Star Trek IV:The Voyage Home

Edited by - CaptainJester on February 5, 2002 3:20:23 PM

##### Share on other sites
Aha! Excellent question, I think that I can be of assistance. Basically I suggest the approach that was suggested above, which is basing the change in acceleration on time, instead of a direct percentage. This will produce the same effect anyway although you might have to mess with the constant until it is right.

Most commonly, people learn of distance-time, velocity-time and acceleration-time graphs. However, what you are talking about it a "rate of acceleration" (or deceleration in this case). Thus, you are talking about a unit such as (pixels/sec/sec)/sec or pixels/sec^3. The math is the same, you just need extra variables now like:
  // General variables for speed and location (but now acceleration too)float Distance, Velocity, Acceleration;// Work out the time changefloat TimeChange = // Whatever it is in seconds// Now work out the new accelerationAcceleration += 0.99 * TimeChange; // (pixels/sec^3 * sec = pixels/sec^2)// Now work out the new speedSpeed += Acceleration * TimeChange; // (pixels/sec^2 * sec = pixels/sec)// And now the new locationDistance += Speed * TimeChange; // (pixels/sec * sec = pixels!)

And there you are, rate of acceleration. The constant in there should be whatever value represents how many units of acceleration in pixels/sec^2 are gained (or lost) every second. Hard to visualize, but it works the same way as normal Physics equations.

Well I hope that helps. Tell me how it works. I can't test it right now since I am not at home but this is the concept that you need. Post back and ask any more questions in this thread if needed.

Edited by - AndyTX on February 5, 2002 2:39:26 PM