Fixed Time Step Game loop (again..)

Started by
5 comments, last by stannic 11 years, 1 month ago

Hey everyone. I'm back again with this damn fixed time step loop. I've been messing with it some more and I want to know where I'm messing up (if I am) and what else am I missing. I think I understand the logic now for the most part and I wrote this code to test out the fixed step loop.


 
#include <SDL.h>
#include <iostream>
 
using namespace std;
 
int main(int argc, char* argv[])
{
    if (SDL_Init(SDL_INIT_EVERYTHING) == -1)
        cout << "Error starting SDL" << endl;
 
    bool quit = false;
    const uint64_t UPDATES_PER_SECOND = 33;
    int positionX = 0;
    uint64_t currentTime = 0;
    uint64_t lastUpdate = 0;
    uint64_t nextUpdate = 0;
    int interpolationPercent = 0;
    while (!quit)
    {
        currentTime = SDL_GetTicks();
        while (currentTime - nextUpdate > UPDATES_PER_SECOND)
        {
            //Do update
            positionX += 2;
            nextUpdate += UPDATES_PER_SECOND;
            
            cout << "CurrentTime: " << currentTime << endl;
            cout << "NextUpdate: " << nextUpdate << endl;
            cout << "AfterUpdate: " << positionX << endl;
        }
        interpolationPercent = ((currentTime - lastUpdate) / UPDATES_PER_SECOND) * 100;
        cout << "Interpolation Percent: " << interpolationPercent << endl;
        lastUpdate = currentTime;
 
    }
    SDL_Quit();
    return 0;
};

I know I'm missing the cap to avoid the spiral of death but I just want to make sure everything else is correct in my implementation. I did notice that interpolation percent was always 0 so I'm not sure if it's because it's just running really fast or if it's a mistake on my part but it seems fishy to me.

Thanks.

Advertisement

I did notice that interpolation percent was always 0...

Integer division. Multiply by 100 before dividing by UPDATES_PER_SECOND.

Thanks. It still doesn't look correct though. Here is a sample output:


 
CurrentTime: 109
NextUpdate: 33
AfterUpdate: 2
CurrentTime: 109
NextUpdate: 66
AfterUpdate: 4
CurrentTime: 109
NextUpdate: 99
AfterUpdate: 6
Interpolation Percent: 330
Interpolation Percent: 36
Interpolation Percent: 6
Interpolation Percent: 3
Interpolation Percent: 6
Interpolation Percent: 3
Interpolation Percent: 6
Interpolation Percent: 3
Interpolation Percent: 6
CurrentTime: 133
NextUpdate: 132
AfterUpdate: 8
Interpolation Percent: 3
Interpolation Percent: 15
Interpolation Percent: 6
Interpolation Percent: 6
Interpolation Percent: 6
Interpolation Percent: 6
Interpolation Percent: 6
Interpolation Percent: 9
Interpolation Percent: 9
Interpolation Percent: 9
Interpolation Percent: 9
Interpolation Percent: 9
CurrentTime: 166
NextUpdate: 165
AfterUpdate: 10
Interpolation Percent: 9
Interpolation Percent: 27
Interpolation Percent: 6
Interpolation Percent: 9
Interpolation Percent: 9
Interpolation Percent: 6
Interpolation Percent: 6
Interpolation Percent: 6
Interpolation Percent: 6
Interpolation Percent: 6
Interpolation Percent: 6
Interpolation Percent: 6
CurrentTime: 200
NextUpdate: 198
AfterUpdate: 12
Interpolation Percent: 9
Interpolation Percent: 18
Interpolation Percent: 6
Interpolation Percent: 9
Interpolation Percent: 6
Interpolation Percent: 6
Interpolation Percent: 6
Interpolation Percent: 6
Interpolation Percent: 6
Interpolation Percent: 6
Interpolation Percent: 6
Interpolation Percent: 6
Interpolation Percent: 6
CurrentTime: 232
NextUpdate: 231
AfterUpdate: 14
Interpolation Percent: 9
Interpolation Percent: 33
Interpolation Percent: 9
Interpolation Percent: 9
Interpolation Percent: 12
Interpolation Percent: 9
Interpolation Percent: 9
Interpolation Percent: 12
CurrentTime: 266
NextUpdate: 264
AfterUpdate: 16
Interpolation Percent: 9
Interpolation Percent: 30
Interpolation Percent: 9
Interpolation Percent: 9
Interpolation Percent: 9
Interpolation Percent: 9
Interpolation Percent: 12
Interpolation Percent: 9
CurrentTime: 298
NextUpdate: 297
AfterUpdate: 18
Interpolation Percent: 9
Interpolation Percent: 30
Interpolation Percent: 9
Interpolation Percent: 9
Interpolation Percent: 9
Interpolation Percent: 12
Interpolation Percent: 9
Interpolation Percent: 12

EDIT: seems like there's a bug with the

. Doesn't show my text after it when I post the first time. Anyway...

Those numbers don't look right. First off, why did interpolate percentage reach 330? It should never reach > 100 no? It's also increasing, then decreasing before an update even occurred. I must be doing something wrong :/

