Jump to content
  • Advertisement
Sign in to follow this  

Frame Rate Independent Game Loop Article Question

This topic is 3772 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I just read this article cause I want to make my game loop frame rate independent as described in the article, but I only just reaslised a problem. http://dewitters.koonsolo.com/gameloop.html Aren't the update and render sections of code mean't to be separate. In the article it is separate, but as he explains, the render function is used to update the cars position. 1) So here in this code I have taken from the article, it is updating the car in the display_game( interpolation ) section of code. I would have prefered to do this somehow in the update_game() section of code. What changes would be made, if any? 2) Also how would this game loop work on slow computers? Any suggestions?
    const int TICKS_PER_SECOND = 25;
    const int SKIP_TICKS = 1000 / TICKS_PER_SECOND;
    const int MAX_FRAMESKIP = 5;

    DWORD next_game_tick = GetTickCount();
    int loops;
    float interpolation;

    bool game_is_running = true;
    while( game_is_running ) {

        loops = 0;
        while( GetTickCount() > next_game_tick && loops < MAX_FRAMESKIP) {

            next_game_tick += SKIP_TICKS;

        interpolation = float( GetTickCount() + SKIP_TICKS - next_game_tick )
                        / float( SKIP_TICKS );
        display_game( interpolation );

About the interplation bit:
Take this example: I have a car that moves every game tick like this: position = position + speed; If in the 10Th gametick the position is 500, and the speed is 100, then in the 11Th gametick the position will be 600. So where will you place your car when you render it? You could just take the position of the last gametick (in this case 500). But a better way is to predict where the car would be at exact 10.3, and this happens like this: view_position = position + (speed * interpolation) The car will then be rendered at position 530. So basically the interpolation variable contains the value that is in between the previous gametick and the next one (previous = 0.0, next = 1.0). What you have to do then is make a "prediction" function where the car/camera/... would be placed at the render time. You can base this prediction function on the speed of the object, steering or rotation speed. It doesn't need to be complicated because we only use it to smooth things out in between the frames. It is indeed possible that an object gets rendered into another object right before a collision gets detected. But like we have seen before, the game is updated 25 frames per second, and so when this happens, the error is only shown for a fraction of a second, hardly noticeable to the human eye.

Share this post

Link to post
Share on other sites
It doesn’t update the position of the car. It predict where the car should be between the updates and display it in that position. In the next update of the game the new position of the car is calculated only using the position of the old update and what happened between the frames.

Share this post

Link to post
Share on other sites
Ok so if I have a car object then during update function I change the position
//car.m_nPosition = car.m_nPosition + car.m_speed;
car.m_nPosition = 500 + 10; // = 510

And in the render function I then just store a interpolated position locally
//int nView_position = car.m_nPosition + (car.m_speed * interpolation);
int nView_position = 510 + (10 * 0.5); // = 515

//render function here
//dxSprite->Draw(...rect based on nView_position ...);

Is that right?

I think I get it now.


Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!