On this page
Lag compensation: hitting what you saw
You see other players where they were a moment ago: your picture of them is as old as your connection is slow. If the server tested your shot against where they are now, you would have to aim ahead of every running target by your own ping. It does not. For every shot it asks how old the shooter's picture was, and tests against where each soldier stood then. This page is that mechanism, start to end.
Your own movement is handled the other way round, by prediction: see the control block and the prediction walkthrough.
1. How old is the shooter's picture?#
The ping (ClientConnection::handlePacketStatusNotification, bf2_w32ded.exe!0x00548a80). For every delivered packet the connection's last pong round trip is sampled, in seconds, into 32 entries. At 32, the samples below 1.5 s are averaged, and the player's ping becomes the mean of that and the previous average: a slow, smoothed number, in milliseconds, clamped to 1023. A spike does not move it much.
The compensation time (Physics::getLatencyCompensationTime, 0x0069e680), for a remote player:
t = latency x 0.001 + ping x 0.5 x 0.001 seconds
t = min(t, 0.3)Half the ping is the one-way trip; latency is a further field of the player, counted while the switch for the client's window is on. The three numbers are engine variables: the factor 0.5 (phy-latencyComp-pingFactor), the cap 0.3 s (phy-latencyComp-max), and a switch for the client's window. A shooter with a ping above 600 ms is compensated for 0.3 s and must lead the rest himself.
2. Where was the target then?#
Every soldier's physics node keeps a ring of 16 past states (updateLatencyState, 0x0069ee80): after each tick's collision response it stores the translation, the rotation as a quaternion, and the stance. Sixteen ticks are 0.53 s, comfortably more than the cap.
The lookup (getLatencyCompensatedTransformation, 0x0069ef50):
t <= 1/30: the current transformation and stance
else: x = t x 29.999998; n = min(trunc(x), 14); w = 1 - frac(x)
blend entry (newest - n) with weight w and entry (newest - n - 1) with weight 1 - w
the stance of the older of the twoPositions blend in a line, rotations on the sphere. The stance matters as much as the place: a soldier who dropped prone 100 ms ago is tested standing by a shooter whose picture is 150 ms old, and his skeleton is posed for that stance (animation).
The ring restarts#
The ring is cleared at every full reset of the node: a spawn, entering or leaving a vehicle, a ladder, a parachute, going man down. Blending across such a jump would put the soldier halfway between a seat and the ground. Right after one, a shot is tested against the current state.
3. Finding the candidates#
A bullet's tick is a segment, and the server first asks a spatial index which objects the segment might touch. For a shooter with t > 0 it asks a second index, the latency-compensation culler, in place of the usual one for moving objects. Every networked root is in both; in the second its radius is larger:
radius = bounding radius + 0.3 x |velocity| + 0.5The slack is exactly the room a target can have moved in the longest rewind, so a soldier who has since run out of the bullet's path is still offered as a candidate, and the fine test then uses his rewound transformation.
4. The fine test#
checkSoldierVsPoint (0x006a2050): never against the shooter's own soldier; otherwise the soldier's third-person skeleton is evaluated for the rewound stance, placed at the rewound transformation, and the bullet's segment is tested against the capsules on its bones. A hit then goes down the common damage path (damage and armor).
What it does not do#
- The ring described here is the soldier's. The function is the soldier node's, and the notes describe no rewind of vehicles.
- Projectiles still fly. Compensation covers the shooter's picture, not the bullet's travel: a target at 300 m must still be led by the bullet's flight time (projectiles).
- The victim pays. A soldier who ran behind a wall 200 ms ago can still be hit there by a shooter with a ping of 400. Every game of this kind makes that trade; the cap of 0.3 s is where this one stops.
What the port has#
| Part | State |
|---|---|
| the ping average | ported, with a unit test of the 32-sample rule |
| the compensation time, the ring, the lookup | ported from the listing |
| the second culler's segment query | verified: 8,429 segment queries with 83,845 results equal to the original's code, in both programs and both precisions |
| which soldiers are evaluated, and at which tick | 129 of 129 evaluations at the same ticks as the original server |
| the ring's restarts | ported |
| the rewound path in play | not compared with the original server: the scripted clients answer pings at once, so both servers see ping 0 and every recorded hit took the unrewound path. A test client that delays its packets does not exist yet |
| the culler's insertion and radius formula | transcribed; no oracle, as the notes say |
So the pieces are each held, and the whole has not been watched working against the original. The wiki says so because "lag compensation is ported" would promise more.
The same mechanism; the notes do not list a difference in it. Heroes sends ten control blocks a second where the others send twenty, which changes what the client sees and predicts, not this server-side rewind.
How it is tested#
- The history ring (
crates/bf2-server/src/damage_anim.rs): a new history is sixteen entries at the origin, the motion between two entries, Heroes' bracket storing the quotient. - The hit test against rewound bones: an oracle test: the original's own function is run in a process of its own on a list of inputs, and the port must give the same output bit for bit (the oracle) holds the bone transform and the ray against the skeleton (
crates/bf2-world/tests/it_bf2/projectile_oracle.rs). - A second contact (
crates/bf2-server/tests/it_bf2/rot_point_second_answer_bf2.rs): a thrown grenade's contact with a soldier as a traced run of the original gave it.
All five addresses of this page stand in a test. What is not compared with the original is the rewound path in play: the scripted clients answer pings at once, so no scenario exercises a real delay, as the table above says.
In the code#
| What | Where |
|---|---|
| the ping meter | crates/bf2-net/src/join.rs (PingMeter) |
| the compensation time, the ring, the candidates | crates/bf2-server/src/damage_anim.rs (latency_compensation_time, LatencyRing, point_candidates) |
| the second culler | crates/bf2-server/src/collision_world.rs, crates/bf2-physics/src/culler_quad.rs |
| the oracle of the segment query | crates/bf2-physics/tests/culler_oracle.rs |
Notes: notes/subsystems/animation.md (sections 11 and 17), notes/tracks/multiplayer.md.
docs/dev/network/lag-compensation.md · built from bf2-rs e3b4f05 · part of the Battlefield 2 / 2142 / Heroes reimplementation. Not affiliated with EA or DICE.