DSHINTERACTIVE
Tutorials

A Hill in a Flat Picture: Terrain Relief Without 3D

Iron Sector has no 3D terrain, yet the Seelow Heights rise 45 metres above the Oder marsh, crests hide what lies behind them and a tank can drive into a gully and out again. This lesson builds that hill from a grid of numbers, a lifted picture and a few tricks for what a flat picture cannot do, with the game's real numbers.

screen lift per unit of ground height
3.25 px
screen lift per unit of ground height
units of dead ground behind every unit of brow
1.73
units of dead ground behind every unit of brow
occluder pieces on the Seelow Heights
1,531
occluder pieces on the Seelow Heights
per frame for 46 x-ray silhouettes
≤ 0.25 ms
per frame for 46 x-ray silhouettes

The problem

In the first lesson the ground was a plane. Every point of it had exactly one screen position, and the whole battlefield was one flat picture, painted once and drawn under the sprites. Real battlefields are not flat. The Seelow Heights rise 45 metres above the Oder marsh, and a game about them has to show the slope, hide what lies behind the crest, and let a tank drive up and come back into view.

The obvious answer, a 3D terrain mesh, is not available: nothing in this renderer is 3D. Every sprite is a picture rendered once from 3D models from a fixed camera, into an atlas, and the ground is a baked image. So the hill has to be built from the same parts as everything else: a grid of numbers, a picture lifted where the ground is high, and a few tricks for the things a flat picture cannot do by itself.

The same 2400 × 1350 map pixels of the Seelow Heights: the graded road up the face and the gully below it. On the flat side every point is at height zero; on the lifted side the ground has risen and the gully has opened. — Lifted: the picture the game draws
The same 2400 × 1350 map pixels of the Seelow Heights: the graded road up the face and the gully below it. On the flat side every point is at height zero; on the lifted side the ground has risen and the gully has opened. — Flat: every point at height 0
Flat: every point at height 0Lifted: the picture the game draws
Drag to compare
The same 2400 × 1350 map pixels of the Seelow Heights: the graded road up the face and the gully below it. On the flat side every point is at height zero; on the lifted side the ground has risen and the gully has opened.

The height grid

The hill begins as numbers. The map carries a height layer: one value for every cell of 2 × 2 units, stored at the cell's centre as a 16-bit integer in steps of a tenth of a unit, so a stored 437 means 43.7 units. Heights are in model units, where one metre is 3.3 units. The Seelow map is 1500 × 1200 cells, 1.8 million heights.

Between the centres the height is bilinear. Blend the two upper centres by the horizontal fraction, blend the two lower ones the same way, then blend those two results by the vertical fraction. Outside the outermost centres the lookup is clamped to the edge cells, so the ground never falls off the map. On Seelow the lowest cell is 2.9 units below the datum and the highest 148.9 above it, about 45 metres.

Exactly one function answers the question "how high is the ground here": h(u, v). The baker calls it, the renderer calls it and the simulation calls it. With three copies, a tank could one day be drawn on a slope it does not drive on. With one, the picture and the physics cannot disagree.

stored 437 × 0.1 = 43.7 u
top = 44.4 + (43.8 − 44.4) · 0.35 = 44.19
bottom = 46.3 + (45.7 − 46.3) · 0.35 = 46.09
h = 44.19 + (46.09 − 44.19) · 0.6 = 45.33 u
Seelow: −2.9 … 148.9 u,   148.9 / 3.3 ≈ 45 m
The Seelow height layer as a grey image (dark is low, light is high) and a few of its cells around the gully. A point 0.35 of the way across and 0.6 of the way down between four centres gets 45.33 units.
The Seelow height layer as a grey image (dark is low, light is high) and a few of its cells around the gully. A point 0.35 of the way across and 0.6 of the way down between four centres gets 45.33 units.

Grade

From heights come slopes. The grade at a point is its steepness, rise over run. Take the height of the cell to the east minus the cell to the west and divide by their distance, two cells or 4 units; do the same from north to south; the grade is the length of that pair. A grade of 0.25 is a 14° slope, 0.7 is 35°, and 1.0 is 45°.

Take the cell of 44.4 in the previous figure. East and west of it lie 43.8 and 45.0, above and below it 42.5 and 46.3. The two differences are −0.3 and 0.95, and the grade comes to 0.996, a slope of almost 45°, just short of the cliff class. The steepest grade anywhere on Seelow is 3.3.

