Walk one tile in Helbreath and your character crosses 32 pixels. How those 32 pixels are spread over time decides whether the walk looks smooth, whether it looks the same on a 60 Hz screen and a 240 Hz one, and whether two players watching the same unit see it in the same place. This is how Helbreath's movement went from animation frames on a fixed clock to delta time, through a small prototype called Tiled that I wrote only to understand how that works.
1. The timestep: a frame every few milliseconds
The original client does not move units. It animates them. Every kind of unit has a table that says how long each frame of each action lasts, in milliseconds:
m_stFrame[i][DEF_OBJECTSTOP].m_sFrameTime = 32;
m_stFrame[i][DEF_OBJECTMOVE].m_sFrameTime = 38 / 1.1;
m_stFrame[i][DEF_OBJECTRUN].m_sFrameTime = 21 / 1.1;
m_stFrame[i][DEF_OBJECTATTACK].m_sFrameTime = 41 / 1.1;
m_stFrame[i][DEF_OBJECTMAGIC].m_sFrameTime = 40;
Each time the client draws, it walks over the visible tiles, and for every unit it asks one question: has more time gone by than one frame lasts? If so, the frame number goes up by one:
dwFrameTime = m_stFrame[m_pData[dX][dY].m_sOwnerType][m_pData[dX][dY].m_cOwnerAction].m_sFrameTime + iDelay;
if ((dwTime - m_pData[dX][dY].m_dwOwnerTime) > dwFrameTime)
{
m_pData[dX][dY].m_cOwnerFrame++;
// ...
m_pData[dX][dY].m_dwOwnerTime += dwFrameTime;
}
Look at the last line, because it is better than it looks. It does not set the unit's time to now. It adds exactly one frame's length to it. If the client is late, the leftover time stays in the difference and the next frame comes a little sooner. That is a fixed timestep with an accumulator, the same idea physics engines use: the animation keeps its rhythm, even when the drawing does not.
Where the unit is drawn follows from the frame number. A walk has 8 frames, a tile is 32 pixels, so each frame moves the unit 4 pixels, and the code writes that out for each of the 8 directions:
cTotalFrame = 8;
cFrameMoveDots = 32 / cTotalFrame;
dx = dy = 0;
switch (m_pData[dX][dY].m_cDir) {
case 1 : dy = cFrameMoveDots * (cTotalFrame - m_pData[dX][dY].m_cOwnerFrame); break;
case 2 : dy = cFrameMoveDots * (cTotalFrame - m_pData[dX][dY].m_cOwnerFrame); dx = -cFrameMoveDots * (cTotalFrame - m_pData[dX][dY].m_cOwnerFrame); break;
case 3 : dx = -cFrameMoveDots * (cTotalFrame - m_pData[dX][dY].m_cOwnerFrame); break;
case 4 : dx = -cFrameMoveDots * (cTotalFrame - m_pData[dX][dY].m_cOwnerFrame); dy = -cFrameMoveDots * (cTotalFrame - m_pData[dX][dY].m_cOwnerFrame); break;
case 5 : dy = -cFrameMoveDots * (cTotalFrame - m_pData[dX][dY].m_cOwnerFrame); break;
case 6 : dy = -cFrameMoveDots * (cTotalFrame - m_pData[dX][dY].m_cOwnerFrame); dx = cFrameMoveDots * (cTotalFrame - m_pData[dX][dY].m_cOwnerFrame); break;
case 7 : dx = cFrameMoveDots * (cTotalFrame - m_pData[dX][dY].m_cOwnerFrame); break;
case 8 : dx = cFrameMoveDots * (cTotalFrame - m_pData[dX][dY].m_cOwnerFrame); dy = cFrameMoveDots * (cTotalFrame - m_pData[dX][dY].m_cOwnerFrame); break;
}
This block appears several times in the same file, once for each place that needs to know where a walking unit is.
It works, and for a game from 2000 drawn at a modest frame rate, it is the natural design. Its limit is that a unit has only 8 places to be on its way across a tile. The screen can be drawn as often as you like, but between two animation frames nothing changes. On a fast monitor the unit stands still for several screen refreshes, then jumps 4 pixels. Speed is tied to the animation too: to make something faster, you change how long its frames last.
2. Delta time, in one sentence
The other way to do it is to stop counting frames and measure time. Every drawn frame, you ask how long the last one took, the delta, and move the unit by its speed times that delta:
position += velocity * delta;
If the game draws 30 frames a second, each step is bigger. At 240 frames a second each step is smaller. A tile still takes the same time to cross, and the unit is drawn exactly where it should be right now at every refresh, not at one of 8 fixed points. That is all "framerate-independent" means.
The sentence is simple. The details are not: what happens at the end of the tile, what happens when a frame takes two seconds, how the camera follows without the world shaking by a pixel. I did not want to learn those inside a live game with years of code around the walk. So I wrote a small one.
3. Tiled: a prototype to learn it
Tiled is a tile-map prototype: a client in SFML, a server over ENet, accounts in MySQL, a 100 by 100 map, and players walking a tile at a time where you click. It was written by hand, before any AI, and its whole point was the walk. You can still play it in the browser.
Its unit is a red square that moves at 64 pixels a second, scaled by the frame's delta:
const auto velocity = 64.0f;
auto delta_clock = lib::thread_time::DeltaF();
auto new_mvpt = _mvpt.MoveTo(
dst_wpt.As<float>(), velocity * delta_clock
);
MoveTo is plain trigonometry. Find the angle to the target, then step that far along it:
P MoveTo(const P & to, const float step) const
{
const auto angle = std::atan2((float)to.y - (float)y, (float)to.x - (float)x);
return { (typename P::Value)(x + std::cos(angle) * step), (typename P::Value)(y + std::sin(angle) * step) };
}
The first thing Tiled taught me is that the last step always overshoots. The delta does not know where the tile ends, so the final move carries the unit a little past it. Each axis is clamped to the target, and only then is the tile reached:
if (delta.x > 0)
{
if (new_mvpt.x >= rpt.x)
_mvpt.x = rpt.x;
}
else if (delta.x < 0)
{
if (new_mvpt.x <= rpt.x)
_mvpt.x = rpt.x;
}
// ... the same for y
if (_mvpt == rpt.As<float>())
{
_last_pt = _pt;
_is_moving = false;
}
The second thing it taught me is in a commit title: "Fixed player moving past the selected position due to clicking while already moving". Clicking while a step is still under way changes the target in the middle of a tile. The time-based model has to decide what a step is, and that question does not exist when you only count frames.
The third is about the camera. Tiled splits each movement in half between the unit and the world offset (_mvpt_decrease += mvpt_diff / 2.f), because when the camera follows the player, every pixel the player moves is a pixel the world moves the other way. Round them differently and the player shivers against the ground.
And the clamp has a cost. Whatever the overshoot was is thrown away, so at the end of every tile the unit loses a sliver of time. Walking several tiles in a row, you can feel the little stop at each one.
4. Helbreath Empire: smooth movement
In August 2023 the lesson went into Helbreath Empire, in a commit called "Smooth movement". A unit keeps how far it has moved into its tile, in pixels, and adds its velocity times the delta every frame:
_p->_move_val += _p->GetVelocity() * Clock.delta();
if(_p->_move_val > kDst)
{
float rest = _p->_move_val - kDst;
// ... the next tile starts here
_p->_move_val = rest;
}
That rest is the fix for Tiled's sliver. The overshoot is not thrown away; it is carried into the next tile, so a unit walking ten tiles crosses them in exactly ten tile times, with no stop between them. The velocity comes from the walk's duration, which is defined in one place for both games:
inline constexpr int kTilePx = 32;
inline constexpr float Velocity(const int tile_ms) { return float(kTilePx) / float(tile_ms); }
K_ float kHumanRunSpeed = hbunit::step::Velocity(hbunit::step::kRunTileMs); // 294 ms a tile
K_ float kHumanMoveSpeed = hbunit::step::Velocity(hbunit::step::kWalkTileMs); // 490 ms a tile
Pixels per millisecond. Haste, Freeze and the other effects multiply the speed, and the animation frames follow the movement instead of driving it.
5. What delta time does when a frame takes two seconds
Delta time has one famous trap. Drag the game's window and Windows stops drawing it. When you let go, the next frame's delta is two seconds long, and velocity * delta says the player walked six tiles in one frame. Helbreath's server watches how fast players move, because that is how speed hacks are caught, and six tiles in one frame looked exactly like one. Honest players were flagged for moving their window.
The answer is one std::min:
// At most one tile per frame: after a stall (a 2 s window drag) the
// whole gap used to become a move per frame, 4-6 in a row, which the
// server's speed check counted against an honest player. The rest of
// the gap is dropped: the walk goes on at its normal pace.
_move_v += std::min(GetVelocity() * Time::Delta(), (float)kDst);
And the camera problem from Tiled came back too, at a larger scale: a background made of thousands of sprites and a player sprite rounded separately can disagree by a pixel at the half-way point of a step. Now both are rounded by the same function, so they move together:
// Rounded as the units are (hb_lib unit/step.h RenderMoved): the background
// and the player never part by a pixel at a half
_move_v += g_player->_dir.GetContrary().GetMovePt().As<float>() *
(float)hbunit::step::RenderMoved(g_player->_move_v / kTileSz);
6. 2026: the step as a window of time
Adding up deltas has one weakness left: it is a sum. Every frame adds a little rounding and a little timing error, and two clients watching the same unit add up different deltas, so they drift. The newest version, shared by Helbreath Millennium and Helbreath Empire in hb_lib's unit/slide.h, does not add anything. A step is a window of time, when it starts and how long it lasts, in microseconds:
struct Slide
{
Us _start_us = 0;
Us _dur_us = 0;
constexpr bool IsSet() const { return _start_us > 0 && _dur_us > 0; }
constexpr Us End() const { return _start_us + _dur_us; }
};
Where the unit is drawn is then a pure function of the clock. How far through the window are we, from 0 to 1? Draw it that far across the tile:
#define HBUNIT_RENDER_PX(progress) ((int)(double(hbunit::step::kTilePx) * (1.0 - (progress)) + 0.5))
template <class PtT>
inline constexpr PtT GetRenderPt(const PtT & dir_pt, const double progress)
{
const int px = HBUNIT_RENDER_PX(progress < 0.0 ? 0.0 : progress > 1.0 ? 1.0 : progress);
return PtT(decltype(dir_pt.x)(-dir_pt.x * px), decltype(dir_pt.y)(-dir_pt.y * px));
}
Compare that with the 8-case switch from 2000. The direction is a unit vector, so one multiplication covers all eight directions, and the 8 fixed positions became every pixel there is.
Nothing accumulates, so nothing drifts. A frame that comes late simply reads a later clock. A frame that takes two seconds lands the unit on its tile, and no tiles appear out of nowhere. Every client that knows when a step began draws it in the same place.
What is left is deciding when a step starts, and that is where the network comes in. The next step's packet never arrives at the exact microsecond the last one ended; a server tick and the network add 30 to 60 milliseconds. If it comes close enough, the new step starts at the old one's end, so the walk does not hitch at the tile's edge. If it comes late, it starts now, and the unit pauses instead of jumping ahead:
I_ K_ Us kChainTightUs = 75000;
K_ Us ChainStart(const Slide & prev, const Us now)
{
if (prev.IsSet())
{
const Us chain = prev.End();
const Us jitter = now - chain;
if (jitter >= 0 && jitter <= kChainTightUs)
return chain;
}
return now;
}
Your own character does not wait for the network at all: its next step is pinned to the last one's end, always. That is the old accumulator from section 1, m_dwOwnerTime += dwFrameTime, coming back in a new form: the rhythm is kept, whatever the frame does.
Since every function here is constexpr, the rules are checked by the compiler on every build. If a change breaks the walk's timing, the code does not compile:
static_assert(HumanTileUs(false, {}) == 490000 && HumanTileUs(true, {}) == 294000, "hbmillennium's tile times");
static_assert(HumanTileUs(false, {._frozen = true}) == 612500 && HumanTileUs(true, {._frozen = true}) == 367500 &&
HumanTileUs(true, {._relic_ctr = true}) == 367500, "Freeze / the relic: +25 % time");
static_assert(ChainStart({1000, 490000}, 491000) == 491000 && ChainStart({1000, 490000}, 491000 + kChainTightUs) == 491000 &&
ChainStart({1000, 490000}, 491001 + kChainTightUs) == 491001 + kChainTightUs,
"the chain: within 75 ms of the end, never before it");
static_assert(RenderPx(1000, 1000, 490000) == 32 && RenderPx(246000, 1000, 490000) == 16 &&
RenderPx(491000, 1000, 490000) == 0, "the drawn offset: a whole tile at the start, none at the end");
Beyond the compiler, an automated routine plays real clients walking, running, stopping and watching each other in every direction: 2941 checks, all passing in its last run.
In the end: the four ways
| What moves the unit | Places on a tile | After a 2 s stall | |
|---|---|---|---|
| Original | the animation frame, on a fixed clock | 8 (4 px apart) | frames catch up one at a time |
| Tiled (2022) | velocity × delta, clamped at the tile | every pixel | one big step, then the clamp |
| Empire (2023) | velocity × delta, the rest carried on | every pixel | at most one tile a frame |
| Both games (2026) | the clock, read against the step's window | every pixel | lands on its tile, nothing drifts |
Each one kept something from the one before. The original's accumulator came back as the pinned step. Tiled's clamp became the carried rest. And the prototype that started it all is still running, a red square walking a grid, at exactly 64 pixels a second.
The projects behind this post
Helbreath Empire
My most important project technically: an online MMORPG whose client and server I rewrote into my own engine. Millennium runs live, Empire is the next generation: OpenGL and GLSL, TCP/UDP networking off the frame, multi-threading, SSE2, an in-house UI, sprite and map editors, in-game Twitch streaming, VoIP, screen recording and more.
kazilib
My own C++ library, under every game on this page: packets and networking on ENet, threads and parallel jobs, time, an SFML 3 and ImGui layer, screen recording, WebAssembly builds for the browser, the Testerino test bridge and one FASTBuild runner. Helbreath Millennium, Helbreath Empire and my prototypes share it, so a fix in one reaches them all.
Tiled
A multiplayer tile-map prototype: make an account, click to walk around a 100x100 grid and watch other players move live. A C++23 SFML client compiled to WebAssembly, talking to an ENet game server on Linux over a WebSocket gateway, accounts in MySQL.