# Time Based Movement?

hi there
i have a simple program were you walk around using the keyboard. right now it is frame based movement-you move the same distance forward every frame but now i want to change it to time based. i know how to calculate the frames per second and i thought all i had to do was: distance_moved = any_number / fps but this is not working very well
is this the way i should be doing it???
thanx
jono You don’t really need the actual FPS-value, but rather the time to process last frame. You specify your movements as units per second, and to get the actual movement per frame, multiply this movement by the time taken to process last frame.

ummmmmmmmmm you’ve lost me
?¿?¿?

Say your aiming for 30 fps, this is what you do:
distance_to_move=speed*last_fps/30.0;
last fps is the fps your program was running it when it rendered the last frame:
1.0/time_to_render_last_frame
that is at least one way of doing time based movement…

What does fps have to do with time base movement.If you consider anything frame related into the calculation it will be (at least in part)frame based.This is real simple:say your player runs at a velocity of V units per second.Then if dt time has passed(in seconds) the player moves by dx=Vdt(simple physics).So measure the time passed since the last time you moved the player(dt) assuming of course the player is still running and calculate dx and move the player(camera whatever) by that.Simple I hope.As you might have guessed if you want the player to accelerate then dx=0.5adtdt.

i was about to ask something of the same. Here is how i do it.

is this the way to have the input and gamelogic run the same speed no matter how fast it renders the scene?

``` QueryPerformanceCounter(&ClockStart); if((double)ClockStart.QuadPart < Timer){ /* drawing */ DrawGLScene(); // Draw The Scene SwapBuffers(hDC); // Swap Buffers (Double Buffering) }else{ Timer += 50000; /* updating logic */ logic(); /* read keys and react */ input(); } /* fps */ QueryPerformanceCounter(&ClockFinish); ElapsedTime = ( (double) ClockFinish.QuadPart - (double) ClockStart.QuadPart ) / ClockFreq.QuadPart; fps = 1.0f / float(ElapsedTime); sprintf(buffer, "Fps %f", fps); SetWindowText(hWnd, buffer); ```

</code>

You calculate a scalar based on time. Usually you will want this to be within the range of 0 to 1. To get the scalar you take the amount of time that has passed divided by some standard time unit. Here is my function that gets the time scalar:

double GetTimeScalar()
{
static unsigned int lastticks = 0;

``````int ticks = SDL_GetTicks();
int tickssince = ticks - lastticks;

double scalar = (double)tickssince/(double)STANDARDTIMEUNIT;

lastticks = ticks;

return scalar;
``````

}

Then I take that scalar and multiply it by my velocity (as well as factoring it into animation interpolation).

Another note: You also need to scale accelerations. However, you cant just scale the acceleration and then also scale the final velocity. You then need to take the square root of the time scalar and multiply both the acceleration and velocity by it.