The map is classified by grade: 0.08 to 0.25 is a gentle hill, up to 1.0 a steep hill, above 1.0 a cliff. On Seelow 14.8 % of the map is gentle, 13.4 % steep and 0.28 % cliff. The simulation reads the same grade. A wheeled vehicle keeps full speed on the flat, 85 % at a grade of 0.1, 55 % at 0.25 and 30 % at 0.4, and above 0.4 the slope is closed to wheels. Tracks climb up to 0.7, infantry up to 1.2.

So the numbers that shade the picture also decide whether a truck can take the road up the heights. The picture is not decoration. It is the terrain.

grade = √((Δh/Δu)² + (Δh/Δv)²),   Δu = Δv = 2 cells = 4 u
(43.8 − 45.0) / 4 = −0.3,   (46.3 − 42.5) / 4 = 0.95
grade = √(0.09 + 0.9025) = 0.996 ≈ 45°
angle = atan(grade):  0.25 → 14°,  0.7 → 35°,  1.0 → 45°
GradeClassWheeledTrackedInfantry
below 0.08flat100 % at 0openopen
0.08–0.25gentle85 % at 0.1, 55 % at 0.25openopen
0.25–0.4steep30 % at 0.4openopen
0.4–0.7steepclosedopenopen
0.7–1.0steepclosedclosedopen
1.0–1.2cliffclosedclosedopen
above 1.2cliffclosedclosedclosed
One grade, two readers: the classes shade and label the map, and the limits close a slope to wheels above 0.4, to tracks above 0.7 and to infantry above 1.2.

Authoring a hill

Nobody types almost two million heights by hand. A map author places shapes: a radial hill, a ridge along a line, a plateau with a rim, a long slope from one edge to another, and a ravine, which is a ridge turned upside down. Each shape has a height and a falloff.

The falloff is the smoothstep curve, t² · (3 − 2t). Its slope is zero at both ends, so it starts flat, rises and arrives flat: the foot and the crest of a hill come out rounded and nothing has a crease. A little value noise on top, ±1.5 units on Seelow, keeps the slopes from looking machine-made.

Then the roads. A road that climbs a ridge is marked as graded: the baker flattens the ground across the road's width and blends it back into the hill over a shoulder on each side, 6 units by default and 10 on the Seelow road, like a real cut and fill. That road's steepest grade is 0.11, well inside the 0.4 a truck can take.

f(t) = t² · (3 − 2t),   f′(t) = 6t · (1 − t)
f(0) = 0,  f(0.25) = 0.156,  f(0.5) = 0.5,  f(1) = 1;   f′(0) = f′(1) = 0
graded road: shoulder 6 u (default), 10 u on Seelow;  max grade 0.1113 < 0.4
The five shapes an author places, each with a height and a falloff, and the smoothstep falloff against a straight line: flat at the foot, flat at the crest, no crease.
The five shapes an author places, each with a height and a falloff, and the smoothstep falloff against a straight line: flat at the foot, flat at the crest, no crease.

The lifted bake

Now the picture. The flat bake drew every ground sample at the map centre plus u × PPU across and plus v × PPU × SE down, the projection of the first lesson. The lifted bake keeps x and subtracts the height from y: v × SE minus h × CE, all times PPU. That is the height term of the first lesson, CE × PPU = 3.25 pixels per unit of height.

The new problem is that two ground samples can now land on the same pixel: one on a far slope, and one on a near crest in front of it. So the bake runs a depth test. Every sample has a depth, v × CE + h × SE, and the sample nearest the camera wins, exactly as a z-buffer would decide it.

The picture also grows. Ground at 148.9 units is drawn 484 pixels higher than on the flat map, so the baker adds 484 rows at the top, and 10 at the bottom for the lowest ground at −2.9. Seelow is 11250 × 4500 pixels flat and 11250 × 4994 lifted. The camera knows about the extra rows: its top row is at y = −484, and it can scroll into them.

