Scene collision trees (.OCT)¶
Every scene archive (SCENES/**.ZIP) holds one .OCT. ZEngineDataBase::LoadBoundTrees (src/engine/enginedata/enginedatabase.cpp:1467) reads the whole file into m_pPackedTreeData, and CreateBoundTrees (line 1436) hands it out after the geoms exist. It holds every static octree of the scene: the bound trees of the static ZTreeGroups (which geoms are where), the per-mesh collision trees (which triangles are where), and the table that names the triangles. There are no triangles in the file. Collision tests read the vertices of the render strips in the .PRM (docs/internals/formats/prm.md).
tools/port/scene/oct.py reads and writes the file and builds new trees. All 71 files on the disc round-trip byte for byte. The rules below were checked against all of them: the builder rebuilds every one of the 20,639 trees from that tree's own objects, and every collision tree from the .PRM triangles.
File layout¶
All values are little-endian. Offsets are from the start of the file. The lColiId values in the prims are byte offsets into this buffer.
| Part | Size | Contents |
|---|---|---|
| header | 0x10 | four u32 words. CreateBoundTrees reads only word 0: non-zero calls m_pRoot->MakeStaticContainer(true). All four words are 1 in every file on the disc. |
| bound-tree blocks | 5 slots each | one block per static-container ZTreeGroup, in the order RecurGetNext walks the scene graph from the root (CreateBoundTrees lines 1444 to 1452). The root comes first. |
| collision tree records | size each |
read until a u32 0xffffffff (LoadInternColiTree, zcollisionbaseold.cpp:466) |
| end marker | 4 | 0xffffffff |
| sub-strip table | 8 per entry, up to the end of the file | LoadUniqueSubStripInfo (zcollisionbaseold.cpp:228): count = remaining bytes / 8 |
The file does not store how many blocks there are. The loader gives one block to each ZTreeGroup that is a static container when the scene graph is built. Every ZROOM and ZBackdrop is one, because their SetStaticContainer setter forces it (zroom.cpp:43, 704), and so is the root ZROOM "ROOT". oct.py tells the parts apart without the scene graph. A bound-tree slot always has 12 zero bytes at +0x14. A collision tree record has its stale scale there, which is never zero.
Bound-tree block (ZTreeGroup::LoadBoundTrees, ztreegroup.cpp:497; writer SaveBoundTrees, line 461)¶
A block is five slots, one per eGlobalTreeType: GT_StdObjs 0, GT_Lights 1, GT_Gates 2, GT_Groups 3, GT_TreeGroups 4. Each slot is either empty or a tree:
| Offset | Size | Empty slot | Tree slot |
|---|---|---|---|
| 0x00 | 4 | 0 | tree size in bytes |
| 0x04 | 4 | 12 zero bytes | fScale (float) |
| 0x08 | 12 | vOrigin (V3) |
|
| 0x14 | 12 | zero | |
| 0x20 | size | compiled tree (below) | |
| pad | zero bytes up to a multiple of 16 (tree sizes are already multiples of 16) |
An empty slot is 0x10 bytes. LoadBoundTrees creates a ZOctreeCompiled over the tree, then sets the slot's scale and origin. These always equal the tree's own header. All five trees of a group share one origin and scale.
The 1,528 blocks contain 1,342 StdObjs trees, 1,053 Lights trees, 823 Groups trees and 53 TreeGroups trees. The Gates slots are empty.
Leaf ids are run-time geometry REFs, and nothing remaps them: RemapGeomIDLoad is the identity (ztreegroup.cpp:453). A REF here is the ZBaseGeom slot index + 1 in ZGeomBuffer's memory manager (fixed_size_memory_manager.h:79, ID 0 at load). It is not the .GMS m_iGeomNr. The root is REF 1. The other slots follow the allocation order of CreateGeoms (docs/internals/formats/gms.md, group counters). On the disc the values run from 2 to 12,931, and the static bit 0x40000000 is never set.
| Slot | Holds (GetBoundTreeType) |
Read by |
|---|---|---|
GT_StdObjs |
ZSTDOBJ and other mesh geoms in the group (zstdobj.cpp:39) |
every collision query (CalcLineColi, GetStripsInsideBox and ZCollisionBox::GetStrips pass GT_StdObjs) |
GT_Lights |
ZLIGHT (zlight.cpp:288) |
light gathering |
GT_Groups |
plain ZGROUPs (zgroup.cpp:722) |
group box queries |
GT_TreeGroups |
child ZTreeGroups, for example the rooms under the root (ztreegroup.cpp:116) |
The bounds are in the group's local space. CalcRoomLineCallBack moves the line into the room with GetLocalPointVect (zcollisionbaseold.cpp:283), and GetGeomsInBox does the same with GetLocalMatPos (line 810).
Collision tree record (ZCollisionBase::LoadInternColiTree, zcollisionbaseold.cpp:466)¶
| Offset | Size | Field | Notes |
|---|---|---|---|
| 0x00 | 4 | record size | 0x20 + tree size, a multiple of 16; the next record follows directly |
| 0x04 | 28 | stale ZOctreeCompiled |
the engine constructs a ZOctreeCompiled (0x18 bytes) here with placement new and overwrites it. The PC tool dumped its own object: vtable 0x00fe497c, origin, scale, its m_pxTree heap pointer, and a zero word. oct.py keeps the pointer (editor_tree_pointer). |
| 0x20 | compiled tree |
A prim with collision stores lColiId = (record offset + 4) | 1. That is SPrimStrips::lColiId (+0x30, zolddrawps2.h:95) for strips and dot3 strips, and SPrimObjectHeader::lColiId (+0x1c, zprimcontrolbase.h:127) for object headers. GetColiOctree (zcollisionbaseold.cpp:246) treats an odd value as an offset into m_pPackedTreeData and an even one as a raw ZOctree* pointer. A prim without collision must therefore have lColiId 0. Only the root prim of a geom is read (CalcGeomLineCallBack, line 300, through GetPrimHandle). The sub-prims of the chain carry unrelated values on the disc. All 17,368 records have an owner. 16,922 have one root prim, and up to ten prims share a record.
Leaf ids are byte offsets into the sub-strip table (multiples of 8, CalcStripLineCallBack line 330 and CallBackStripInsideBox line 490). Each tree uses a contiguous run of the table, and the trees take their runs in file order. Every entry is used by exactly one leaf.
The tree is in the prim's local space. CalcGeomLineCallBack moves the line with pBaseGeom->GetLocalPointVect (line 312).
Sub-strip table entry (STempStripsUniqueId, zcollisionboxold.h:13)¶
Each entry is one window of one triangle strip of one chunk of one sub-prim. The triangles are read straight from the .PRM vertex packet (ZCollisionPS2::GetVerticesFromId, zcollisionps2.cpp:268).
| Word | Bits | Field | Meaning |
|---|---|---|---|
lIdLo |
0 to 15 | lEntryCount |
vertices in the window (3 to 16 on the disc); triangles = count − 2 |
| 16 to 23 | lEntryOffset |
element index in the chunk (UnpackVerticesFromStrip*Asm skips this many deltas) |
|
| 24 to 30 | lChunkAndWinding (low seven bits) |
index into the sub-prim's ZPrimChunk array (0 to 117 on the disc) |
|
| 31 | lChunkAndWinding (high bit) |
parity of the first vertex within its strip. It flips the winding of the first triangle (CalcSubStripLineCollision, zcollisionps2.cpp). It matches the parity in every entry on the disc. |
|
lIdHi |
0 to 31 | sub-prim handle | a strips or dot3 strips record in the owning chain (bit 31 set would be a raw pointer, ResolvePrim) |
GetStripsFromPrim (line 197) builds the same ids: (chunk << 24) | (index << 16) | (parity << 31), then CreateSubStripId ORs in the count. The line tests stop with ZDebugBreak when count ≥ 18 (STRIP_MAX_NR_VERTICES). GetVerticesFromId unpacks at most 18 vertices into its 72-float buffer.
How the table was generated, checked against the .PRM:
- The chain is walked last sub-prim first, because
GetStripsFromPrimprepends. Within a sub-prim, chunks, strips and windows go in order. This order reproduces 17,309 of the 17,368 trees' runs. The rest cover chain members the walk stops at. - Each strip is cut into windows of at most 16 vertices. Consecutive windows overlap by two vertices, so window k starts at 14k. This matches 419,038 of 420,450 strips. The other 0.3% skip degenerate triangles in the tool's unquantised vertices, which cannot be seen in the
.PRM.split_stripimplements the rule, and it regenerates 16,779 of the 17,368 trees' tables exactly. - Sub-prims without collision bits were included (52 sub-prims with
lColiBits0 have entries). The line and box tests mask them out later (CalcLineCollisionline 127,GetStripVerticesFromIdline 49). 28 sub-prims that have bits got no entries. The rule that excludes them is unknown.
Compiled tree (ZOctreeCompiled, src/engine/collision/octreecompiled.h)¶
Bound trees, collision trees and the room trees (docs/internals/formats/rooms.md) share this format. ZOctreeCompiled::Init (octreecompiled.cpp:97) points at the image.
| Offset | Size | Part |
|---|---|---|
| 0x00 | 0x14 | SCompiledTreeHeader: iObjectOffset (u32), vOrigin (V3), fScale (float) |
| 0x14 | 6 × nodes | ZNodeCompiled array; node 0 is the root (GetBasePtrNodes, line 112) |
to iObjectOffset |
padding: 0 to 14 bytes of 0xcd (MSVC debug fill of the PC tool) | |
iObjectOffset |
16 × objects | CObjectInfo array (GetBasePtrObjects, line 117) |
iObjectOffset = align16(0x14 + 6 × nodes) in every tree, and the tree size is iObjectOffset + 16 × objects. The node count is not stored. It is the number of nodes reachable from the root, and they are always contiguous.
ZNodeCompiled (6 bytes):
| Offset | Field | Meaning |
|---|---|---|
| 0x0 | m_iInfo |
bits 0 to 2: octant of the node in its parent (bit 0 +x, bit 1 +y, bit 2 +z); bits 3 to 14: object count (≤ 4095; at most 457 on the disc); bit 15: last sibling |
| 0x2 | m_iFirstChild |
node index of the first child, 0 for none; the children are contiguous up to the one with bit 15 |
| 0x4 | m_iFirstObject |
object index of the node's first object |
CObjectInfo (16 bytes, quadtree.h:26): iID (u32), then the minimum and maximum x, y, z as u16 octree coordinates.
Octree coordinates¶
A point maps to (p − vOrigin) × fScale + 32768, truncated (CheckCube line 128, CheckPoint line 400). The tree spans 0 to 0xffff on each axis. A query box takes int(min) and int(max) + 1, clamped. Points outside the range are clamped (cube), clipped (line, CheckLinesegment2 line 201), or miss.
Node geometry (loose octree)¶
The root is the cell 0 to 0x10000 centred on 0x8000. A node at depth d has cell edge 0x10000 >> d. Its children's centres lie at ± 0x8000 >> (d + 1) on each axis, chosen by the octant bits. A child is visited when the query overlaps the child's centre ± its cell edge: the cell doubled, a loose octree with factor 2. ZNodeCompiled::CheckPoint (line 60), OctreeCompiled_CheckCube (old/_ps2/octreeasm.cpp, original assembly) and CheckLineOct (old/_ps2/checklines.cpp) agree on this. Every object on the disc lies inside its node's loose box. The cube and line walks test each object's bounds (pcgth on the packed u16s, then LineVS_AAbox for lines) before they call the leaf callback.
How the tool built the trees¶
These rules reproduce all 20,639 .OCT trees on the disc byte for byte from their own objects (oct.py rebuild), and all 142 room trees (rooms.py rebuild).
- Object bounds:
min = int((v_min − origin) × scale + 32768)andmax = int((v_max − origin) × scale + 32768) + 1, evaluated in double precision, then clamped to 0 to 0xffff. In single precision about 17% of objects come out one unit off. - Depth: an object covering units min to max goes to the deepest depth d (at most 15) where
max − min + 1 < 0x10000 >> d, taking the largest of the three axes. This holds for every object on the disc. The depths reached are 0 to 14. - Node: at each level the octant bit for an axis is bit
16 − dof(min + max + 1) >> 1. That is the centre of the half-open range min to max + 1. This also holds for every object. - Nodes exist only on paths to objects. Siblings are sorted by octant. Numbering is root first, then each node's child block, then the blocks of those children, depth first (
build_nodes). - Objects are stored node by node in depth-first order. Within a node they appear in reverse insertion order, as if the tool's list was prepended to. Collision trees were filled in ascending leaf id, so their objects appear in descending id. For bound trees the order is the tool's geometry walk, which is not stored. 89% of their multi-object nodes are in descending REF order.
The frame differs by tree kind:
- Collision tree: origin per axis is
max − (max − min) × 0.5, all in single precision, over every vertex of the tree's windows. Scale is32000 / max(largest half extent, 10). This reproduces 17,366 of the 17,368 frames from the.PRMvertices. With those frames,oct.py compare-prmrebuilds all 463,623 object bounds and all 17,368 trees exactly (17,366 when the frame is also rebuilt). - Bound tree: scale is
32000 / max(half extent, 1000). 1000 is the smallest half extent on the disc, as inZTreeGroup::CreateDynamicTrees(ztreegroup.cpp:613), which uses 5000 for the root. The extent comes from the group's own bounds widened by 0.1 (GetOriginAndSize, line 754). No bound-tree object on the disc touches the clamp. The origin was not checked against the.GMSbounds.
What the queries read¶
| Query | Path through the files | Fields that matter |
|---|---|---|
Line of sight, bullets, CalcLineColi with bCheckStatic (zcollisionbaseold.cpp:727) |
.RMC tree → room (CalcRoomLineCallBack, no box test) → the room's GT_StdObjs bound tree in room space → geom (CalcGeomLineCallBack: geom must be active and visible, m_lControl & GeomConMask, prim handle set) → its prim's collision tree in geom space → sub-strip (CalcStripLineCallBack) → CalcLineCollision checks lColiBits & mask of the sub-prim, unpacks the window and intersects its triangles |
RMC object bounds; bound-tree object bounds and REFs; lColiId; tree objects; sub-strip ids; the PRM vertex packet, chunk sizes and m_fVertexScale; lColiBits |
Movement and pushing (ZCollisionBox::GetStrips → GetStripsInsideBox, line 507, GT_StdObjs) |
GetGeomsInBox (line 799) → rooms from the .RMC with the box test (InnerRoomFuncBox) → GetStaticGeomsInBound (ztreegroup.cpp:683; GeomBoundCubeChk rejects dynamic, inactive and hidden geoms and needs (coli bits \| control) & mask) → each prim's collision tree CheckCube → GetStripVerticesFromId (zcollisionps2.cpp:49) copies the triangles that touch the box |
the same, plus the room boxes |
Material of a hit (FindMaterialDescriptor, line 706) |
m_HitCache.lIdHi = the sub-prim → its lTextureId → ZTexturePS2::m_lMaterialDescription |
the sub-strip's prim handle; texture |
Sphere tests (CheckSphereCollision) |
whole sub-prim chains, no tree | .PRM only |
lColiBits on the disc is mostly 0x2 (COLIMASK_Background, on 28,538 of the sub-prims with entries), then 0x70 (NoWalk | Sight | Hero, invisible blockers, 5,709), 0x0 (1,402) and 0x72 (690). The geometry's own collision byte is the low byte of lGeomAndGroupCon in the .GMS.
What a new scene needs¶
- A scene with no static collision can copy the 100-byte
Loader_Sequence.OCT: the header, one root block of five empty slots, and the end marker. Every prim must then have lColiId 0. A missing.OCTalso loads, but any odd lColiId would then dereference a null buffer. - For one room with a floor and four walls,
oct.py selftestbuilds this (492 bytes):- header
1, 1, 1, 1; - one block per static container in scene-graph order. The root block lists the room's REF in
GT_TreeGroups, or the mesh geoms directly when the root is the only room. The room block lists the mesh geom REFs with their local bounds inGT_StdObjs(build_bound_tree). Lights go inGT_Lightsif light lookups should find them; - one collision tree per mesh prim chain (
build_coli_tree_for_chain, orbuild_filefor all of it). Write the returned lColiId into the root prim (prm.pywrites 0 by default). Give the sub-prims non-zerolColiBits, for example 0x2, and give the geom matching coli bits inlGeomAndGroupCon; - the end marker and the concatenated sub-strip table.
- header
- The REFs must be the run-time slots that
CreateGeomswill assign, so the.OCThas to be written after the.GMSlayout is fixed. Every block also needs its group to be a static container (rooms always are), and the number of blocks must equal the number of such groups. - A
.RMCis required as soon as anything runs a static line or box query (docs/internals/formats/rooms.md). It can be the 32-byte empty tree. - Build strips of at most 16 vertices, or let
split_stripcut them. Keep each window within the 18-vertex limit of the line test.
Unknown¶
- Header words 1 to 3: always 1, never read.
- The exact rule that left 28 collision-bit sub-prims out of the table, and the degenerate-triangle rule behind 0.3% of the windows. Both depend on the tool's unquantised geometry.
- The origin of the bound trees: it should follow
GetOriginAndSizeover the group's.GMS/.PRPbounds, but this has not been checked against those files. - The insertion order of bound-tree objects (the tool's geometry walk). Any order works for the engine.
- Which geoms the tool put into each bound tree: direct children only, or every descendant up to the next tree group. Checking needs the
.GMSslot allocation. - The order of the collision tree records. It is not the order of prim handles, and nothing depends on it.
GT_Gatesis never filled on the disc, and its readers were not traced.
PC version¶
The Steam release's .OCT has the layout above (oct.py parses all 68 PC files), and its bound-tree blocks, whose leaves are geom REFs, mean what the PS2 ones mean. Its collision trees do not: the PC tool indexed the PC meshes, and the converted prims (prm.md "PC version") are PS2 strips, so the engine read every PC sub-strip entry as a strip window that does not exist (GetVerticesFromId on a count of 32 runs past its 18-vertex buffer). port/engine/system/_sdl/pcdata/pccollision.{h,cpp} rebuilds the collision trees for the converted strips. Measured on the 68 PC archives (23,724 collision trees, 104,585 sub-strip entries).
PC sub-strip table entry¶
| Word | Meaning |
|---|---|
lIdHi |
the handle of a PC mesh object (type 8) of the header whose lColiId names the tree |
lIdLo |
the first triangle of the leaf in the object's index buffer, a multiple of 32; the leaf covers triangles lIdLo to min(lIdLo + 32, triangle count) − 1 |
Checked over all 68 files: the entries of an object are exactly 0, 32, 64, ... up to its triangle count once per tree that lists it (41,707 objects; 244 more are shared by 2 to 7 headers and appear once in each of their trees), so a tree covers an object completely or not at all. The PC executable reads them through ZPrimAccess, whose triangle accessors (0x004a3080, 0x004a3190) take a first triangle and a count against the index buffer. The lColiId of a PC object header is (record offset + 4) | 1, as on the PS2, and on every PC header with collision it names a record; 1,405 objects of those headers have no entries (their headers' other objects carry the collision).
Bound trees: leaf ids are geom REFs, so they stay. The .RMC and .RMI room trees (rooms.md) hold room boxes with room and ZBound REFs and offsets into their own files, so they need no change either; the ZBound prims' inside tests use the InsideBound plane lists the prim converter writes.
The conversion¶
PcConvertCollision(pOct, lOct, pPrm, lPrm, pMat, lMat, pPrp, lPrp, &pOut, &lOut, &Report) converts the scene's prims exactly as PcConvertPrims does (deterministic, so the handles agree) and keeps an object map: the converted sub-prims of every PC object. Then, for each collision tree record in file order:
- the PC objects the tree covers are the
lIdHiof its leaves; - each of their converted strips sub-prims (strips and dot3 only, as
GetStripsFromPrim; bone strips and shadow meshes have no collision windows) contributes every window of every strip: at most 16 vertices, consecutive windows overlapping by two,lIdLo = (start & 1) << 31 | chunk << 24 | first element << 16 | count(theGetStripsFromPrimandCreateSubStripIdencoding),lIdHithe sub-prim handle. The prim converter keeps a sub-prim to 127 chunks, the seven chunk bits of the id; - each window becomes one tree object: its leaf id is its byte offset in the new sub-strip table, its bounds the window's decoded vertices quantised in the PC tree's own frame (origin and scale), with the tool's rule (double precision, truncation, max + 1, clamped);
- the tree is laid out by the tool's rules above (depth from the extent, octants from the centre, nodes on object paths only, siblings by octant, objects in descending insertion order); oct.py's
rebuild_tree(sort_ids=True)reproduces all 23,724 converted trees byte for byte; - the record keeps the PC record's stale 28 bytes (same frame) and gets its new size.
The header and the bound-tree blocks are copied byte for byte, the records keep their order, the end marker follows, then the new table. PcConvertPrims given the same .OCT runs the rebuild too and writes (new record offset + 4) | 1 into the lColiId of every strips, bone strips or dot3 root whose PC header named a record (a value that names none would become 0; none does). A water-patch root gets 0, as the PS2 water records have: the 31 PC water headers with collision name empty trees.
The converted prims start with the decal prims of the scene's .PRP (prm.md "Decal prims"), which move every later new handle, so PcConvertCollision takes the same .PRP as PcConvertPrims; the two must be given the same members.
Measurements¶
tools/port/tests/pc_prims_test.cpp converts each scene's OCT with its PRM and MAT and walks every converted tree as OctreeCompiled_CheckCube does. On 24 November 2026, over all 68 archives:
- 68 of 68 convert; 23,724 trees, 611,963 windows, 2,583,885 triangles;
- every object lies in its node's loose box, every window vertex quantises inside its object's bounds, every entry decodes through the
GetVerticesFromIdlogic (sub-prim, chunk, first element, count ≤ 16) to a window of a strips record; - coverage: the windows' triangles are exactly the triangles of the PC objects each PC tree covered: 2,583,885 of 2,583,885 matched vertex by vertex (positions within the chunk quantisation), none missing and none extra; 2,580,622 (99.87%) in the PC winding with
CalcSubStripLineCollision's alternation from the window's direction bit, the rest slivers that also match the other way; - 23,641 converted roots name their rebuilt tree (of the other 83 headers with collision, 52 become shadow-mesh roots, whose
lColiIdthe engine does not read, and 31 water-patch roots with no tree); - 7.1 MB of PC OCTs become 21.1 MB (a 16-vertex window holds at most 14 triangles where a PC leaf held 32); the largest tree has 1,519 objects, the fullest node 93 (limits 65,535 and 4,095); at most 106 ms per scene, prim conversion included;
--fuzzalso changes 1 to 32 bytes of the OCT per round: 501 rounds of M01_postmission convert or are rejected without a crash.