I'm working on a game where I use this basic game loop design:
[source lang="cpp"]int main ( int argc, char* argv[] )// main entry point{ Load ( ); while ( window->IsActive ( ) ) // game loop { // update the ticks for this and the last frame last_frame_tick = this_frame_tick; this_frame_tick = SDL_GetTicks ( ); // update the scene depending on how long // it has been since we last updated Update ( this_frame_tick - last_frame_tick ); // render the scene Render ( ); } Destroy ( ); return 0;}[/source]
So I update and render the scene as much as possible (with VSYNC off). SDL_GetTicks ( ) is an API function that returns the number of milliseconds that have passed since the API (ie. the game) started. In my Update ( float time ) function, I then multiply every speed (that is, a bullet moving forward for example, but also a character turning their head to one side) by that time.
In theory, this should give me a framerate independent gaming experience - if we're running on an older machine, we might just get 20 fps so the time it takes to render each frame is 50 ms, on a modern machine we could get 200 fps (5 ms per frame). A bullet moving forward at an original speed of 2.5 units per frame will move at 2.5 * 50 = 125 units per frame on the slow machine and 2.5 * 5 = 12.5 units per frame on the fast one. In 1 second, on the slow machine (since it renders 20 fps) the bullet will have moved 125 * 20 = 2500 units total, on the fast one (at 200 fps) 12.5 * 200 = 2500 as well. No matter what speed the computer can run the game at, all the action will always be going on at the same rate, it's just going to look and feel a lot smoother on a better system for obvious reasons.
This is the (very basic) theory from what I understand reading tutorials such as this one. But for some reason, this doesn't work for my games, when I set everything up as explained, my bullets (etc.) will still move a lot faster with a higher framerate!
[source lang="cpp"]// in Update ( float time ):// GetForward ( ) returns a normalized forward vector for the projectilebullet->SetPosition ( bullet->GetPosition ( ) + bullet->GetForward ( ) * 2.5f * time );[/source]
What am I doing wrong or misunderstanding here? Is the entire game loop design faulty? The article linked makes it sound like this should work (albeit saying that speed prediction methods are better, stuff I'd rather not get into without it being necessary).
Any help would be greatly appreciated, as always! I'm lost!
Edited by d k h, 14 August 2012 - 08:05 AM.