flat:    x = W/2 + u · PPU,   y = H/2 + v · SE · PPU
lifted:  y = H/2 + (v · SE − h · CE) · PPU
CE · PPU = 0.866 · 3.75 = 3.2476 ≈ 3.25 px per unit of h
depth = v · CE + h · SE   (the nearer sample wins)
top: ceil(148.9 · 3.2476) = ceil(483.6) = 484 px;   bottom: ceil(2.9 · 3.2476) = 10 px
4500 + 484 + 10 = 4994
The plan view of the Seelow face projected forward with its full lift. Where two ground samples land on one pixel, the depth test keeps the near crest and hides the far slope.
The plan view of the Seelow face projected forward with its full lift. Where two ground samples land on one pixel, the depth test keeps the near crest and hides the far slope.

Shading the slope

A lifted picture with flat colours would still look flat, because the eye reads shape from light. So the bake shades every sample by its slope. The sun stands in the north-west, 28° above the horizon, and the ground receives light by Lambert's rule: the cosine of the angle between the surface normal and the direction to the sun.

The rule is normalised so that flat ground keeps exactly its painted colour, and only slopes change. A slope of 0.3 facing straight away from the sun gets about 34 % darker; the same slope facing the sun gets about 29 % brighter. The sunlight term is clamped between 0.5 and 1.45, so a slope in full shade is never a black slot.

On top of Lambert come three more passes. Hills cast shadows onto the ground behind them, and valleys get a little ambient occlusion. A convex crest is lifted by up to 12 %, so ridges catch the eye. Where the grade passes 0.8, bare earth shows through the grass, strongest by 1.6, which is how a cliff announces itself. Water takes no part: a lake is level, so it stays level-coloured. Trench tiles are draped the same way, pixel column by pixel column, each moved up by its own height.

lam = (n · sun),   light = 1 + 1.25 · (lam − sin 28°),   clamped to 0.5 … 1.45
flat:  lam = sin 28° = 0.469  →  light = 1
0.3 towards the sun:  lam = (0.3 · cos 28° + sin 28°) / √1.09 = 0.703  →  1.29
0.3 away from it:     lam = (−0.3 · cos 28° + sin 28°) / √1.09 = 0.196  →  0.66
The lifted Seelow face before and after shading: Lambert from a north-west sun, cast shadows, ambient occlusion and the crest rim. Without light the hill is lifted but reads as flat. — As baked
The lifted Seelow face before and after shading: Lambert from a north-west sun, cast shadows, ambient occlusion and the crest rim. Without light the hill is lifted but reads as flat. — Flat colour
Flat colourAs baked
Drag to compare
The lifted Seelow face before and after shading: Lambert from a north-west sun, cast shadows, ambient occlusion and the crest rim. Without light the hill is lifted but reads as flat.

A unit on a slope

What about the things on the hill? Every ground-anchored sprite is drawn at the lifted position: the same projection, with the ground's height added to the object's own z. A tank at height 100 is drawn 325 pixels higher than on the flat map, exactly where the ground under it moved.

Two things do not change. The sort key, which decides who is drawn in front of whom, is still the flat screen y of the ground point, with no height in it; the depth sort of the previous lesson works on a hill unchanged. And the sprite is never tilted. A tank on a 35° slope is drawn upright, because there is no picture of a tilted tank, and at this scale the eye accepts it.

Bridges get one rule of their own: a crossable deck is drawn at the average height of its two ends, so the roadway stays level even when the banks are not.

drawn y = sy(v, z + h(u, v))
sort key = sy(v)   (flat, unchanged)
h = 100:  100 · 3.25 = 325 px higher
bridge deck:  h = (h_a + h_b) / 2
A T-34-85 on the graded road at zoom 1. Its picture is lifted by h · 3.25 = 87 px; its sort key stays on the flat line below it.
A T-34-85 on the graded road at zoom 1. Its picture is lifted by h · 3.25 = 87 px; its sort key stays on the flat line below it.

Dead ground

Here is the first thing a flat picture cannot do by itself. Look at a hill from a camera 30° above the horizon. The ground just behind the brow is hidden in the shadow of the line of sight. How much of it? Every unit of height hides CE / SE of ground, the cosine over the sine of 30°, which is 1.73. A brow 10 units high hides 17 units of ground behind it.

Soldiers call that dead ground, and the picture has to respect it: a house or a tree standing there must be hidden, because the crest is in front of it. For the back slope itself to be hidden it has to be steeper than the line of sight, a grade of tan 30° = 0.58. Gentler back slopes stay visible, just shortened.