lastUpdate = currentTime; should be moved inside the update loop.

lastUpdate will then need to be init with the SDL_GetTicks() before ever looping.

UPDATES_PER_SECOND should probably be renamed to MILLISECONDS_PER_UPDATE

then have it equal to 1000/UPDATES_PER_SECOND


//********************************************************************
void CGame::physics(){
    
    physics_FPS->add( passed_secs_real);
    
    if( game_is_paused)
        return;


    float time_per_physics_iteration = SECS_PER_PHYSICS_ITERATION;
    float temp_passed_secs = passed_secs + secs_to_add;
    int physics_iteration = int( temp_passed_secs / time_per_physics_iteration);


    secs_to_add = temp_passed_secs - physics_iteration * time_per_physics_iteration;


    for(int i= 0; i < physics_iteration; ++i)
        iteratedPhysics( time_per_physics_iteration);


    physicsWithoutIteration();
    callAfterPhysics();
    
}
 

....you could use something like this.

Thank you for the help. Everything looks good now. I know I'm missing the frame cap so I need to add that but does everything else look correct now with the updates?


 
#include <SDL.h>
#include <iostream>
 
using namespace std;
 
int main(int argc, char* argv[])
{
    if (SDL_Init(SDL_INIT_EVERYTHING) == -1)
        cout << "Error starting SDL" << endl;
 
    bool quit = false;
    const uint64_t UPDATES_PER_SECOND = 33;
    const uint64_t MILLISECONDS_PER_UPDATE = 1000/UPDATES_PER_SECOND;
    int positionX = 0;
    uint64_t currentTime = 0;
    uint64_t lastUpdate = 0;
    uint64_t nextUpdate = 0;
    int interpolationPercent = 0;
    lastUpdate = SDL_GetTicks();
    while (!quit)
    {
        currentTime = SDL_GetTicks();
        while (currentTime - nextUpdate > MILLISECONDS_PER_UPDATE)
        {
            //Do update
            positionX += 2;
            nextUpdate += MILLISECONDS_PER_UPDATE;
            
            cout << "CurrentTime: " << currentTime << endl;
            cout << "NextUpdate: " << nextUpdate << endl;
            cout << "AfterUpdate: " << positionX << endl;
            lastUpdate = currentTime;
        }
        interpolationPercent = ((currentTime - lastUpdate) * 100) / MILLISECONDS_PER_UPDATE;
        cout << "Interpolation Percent: " << interpolationPercent << endl;
        
 
    }
    SDL_Quit();
    return 0;
};

And here is some sample output:


CurrentTime: 1651
NextUpdate: 1650
AfterUpdate: 110
Interpolation Percent: 0
Interpolation Percent: 6
Interpolation Percent: 10
Interpolation Percent: 13
Interpolation Percent: 16
Interpolation Percent: 20
Interpolation Percent: 20
Interpolation Percent: 23
Interpolation Percent: 26
Interpolation Percent: 30
Interpolation Percent: 33
Interpolation Percent: 36
Interpolation Percent: 40
Interpolation Percent: 43
Interpolation Percent: 46
Interpolation Percent: 46
Interpolation Percent: 50
Interpolation Percent: 53
Interpolation Percent: 56
Interpolation Percent: 60
Interpolation Percent: 63
Interpolation Percent: 66
Interpolation Percent: 70
Interpolation Percent: 73
Interpolation Percent: 76
Interpolation Percent: 80
Interpolation Percent: 83
Interpolation Percent: 86
Interpolation Percent: 90
Interpolation Percent: 93
Interpolation Percent: 96
Interpolation Percent: 96
CurrentTime: 1681
NextUpdate: 1680
AfterUpdate: 112
Interpolation Percent: 0
Interpolation Percent: 10
Interpolation Percent: 13
Interpolation Percent: 13
Interpolation Percent: 16
Interpolation Percent: 20
Interpolation Percent: 23
Interpolation Percent: 26
Interpolation Percent: 30
Interpolation Percent: 33
Interpolation Percent: 33
Interpolation Percent: 36
Interpolation Percent: 40
Interpolation Percent: 43
Interpolation Percent: 46
Interpolation Percent: 50
Interpolation Percent: 53
Interpolation Percent: 56
Interpolation Percent: 56
Interpolation Percent: 60
Interpolation Percent: 63
Interpolation Percent: 66
Interpolation Percent: 70
Interpolation Percent: 73
Interpolation Percent: 73
Interpolation Percent: 76
Interpolation Percent: 80
Interpolation Percent: 83
Interpolation Percent: 86
Interpolation Percent: 90
Interpolation Percent: 90
Interpolation Percent: 93
Interpolation Percent: 96
CurrentTime: 1711
NextUpdate: 1710
AfterUpdate: 114

Seems like lastUpdate = nextUpdate

So I'd probably replace lastUpdate with nextUpdate, though lastUpdate would probably be a more correct term here.

So you don't really need the line lastUpdate = currentTime;

This topic is closed to new replies.

Advertisement