I am trying to implement the Game Loop interpolated found in this website http://www.gameprogblog.com/generic-game-loop/. Here is my implementation:

long frameRate = 50000000; 
int maxUpdates = 3; 
boolean yield = true; 

private long time =0; 

private long lastTime;
private long currentTime;

public static float interpolation;

public void gameLoop() {

    long nanoElapsed = tick();

    System.out.println(time + "\t Framerate: " + frameRate + "\t"
            + "Nanoelapsed: " + nanoElapsed + "\t Interpolation: "
            + interpolation);

    time += nanoElapsed;
    int updateCount = 0;

    while (time >= frameRate && updateCount < maxUpdates) {
        time -= frameRate;

    interpolation = getInterpolation();


long tick() {
    lastTime = currentTime;
    currentTime = TimeUtils.nanoTime();
    return currentTime - lastTime;

float getInterpolation() {
    return (float) ((double) time / (double) frameRate);

However I cannot figure out the problem I am having. I am logging most of the variables and for some reason the time variable is very big and does not stay in an acceptable range. It also decreases over time. I have spent a long time trying to figure out why and I'm now stuck. Any suggestions and help would be appreciated. Thanks

  • \$\begingroup\$ why dont you download gameprogblog.com/wp-content/uploads/2013/01/… tutorial from the blog then ? or any working demos written in libgdx \$\endgroup\$
    – dimitris93
    Jan 17 '15 at 19:21
  • \$\begingroup\$ I only want to implement the game loop as I have my own game structure that I already use and do not want to implement his. Also I couldn't find any libgdx demos that use a fixed step game loop with interpolation. \$\endgroup\$ Jan 17 '15 at 19:33
  • \$\begingroup\$ by using libgdx aren't you already kind of committed to use "his" game loop implementation in the first place ? i can't completely grasp what you are trying to achieve here. games should run on stable fps (generally 60) for better performance, also why is interpolation float and not double ? \$\endgroup\$
    – dimitris93
    Jan 17 '15 at 19:47
  • \$\begingroup\$ The reason I'm doing this is because when I use delta time and multiply it by my velocity it, my gameobject stutters on the screen and looks like it is not moving smoothly. Also the tutorial used the type float for interpolation. \$\endgroup\$ Jan 17 '15 at 19:51
  • \$\begingroup\$ i haven't used java or libgdx, but the way i calculate the next position is position = position*speed*velocity.normalize() on every update() call, now the downside of this is that when you drop frames, your game objects will move slower because update() will be called less times, but this might be just fine granted that you don't lose frames... but again, i suggest you download any working demo from libgdx and see how he handles movements on that demo, that library is way too popular, it should be too easy to find a demo \$\endgroup\$
    – dimitris93
    Jan 17 '15 at 19:57

edit: If you are reading this and are experiencing bizarre results with your physics bodies or gameloop in general - I highly recommend that you read as many articles on this page as you can handle. Its also highly advised to continue further reading on the topics mentioned, most notably integration techniques and interval arithmetic.

Maybe a bit late to answer this question, but I guess I will give some insight.

Firstly I'm noticing that you are not using a fixed timestep correctly, instead you have coupled the FPS with each update.

The problem comes from the following line

 while (time >= frameRate && updateCount < maxUpdates) {

Every update you are comparing your accumulated delta time against a frame rate of 500000, the delta time will never be this high for a start.

Instead you want to define a TIMESTEP which is based on a maximum fps (50000 if you want) and compare your accumulated frame time against this value.


Have a look at this quick javascript loop implementation

 var start = function(){

    this.gameLoop = requestAnimationFrame((t)=>{interval(t)});


const MAX_FPS = 60;
const TIMESTEP = 1000/MAX_FPS;

var lastFrame = performance.now();
var accumulator = 0;

var gameLoop;

function interval(t){

    var frameTime = t - lastFrame;
    lastFrame = t;

    //add frame times to an accumulator to make up for any excess time
    //for example if the last frame only took 2ms out of a 16ms - we will want to account for this - otherwise the simulation will start to fall behind
    accumulator += frameTime;

    //if the accumulator has enough time to warrant a timestep then we will want to simulate this
    //if the accumlator is greatly over the timestep, then we will want to simulate all the excess time in discrete time steps
    if(accumulator > TIMESTEP){
        update(TIMESTEP) //note the fixed timestep used to ensure every update is consistent

        accumulator -= TIMESTEP


    requestAnimationFrame((t) => {interval(t)});

Finally in your update you will need to take into account the timestep for any motion integration.

    velocity.x += acceleration.x * dt //this dt is a fixed value to ensure consitent movement

Also note that this is a basic Euler integration, there are countless other integration techniques for use in an update, Euler is the most simple but it has many shortcomings.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.