Who Draws First: Depth Sorting in a 2D Isometric Game
A 3D graphics card keeps a depth for every pixel; a 2D game has only a pile of flat pictures and the order it paints them in. This lesson follows how Iron Sector decides that order, from one sort key and its tie-breakers to a stack of eighteen layers, trenches, tree crowns, buckets and culling, with the game's real numbers.
- layers is sorted; the rest have a fixed place
- 1 of 18layers is sorted; the rest have a fixed place
- the turret's tie-breaker over its own hull
- +0.5the turret's tie-breaker over its own hull
- after death, a body stops being a sprite
- 3 safter death, a body stops being a sprite
- a frame with 1,100 dead, after culling and baking
- 5.7 → 2.4 msa frame with 1,100 dead, after culling and baking
The painter's problem
A 3D renderer does not have this problem. It keeps a depth value for every pixel on the screen, and a nearer surface simply overwrites a farther one, whatever order the triangles arrive in. A 2D renderer has no depth per pixel. It has a stack of flat pictures and the order in which it paints them, and the later picture covers the earlier one.
So the rule is the painter's algorithm: paint the far things first and the near things last, the way a painter lays the hills before the house and the house before the tank in front of it. It sounds trivial until there are two thousand sprites on screen, each with a different shape, and a house whose doorstep is nearer than the tank behind it but whose roof is farther. Get the order wrong and a tank floats over the house it is hiding behind, or a house swallows the tank in front of it.
The whole method fits in one sentence: find one number per sprite that puts the sprites in the right order, and then handle every case where one number is not enough, preferably without touching the number.


One number per sprite
The number is the screen y of the sprite's ground point, the point the camera maths of the first lesson places for every object. For a tank it is the point under the centre of the hull; for a house, the point under the middle of its footprint. The renderer sorts the sprites by that y and paints the smallest first. A smaller y is higher on the screen and farther from the camera; a larger y is nearer, and is painted last, on top. This y is the sort key.
On the Kursk map the farmhouse stands at v = 86.93 units, so its key is 1603.0. A T-34 driving along the lane behind it has a key of 1561.2: smaller, so the tank is painted first and the house covers it. As the tank comes round in front of the house its key grows past 1603.0, to 1633.9 where it stops, and from the frame the two numbers cross, the house is painted first and the tank over it. Nothing else has to change.
Notice what the key is not. It is not the sprite's top edge or its centre, because a tall house would then be sorted by its roof. It is not u + v, the diagonal used by some tile games, because this camera's depth runs straight down the screen and v alone carries it. And it ignores height: a gunner standing on the platform of a gun is drawn higher, but sorted where he stands. The key is a property of the ground, never of the picture.
key = sy(v) = H/2 + v · PPU · SE, PPU = 3.75, SE = sin 30° = 0.5 house: 1440 + 86.93 · 3.75 · 0.5 = 1603.0 tank behind: 1561.2 < 1603.0 → tank, then house tank in front: 1633.9 > 1603.0 → house, then tank

When two numbers are equal
One number is not enough when two sprites share it. A turret stands exactly on its hull's ground point, and the turret must be on top, so the turret's key is the hull's key plus one half. A T-34 with its hull at 821.25 has its turret at 821.75. Put the turret half a pixel below the hull instead, at 820.75, and it vanishes under the hull: only the gun sticks out.
The other tie-breakers follow the same idea. A soldier standing beside a tank at the same depth gets +0.2, so he is drawn over the hull that would otherwise swallow his legs. A satchel charge gets +0.1, fire and smoke +1.5, a window flash +2, smoke from a gun +3 and a burning house's flames +4. These offsets are a few pixels at most, small next to the size of any sprite, so they settle a tie and almost never change the order of things that really stand apart. The one deliberate exception, −16 for the pieces of the relief, belongs to the next lesson.
Static objects, the houses, trees and fences that never move, get the opposite treatment: a tiny negative bias, less than a thousandth of a pixel, that depends on the order n in which each one was registered. Of two houses at the same y the earlier one is always drawn first, and both are drawn before a unit standing at that depth, whatever order their map cells are attached in. And the sort itself is stable: sprites with truly equal keys keep the order in which they were added.
turret = hull + 0.5: 821.25 + 0.5 = 821.75 static: key −= (1 − n/10⁶) · 10⁻³ n = 1: 1603.0 − 0.000999999 = 1602.999000001 n = 2000: 1603.0 − 0.000998 = 1602.999002 a unit at 1603.0 → static n = 1, static n = 2000, unit