The bake already handles the ground correctly with its depth test. But sprites are drawn after the ground and on top of it, so the crest, which is part of the ground picture, can never cover a sprite. That needs pieces.

CE / SE = cos 30° / sin 30° = 0.866 / 0.5 = 1.732
Δh = 10 u  →  10 · 1.732 = 17.3 u hidden
back slope hidden if grade > tan 30° = 0.577
The camera's line of sight at 30° grazes the brow. Behind a brow of height Δh, 1.73 · Δh of ground is hidden from it.
The camera's line of sight at 30° grazes the brow. Behind a brow of height Δh, 1.73 · Δh of ground is hidden from it.

Occluder pieces

Occluder pieces are how the hill joins the depth sort. While baking, the tool finds every pixel of a front slope that covers ground behind it: a silhouette begins wherever ground reappears above a crest after more than 2 units of hidden depth. Those pixels are cut out in columns 256 pixels wide and in horizontal bands of 4 pixels, and each band becomes a small sprite with a sort key: the flat screen y of the ground it stands on, rounded down to the band.

Then the key is moved up by 16 pixels. The footprint and the shadow of a sprite reach 8 to 12 pixels below its anchor, and a crest must cover the whole footprint of a house behind it, not cut it in half.

The pieces are not copies of the picture. They are alpha masks over the live ground, so a crater stamped on a slope shows through a piece exactly as it does beside it. Seelow has 1,531 of them, 352 in a single view at zoom 1. They cover houses, trees, wrecks, bodies and fires. They do not cover units: for units there is something better.

k = H/2 + v · SE · PPU
key = floor(k / 4) · 4,   zIndex = key − 16
k = 2187.3  →  key = 546 · 4 = 2184  →  zIndex = 2168
The south brow of the Seelow gully at zoom 2. Without the pieces, the trees on the gully floor are drawn over the crest in front of them; with the pieces in the sort, the crest covers their trunks and footprints. — Pieces on
The south brow of the Seelow gully at zoom 2. Without the pieces, the trees on the gully floor are drawn over the crest in front of them; with the pieces in the sort, the crest covers their trunks and footprints. — Pieces off
Pieces offPieces on
Drag to compare
The south brow of the Seelow gully at zoom 2. Without the pieces, the trees on the gully floor are drawn over the crest in front of them; with the pieces in the sort, the crest covers their trunks and footprints.

The brow line

Where far ground reappears above a crest there is a seam, and the eye needs a hint that it is a seam, not a stripe of odd grass. So the bake draws a brow line. The one-pixel row of far ground directly above the silhouette is darkened to 66 %, and the top row of the crest below it is brightened to 118 %: a thin dark edge over a thin bright edge, the same cue a painter uses for a ridge against the sky.

The line is at full strength where the crest hides 6 units of ground or more, and fades to nothing where the hidden depth drops to 2 units, so a small fold gets no outline. It is one map pixel wide and baked into both the ground picture and the occluder pieces, so it never shifts against them.

far ground row × 0.66,   crest row × 1.18
strength = clamp((hidden − 2) / 4, 0, 1)
hidden 2 u → 0,   4 u → 0.5,   6 u → 1
The gully's brow at zoom 2, enlarged: a dark row of far ground over a bright row of crest, one map pixel each.
The gully's brow at zoom 2, enlarged: a dark row of far ground over a bright row of crest, one map pixel each.

X-ray silhouettes

A tank in dead ground would be invisible, and a player who cannot see their own tank is lost. So units get an x-ray. After a unit is placed, the renderer checks whether any occluder piece in front of it meets its rectangle: the piece's key, minus the 16-pixel bias, has to be greater than the sprite's own sort key.

If so, the sprite is drawn a second time, in a layer above the sorted world, as a flat silhouette, through a mask that is the union of the covering pieces. Only the covered part shows. The shader takes the luma of each pixel and remaps it into a tint, so the shape keeps its tracks and turret but loses its colour. Baked shadow pixels are dropped, because a shadow through a hill makes no sense.

Friends are pale blue, enemies pale red, remembered enemies grey, at 55, 50 and 30 % opacity. Wrecks, fires and bodies get no x-ray: the dead can stay in dead ground. Picking was changed to match, so a click on the silhouette selects the tank.

