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.