| Sprite | Added to the key |
|---|---|
| Turret | +0.5 |
| Soldier | +0.2 |
| Satchel charge | +0.1 |
| Fire and smoke | +1.5 |
| Window flash | +2 |
| Gun smoke | +3 |
| Burn layers of a house | +4 |
| Relief piece (lesson 5) | −16 |
| Static object, registered n-th | −(1 − n/10⁶) · 10⁻³ |
The stack of layers
Sorting is expensive, so the renderer sorts as little as it can. Only one layer is sorted at all, the object layer, which holds units, soldiers, buildings, trees, props and the fires and smoke that belong to them. In a frame of the farm fight at zoom 1 that is 225 sprites. Every other picture has a fixed place in a stack of eighteen layers.
At the bottom lies the baked ground and, with the high-definition set, its detail overlay. Then bridge decks and ferries, which are under objects. Then the selection rings and order markers that must sit on the ground under the units. Then the sorted objects. Above them come the silhouettes that the relief needs behind hill crests, the additive effects (muzzle flashes and embers), and the tracers and their glow. Then aircraft and overhead wires, which are over everything on the ground. Then mist, the fog of war, the time of day tint, two debug overlays, the health bars and digits, the falling snow and, on top, the interface.
Nothing in that list ever has to be compared with anything else. A layer is a container, and its place in the stack is its order. The order was decided once, when the renderer was built, and it costs nothing per frame.

Drawn here, sorted there
Here is the rule that makes everything else possible, and it was already hiding in the key. Where a sprite is drawn and where it is sorted are two different numbers. The drawn position includes the height: a thing at height z is lifted by z × CE × PPU, the cosine of 30° times the pixels per unit, about 3.25 pixels per unit. The key does not include the height.
The crew of the 88 mm gun shows it in miniature. The two men who aim it stand on the gun's platform, 1.45 units above the ground, so they are painted 4.7 pixels higher than their boots would be. Each of them is still sorted at the spot on the ground under him.
On a map with hills the same rule works on a much bigger scale. Every ground object is lifted by the height of the ground under it: on the Seelow map the highest ground is 148.9 units up, and a tank standing there is drawn 484 pixels higher than on a flat map. Its sort key stays flat, as if the map had no hills. That split is where the next lesson begins, and where the exceptions get interesting. For now: the picture moves, the key does not.
drawn y = sy(v) − z · CE · PPU sort key = sy(v) CE · PPU = cos 30° · 3.75 = 3.2476 ≈ 3.25 px per unit 88 mm crew: 1.45 · 3.25 = 4.7 px Seelow hill: 148.9 · 3.25 = 484 px

Cropped in the trench
A trench is a line drawn on the ground. A soldier standing in it is a sprite standing on the ground. Nothing in the picture puts him below the lip, so the renderer fakes it. When a man's ground point is in a dug-in cell, his sprite is moved down by 11 pixels and the bottom 11 pixels of his frame are cut away. His boots and shins disappear, as if the trench wall hid them. A foxhole cuts 9, and the trench cut also puts a soldier into a boat seat.
The arithmetic happens in the frame's full box, in logical pixels, where the anchor lives (lesson 2). The anchor's height in the box is ay, the anchor fraction times the box height. The frame's new height is the anchor's height minus the cut, never less than 4 rows, and the anchor is rescaled to the new height so the ground point stays put.
One detail took a bug to learn. At first the cut followed the soldier's state, so a man advancing out of a trench kept his legs cut off on open ground. Now the cut is applied only while the ground under him says dug in: the moment he leaves the dug-in cell he is drawn whole.
TRENCH_CUT = 11 px, FOXHOLE_CUT = 9 px ay = anchor_y · orig.h h' = max(4, round(ay − cut)) anchor_y' = ay / h'


