Archived

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

Redoc

Another q. about time based movement

Recommended Posts

Redoc    122
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]

Share this post


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

Share this post


Link to post
Share on other sites
Redoc    122
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


Share this post


Link to post
Share on other sites
Sneftel    1788
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]

Share this post


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

Share this post


Link to post
Share on other sites
Redoc    122
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

Share this post


Link to post
Share on other sites
Zipster    2365
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?

Share this post


Link to post
Share on other sites
Redoc    122
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

Share this post


Link to post
Share on other sites
Airo    197
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

Share this post


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

Share this post


Link to post
Share on other sites
Redoc    122
Airo: Thanks, would that work on negative speeds though? Just a thought..

AP: thanks for that too! Im not sure why PI is in the formulae, what is ''e'' for?

And about this:

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

This is just my version I can read better, I want to see if that read the same as the formulae Sneftel has (I dont know how to make the floaty letters / numbers )

Thanks again

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
you should be careful with x subscript 0, it is different to x. (x sub 0 is a constant)

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
e is an un-natural number made up by mad scientist.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
quote:
Original post by Anonymous Poster
Secondly e does not equal 2.71... it is infact 3.14... I''m sure everyone will agree on this point too.

Yeah, I''m sure they will...

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
pi/pi = 1
e/e = 1,
thus it stand to reason pi = e

Share this post


Link to post
Share on other sites
brassfish89    122
quote:

maybe you could use a different approach?

like speed += acceleration*time-elapsed;


i agree. here is an example:

  
double last_frame=/*time last frame*/;
double this_frame=GetTickCount();
double delta=this_frame-last_frame;
double x=0.0f,y=0.0f;
// speed==amount of pixels you want to move

// in a second

int speed=300;

x+=((float)delta/1000)*speed;




doh, nuts. Mmmm... donuts
My website

Share this post


Link to post
Share on other sites
Redoc    122
Thanks for all the messages so far.

Brassfish, I know how to speed my object up I need to know how to slow it down naturally.. What I have done to slow the sprite down works exactly how I want it here:

float SlowRate = 0.96;

// each frame aceleration is added (Speed += Aceleration * FrameTime)
// then to slow down I have
Speed = Speed * SlowRate;

BUT, on fast computers the speed slows very FAST and on slow computers it slows slower. I want to adapt that formulae to reach 0 no matter what FPS is at.

Does that make sense? I hope it does. thanks

Share this post


Link to post
Share on other sites