0
\$\begingroup\$

Today I wanted to ask you about the server world simulation loop that I have implemented and it acts weird.

I am using Java, LibGDX and KryoNet to create 2d rpg co-op game based on server-clients architecture where server should update clients with game state 20 times per second. Unfortunetly even if I use fixed step on below while(running) loop the game state is sent almost each ms and i honestly cannot see why.

Server simulation loop:

public void gameServerLoop(){

        new Thread(() -> {
            double simulationTicks = 1000000000.0 / 60.0;
            double gatherDataTicks = 1000000000.0 / 20.0;
            double simulationDelta = 0;
            double gatherDataDelta = 0;

            long lastTime = System.nanoTime();
            long lastTimeDataGathered = System.nanoTime();
            boolean running = true;

            while (running){
                long now = System.nanoTime();
                simulationDelta += (now - lastTime) / simulationTicks;
                gatherDataDelta += (now - lastTimeDataGathered) / gatherDataTicks;
                Gdx.app.log("GatheredData Delta", LocalDateTime.now() + " : " + String.valueOf(gatherDataDelta));
                while (simulationDelta >= 1){
                    update();
                    simulationDelta--;
                    lastTime = now;
                }
                while (gatherDataDelta >= 1){
                    Gdx.app.log(String.valueOf(LocalDateTime.now()), "updaing...");
                    gatherDataDelta--;
                    gatherPlayerDataAndSend();
                    gameStateId++;
                    Gdx.app.log("Server ", "Game State id: " + gameStateId);
                    lastTimeDataGathered = now;
                }
            }
        }).start();
    }

logged time until game state updated