covered if piece.key − 16 > sprite.zIndex  and the rectangles meet
luma = 0.299 R + 0.587 G + 0.114 B
col = tint · (0.3 + 1.4 · luma)
luma 0.2 → 0.58 · tint,   luma 0.5 → 1.0 · tint
friend #9fd4ff α 0.55,  enemy #ffb0a0 α 0.50,  remembered #9a9a9a α 0.30
Three tanks behind the same crest: a friendly T-34-85, an enemy Panther in sight, and the same Panther remembered after it was lost from view. Only the part behind the hill is x-rayed.
Three tanks behind the same crest: a friendly T-34-85, an enemy Panther in sight, and the same Panther remembered after it was lost from view. Only the part behind the hill is x-rayed.

A click on a hill

On the flat map a click was the projection run backwards: one division gave the ground depth v. On a hill that formula has no single answer, because one screen pixel can belong to a far valley or to a near crest. So the renderer searches.

For a screen point it computes the range of ground depths that could project there: from the flat v plus the highest possible lift, down to the flat v minus the deepest dip, each scaled by 1.73, with one unit of margin at each end. Then it marches along that range in 1-unit steps from the near end, projecting each candidate with its real height, until the projected y crosses the clicked y, and halves the step 14 times to land on the crossing. It stops at the first crossing from the near side, so the front-most surface wins, like the bake's depth test.

Everything that drapes the ground follows the same lift. The fog of war, for example, is a mesh with every vertex lifted by its height, up to 65,536 vertices, and the camera may scroll into the negative rows above the map, down to −484 on Seelow.

flat:  v = (y − H/2) / (SE · PPU)
v_hi = v_flat + max(0, h_max) · 1.73 + 1
v_lo = v_flat + min(0, h_min) · 1.73 − 1
Seelow: 148.9 · 1.73 + 2.9 · 1.73 + 2 ≈ 265 steps at most
after 14 halvings:  1 / 2¹⁴ = 1/16384 u
One screen column over a crest. Three ground points project to the clicked row; the march from the near end stops at the first crossing and 14 halvings close on it.
One screen column over a crest. Three ground points project to the clicked row; the march from the near end stops at the first crossing and 14 halvings close on it.

Relief in 2D games, briefly

Two-dimensional games have drawn hills for thirty years, in two families. The first is tiles. Transport Tycoon, in 1994, gave every tile corner a height and drew each tile from a small set of slope pictures, so the land rose in neat steps. Strategy games of the nineties did the same with cliff tiles.

The second family is the height field under flat sprites, used by the 2.5D games of the 2000s: a real 3D ground mesh with 2D pictures standing on it. Iron Sector is neither. The ground is still one painted picture, baked lifted with a depth test, then cut into pieces that join the sorting. The cost is a bigger picture and a cleverer click. The gain is that the painted ground survives: every crater, every track, every blade of grass is still in the same image, now on a hill.

What it costs

At run time a hill costs very little. A lifted position is the same projection with one extra height lookup. The occluder pieces are ordinary sprites in the sorted layer, bucketed like every other static object. The x-ray pass, with 46 silhouettes in the gullies at once, adds between 0.06 and 0.25 milliseconds to a frame of about 2.3 ms.

The real costs are paid once, at bake time: 484 extra rows of picture, the depth test, and the cutting of 1,531 pieces. And the author pays with a report. The relief tool prints the lowest and highest point, the steepest grade, the share of the map in each slope class, the steepest grade of each graded road, and a warning for every graded road a wheeled vehicle could not climb. For Seelow that is −2.9, 148.9, 3.3, 14.8 / 13.4 / 0.28 %, one road at 0.11, and no warnings.

A map ships when that report is clean. One height function, three readers, and a flat picture that holds a hill.

x-ray, 46 silhouettes:  +0.06 … +0.25 ms per frame
bake:  +484 rows, depth test, 1,531 pieces
report:  min −2.9,  max 148.9,  maxGrade 3.30,  roads [0.1113],  warnings []
New lessons

Get new tutorials by email

One email when a new lesson comes out, with the video and the write-up.

We use your address only for these updates. No spam.

New tutorials on YouTube.

Devlogs, breakdowns and step-by-step lessons from the DSH Interactive team.

Subscribe on YouTube