Tetramputechture
Tetramputechture

Reputation: 2921

Fixed Time Step in Game Loop

I have this game loop:

public void show() {

        // other stuff...

        // set delta time
        float currentTime = clock.getElapsedTime().asSeconds();
        float dt = currentTime - lastTime;

        // iterate through entities
        for (Entity e : entities) {
            e.draw();
            e.update(dt);
        }

        // other stuff...

        lastTime = currentTime;
    }

How would I implement a fixed time step for the update process? That is, how can I update my process based on a constant fps (not updating every time the loop runs?) I know I'll just iterate through entities twice (one for update, one for draw), but how would I fix a time step for the update process?

Upvotes: 1

Views: 1378

Answers (1)

lolwel21
lolwel21

Reputation: 67

I would recommend using multiple Threads, if you can:

you'll want a separate class containing a clock on which to call a frame update. For structure purposes, you might want this class to create an instance of the window class.

public void run(){
    for(;;){
        Thread.sleep([milleseconds between frames]);
        [graphic class].nextFrame();
    }
}

If you need it, here's a tutorial on Threads: https://docs.oracle.com/javase/tutorial/essential/concurrency/runthread.html

Upvotes: 1

Related Questions