PDA

View Full Version : Time Based Movement?



jono_123
06-15-2002, 12:10 AM
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 http://www.opengl.org/discussion_boards/ubb/smile.gif

Bob
06-15-2002, 12:18 AM
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.

jono_123
06-15-2002, 12:24 AM
ummmmmmmmmm you've lost me
???

MrShoe
06-15-2002, 01:35 AM
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...

zen
06-15-2002, 06:44 AM
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=V*dt(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.5*a*dt*dt.

zauron
06-15-2002, 07:02 AM
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?



<code>
QueryPerformanceCounter(&amp;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(&amp;ClockFinish);
ElapsedTime = ( (double) ClockFinish.QuadPart - (double) ClockStart.QuadPart ) / ClockFreq.QuadPart;
fps = 1.0f / float(ElapsedTime);
sprintf(buffer, "Fps %f", fps);
SetWindowText(hWnd, buffer);

</code>

ioquan
06-15-2002, 11:51 AM
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.