I've read two other threads here on movement: Time based movement Vs Frame rate based movement?, and When should I use a fixed or variable time step?
but I think I'm lacking a basic understanding of frame independent movement because I don't understand what either of those threads are talking about.
I'm following along with lazyfoo's SDL tutorials and came upon the frame independent lesson. http://lazyfoo.net/SDL_tutorials/lesson32/index.php
I'm not sure what the movement part of the code is trying to say but I think it's this (please correct me if I'm wrong): In order to have frame independent movement, we need to find out how far an object (ex. sprite) moves within a certain time frame, for example 1 second. If the dot moves at 200 pixels per second, then I need to calculate how much it moves within that second by multiplying 200 pps by 1/1000 of a second.
Is that right? The lesson says:
"velocity in pixels per second * time since last frame in seconds. So if the program runs at 200 frames per second: 200 pps * 1/200 seconds = 1 pixel"
But...I thought we were multiplying 200 pps by 1/1000th of a second. What is this business with frames per second?
I'd appreciate if someone could give me a little bit more detailed explanation as to how frame independent movement works.
Thank you.
ADDITION:
SDL_Rect posRect;
posRect.x = 0;
posRect.y = 0;
float y, yVel;
y = 0;
yVel = 0;
Uint32 startTicks = SDL_GetTicks();
bool quit = false;
SDL_Event gEvent;
while ( quit == false )
{
while ( SDL_PollEvent( &gEvent ) )
{
if ( gEvent.type == SDL_QUIT )
quit = true;
}
if ( y <= 580 )
{
yVel += DOT_VEL;
y += (yVel * (SDL_GetTicks() - startTicks)/1000.f);
posRect.y = (int)y;
}
startTicks = SDL_GetTicks();
SDL_BlitSurface( bg, NULL, screen, NULL );
SDL_BlitSurface( dot, NULL, screen, &posRect );
SDL_Flip( screen );
}
That's code that moves a dot down the screen. I think I have everything correct so far. It moves down the screen but there is a bit of an odd thing that happens that I can't explain. The dot is supposed to stay at the y=580 when it gets to greater than that y-value. However, every time I run the program, the dot will end up in a different location, meaning a little bit to a lot more than 580, so the dot is halfway or more than halfway off the screen (the dot is 20 pixels, screen dimensions 800x600). If I do something like click and hold the title-bar of the program, and then release, the dot disappears off the screen. How come it's variable each time? As for the titlebar problem I think it's because when I hold on to the titlebar, the timer is still running, and the time elapsed gets larger, resulting in a larger distance the dot moves in the next frame. Is that right?
yMovement = (yVel * (SDL_GetTicks() - startTicks)/1000.f);
then do:if(y + yMovement <= 580){ y += yMovement; } else { y = 580; }
\$\endgroup\$