Into the ground
There is one more way out of the sorted layer, and it is the cheapest of all. A body that will never move again does not need to be a sprite. Three seconds after a soldier dies, his last frame is copied into the ground picture at the position his sprite occupied, and the sprite is destroyed. In the shot below the sorted layer drops from 119 sprites to 118.
The copy uses the same anchor and trim arithmetic as the sprite. Take the screen point (sx, sy), step back by the anchor times the full box, round to a whole pixel and add the offset of the trimmed rectangle. For a man who died in a trench the cut is added to y, so the body stays as low as the living man was drawn. A body on a bridge deck stays a sprite.
From then on the soldier is part of the ground, drawn in the ground layer for free and never sorted; a thousand dead on a map cost the sorter nothing. Lesson 8 is about that ground picture and everything else that is stamped into it. For today it is one sentence: the fastest sprite to sort is one that is no longer a sprite.
bake after 3 s x = round(sx − ax · orig.w) + trim.x y = round(sy + cut − ay · orig.h) + trim.y sorted layer: 119 → 118 sprites


Seeing through the trees
Sorting puts a tree in front of a man walking behind it, correctly, and now the player cannot see their own soldier. The fix is not to break the order but to let the tree become glass. Every frame, the renderer checks whether a tree's crown rectangle covers one of the player's own men or vehicles, or an enemy vehicle the player can see (an enemy soldier counts only when he is selected). If it does, the crown is drawn at 45 % opacity, with a 6 pixel fade at its edge so the change does not pop.
The check is cheap because the points of those units are collected in cells of 64 pixels, not tested sprite by sprite. And one condition keeps it honest: only points behind the tree's ground line count, with a y smaller than the trunk's. A man standing in front of the trunk is sorted in front of the tree anyway, and the tree should not go transparent for him.
Banks, hedges and walls use the same machinery, and so do tall buildings, so a squad lying behind a hedge is never lost to its own cover. The order stays true; only the paint gets thin.
CANOPY_ALPHA = 0.45, EDGE = 6 px, cells of 64 px alpha = 1 − (1 − 0.45) · min(1, depth / 6) depth 3 px: 1 − 0.55 · 0.5 = 0.725 depth ≥ 6 px: 0.45 counts only if y < y₁ (behind the ground line)


Buckets
Sorting costs time in proportion to what is sorted, and a big map holds thousands of static objects: Goodwood, a tank battle across the Normandy plain, has 6,245. Most of them are out of view at any moment, and sorting all of them every frame would be a waste. So the statics live in buckets. The map is divided into cells of 512 pixels, and the statics whose ground point falls in a cell share one bucket.
Only buckets whose sprites reach into the view are attached to the sorted layer, with 16 pixels of slack, because a house that is hit can change to a slightly bigger picture. The rest exist, but the sorter never meets them. Goodwood has 443 buckets; during a pan across it at zoom 0.5, between 42 and 51 of them are attached, which is 911 to 1,051 statics instead of 6,245. When the camera pans, a bucket attaches on the leading edge and detaches on the trailing one.
This is where the creation-order bias of the tie-breakers earns its keep. Because a static's key already encodes its order, attaching a bucket late does not shuffle the houses inside it, and two houses at the same y never swap places when the camera moves. Attach, detach, nothing moves.
cell = 512 map px, slack = 16 px bucket of a static = the cell of its ground point Goodwood, zoom 0.5: 443 buckets, 42–51 attached sorted statics: 911–1,051 of 6,245

