在多人游戏中使用锁相环同步客户端时间

问题描述 投票:0回答:1

我正在阅读《在线多人游戏的开发和部署》第一卷,正在看第 3 章,关于时间同步的部分。本章摘录如下 -

至于在软件中实现 PLL 本身,它比乍一看要“容易得多”。本质上,它由一个相位检测器(它表示我们对下一个服务器的预测数据包出错了多少,这基本上是predicted_timearrival_time之间的差异)、积分器(可以是简单为“将当前积分器值乘以 k <1 and add new value" on each oscillation), and a variable-frequency oscillator (which can be implemented by simply using the current output of the integrator to calculate the period of the next oscillation, which in turn will be implicitly based on the Client's system clock frequency, but this is exactly the point).

振荡器的输出将是
predicted_time

我特别困惑如何获取积分器值,或者甚至最初将其定义为什么。我可以开始预测下一个服务器数据包为当前时间 + 100 毫秒,但是当涉及到如何使用积分器值来预测下一个值时,我不知所措。

书中提到 k 应非常接近 1,但严格小于 1,并且积分器输出对振荡周期的改变不应超过 1e-4 (.0001) 左右。此外,当积分器的输入为 0 时,振荡器应该生成服务器数据包的正常/预期频率。

如有任何帮助,我们将不胜感激!

c++ time network-programming game-engine
1个回答
0
投票

predicted_period_time = 1/20 // server runs at 20fps last_time_packet_arrived = Time.now() K = 0.5 // This value the author says you need to test to find the best value. while game.is_running() sleep(predicted_period_time) packet = buffer_on_receipt.unqueue() real_measured_time = packet.time_arrived - last_time_packet_arrived error_time = predicted_period_time - real_measured_time predicted_period_time += error_time * K last_time_packet_arrived = packet.time_arrived end while

类似的事情。您基本上是根据误差乘以常数来调整帧循环。

© www.soinside.com 2019 - 2024. All rights reserved.