[GatheredData Delta] 2019-09-16T08:31:30.880 : 0.0035180000000135858
[GatheredData Delta] 2019-09-16T08:31:30.880 : 0.004894000000013585
[GatheredData Delta] 2019-09-16T08:31:30.880 : 0.006374000000013586
[GatheredData Delta] 2019-09-16T08:31:30.880 : 0.007930000000013586
[GatheredData Delta] 2019-09-16T08:31:30.880 : 0.009562000000013586
[GatheredData Delta] 2019-09-16T08:31:30.880 : 0.011266000000013586
[GatheredData Delta] 2019-09-16T08:31:30.880 : 0.013046000000013587
[GatheredData Delta] 2019-09-16T08:31:30.880 : 0.014900000000013586
[GatheredData Delta] 2019-09-16T08:31:30.880 : 0.016828000000013586
[GatheredData Delta] 2019-09-16T08:31:30.880 : 0.018832000000013584
[GatheredData Delta] 2019-09-16T08:31:30.880 : 0.020908000000013586
[GatheredData Delta] 2019-09-16T08:31:30.880 : 0.023056000000013586
[GatheredData Delta] 2019-09-16T08:31:30.880 : 0.025278000000013588
[GatheredData Delta] 2019-09-16T08:31:30.880 : 0.02757600000001359
[GatheredData Delta] 2019-09-16T08:31:30.880 : 0.03030400000001359
[GatheredData Delta] 2019-09-16T08:31:30.880 : 0.03326200000001359
[GatheredData Delta] 2019-09-16T08:31:30.880 : 0.03638400000001359
[GatheredData Delta] 2019-09-16T08:31:30.880 : 0.03964400000001359
[GatheredData Delta] 2019-09-16T08:31:30.880 : 0.043040000000013595
[GatheredData Delta] 2019-09-16T08:31:30.880 : 0.046592000000013595
[GatheredData Delta] 2019-09-16T08:31:30.880 : 0.0503000000000136
[GatheredData Delta] 2019-09-16T08:31:30.880 : 0.0541440000000136
[GatheredData Delta] 2019-09-16T08:31:30.880 : 0.058118000000013596
[GatheredData Delta] 2019-09-16T08:31:30.880 : 0.062238000000013595
[GatheredData Delta] 2019-09-16T08:31:30.880 : 0.06650000000001359
[GatheredData Delta] 2019-09-16T08:31:30.880 : 0.07089400000001359
[GatheredData Delta] 2019-09-16T08:31:30.880 : 0.07543800000001359
[GatheredData Delta] 2019-09-16T08:31:30.880 : 0.0801180000000136
[GatheredData Delta] 2019-09-16T08:31:30.880 : 0.0849320000000136
[GatheredData Delta] 2019-09-16T08:31:30.880 : 0.08988200000001359
[GatheredData Delta] 2019-09-16T08:31:30.880 : 0.0949660000000136
[GatheredData Delta] 2019-09-16T08:31:30.880 : 0.1001880000000136
[GatheredData Delta] 2019-09-16T08:31:30.880 : 0.1055660000000136
[GatheredData Delta] 2019-09-16T08:31:30.880 : 0.11110000000001359
[GatheredData Delta] 2019-09-16T08:31:30.880 : 0.11679000000001359
[GatheredData Delta] 2019-09-16T08:31:30.880 : 0.12261400000001359
[GatheredData Delta] 2019-09-16T08:31:30.880 : 0.1285760000000136
[GatheredData Delta] 2019-09-16T08:31:30.880 : 0.1346680000000136
[GatheredData Delta] 2019-09-16T08:31:30.880 : 0.14089600000001362
[GatheredData Delta] 2019-09-16T08:31:30.880 : 0.14725800000001363
[GatheredData Delta] 2019-09-16T08:31:30.880 : 0.15375800000001363
[GatheredData Delta] 2019-09-16T08:31:30.880 : 0.16039000000001363
[GatheredData Delta] 2019-09-16T08:31:30.880 : 0.16716800000001364
[GatheredData Delta] 2019-09-16T08:31:30.880 : 0.17407800000001364
[GatheredData Delta] 2019-09-16T08:31:30.880 : 0.18112600000001364
[GatheredData Delta] 2019-09-16T08:31:30.880 : 0.18830400000001363
[GatheredData Delta] 2019-09-16T08:31:30.880 : 0.19563800000001363
[GatheredData Delta] 2019-09-16T08:31:30.880 : 0.20309600000001363
[GatheredData Delta] 2019-09-16T08:31:30.880 : 0.21068800000001361
[GatheredData Delta] 2019-09-16T08:31:30.880 : 0.21840800000001362
[GatheredData Delta] 2019-09-16T08:31:30.880 : 0.22625800000001361
[GatheredData Delta] 2019-09-16T08:31:30.880 : 0.23425200000001362
[GatheredData Delta] 2019-09-16T08:31:30.880 : 0.2423800000000136
[GatheredData Delta] 2019-09-16T08:31:30.880 : 0.25064200000001363
[GatheredData Delta] 2019-09-16T08:31:30.880 : 0.2590640000000136
[GatheredData Delta] 2019-09-16T08:31:30.880 : 0.26763800000001364
[GatheredData Delta] 2019-09-16T08:31:30.880 : 0.27635600000001365
[GatheredData Delta] 2019-09-16T08:31:30.880 : 0.2852080000000137
[GatheredData Delta] 2019-09-16T08:31:30.880 : 0.2942140000000137
[GatheredData Delta] 2019-09-16T08:31:30.880 : 0.30335400000001367
[GatheredData Delta] 2019-09-16T08:31:30.880 : 0.31262000000001366
[GatheredData Delta] 2019-09-16T08:31:30.880 : 0.3220220000000137
[GatheredData Delta] 2019-09-16T08:31:30.880 : 0.3315700000000137
[GatheredData Delta] 2019-09-16T08:31:30.880 : 0.34125600000001366
[GatheredData Delta] 2019-09-16T08:31:30.880 : 0.35108400000001366
[GatheredData Delta] 2019-09-16T08:31:30.881 : 0.3611020000000137
[GatheredData Delta] 2019-09-16T08:31:30.881 : 0.3712600000000137
[GatheredData Delta] 2019-09-16T08:31:30.881 : 0.38155800000001366
[GatheredData Delta] 2019-09-16T08:31:30.881 : 0.3920100000000137
[GatheredData Delta] 2019-09-16T08:31:30.881 : 0.40260200000001367
[GatheredData Delta] 2019-09-16T08:31:30.881 : 0.4133360000000137
[GatheredData Delta] 2019-09-16T08:31:30.881 : 0.4242100000000137
[GatheredData Delta] 2019-09-16T08:31:30.881 : 0.4352420000000137
[GatheredData Delta] 2019-09-16T08:31:30.881 : 0.4464180000000137
[GatheredData Delta] 2019-09-16T08:31:30.881 : 0.4577300000000137
[GatheredData Delta] 2019-09-16T08:31:30.881 : 0.4691840000000137
[GatheredData Delta] 2019-09-16T08:31:30.881 : 0.4807800000000137
[GatheredData Delta] 2019-09-16T08:31:30.881 : 0.4925300000000137
[GatheredData Delta] 2019-09-16T08:31:30.881 : 0.5044640000000137
[GatheredData Delta] 2019-09-16T08:31:30.881 : 0.5165500000000137
[GatheredData Delta] 2019-09-16T08:31:30.881 : 0.5287840000000137
[GatheredData Delta] 2019-09-16T08:31:30.881 : 0.5412060000000137
[GatheredData Delta] 2019-09-16T08:31:30.881 : 0.5538840000000137
[GatheredData Delta] 2019-09-16T08:31:30.881 : 0.5667540000000137
[GatheredData Delta] 2019-09-16T08:31:30.881 : 0.5798100000000137
[GatheredData Delta] 2019-09-16T08:31:30.881 : 0.5930340000000137
[GatheredData Delta] 2019-09-16T08:31:30.881 : 0.6064300000000137
[GatheredData Delta] 2019-09-16T08:31:30.881 : 0.6200580000000137
[GatheredData Delta] 2019-09-16T08:31:30.881 : 0.6339320000000137
[GatheredData Delta] 2019-09-16T08:31:30.881 : 0.6480500000000137
[GatheredData Delta] 2019-09-16T08:31:30.881 : 0.6623740000000137
[GatheredData Delta] 2019-09-16T08:31:30.881 : 0.6769040000000137
[GatheredData Delta] 2019-09-16T08:31:30.881 : 0.6916760000000137
[GatheredData Delta] 2019-09-16T08:31:30.881 : 0.7066700000000137
[GatheredData Delta] 2019-09-16T08:31:30.881 : 0.7218920000000136
[GatheredData Delta] 2019-09-16T08:31:30.881 : 0.7373600000000137
[GatheredData Delta] 2019-09-16T08:31:30.881 : 0.7530160000000137
[GatheredData Delta] 2019-09-16T08:31:30.881 : 0.7688480000000136
[GatheredData Delta] 2019-09-16T08:31:30.881 : 0.7848340000000136
[GatheredData Delta] 2019-09-16T08:31:30.881 : 0.8009800000000136
[GatheredData Delta] 2019-09-16T08:31:30.881 : 0.8172820000000136
[GatheredData Delta] 2019-09-16T08:31:30.881 : 0.8337180000000136
[GatheredData Delta] 2019-09-16T08:31:30.881 : 0.8503120000000136
[GatheredData Delta] 2019-09-16T08:31:30.881 : 0.8670500000000136
[GatheredData Delta] 2019-09-16T08:31:30.881 : 0.8839320000000136
[GatheredData Delta] 2019-09-16T08:31:30.881 : 0.9009560000000136
[GatheredData Delta] 2019-09-16T08:31:30.881 : 0.9181240000000136
[GatheredData Delta] 2019-09-16T08:31:30.881 : 0.9354260000000136
[GatheredData Delta] 2019-09-16T08:31:30.881 : 0.9528620000000136
[GatheredData Delta] 2019-09-16T08:31:30.881 : 0.9704600000000136
[GatheredData Delta] 2019-09-16T08:31:30.881 : 0.9881960000000136
[GatheredData Delta] 2019-09-16T08:31:30.881 : 1.0060820000000137
[2019-09-16T08:31:30.881] updaing...
[Server ] Game State id: 4298

