🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Is time dilation the best way to deal with ping fluctuations?

Started by
2 comments, last by hplus0603 2 years ago

I'm writing an engine with a fixed timestep, which means I can use frame number instead of time in my networking. The client frame number is slightly ahead of the server, ideally 1-2 frames for smooth input buffering (or more in the case of packet loss).

In order for the client to determine what frame number they should be on, the server updates the client with the difference between the last input frame number received from the client and the current frame on the server. Using this difference, the client knows how many frames “off” it is from where it should to be.

My current system dilates time on the client to reach the target frame, usually within 5% of real time unless there is a large gap between the current and target frame. It calculates the number of frames needed to pass at a certain speed for the target to be reached, runs those frames, then waits for the server to report the frame difference before being allowed to adjust time again. When I use tools to change my ping, the frame difference grows and then this brings the frame number back to where it should be.

I've only heard a method like this mentioned somewhere, but never seen any implementation. In my case, where it's very important that the server already has the client's input for a given frame at the time it's ready to simulate, are there better methods to deal with ping fluctuations? Is there an existing open source game I can look at to see a good implementation of this so I can fix stability issues with my own?

Advertisement

Why time dilation?

So that I understand the question: Do you mean “time dilation” in the sense of “slowly drift the clock until you are at the desired offset?”

Or, put another way: What is the other option you're comparing against?

Yes, you need the client to run its local command inputs ahead of the server.

Yes, the server needs to tell you whether you're late or early.

When you find that you're too early, you can slow down the local clock until you're caught up, or you can immediately delay by a tick or two, causing a “jump.” If by “time dilation” mean you “run the clock slower by 5%,” then that's a reasonable choice for the case when you're ahead of where you need to be, as it will create a less jarring hitch than a simple “delay for N frames.”

When you find that you're too late, you need to catch up much quicker, because you will be sending commands that won't take effect because they're too late. Time dilation will just prolong how long it takes before you're in synch, so to catch up, you should immediately step all the necessary steps to get to the target time delay, to reduce player discrepancy. At least, once you're actually behind (not just later than desired, but actually missing the step,) immediately catch up.

So, let's say that you aim for the player to be 2 steps ahead on average. If it's between 1.5 and 3.0 steps ahead, run at regular clock. If it's more than 3.0 steps ahead, slow down clock by 5%. If it's between 0 and 1.5 steps ahead, speed up by 5%. And if it's below 0 steps ahead, immediately step forward by 2.0 + time-behind steps.

enum Bool { True, False, FileNotFound };

This topic is closed to new replies.

Advertisement