BF2.US bf2-rsdeveloper wiki Parity Discord bf2.us
On this page

The frame and the tick

BF22142Heroes

Refractor 2 simulates at a fixed 30 ticks a second and draws as often as it can. Everything that must be the same on the server and on every client happens in a tick; everything that only has to look smooth happens in a frame. This page walks through one frame of the main loop and then through one tick.

simulation ticks: 1/30 s each, whatever the frame rate tick 0 tick 1 tick 2 tick 3 tick 4 tick 5 tick 6 tick 7 tick 8 client frames (up to lockFps = 100 a second); the number is the ticks that frame runs 0 0 1 0 1 2 0 1 0 1 0 0 1 0 a slow frame catches up: 2 ticks (at most 10) what is drawn: alpha = (t - previous tick) / (1/30), clamped to [-0.1, 1.1] big dots: tick states; small dots: the moments drawn, each blended between the two ticks around it BF2Engine::updateFrame bf2_w32ded.exe!0x00403570, BF2.exe!0x0040ca80; TickClock::tick 0x00656a60
Frames have any length; ticks do not. A frame runs as many ticks as have come due (often none on a fast client), and the picture is blended between the last two tick states.

One frame of the main loop#

BF2Engine::updateFrame (bf2_w32ded.exe!0x00403570, client BF2.exe!0x0040ca80) is the body of the main loop in both programs. Its steps, in order:

  1. Quit and pause. The quit flag ends the loop; a process without a game sleeps 100 ms.
  2. The frame's target. 1 / lockFps (100 by default, 60 on a dedicated server), unless a fixed-step demo forces 1/30 s.
  3. The client waits busily until that time has passed since the previous frame. The dedicated server does not.
  4. The timer. Game time advances by the real time that passed, times the time scale. The frame's own delta is clamped to [0.00001, 1/15] s (0x00402500).
  5. The tick clock says how many ticks are due (below).
  6. Client: BF2FrameInterpolator::preSimulate (BF2.exe!0x0045bbb0) takes a snapshot of the transforms of the objects in view.
  7. Game::simulate(ticks): the simulation ticks, when a game exists and the time scale is not 0.
  8. Per-frame updates: the event manager and the systems that run on frame time rather than tick time.
  9. Client: interpolate, draw, restore (BF2.exe!0x0045c190, 0x0045c4b0): the objects are put between their last two tick states, the frame is drawn, and the objects are put back.
  10. The throttle. A dedicated server sleeps what is left of 1/30 s, less the variable server-sleep-overhead-time (default 0.005 s): at most 30 frames a second, one tick a frame on average.

The tick clock#

TickClock::tick(gameTime) (bf2_w32ded.exe!0x00656a60, BF2.exe!0x006a41c0) counts the whole steps of 1/30 s (the float 0x3d088889) since the last tick:

Steps dueWhat the clock does
0nothing: this frame draws between the same two ticks as the last
1 to 10previous = next, last += n x step, next = last + step; the frame runs n ticks
11 or morethe clock gives up catching up: last = previous = next = now, and the frame runs 10 ticks

Set the frame rate under 30 and every frame runs more than one tick; set it high and most frames run none. Add one slow frame of more than a third of a second and the clock stops catching up.

The arithmetic is x87 double, not float. A decompiler shows float casts here that the listing does not have (fild / fmul / fadd qword at 0x00656acf); this is one of the places where the port was written from the disassembly.

The blend#

BF2FrameInterpolator::interpolate(gameTime, clock.previous) computes alpha = (t - previous) / (ticks x 1/30), 0 when the frame ran no tick, clamped to [-0.1, 1.1]. The chase camera is drawn from the blended vehicle, not from its tick state, which is why a vehicle does not shake in its own camera on a fast client.

Writing a browser client

A page must take its frame time from the animation frame's own time stamp, not from a clock read inside the frame. Reading a clock gives a time that jitters against the display's refresh, and the blend factor jitters with it.

One tick#

