views:

855

answers:

5

I've a server/client arcitecture implemented, where all state changes are sent to the function, validated and broadcasted to all clients connected. This works rather well, but the system does not maintain syncrynization between the client instances of the game as of now.

If there happened to be a 5 second lag between the server and a particular client then he would recieve the state change 5 seconds after the rest of the clients thus leaving him with game state out of sync. I've been searching for various ways to implement a syncronization system between the clients but haven't found much so far.

I'm new to network programming, and not so naive to think that I can invent a working system myself without dedicating a severe amount of time to it. The ideas I've been having, however, is to keep some kind of time system, so each state change would be connected to a specific timestamp in the game. That way when a client recieved a state change, it would know exactly in which period of the game the changed happened, and would in turn be able to corrolate for the lag. The problem with this method is that in those n seconds lag the game would have had continued on the client side, and thus the client would have to rollback in time to update for the state change which definitely would get messy.

So I'm looking for papers discussion the subjects or algorithms that solves it. Perhaps my whole design of how the multiplayer system works is flawed, in the sense that a client's game instance shouldn't update unless notion is recieved from the server? Right now the clients just update themselves in their game loop assuming that any states haven't changed.

A: 

Your best option is to send the changes back to the client from the future, thereby arriving at the client at the same point in time it does for other clients that does not have lag problems.

Lasse V. Karlsen
THat's an obvious answer, but surely opens up a potential flaw of people bottlenecking their machine purposefully before speeding it up just after a refresh? Having the chance to carry out actions before anyone else..?
Ian
Sending the packet back in time is the obvious answer? wow...
Martin
+5  A: 

The basic approach to this is something called Dead Reckoning and a quite nice article about it can be found here. Basically it is a predication algorithm for where entities positions will be guessed at for the times between server updates.

There are more advanced methodologies that build on this concept, but it is a good starting point.


Also a description of how this is handled in the source engine (Valve's engine for the first Half Life game) can be found here, the principle is basically the same - until the server tells you otherwise use a prediction algorithm to move the entity along an expected path - but this article handles the effect this has on trying to shoot something in more depth.

Martin Harris
+6  A: 

The best resources I've found in this area are these two articles from Valve Software:

Iceman
+2  A: 

If client see events happening at the rate the server is feeding him, which is the normal way to do it (I've worked with protocols of Ultima Online, KalOnline and a little bit of World of Warcraft), then this momentaneous 5 secounds delay would just make him receive this 5 secounds of events all at once and see those events passing really fast or near instantly, as other players would see him "walking" really fast for a short distance if his outputs delay too. After that everything flows normally again. Actually, except for graphic and physics normalization, I can't see any special needs to make it synchronize properly, it just synchronize itself.

If you ever played Valve games in two near computers you would notice they don't care much about minor details like "the exact place where you died" or "where you dead body gibs flyed to". It is all up to client side and totally affected by latency, but this is irrelevant.

After all, lagged players must accept their condition, or close their damn eMule.

Havenard
So you're saying that I should go back in time on my client and reupdate the state of the game? Say I've a player moving south. He will continue to do this on all client machines without any word from the server. If at some point he starts moving north, then all the clients will still percieve him as moving south until he they recieve notice, and once they do, they'll have to go back in time and reupdate his position since he actually wasn't moving south all the time.Since going back in time and reevaluating is not feasible I guess I could send player absolute positioning at specific inteval
Qua
Thats now how it should be done. This movement interpolation must have a limit, he will hang some few steps ahead unless server intervine with new information about his movements.
Havenard
In KalOnline it is made by coordenate correction, each step he does updates the player xyz point in the world with 3 signed bytes for X, Y and Z. Other clients just interpolate the model position to the final xyz point. The interpolation is not even skipped as new changes come, it just consider the new final xyz from that moment on.
Havenard
+2  A: 

There will never be a way to guarantee perfect synchronisation across multiple viewpoints in real time - the laws of physics make it impossible. If the sun exploded now, how could you guarantee that observers on Alpha Centauri see the supernova at the same time as we would on Earth? Information takes time to travel.

Therefore, your choices are to either model everything accurately with latency that may differ from viewer to viewer (which is what you have currently), or model them inaccurately without latency and broadly synchronised across viewers (which is where prediction/dead reckoning/extrapolation come in). Slower games like real time strategy tends to go the first route, faster games go the second route.

In particular, you should never assume that the time it takes to travel will be constant. This means that merely sending start and stop messages to move entities will never suffice under either model. You need to send periodic updates of the actual state (typically several times a second for faster games) so that the recipient can correct error in its predictions and interpolations.

Kylotan