Advertisement

Checking sanity of my tick synchronization

Started by January 10, 2014 05:07 PM
4 comments, last by hplus0603 10 years, 10 months ago

So I have been working on my FPS game and its networking lately and I have been thinking about a case I have not really considered yet. My server sends data to the clients on every third simulation frame, this ends up being 20 times per second with my 60 Hz simulation rate. Every packet has the servers simulation frame number (tick) attached to it.

Currently the client synchronizes it's own tick number to the first packet received from the server like this:


int clientTick = serverTick - 3;

And then increases it's own tick for every simulation-frame which passes locally after this. The reason that i subtract 3 from the server tick is because I buffer the packets locally on the client in a de-jitter buffer, and then de-queue them from the buffer based on the client local tick number. So we basically "rewind" 3 ticks in time to setup this local delay to handle packet jitter.

The end result is that we expect to have 1-2 packets in this buffer on the client, and that if we have > 2 packets in the buffer we have drifted behind the server more then intended and need to speed up our local simulation a bit. Now, this seems to be working and is the by far best solution to this problem I have come up with.

Is this a sane way to solve this problem? It seems sane to me, and seems to be working, but just wanna get some feedback.

Yes, it seems sane. You build up a little more latency than you have to, though. With just 3 steps per packet, one packet of buffer might not be so bad.

There's the converse case you need to deal with: The client runs ahead of the server. If you find that it's time to decode more data, but you haven't yet received anything from the server, you want to be bumping the tick offset by one or two. Same thing if you find that you have two packets in the buffer when there should only be one; bump the offset the other way (it sounds like you already do this part.)

enum Bool { True, False, FileNotFound };
Advertisement

So yeah, the inverse where the client is too far ahead, what you are saying then is to just do ++clientTick once or twice to let the client keep stepping forward? I was thinking i could just stop the world on the client completely, I mean this is an FPS after all and if you are not receiving your data on time your experience is going to suck anyway.

Edit: Or maybe you mean to decrease the client tick count if we have no data, actually ;p

Edit: Or maybe you mean to decrease the client tick count if we have no data, actually


Exactly.
enum Bool { True, False, FileNotFound };

Edit: Or maybe you mean to decrease the client tick count if we have no data, actually


Exactly.

This seems to be working wonderfully, one followup question: Would you stall the local tick count to so it stays on the current tick it expects a packet for constantly? basically at the end of every frame, subtract one from the tick count. Currently I am doing this and it seems to be working fine.

Or would you subtract more each frame, and let it "run up" to the frame it's looking for data for every time, and if the data is still not there, reset it and run up to it again?

Would you stall the local tick count to so it stays on the current tick it expects a packet for constantly?


That sounds like a fine way of doing it.

If you want to do extra effort, you can keep a relative speed between client and server, so that if you find that you keep having to stall (or keep having to jump ahead,) you can adjust the scaling factor to adjust for clock rate differences. For 99% of games, though, this is unlikely to be needed.
enum Bool { True, False, FileNotFound };

This topic is closed to new replies.

Advertisement