Game::simulateTick (bf2_w32ded.exe!0x0044a840; GameServer::simulateFrame BF2.exe!0x004cf470, bf2142ded!0x080b55d0, BF2142.exe!0x004afb20) runs, in this order:

  1. The round's state machine (ServerGameLogic::update 0x00459950): pre-game, playing, end of round; a reset takes away the objects created after the load and resets control points and spawners.
  2. The objects' update (ObjectManager::updateObjects 0x006e2270), unless paused: every object's own update, the control points, the object spawners, then the objects marked for destruction go.
  3. The physics, five stages in a fixed order (the picture below).

After the frame's ticks the server writes the packets: per connection whose packet timer is due, the control object, the scope pass and one packet (packets, ghosts).

Game::simulateTick: after the objects' update, before the packets 1 TitanManager::setTick 2142 only: the Titans move first, so nodes and responses meet them at this tick's state 2 players' node stage the players in list order: each one's soldier on foot, or the tree of his seat 3 PhysicsNodeManager::update every other node: the vehicles nobody drives, the soldiers without their own update 4 ResponsePhysicsManager::updateAllObjects those soldiers' collision passes, the rigid list, then their solve 5 players' response stage the players in list order again: soldier or seat tree whole; then the vehicles' frame end bf2_w32ded.exe!0x0044a840, 0x00445b10, 0x00445b70; bf2142ded!0x080b55d0, 0x080b5d70, 0x080b5e20
The physics of one tick. The players come first and last, each in list order; everything nobody steers runs between them.

Why the players have stages of their own#

A player's soldier, or the whole tree of the vehicle he sits in, is simulated at his turn, in the order of the player list, once for the nodes (stage 2, Game::updateControlledTreeNodes bf2_w32ded.exe!0x00445b10) and once for the collision responses (stage 5, Game::updateControlledTreeResponses 0x00445b70). The objects nobody steers run between the two, in the managers' own order. The order decides who pushes whom when two players' vehicles touch in the same tick, so a port that simulates "all vehicles, then all soldiers" gives other results in exactly the cases a player notices: a ram, a road kill, a soldier standing on a moving vehicle.

In Battlefield 2142 the Titans move first (TitanManager::setTick): the objects update with the Titans where the tick before left them, and the nodes and the responses then meet them at this tick's state.

Heroes
Heroes

Heroes' frame was not read to its end yet. The port keeps the order it had there: every vehicle node, every soldier in the managers' passes, every player's tree.

How it is tested#

What the tests prove here is order, which no single function's test can:

  • crates/bf2-server/tests/it_bf2/soldier_tick_order_bf2.rs builds a server with several players and asserts where in a tick each soldier's physics runs: a player's own soldier in the player's stage, the others in the manager's, and a driver between two walkers takes his turn between theirs. It was written from the original's listing of the tick.
  • The frame timer's tests (crates/bf2-client/src/frame.rs, crates/bf2-engine/src/timing.rs) hold the arithmetic of this page: thirty ticks a second at sixty frames, the coarse clock, the catch-up after a long frame.
  • crates/bf2-client/tests/it_bf2/spawn_frame_order_bf2.rs and predict_titan_order_2142.rs do the same for the client's frame.
  • In play: the soldier movement and vehicle rigs compare positions with the original server's on every tick of a recording, so a step in the wrong place shows as a state that differs one tick early or late (test rigs).

Not proven by a test of its own: the order of the render half of the client's frame, which is checked by eye on the render pipeline's stage pictures.

In the code#

WhatWhere
the framecrates/bf2-server/src/lib.rs (Session::frame), crates/bf2-client/app/src/client.rs
the tick clock, the timercrates/bf2-engine/src/timing.rs (TickClock)
the round state machine and the objects' updatecrates/bf2-server/src/world_tick.rs (tick)
the five physics stagescrates/bf2-server/src/lib.rs (tick_physics)
the stages' order as a test reads itcrates/bf2-server/src/tick_order.rs (a log that only a test starts)

Notes: notes/subsystems/engine.md section 7 (the main loop), notes/subsystems/client.md (frame interpolation).

The server's side of the loop, from its command line to its first tick: start-up and the main loop.

Source: docs/dev/engine/tick-order.md · built from bf2-rs e3b4f05 · part of the Battlefield 2 / 2142 / Heroes reimplementation. Not affiliated with EA or DICE.