Hi,
This is a difficult problem I’ve been struggling with that’s not really about network API’s but about how to recieve server updates on the client using the following architecture:
Client-server framework where clients run asynchronously
the server sends the whole game world to clients who replace their old one
the game world is updated by the clients between server updates using the time elapsed since the last re-draw,
all game worlds have a nano-second time-stamp of when they were last updated
clients and server do not coordinate their times and no ping is measured.
My client code is as follows, but is this the best way to calculate the new time elapsed after a server-update?
long nanoTimeLastUpdated = 0;
long newGameWorldRelativeNanos = 0;
// + means the new gameWorld is ahead of the replaced one, - means it is behind.
public void run(){
nanoTimeLastUpdated = System.nanoTime();
while(shouldRun){
long currentTime = System.nanoTime();
long timeSinceLastUpdate = (currentTime - nanoTimeLastUpdated);
long timeElapsedNanos = timeSinceLastUpdate - newGameWorldRelativeNanos;
if (timeElapsedNanos > 0){
gameWorld.update(timeElapsedNanos);
}
else{
// do nothing, can't update the game world with a negative time
}
newGameWorldRelativeNanos = 0;
nanoTimeLastUpdated = currentTime;
view.reDisplay();
doMinSleep(); // Thread.sleep(...) done here
networkPlayer.recieveAll();
// calls setGameWorld(...) below if a new gameworld was recieved from server. Otherwise does nothing.
}
}
public void setGameWorld(GameWorld gameWorld, long oldGameWorldTotalNanosElapsed){
newGameWorldRelativeNanos = (long)(gameWorld.getTotalNanosElapsed() - oldGameWorldTotalNanosElapsed);
this.gameWorld = gameWorld;
}
This method seems to work OK in a LAN setup, but on the clients it is much less smooth than on the server and I am wondering whether it could be due to the wrong time being chosen to update the game world when the new game world is recieved from the server. It’s numbing my brain trying to visualise the parallel server and client game worlds and their different timing.
Many thanks,
Keith
PS: I can’t determine whether the client bumpyness is only the GC or my own faulty code above