协作头脑风暴
brainstorming
在实现前澄清需求、探索设计选择,并形成清晰的开发方向。
multiplayer-netcode【开发与工程】Expert multiplayer netcode - transport, client prediction, server reconciliation, interpolation, lag compensation, rollback, lobbies, and replication architecture.
独立 GitHub 快照 · 自动分类 · 以原作者文档为准
Building robust multiplayer requires understanding latency, jitter, bandwidth, and how to hide them. This guide covers the full stack: transport, protocol design, client-server architecture, prediction, reconciliation, interpolation, lag compensation, rollback, and scalability.
Unity of correctness over the wire is bounded by RTT (round-trip time).
Local: editorial latency
LAN: 1-10ms -> near-identical
Wifi/regional: 20-80ms
Internet worst: 150-250ms
Netcode turns "what happened at t" into "what each client believes happened at t".
| Aspect | TCP | UDP |
|---|---|---|
| Delivery | guaranteed, ordered | best-effort |
| Use | HTTP auth, file sync, chat | game state every frame |
| Problem | head-of-line blocking, retransmit stalls | reordering, loss |
Rule: game-critical state on UDP (custom reliable layer or RakNet-like reliable-UDP).
Split messages into channels:
Reliable-UDP uses ACKs + retransmit with sequence numbers; ack a byte range, not one packet.
[Magic][Seq#][Ack#][AckBits][MessageCount][Message0][Message1]...
Header + per-message headers with bitpacking. Aim <1200B (Ethernet MTU 1500 minus IP/UDP headers); fragment bigger messages.
Client ----inputs----> Server ----authoritative state----> Clients
<----snapshots (30-60Hz)----
| Game | Type |
|---|---|
| Competitive FPS/MOBA | server-authoritative + prediction |
| Top-down RTS/co-op | lockstep deterministic |
| Small co-op party | listen server with a client |
| Battle royale (40+ players) | dedicated server clusters |
Client runs a local simulation of its controlled entity:
struct InputState { bool forward, back, left, right, fire; uint16_t seq; };
void ClientFrame() {
InputState in = CaptureInput();
pending.push_back({in, localTick});
ApplyToLocalSimulation(in); // PREDICT immediately
SendInputToServer(in); // UNRELIABLE channel
}
Authoritative snapshots from server; client compares against its own predicted saved state at that point in time:
Server sends: tick, position, velocity, HP
Client: find the local state saved under same tick (input seq)
if position differs by > threshold -> snap BACK correction,
then re-apply all pending inputs up to now (fast-forward)
void OnSnapshot(tick, serverState) {
Saved& saved = history[tick];
if (distance(saved.pos, serverState.pos) > tolerance) {
world.state = serverState; // correct
for (pending input after tick) ApplyToLocalSimulation(input); // re-simulate
}
}
Render other players via interpolation, not prediction:
serverTime - interpolationDelay (e.g., 100ms = RTT) → smooth motion.Never mix interpolation with prediction on the same entity without buffers — jerky or doubled.
Server-hit detection at the shooter's perceived time:
Implementation notes:
| Technique | Gain |
|---|---|
| Bitpack to minimum bits (position into quantized grid) | ~50% |
| Skip unchanged actors (only send those changed) | huge |
| Aggregate movement (encode relative to previous) | ~30% |
| Interleave (send each entity at 15-30Hz instead of 60) | bandwidth/priority control |
| Contact-rate based interest management | scalability |
Don't send everything to everyone. Send only what's "interesting":
(relevance/changeLevel), flush in remaining bandwidth.Client needs a server-relative clock:
Client sends: t_client (its clock at send)
Server replies with t_server + server timestamp
RTT ≈ (t_nowClient - t_client)
Clock offset ≈ t_server - ((t_nowClient + t_client)/2)
Use smoothed RTT (EWMA) so snapshots' timestamps convert into client time reliably. Sync at load + periodically.
Opposite of prediction-with-snapshots: keep a rolling buffer of the last N ticks of world state. When input arrives for a past tick:
Deterministic sim highly recommended for exact rollback.
skills/game/game-engine/multiplayer-netcode/SKILL.md — engine-level netcode architectureskills/game/game-engine/ecs-pattern/references/ecs-netcode.md — deterministic simulation patternsskills/game/game-development/physics-engine/SKILL.md — authoritative physics stepskills/game/unity/SKILL.md (§9), skills/game/unreal/SKILL.md (§7), skills/game/godot/SKILL.md (§8) — engine netcode APIs