多人游戏与网络同步 / 系统课时
预测错了怎样回滚再重放
预测错了怎样回滚再重放
客户端预测能让操作立即生效,但权威快照到达后,本地位置可能已经偏离服务器。本课实现一条完整的校正链路:比较同一确认点的误差、回滚到权威状态、丢弃已确认输入,再按原顺序重放仍未确认的输入。
学习目标
- 理解
ackSeq如何划定已确认输入与待重放输入的边界。 - 实现权威状态回滚、未确认输入重放与误差阈值判断。
- 避免重复执行、错误重放和逻辑状态与表现状态混用。
回滚不是把角色简单拉回去
客户端通常会保存当前预测状态、输入历史,以及必要时每个确认点对应的预测状态。服务器快照至少需要带回权威状态和累计确认序号,例如 { position, velocity, ackSeq }。
收到快照后不能只把 position 覆盖成服务器值。如果本地在快照返回期间又执行了 seq=4、seq=5,直接覆盖会把这两次尚未确认的操作一起抹掉。正确做法是先比较客户端在 ackSeq 时刻的历史预测与服务器权威状态,再决定是否回滚,并只重放 ackSeq 之后的输入。

这里的核心约束是同一输入在一次校正中只能属于一个集合:seq <= ackSeq 已经体现在权威状态里,不能再次重放;seq > ackSeq 尚未被服务器确认,需要继续保留并重放。
用同一确认点检测预测误差
不能拿“当前预测位置”直接和“服务器在 ackSeq 时的权威位置”比较,因为当前预测位置已经包含更晚的未确认输入。误差检测必须对齐到同一个序号:先取客户端执行完 ackSeq 时保存的预测状态,再与服务器快照比较。
下面用一维位置演示完整流程。客户端执行 seq=1..5 后当前预测位置为 5.0;服务器确认到 seq=3,权威位置为 2.8,而客户端在 seq=3 时曾预测为 3.0,所以真正需要校正的误差是 0.2。
const DT = 0.1;
const SPEED = 10;
const RECONCILE_THRESHOLD = 0.05;
function applyInput(state, input) {
return {
position: state.position + input.axis * SPEED * DT,
velocity: input.axis * SPEED
};
}
function buildPrediction(inputs) {
let state = { position: 0, velocity: 0 };
const stateBySeq = new Map();
for (const input of inputs) {
state = applyInput(state, input);
stateBySeq.set(input.seq, { ...state });
}
return { current: state, stateBySeq };
}
function reconcile(prediction, pendingInputs, snapshot) {
const predictedAtAck = prediction.stateBySeq.get(snapshot.ackSeq);
if (!predictedAtAck) {
throw new Error("missing prediction history for ackSeq");
}
const error = Math.abs(predictedAtAck.position - snapshot.position);
const remaining = pendingInputs.filter((input) => input.seq > snapshot.ackSeq);
if (error <= RECONCILE_THRESHOLD) {
return { state: prediction.current, pending: remaining, error, corrected: false };
}
// 从服务器已经确认的权威状态重新开始,只重放未确认输入。
let state = {
position: snapshot.position,
velocity: snapshot.velocity
};
for (const input of remaining) {
state = applyInput(state, input);
}
return { state, pending: remaining, error, corrected: true };
}
function main() {
const pendingInputs = [
{ seq: 1, axis: 1 },
{ seq: 2, axis: 1 },
{ seq: 3, axis: 1 },
{ seq: 4, axis: 1 },
{ seq: 5, axis: 1 }
];
const prediction = buildPrediction(pendingInputs);
const snapshot = { position: 2.8, velocity: 10, ackSeq: 3 };
const result = reconcile(prediction, pendingInputs, snapshot);
console.log(`predicted=${prediction.current.position.toFixed(1)}`);
console.log(`errorAtAck=${result.error.toFixed(1)}`);
console.log(`corrected=${result.corrected}`);
console.log(`pending=${result.pending.map((input) => input.seq).join(",")}`);
console.log(`reconciled=${result.state.position.toFixed(1)}`);
}
main();预期输出:
predicted=5.0
errorAtAck=0.2
corrected=true
pending=4,5
reconciled=4.8结果中的 4.8 来自服务器确认的基线位置 2.8,再重放 seq=4、seq=5 各产生的 1.0 位移。这样误差判断和重放都与同一个确认边界对齐。
把输入历史当作可重放日志
输入历史必须保存重算所需的最小信息,而不是只保存“角色已经移动到哪里”。例如移动可以记录方向、按钮状态和固定步长;技能则可能需要记录技能 ID、目标信息和本地指令序号。确认点历史只需保留到服务器可能回执的窗口,确认后即可释放更早状态。
重放成立有三个前提:
- 同一输入在客户端预测和重放时走同一套模拟规则,不能一边按帧率更新、一边按固定步长更新。
- 输入按
seq严格递增并按原顺序执行,服务器回执使用累计ackSeq,不要只返回“最后收到哪个包”。 - 已产生外部副作用的逻辑要与可重放模拟隔离。例如音效、埋点、网络发送不能在重放时再次触发,否则一次输入可能产生多次表现或请求。
校正链路的验证点
调试时至少记录 snapshot.ackSeq、同一确认点的误差、丢弃后的 pending 数量和重放后的状态。出现频繁回滚时,先判断是网络延迟导致 pending 变长,还是客户端与服务器的模拟规则本身已经不一致。
验收一条正确链路时,应能观察到:服务器确认序号单调前进;seq <= ackSeq 的输入从缓存中移除;误差比较使用同一 ackSeq 的历史状态;重放顺序与原始输入顺序一致;相同权威快照与相同 pending 输入得到相同结果。满足这些条件后,回滚不会把已确认输入执行两次,也不会漏掉仍在飞行中的本地操作。