多人游戏与网络同步 / 系统课时
综合实现移动预测与命中补偿
综合实现移动预测与命中补偿
这一节把远端插值、本地预测、服务器纠错和历史命中回溯接成一条可运行链路。目标不是堆叠四套算法,而是明确各自使用的时间、状态和确认边界,并用统一日志验证一次移动与射击从客户端到服务器的完整结果。
学习目标
- 组合远端插值、本地预测、服务器纠错与历史命中回溯。
- 用输入序号、服务器时间和历史快照避免重复执行或错误回溯。
- 为位置纠错和命中判定补上可定位问题的观测字段。
先固定一条权威链路
四种补偿不能共享同一份“当前状态”。本地角色为了手感先按输入预测;服务器按输入序号执行权威模拟,并在快照中返回 ackSeq;客户端收到快照后重置逻辑状态,只重演 ackSeq 之后的输入。远端角色则不做本地预测,而是在一个稍早的渲染时间上对服务器快照插值。
射击判定又是另一条时间路径:客户端发送开火时间和必要的瞄准数据,服务器把开火时间映射到自己的权威时间轴,在允许的历史窗口内恢复目标位置,再执行命中测试。这样移动纠错与命中回溯都依赖服务器状态,但不会把“当前渲染位置”误当成“开火时历史位置”。

这条链路的关键边界只有三个:seq 决定哪些本地输入仍需重演,serverTime 决定远端插值采样位置,shotTime 决定服务器回溯位置。不要用客户端收到快照的本地时间替代后两者。
用最小可运行模型串起来
下面的 Node.js 脚本把三条路径放进同一次确定性模拟:远端角色在 150 ms 的渲染时刻插值;本地角色先预测三个输入,再用服务器确认到 seq=2 的快照纠错并重演;同一台服务器随后把射击回溯到 150 ms 的目标历史位置。
function lerp(a, b, t) {
return a + (b - a) * t;
}
function sampleAt(history, time) {
if (time <= history[0].time) return history[0].x;
if (time >= history[history.length - 1].time) {
return history[history.length - 1].x;
}
for (let i = 0; i < history.length - 1; i++) {
const left = history[i];
const right = history[i + 1];
if (time >= left.time && time <= right.time) {
const alpha = (time - left.time) / (right.time - left.time);
return lerp(left.x, right.x, alpha);
}
}
throw new Error("history gap");
}
class PredictedClient {
constructor() {
this.x = 0;
this.pending = [];
}
predict(seq, dx) {
this.x += dx;
this.pending.push({ seq, dx });
}
reconcile(snapshot) {
// 先采用服务器权威位置,再只重演尚未确认的输入。
this.x = snapshot.x;
this.pending = this.pending.filter((input) => input.seq > snapshot.ackSeq);
for (const input of this.pending) {
this.x += input.dx;
}
}
}
const targetHistory = [
{ time: 100, x: 10 },
{ time: 200, x: 12 },
{ time: 300, x: 14 }
];
const remoteRenderTime = 150;
const remoteX = sampleAt(targetHistory, remoteRenderTime);
const client = new PredictedClient();
client.predict(1, 1);
client.predict(2, 1);
client.predict(3, 1);
const predictedBeforeSnapshot = client.x;
// 服务器已处理到 seq=2,但权威模拟得到的位置略小于客户端预测。
client.reconcile({ ackSeq: 2, x: 1.8 });
const reconciledX = client.x;
const serverNow = 300;
const shotTime = 150;
const maxRewindMs = 200;
const rewindAgeMs = serverNow - shotTime;
if (rewindAgeMs > maxRewindMs) {
throw new Error("shot is outside rewind window");
}
const rewoundTargetX = sampleAt(targetHistory, shotTime);
const aimX = 11;
const hitTolerance = 0.25;
const hit = Math.abs(aimX - rewoundTargetX) <= hitTolerance;
console.log(`remote render @150ms -> x=${remoteX.toFixed(2)}`);
console.log(`local predicted before snapshot -> x=${predictedBeforeSnapshot.toFixed(2)}`);
console.log(
`local reconciled ack=2 -> x=${reconciledX.toFixed(2)}, pending=${client.pending.length}`
);
console.log(
`rewind shot age=${rewindAgeMs}ms -> targetX=${rewoundTargetX.toFixed(2)}, hit=${hit}`
);
console.log(
JSON.stringify({
shotId: "s-1042",
ackSeq: 2,
correctionDistance: Number(
Math.abs(predictedBeforeSnapshot - reconciledX).toFixed(2)
),
rewindAgeMs,
hit
})
);预期输出:
remote render @150ms -> x=11.00
local predicted before snapshot -> x=3.00
local reconciled ack=2 -> x=2.80, pending=1
rewind shot age=150ms -> targetX=11.00, hit=true
{"shotId":"s-1042","ackSeq":2,"correctionDistance":0.2,"rewindAgeMs":150,"hit":true}这个结果说明三个边界同时成立:远端显示采样历史快照,本地纠错没有重复执行 seq=1 和 seq=2,命中判定也没有拿服务器当前 300 ms 的目标位置代替 150 ms 的历史位置。
让不同补偿只消费自己的数据
实际项目中可以共用快照传输通道,但不要让四个子系统直接互改状态。远端插值只读带 serverTime 的远端快照缓冲;本地预测只追加自己的输入历史;服务器纠错只通过 ackSeq 淘汰已确认输入;命中回溯只读服务器保存的历史碰撞状态。
当逻辑状态被服务器快照纠正时,可以立即采用权威位置,再单独让渲染层平滑追上逻辑层。这样玩家看到的修正较柔和,但下一次碰撞、输入重演和射击请求仍基于确定的逻辑状态。
用统一字段还原一次争议
综合链路最容易出现“画面看起来对,判定却不对”的问题,因此日志至少要能把一次输入纠错与一次射击串起来。移动侧记录 ackSeq、未确认输入数量和 correctionDistance;射击侧记录 shotId、shotTime、rewindAgeMs、回溯目标位置与最终 hit。
如果还要分析远端画面抖动,再补充快照的 serverTime、客户端选择的 renderTime 和缓冲深度。这样可以区分三类问题:画面插值缺少快照、客户端预测逐步漂移、服务器回溯使用了错误时间,而不是只看一个笼统的 RTT。
按边界验收整条链路
验收时先验证状态正确,再观察画面是否平滑。给输入连续编号,制造一次客户端与服务器移动结果的小偏差,确认快照到达后只重演 ackSeq 之后的输入;随后在同一历史缓冲中选择一个早于 serverNow 的合法 shotTime,确认命中测试使用回溯位置。
再分别扩大插值缓冲、制造更大的预测误差和缩小最大回溯窗口。预期现象应该彼此独立:插值缓冲只改变远端显示延迟,预测误差只增大纠错距离,超过回溯窗口的旧射击则应被服务器拒绝。能把这三种现象在日志里清楚区分,才说明综合补偿链路不仅能运行,也真正具备可调试性。