Make online play feel instant despite lag. Client-side prediction of your own player with server reconciliation (replay unconfirmed inputs), smoothing small corrections and snapping large ones, entity interpolation for other players with a render delay, limited extrapolation, angle wrapping, lag compensation (rewind) for hit checks, choosing delays, and what not to predict. Tested module. Use f...
Download skill (.zip) Files on GitHubMultiplayer prediction
The three techniques
| For | Technique | Result |
|---|---|---|
| Your own player | Prediction + reconciliation | Moves the instant you press, corrected quietly when the server disagrees |
| Everyone else | Interpolation (drawn ~100 ms in the past) | Smooth movement between snapshots |
| Hits on moving targets | Lag compensation on the server | “I hit them on my screen” counts |
All three need one shared movement function step(state, input, dt) used by the server and the client.
assets/predict.js
import { createPredictor, createInterpolator, createRewind } from "./predict.js";
const me = createPredictor({ step, initial: { x, y }, smooth: ["x", "y"], snapDistance: 120 });
// fixed 60 Hz:
const seq = me.input(input, 1 / 60); send({ t: "input", seq, ...input });
// on snapshot:
me.reconcile(snap.ents[myId], snap.ack); // ack = last input seq the server applied
// each frame:
me.update(dt); drawPlayer(me.view()); // view = predicted state + fading correction offset
const others = createInterpolator({ delay: 100, maxExtrapolate: 120 });
others.push(snap.time, snap.ents); // server time of the snapshot
const ents = others.sample(clock.now()); // clock from network-synchronization
- Reconcile = take the server’s state, replay the inputs it hasn’t seen yet. With identical
stepcode the error is 0. - Small errors fade over ~0.1 s (
correctionRate); big ones (teleport, respawn) snap. - Interpolation delay ≈ 2 snapshot intervals: 20 Hz sends → 100 ms. Too small = stutter when a packet is late.
- Extrapolation past the newest snapshot is capped, then the entity holds still (no flying off).
- Angles (
a) blend the short way round.
What to predict
Predict: your movement, your aim, starting your own attack animation and sound, UI feedback. Don’t predict: damage, deaths, scores, pickups that others can grab. Show those when the server confirms (or show a soft effect and confirm later).
Lag compensation (server)
const rewind = createRewind({ keepMs: 500 });
each tick: rewind.record(now, positions);
on shot: const seen = rewind.at(now - shooterRtt / 2 - interpDelay); check the hit against seen[targetId]
Cap the rewind (250-500 ms) so a very laggy player can’t hit people who are long gone.
Tested
Node tests: over 120 frames with 100 ms one-way lag, the prediction equals the server exactly; correction offset hides a 20 px jump and fades out; interpolation, short-way angles and capped extrapolation; rewind lookup. In the websocket example with real sockets, predicted and server positions match to 0.1 px (snapshot rounding).
Pitfalls
Different dt on client and server (send fixed steps), predicting with Math.random(), applying the snapshot directly to
your own player (rubber banding), interpolating your own player, and an interpolation delay smaller than the send interval.