Hello I have game loop where I can change framerate. But when I change framerate to other, delta time not working fine. For example when framerate is lower everything is faster.

Here is game loop:

double delta = 0.0;
private double framerate = 60.0;
private double frameTime = 1.0/framerate;

public void run() {
    runFlag = true;
    int frames = 0;
    double frameCounter = 0;

    double lastTime = (double)System.nanoTime()/(double)1000000000L;
    double unprocessedTime = 0;

    while(runFlag) {
        boolean render = false;

        double startTime = (double)System.nanoTime()/(double)1000000000L;
        double passedTime = startTime - lastTime;
        lastTime = startTime;

        unprocessedTime += passedTime;
        frameCounter += passedTime;

        while(unprocessedTime > frameTime) {
            render = true;

            unprocessedTime -= frameTime;


            delta = frameTime;

            if(frameCounter >= 1.0) {
                this.frames = frames;
                Display.setTitle("  FPS: "+frames+"  DELTA: "+(float)delta);

                frames = 0;
                frameCounter = 0;
        if(render) {

        } else {
            try {
            } catch (InterruptedException e) {

What is wrong with this game loop ?

  • \$\begingroup\$ where do you declare delta variable ? \$\endgroup\$ – dimitris93 Feb 1 '15 at 16:47
  • \$\begingroup\$ double delta is over the constuctor so I don't write it. \$\endgroup\$ – Suak Feb 1 '15 at 17:25

Your delta will be always equal to 1/60, but that's not what delta time is. Delta is the time that passed between current and previous call. Here's an example:

double delta;
double currentTime, lastTime;

while (doingStuff)
    currentTime = getCurrentTime() //You have to define this method somewhere

    delta = currentTime - lastTime;

    lastTime = currentTime;

In this example, delta is the difference between time of last and current iteration of the loop.

The "delta" you calculated is the perfect time you want between frames. And there's a giant difference between the time you want, and the time you actually have.

  • \$\begingroup\$ where i can put render ? \$\endgroup\$ – Suak Feb 1 '15 at 19:57
  • 1
    \$\begingroup\$ I see you just copy code. I don't encourage that. You should be able to figure it out yourself. Only thing you have to change is the delta time calculation. It's not that hard. \$\endgroup\$ – Mr. Nerd Feb 1 '15 at 20:07

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.