Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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;

            tick();

            delta = frameTime;

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

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

            frames++;
        } else {
            try {
                Thread.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        Display.update();
    }
    destroy();
}

What is wrong with this game loop ?

share|improve this question
    
where do you declare delta variable ? –  Shiro Feb 1 at 16:47
    
double delta is over the constuctor so I don't write it. –  Suak Feb 1 at 17:25

1 Answer 1

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.

share|improve this answer
    
where i can put render ? –  Suak Feb 1 at 19:57
1  
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. –  Mr. Nerd Feb 1 at 20:07

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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