Culling
Buckets handle the statics. Moving things are culled one by one, with a margin around the view that depends on how tall their picture can be. A soldier is drawn if his ground point is within 96 pixels of the screen edge, a vehicle within 192. A wreck gets 320, because a burning wreck carries a column of smoke about 160 pixels tall, and a column cut at the screen edge is an ugly thing. Effects use 480, and the see-through check of the tree crowns looks 1,024 pixels out, since a big sprite can reach far into the view. All margins are in map pixels.
There was also a level of detail scheme for far zoom, which would have replaced soldiers with simpler frames when the camera pulled back. It is still in the code, and it is switched off, because it bought no measurable time, and full detail at every zoom was the better picture. Not every optimisation survives contact with the numbers.
soldier 96 · vehicle 192 · wreck 320 · effects 480 · canopy check 1024 wreck: 320 > 160 px fire column LOD_ZOOM = 0 (switched off)

Above it all
One kind of object never enters the sort at all. An aircraft is drawn in its own layer above everything on the ground, the layer it shares with overhead wires, because nothing on the ground is ever in front of it.
What ties it to the world is its shadow, a separate sprite at the plane's ground point, pushed sideways by the altitude, so the shadow drifts away from the aircraft as it climbs and closes in as it dives. The shadow also fades with altitude, from 80 % opacity down to a quarter. At cruise height it is drawn at 0.375, so a bomber at cruise height casts a faint shadow and a diving one a dark one.
The lesson is the same as the layer stack. If a thing can never be behind anything, do not sort it. Give it a place.
shadow = ground point + shadow_shift · z alpha = clamp(1 − z / (1.6 · cruise), 0.25, 0.8) z = 0: 0.8 z = cruise: 1 − 1/1.6 = 0.375 z ≥ 1.2 · cruise: 0.25

A short history of the order
The painter's algorithm was described in 1972 by Newell, Newell and Sancha, as a way to draw polygons without a depth per pixel. Two years later Ed Catmull's thesis gave us the z-buffer, a depth value for every pixel, and 3D graphics never looked back.
2D games could not afford a z-buffer in the 1980s and did not need one. The tile games of the 1990s settled on the rule this lesson has been circling all along: sort every character by the position of its feet, and paint from the top of the screen down. Modern 2D engines ship it as a sortable container with a stable sort and a numeric key.
What Iron Sector adds is only the discipline: one key, from the ground, with the tie-breakers written down, and every exception (crops, canopies, bakes, aircraft) handled outside the sort instead of by bending it.
What it costs
On a stress battle with 1,470 living units, a minute into the fight, the sorted layer holds 705 objects at zoom 1, 959 zoomed out to one half and 1,421 at a quarter (a free zoom used only for measurements and videos; players stop at one half), the same counts in both sprite sets. Sorting a thousand numbers takes well under a millisecond (a general figure, not a measurement of this game), which is why the number that matters is how many sprites reach the sorter at all.
The buckets, the culling and the baking are the real savings. In another stress test, with 1,100 dead soldiers and 120 wrecks on the map, culling and baking together cut the frame from 5.7 to 2.4 milliseconds with the standard set, and from 4.9 to 2.1 with the high-definition set. The remaining cost is not the sort but the engine around it: the batching of the sprites measured one to two milliseconds per frame on the biggest battles, and that is the floor the project lives on.
One key from the ground, a stack of layers, and every exception handled outside the sort. Everything else is about keeping the sorted set small.
| Measurement | Value |
|---|---|
| Sorted layer, 1,470 units alive, zoom 1 | 705 sprites |
| The same, zoom 0.5 | 959 sprites |
| The same, zoom 0.25 (test only) | 1,421 sprites |
| Frame with 1,100 dead and 120 wrecks, SD, before → after | 5.7 → 2.4 ms |
| The same, HD | 4.9 → 2.1 ms |
| Sprite batching, biggest battles | 1–2 ms per frame |