0

i'm a beginner at java. I'm trying to learn game developing. I'm watching the tutorials of this great programmer on youtube! I'm following him exactly and reached up to "#9 - Basic Collision Bounds" of this playlist.

He created the following game loop...

public void run(){
        init();
        long lastTime = System.nanoTime();
        final double amountOfTicks = 60.0;
        double ns = 1000000000 / amountOfTicks;
        double delta = 0;
        int updates = 0;
        int frames = 0;
        long timer = System.currentTimeMillis();

        while(running){
            long now = System.nanoTime();
            delta += (now - lastTime) / ns;
            lastTime = now;
            if(delta >= 1){
                tick();
                updates++;
                delta--;
            }
            render();
            frames++;

            if(System.currentTimeMillis() - timer > 1000){
                timer += 1000;
                System.out.println(updates + " Ticks, Fps " + frames);
                updates = 0;
                frames = 0;
            }

        }
        stop();
    }

I'm very confused about this method. How is this method functioning? How the following variables are working?

amountOfTicks

delta

updates

frame

I understand explaining this here is not very feasible but please help me. You can also give me any reference to study and understand this. If you need any further information, i'll provide it straight away!

1
  • i think they are calculations so he can control what happens at 60 frames per second Commented Nov 3, 2014 at 0:37

2 Answers 2

4

The variables have been explained, but if you are still having trouble understanding what a game loop does in a general sense (I don't know how much he explained in the tutorial), hopefully I can shed some light from what I have learned:

Basically, the game loop, well, runs (renders, updates, etc.) the game of course, but it also sets a constant, controlled speed at which the game operates.

For example, imagine you wanted to make a 2D RPG game, and you want a mob (character) to move across the screen at, say, 200 pixels per second. You can do this with a loop, but the thing is, different computers will cause the character to move faster or slower at different times because the computer will just execute the code in the loop over and over as fast as it can.

This is why you implement code to control how fast the code is executed, and thus, how fast the game updates. You can do this by using the real world time (using methods that get time in nanoseconds). You limit the game to only updating the frame 60 times per second.

Using the if statement in the while (running) loop, you make the game update only when the program goes through the loop and the time since the last time the if statement executed is atleast 1/60 of a second.

Hopefully that makes sense. Sorry for the long response. If you have more questions feel free to ask!

Sign up to request clarification or add additional context in comments.

Comments

1

amountOfTicks basically defines how many ticks you want to have in one second! so with the value of 60 the method tick() will be called approx. 60 times a second. within in the tick() method usually your game logic should happen!

the delta describes the time difference in nanoseconds between System.nanoTime (from now) and the last tick(). When that value gets above 1, the tick is called and the delta is reduced by one.

Every second the 2nd if-statement prints how often the screen was rendered in that second and how many updates took place. Updates is incremented with every tick() so this should be around 60. the frames counter will be a lot higher, depending on the framework and the underlying hardware.

what I find weird is, that render() is possibly called very often. This can be a very expensive call, but this depends on the given framework and hardware.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.