Search code examples
c++timeropengl-esgame-loopskeletal-animation

MD5 animation and game loop


I have implemented loading MD5 model with skeletal animation and it works fine. now I am trying enhance the game loop to play everything smoothly and correct timing. I read about different game loops then implemented the best one as I read that has the following scenario:

accumulatedTime += timer.elapsed();
nLoops = 0;
processInputs();
while (accumulatedTime >= dt && nLoops < maxSkipFrames)
{    
    updateGame(dt/20.0);
    accumulatedTime -= dt;
    nLoops++;
}
timer.reset();

I implement CPU skeletal animation so I update all vertices and normals each frame. the updateGame(dt) method updates the mesh vertices based on the skeleton animation it should take a time and add it to the animation time the update the vertices. there are somethings wrong in my implementation of the game loop which make updating rate is different on different devices. I am using OpenGL with C++ on Android.


Solution

  • It is no surprise that it runs at different frame rate on different devices since some are faster that others. To make it always 60 frames per second, each frame must last 16ms, since some devices can process update loop in less time, it might take them about 10ms (in this situation you might want to sleep for 6ms at the end of the frame) and in other devices it might take them 17ms (as far I'm aware, there is nothing you can do here).

    There is a good read about this in Game Programming Patterns book (Game Loop chapter) which is available online for free to read. But this book is so popular that I'll guess that you have already peeked it :p but it is worth mention it anyway.