Another q. about time based movement

Started by
15 comments, last by Redoc 20 years, 12 months ago
Hi there!, I hope you can help me Im trying to get the hang of movement of time, instead of movement of frames. To begin with I made a test application, this holds a variable of the sprites speed. float Speed = 300; and I made a slow down rate for this object float SlowRate = 0.96; I calculate the time each frame took, known as TimeElapsed but I dont know what to do with it, its calculation is 17. Inside my main loop I apply the slow down to the speed as so: Speed = Speed * SlowRate; I measured the amout of time this takes to reach 0, at 60 FPS it takes just over 3 seconds. After that test I added a Sleep(30); in to the main loop reducing the FPS to 20 and this time it took just over 9 seconds for Speed to reach 0 So it must be something so simple I am missing Anyone available to help me? I want to make Speed reach 0 at the same rate no matter what the FPS is. Thanks I hope I made everything clear! [edited by - redoc on April 30, 2003 4:53:14 AM]
Advertisement
You need to multiply by time. The speed is a change in position per unit of time, i.e. 60mph means you move 60 miles in one hour. Additionally your SlowRate is acceleration and it is the change in speed per unit of time, i.e. 0 to 60mph in 10 seconds. Due to fricition acceleration is seldom truly constant but you can often get by with pretending it is. If you are going 60mph now and 30mph a half hour later under constant acceleration (deceleration here) then you average speed for that half hour is 45mph. During that half hour you will travel 22.5 miles, not 15 or 30 miles.
Keys to success: Ability, ambition and opportunity.
Hi again,

Thank you for your reply!

So my slow rate is simply ''deceleration'' .. that makes sense

However Im still unsure how to multiply by time to get the same results on any rate of FPS.

are you saying something like:

Speed = (Speed * SlowRate) * TimeElapsed;

?

That didnt seem to work when I tried it, Thanks for your patience with me


What you're looking at right there is an "exponential decay curve". It's kinda similar to interest on a bank account. Let's see how to make it framerate independent.

Step one: throw your algorithm out the window. Here's the new one: x = x0ekt.
x is the new speed.
x0 is the initial speed... here, the speed from the previous frame.
e is approximately 2.71828182846. It's a 'special' number. Just use it.
k is your decay rate, about what you were using .96 for before. Try other numbers between 0 and one.
t is the time since the last frame.

How appropriate. You fight like a cow.

[edited by - sneftel on April 30, 2003 12:11:08 AM]
Your value won''t actually reach zero in a LOONG time, unless Speed is an integer. It may go below 1, or below 0.5, or whatever, but it''ll take a very long time to get even close to actual 0 (as opposed to 0.00000000001 which will be reached reasonably quickly).

Oh, and on the way towards 0, your float (or double) will go through the "denormal" zone, which will make your calculations 830 times slower (real metric). Denormals are bad, which is why you have to be ultra careful with exponential decay such as you have there.

You are going to hate me for this, but could you please tell me how to translate your formulae. The letters ''k'' and ''t'' are floating, does that mean I need to multiply?

Im very sorry about this but a little explanation on how to read formulaes like that would help me a lot.

I will look for math tutorials too

Thank you very very much
The floating means that you raise e to a power, namely the power of kt. Programatically, it''s the exp() function, and mathamatically, it''s a calculator

So you raise e to the power of kt, the multiply by x0.

It''s odd you don''t know that, because exponents (and their related notation) are a pretty trivial algeabraic topic. What maths have you taken so far?
Well I guess I havent taken maths

I suppose if I thought about it longer it could have made sense since I knew a floating 2 meant to the power of 2. just looks scarey with variables.

So the formulae breaks down to something like

x = x * (e ^ (k * t))

providing ^ means "to the power of" ... does it?

and I also guess you multiply k by t ... ?

hopefully that is correct, Im currently reading lots of websites and I have a book which Im going to get through.

Just need to check thats all right

Thanks for not laughing too much at me :D
maybe you could use a different approach?

like speed += acceleration*time-elapsed;

if you want to go to zero speed fill in a negative acceleration.

for example if you want to slow down to zero in 5 seconds, you take acceleration = -speed/5

i say this because you are talking about sprite speed, so you probably don''t want to use exponential functions
Firstly, may I suggest you use doubles instead of floats, I''m sure everyone will agree that (given you have a fast enough computer) it''s alot more accurate.
Secondly e does not equal 2.71... it is infact 3.14... I''m sure everyone will agree on this point too.
And lastly using ^ will always give you compile errors, you should use always hard code it as in x^3 should be x*x*x.
Hope this has been useful.

This topic is closed to new replies.

Advertisement