• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
SonicD007

Fixed Time Step Game loop (again..)

6 posts in this topic

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.

Edited by SonicD007
0

Share this post


Link to post
Share on other sites

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

Integer division. Multiply by 100 before dividing by [tt]UPDATES_PER_SECOND[/tt].
0

Share this post


Link to post
Share on other sites

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 [/code].  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 :/

0

Share this post


Link to post
Share on other sites

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

0

Share this post


Link to post
Share on other sites

 

//********************************************************************
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.

0

Share this post


Link to post
Share on other sites

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
Edited by SonicD007
0

Share this post


Link to post
Share on other sites

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;
 

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0