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

Collision queries

BF22142Heroes

Everything that moves asks the same few questions of the world: does this segment hit something, what does this moving sphere touch. The world has two kinds of answer: collision meshes, each with a BSP tree over its triangles, and the terrain's heightmap. This page is about the questions; what a body does with the answer is the response code's business.

Three askers, two kinds of world projectile a ray per tick: type 0 vehicle, rigid body the response's ray queries: type 1 soldier moving sphere, capsule: type 2 Collision mesh per collision type its own LOD (colMap[type]), per damage state its own sub-geometry BSP tree 16-byte nodes, a split per axis leaf: its faces segment vs triangle 0x006b5e20 Heightmap a grid of heights, two triangles a cell max-height pyramid: skip what lies below What comes back the nearest hit: point, face normal, material (remapped by the template), and the face for a later test the soldier's capsule query collects every contact instead; the response code resolves them bf2_w32ded.exe!0x006b96c0 mesh ray, 0x006c3100 BSP walk, 0x006a9870 heightmap walk, 0x006bac50 moving sphere
Three askers and the two structures that answer. Each collision type reads its own level of detail of a mesh.

Who asks, and with which collision type#

A collision mesh holds several versions of itself, and a query says which one it wants by its collision type:

TypeAskerQuery
0projectiles (and the soldier's own ray tests)a ray
1vehicles and other rigid bodies, through the basic response's collide (bf2_w32ded.exe!0x0069b680)a ray per pair, with the other node's otherCollisionLod when it has one
2soldiersa moving sphere and a capsule (CollisionMeshTemplate::intersectMovingSphere 0x006bac50), only from the soldier's response code
3the AI

The mesh picks its level of detail as colMap[type] of the active sub-geometry (the instance's damage state), so the hull of a tank and the capsule of a soldier can be given different shapes of the same building, and a wrecked building collides as its wreck. That is why "the bullet went through the wall" and "I got stuck on nothing" are usually faults of one level of detail of one mesh, not of the code.

See it: one mesh, three collision types#

The viewer draws a collision mesh as our decoder reads it: per damage state and collision type its vertices and faces, coloured by material. Switch the collision type and the doorway closes for a vehicle; switch the damage state and the hut is its wreck.

What this mesh is

It is not a file of the game. The hut is drawn from boxes and one wedge by a script of this wiki (tools/devwiki/viewer_example.py), written by that script as a .collisionmesh file (version 0.9, without a BSP), and decoded by the port's own reader (crates/bf2-collision, its example collision_json). The game's meshes have the same layout: one geometry, a sub-geometry per damage state, a level of detail per collision type.

The basic test: segment against triangle#

0x006b5e20 (ours geom::ray_triangle in crates/bf2-physics/src/geom.rs):

  1. d = end - start; nothing to do when it is zero.
  2. The end's distance to the triangle's plane must not be positive, the start's not negative: the segment has to come from the front and reach the plane.
  3. One-sided queries drop faces that the direction does not oppose.
  4. t = -(startDistance / (d . n)).
  5. The hit is projected onto the plane's two major axes: a normal with |n.y| >= 0.7 drops y, else |n.z| <= 0.3 drops x, else z goes.
  6. Three edge functions decide whether the hit lies inside.
  7. The result's metric is (t - 1) x |d|: minus the distance from the hit to the segment's end. Queries keep the smallest, which is the hit nearest the start.
A quirk the port keeps

Step 6 rejects only when the first edge function disagrees in sign with the second or with the third, beyond FLT_EPSILON. The second and the third are never compared with each other. When the first is within epsilon of zero, any point on the line through the triangle's first two corners is accepted, even outside the triangle. The original does this, so the port does.

Collision meshes: the BSP walk#

CollisionMeshTemplate::intersectRay (0x006b96c0, client BF2.exe!0x00701b50; ours QueryGeometry::intersect_ray in crates/bf2-physics/src/mesh.rs) moves the ray into the mesh's space (local = (p - T) x inverse), walks the tree, and takes the hit point and the face's normal back through the world matrix. The material is remapped by the template's table.

The tree (Bsp, ours crates/bf2-physics/src/bsp.rs) is flattened to 16-byte nodes, each a split along one axis. The segment walk (0x006c3100):

  • clips the segment to the node with a little slack (1e-4 / |d|);
  • goes into the near child first, with the unclipped interval;
  • visits the far child when the segment, widened by the query's radius, reaches across the split;
  • at a leaf tests every face (0x006c2db0) and keeps a hit that beats the best so far and lies on this leaf's side of the parent's split.

Two entry points use the walk: Bsp::intersectRay (0x006c3600) for the nearest hit, and Bsp::querySegment (0x006c3490), which collects faces for the sphere and capsule queries; each face is collected once per query by a stamp. A segment shorter than 2^-23 becomes a sphere query around its start.

The terrain: a pyramid of maximum heights#

Heightmap::intersectSegment (0x006ab850; ours crates/bf2-physics/src/heightmap.rs) works in grid coordinates. To avoid testing every cell under a long segment it walks a pyramid of maximum heights (0x006a9870, built by 0x006aada0): the finest level holds, per 2 x 2 cells, the maximum of the 3 x 3 samples around them, rounded up to a byte; each coarser level the maximum of four children. A node is skipped when its maximum is below both ends of the part of the segment inside it.

The same walk in a side view: move the segment's ends and see which nodes are skipped and how few cells are tested.

A cell is two triangles, split along the diagonal from (x + 1, z) to (x, z + 1). The cell test takes the triangle the segment enters first, then the other:

OrderTriangleNormal before normalising
upper first (entry fx + fz >= 1)upper(h01 - h11, 1, h10 - h11)
then, when the exit's sum is below 1lower(h00 - h10, 1, h00 - h01)
Nodes on the border reach to infinity

Splits happen only at inner grid lines, so the pyramid's border nodes extend outward without end, and their cells test extrapolated planes. A segment outside the grid is tested against the border cells' planes continued outward, and the culling decides which of those planes can be hit.

Around the playable heightmap a level may have surrounding ones; a cluster (HeightmapCluster::intersectSegment 0x006972a0) hands the segment to the heightmaps it crosses. Nearly vertical segments take a shortcut through the height and normal at their end.

The soldier: a moving sphere and a capsule#

intersectMovingSphere (0x006bac50; ours QueryGeometry::intersect_moving_sphere):

  1. The start and the motion go into the mesh's space.
  2. The query is moving when the motion is longer than a centimetre, else static.
  3. Faces are collected along the motion widened by the radius, or, static, in a sphere of 1.5 radii.
  4. The face pass: moving, a sphere swept against each face; static, a sphere against each triangle. A contact gives a point, a normal, a depth and a material.
  5. The edge pass, only when moving and the face pass found nothing: the sphere against the edges of every collected face. This is the case of a corner or a ledge.

The capsule query collects faces along its axis and tests their edges (0x006b78d0). All contacts go to the soldier's response code, which resolves them (the soldier).

How it is tested#

Every function on this page is identical in the client and the dedicated server (matched byte for byte with the call targets masked), and the port is held to them bit for bit, at both x87 precisions:

Check53 bits (server)24 bits (client)
segment distance and basis8,000 cases3,000
line against capsule8,0003,000
moving-sphere query on real meshes19,600 queries, 106,217 contacts7,840 queries, 42,803 contacts
ray query, two-sided, front, back, stop-first19,600 rays, 4,830 hits7,840 rays, 1,926 hits
capsule query19,600 queries7,840

The mesh queries run on 24 meshes of an install, each level of detail laid out with the original's own face-record builder, at random placements, aimed at random faces. One finding of these runs is on the float precision page's theme: a rotation that summed its three products in another order than the original made the mesh ray query differ at 24 bits only.

Notes: notes/subsystems/physics-collision.md, notes/subsystems/physics.md, notes/formats/collisionmesh.md. Code: crates/bf2-physics/src/ (geom.rs, bsp.rs, mesh.rs, heightmap.rs, sweep.rs, capsule.rs), crates/bf2-collision. The file format: meshes. The terrain as a whole (heights, materials, growth): terrain.

What is tested against these meshes each tick: projectiles, and a soldier's bone capsules from animation.

In the code#

The crates this page names, each with its line of docs/release/LAYOUT.md (the list is gathered by the wiki's build from the page itself):

CrateWhat it is
crates/bf2-physicsphysics: collision queries, materials, rigid-body integration
crates/bf2-collisioncollision mesh files

Files this page names: crates/bf2-physics/src/geom.rs, crates/bf2-physics/src/mesh.rs, crates/bf2-physics/src/bsp.rs, crates/bf2-physics/src/heightmap.rs.

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