Basicly there are 3 loops in total. the base one which runs as fast as processor allows (while(running)), 2nd which is a world simulation 60 times per second, and a third one which is a game state update that should run 20 times per second. Unfortunetly I end up with a game state being updated each ms. It just looks like time passed is calculated faster (?). I can't find a root cause of this.

I will appreciate any suggestions and help.

\$\endgroup\$
0
\$\begingroup\$

You're updating your lastTime / lastTimeDataGathered values inside your fixed update loops, rather than every time you accumulate a time interval.

That means the interval you add to your simulationDelta and gatherDataDelta accumulators keeps growing with every cycle.

So the first loop after a simulation update, you add one outer loop's worth of time to the accumulator.

On the second loop after a simulation update, you add two outer loops' worth of time to the accumulator, for a total of three.

On the third loop after a simulation update, you add three loops' worth, for a total of six.

Your accumulator values end up growing as a triangular number, quadratically in actual wall time.

So, to fix this, make sure the increment you add to each accumulator in the outer loop is only the time passed since the last outer loop.

You may also want to add a sleep in here somewhere, otherwise your server will keep the thread continually busy, burning most of its time adding tiny fractions nowhere near your thresholds, instead of doing useful work for you.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ ♦ you were right, I have totally missed it. About the thread being busy, I could sleep the thread for a litlle shorter amount of time then the world simulation gap between ticks. Do you recon it would be a good idea? Thank you for the spot with the accumulators ;) \$\endgroup\$ – Jarek Sep 16 '19 at 11:17
  • \$\begingroup\$ I don't think I'd have suggested it if I thought it was a bad idea, but it is early morning and I haven't had my coffee yet, so I'll admit the possibility. ;) \$\endgroup\$ – DMGregory Sep 16 '19 at 11:19

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.