On this page
Terrain: heights, the pyramid, materials and growth
The ground of a level is one grid of heights that four parts of the game read in four ways: physics asks for the exact height under a wheel, rays ask what they hit, the renderer draws it in patches, and the plants grow on it by a map of materials. This page is the engine's side. The files themselves, layer by layer, are on What goes into a terrain in the assets area.
The files: what goes into a terrain. The ray walk in detail, with its interactive side view: collision.
The heightmap cluster#
A level has a cluster of heightmaps: the primary one, where the game is played, and up to eight secondary ones around it that only give the far view its hills. Heightdata.con builds it with a handful of commands:
| Command | What it sets | Original (BF2.exe) |
|---|---|---|
heightmapcluster.setClusterSize | cells a side of the cluster (3 for a primary with its eight neighbours) | 0x00696910 |
heightmapcluster.setHeightmapSize | the world size of one cell | 0x006983a0 |
heightmapcluster.addHeightmap | a heightmap at a cell; the one at (0, 0) is the primary | 0x00698120 |
heightmap.setSize, setScale, setBitResolution | samples a side, the metres a sample and a height step, 8 or 16 bits | 0x006a83e0, 0x006a92f0, 0x006a8470 |
heightmap.loadHeightData | the .raw file: rows of samples, little endian | 0x006ab4a0 |
A 16-bit sample above 0xff00 is clamped to 0xff00 at the load. A large Battlefield 2 level is 1025 x 1025 samples at a scale of 2 m, so 2,048 m a side; the height scale turns the 16-bit sample into metres.
The height at a point#
"How high is the ground at (x, z)?" is asked thousands of times a tick, by soldiers, wheels, projectiles and bots. The answer must be the same bits on the client and the server, so the port follows the original's statement order exactly (getHeightLocal, BF2.exe!0x006a8730):
fx = f32(x / scale.x); fz = f32(z / scale.z)
ix = floor(fx); iz = floor(fz); dx = f32(fx - ix); dz = f32(fz - iz)
lower triangle (dz < 1 - dx): (h(ix+1,iz) - h00) * dx + (h(ix,iz+1) - h00) * dz + h00
upper triangle: (h(ix,iz+1) - h11) * (1 - dx) + (h(ix+1,iz) - h11) * (1 - dz) + h11
height = that * scale.yA cell is two triangles, split along the diagonal from (x + 1, z) to (x, z + 1); the point's triangle decides which three samples count. The f32(..) marks are where the original stores a value to a 32-bit float: leave one out and the last bit differs on some inputs. The cluster's getHeight (BF2.exe!0x00696bb0) answers 0 outside the cluster, finds the heightmap of the cell and asks it.
The raw sample access (BF2.exe!0x006a85f0) reads data[(z + h/2) * w + (x + w/2)] with no test. Callers keep inside; the port's sampler keeps the same index arithmetic and tests where the original's callers do.
Rays: the pyramid of maximum heights#
A bullet's path can cross a thousand cells. The heightmap therefore keeps a pyramid: per 2 x 2 cells the maximum height around them, rounded up to a byte, and above that level after level of maxima of four children. A ray walks the pyramid from the top and skips every node whose maximum lies below the part of the ray inside it, so a shot over open ground tests a handful of cells (Heightmap::intersectSegment, BF2.exe!0x006ab850; the pyramid is built by 0x006aada0). The port builds the pyramids once at the level's load, on the server too. The collision page has the walk with a piece to try.
Materials#
Beside the heights lies HeightmapPrimary.mat: one byte a sample, a material id (grass, sand, dirt, road ..). getMaterialWorld (BF2.exe!0x006a9070) turns a world point into that byte. Who reads it:
- physics: a wheel's friction and a soldier's footing come from the material pair of the two things that touch;
- effects and sound: the dust behind a wheel, a bullet's impact effect, the footstep sound;
- bots: a navigation mesh has a cost per material and vehicle; a negative cost is impassable;
- undergrowth: which plants grow where (below).
The material definitions (names, friction, damage factors, the effect of each pair) are scripts of the game's data, the same for every level.
The drawn terrain#
The renderer does not draw the heightmap directly. A level ships terraindata.raw, the compiled terrain: the grid cut into patches, each with its vertices, a tree of index lists for the levels of detail and culling, and the names of its colour map, detail map, low-detail map and lightmap (Terrain::load, RendDX9.dll!0x100de6d0). Near the camera a patch is drawn with detail layers blended by weight maps; far away one low-detail texture covers it. The secondary heightmaps become the surrounding terrain, drawn coarse.
The patch above is made by a formula of the page's own, to show the idea: each coarser level here takes every second sample of the one before it, so a quarter of the triangles, and lies a little farther from the true surface. The game's own patches, their index tree and how levels are stitched at their borders are on the assets page.
Which level a cell draws at#
The terrain is not switched patch by patch. Under each patch the cull tree goes down to cells of 64 m, and every frame each visible cell is given one of three levels (TerrainCuller::update, RendDX9.dll!0x100e7750; the cell's test 0x100e6070):
D = max(round(fovScale x lodSwitchDistance x 0.75), 64)
d = the distance in x and z from the camera to the cell's box + the camera's height above the terrain
level = 0 if d <= D, 1 if d <= D x lod2Start, else 2lodSwitchDistance is 64, 80 or 100 by the terrain option (low, medium, high), and lod2Start is 3.0. fovScale is 1 unless the view is zoomed: through a scope the detail reaches farther. Two things follow from the floor of 64. The low and the medium option give the same distance at no zoom (48 and 60 are both raised to 64). And a camera more than D above the ground, a pilot's, sees no cell at level 0 at all, because the height is added to every cell's distance.
2142 cuts the product toward zero where Battlefield 2 rounds it: at the same zoom the two can differ by a metre (the port's test has 93 against 94). Heroes sets the distance to 200, 300 or 400 by its option.
Beyond the playable square lie eight surrounding patches, made from the secondary heightmaps. Each is drawn with a fixed grid that coarsens in three steps: an edge patch in three bands of step 1, 2 and 4, changing at a quarter and at a half of its width; a corner patch in three rings of the same steps (Terrain::createSurroundingGrids, RendDX9.dll!0x100df110). The piece shows them with its last switch.
Between two cells of different levels the finer one draws a stitching variant of its index list so that no crack opens (0x100e3bb0), and the vertices slide between levels by morph values instead of popping. Neither is in the piece above. The stitching was compared with the original's own function on 3000 random graphs of cells, with no line differing (the oracle).
The port reads all of this in bf2-terrain and draws it in bf2-render. Because the editor can sculpt, the port can also recompile a patch from the heightmap (vertices, bounds, the cull tree), which the game itself never does.
Undergrowth and overgrowth#
| Undergrowth | Overgrowth | |
|---|---|---|
| what | grass, flowers, small stones | trees and bushes |
| placed by | Undergrowth.raw: a material id a sample, and Undergrowth.cfg / .dat: per material the types with mesh, texture, density, size, slope range | Overgrowth.raw and Overgrowth.con: types with their rules |
| made | near the camera only, patch by patch, within a view distance | over the whole level; far ones are drawn in a cheaper form |
| collides | no | yes: OvergrowthCollision.con creates the collision objects |
| moves | sways (speed and scale in the config) | not read for this page |
The undergrowth's config reader is RendDX9.dll!0x1014bc30, its compiled form's 0x1014b8f0; the port checked that all 316 types of the shipped levels are identical in the two forms.
What the port has#
| Part | State |
|---|---|
| heights, the cluster, materials | ported; the sampler is exact and used by client and server |
| the pyramid and the ray walk | ported, with tests from the addresses; built at load |
| the compiled terrain's reader | ported; the shipped Battlefield 2 levels load in the tests, and the assets page shows a Heroes level decoded layer by layer |
| drawing: patches, detail layers, lightmaps, sun shadow receivers | ported; compared with native Direct3D 9 pictures case by case, with small residuals listed in the notes |
| undergrowth | ported: drawn near the camera; its alpha test follows the rule measured on native Direct3D 9 |
| overgrowth | near instances and collision ported; the far overgrowth stand-ins are listed as open in the renderer notes |
How it is tested#
- Every shipped level (
crates/bf2-terrain/tests/retail_levels.rs): each terrain format is read from all 30 levels and consumed exactly: heightmaps and sampling, the compiledterraindata.raw, the byte maps, undergrowth, the atlas index. It needs an install and is skipped without one. - Rays (
crates/bf2-physics/src/heightmap.rs): the pyramid of maximum heights, a segment that meets water first, a vertical segment across two heightmaps. - Levels of detail (
crates/bf2-render/src/terrain/lod.rsandmod.rs): the switch distance per game and option (2142's truncation against Battlefield 2's rounding, Heroes' three values), unvisited neighbours counting as level 0, and the stitching and level-setting functions compared with the original's on 3000 random graphs of cells with no line differing. - The cull tree the port can rebuild equals the tree in every shipped level's file, node for node.
Not compared with the original: the far overgrowth stand-ins, which the renderer notes list as open.
In the code#
| What | Where |
|---|---|
| file formats | crates/bf2-terrain/src/ (heightmap.rs, heightdata.rs, terraindata.rs, bytemap.rs, undergrowth.rs, overgrowth.rs) |
| physics' sampler and pyramid | crates/bf2-physics/src/heightmap.rs |
| drawing | crates/bf2-render |
| the tests on shipped levels | tests/retail_levels.rs |
Notes: notes/formats/terrain.md, notes/subsystems/renderer.md (terrain and growth sections).
docs/dev/engine/terrain.md · built from bf2-rs e3b4f05 · part of the Battlefield 2 / 2142 / Heroes reimplementation. Not affiliated with EA or DICE.