#Why do i need to create game loop like those on yt with delta and other stuff if my game loop works

1 messages · Page 1 of 1 (latest)

brittle karma
#

Why do i need to create game loop like those on yt with delta and other stuff if my game loop works fine
​​​​java
public void run(){

    while(gameThread != null){

        update();
        repaint();
        try {
            Thread.sleep(9);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }


    }
}

​​​​

ripe reefBOT
#

<@&987246399047479336> please have a look, thanks.

coral hinge
#

I'm not sure what exactly do you mean by delta, but

#

in your example, your thread will always sleep 9 milliseconds no matter how long the update and repaint took

#

but if you want to maintain a constant tickrate and fps, you need to only sleep for the amount you actually need

crimson crystal
#

and even if you have a sleep, the time it takes to run update and repaint changes, so you still have that issue

#

And that's just a simplified explanation

mental patio
#

different computers will run at different speeds based on how fast they update

brittle karma
#

ah okay

#

can you help me write correct loop

#

like pseudocode

pearl wigeon
#

This is THE classic article on game loops

#

I came back to this a year or two ago when I was testing real time sound generation in Java

mental patio
#

I had to take myself step by step through how game loops worked, I can help you out if you're missing specific concepts

brittle karma
#

public void run(){

    double drawInterval = 1000000000/FPS;
    double delta = 0;
    long lastTime = System.nanoTime();
    long currentTime;
    long timer = 0;
    int drawCount = 0;



    while (gameThread != null) {

        currentTime = System.nanoTime();

        delta = delta + (currentTime - lastTime) / drawInterval;
        timer = timer + (currentTime - lastTime);
        lastTime = currentTime;

        if(delta >= 1) {

            update();

            repaint();

            delta--;

            drawCount++;
        }
            if(timer >= 1000000000){
                System.out.println("FPS:" + drawCount );
                drawCount = 0;
                timer = 0;
            }
    }
}
#

this game looop i used

#

but i have hard time breaking it in my mind

mental patio
brittle karma
#

okay can you explein this currentTime = System.nanoTime();

        delta = delta + (currentTime - lastTime) / drawInterval;
        timer = timer + (currentTime - lastTime);
        lastTime = currentTime;
#

also thanks for all help

mental patio
mental patio
#

I would actually suggest using the delta time loop

#

Delta is the time between ticks so you can calculate what fraction of a second that is and multiply it with your movement to get movement/second instead of movement/tick

#

So say your game is running at 100 FPS, every tick (if it’s running at a consistent speed which it’s not) is taking 0.01 seconds because 1/FPS is 0.01. Now say you want to move an object across the screen at a rate of 200 pixels per second. You would do velocityX = 200 * delta. This works because 200 * delta (0.01) is 2 meaning the object is moving right at a rate of 2px/tick, and if there’s 100 ticks per second, that’s 200px/second. This works with any FPS, say 60, 200 pixels * delta (0.016667), 3.3333px/tick, 3.3333*60 = 200.