BGE/BSP
Alias: BGE.BSP
Static Methods
buildTree(
items: Array.<BGE.BSP.StaticItem>,
counter?: object,
): object
Builds a classification-only BSP tree over static quads - no polygon splitting (see the design spec for why). Picks the first item as each node's splitting plane and partitions every other remaining item to front/back by which side of that plane its center falls on. Each node gets an id (0 up, in build order) for findLeaf().
Parameters
items(Array.<BGE.BSP.StaticItem>)counter(object, optional, default: "invalid") — internal: shares the next node id across the recursion
Returns
object—invalidfor an empty list, else{item, front, back, id}
findLeaf(root: object, x: float, y: float, z: float): integer
Which leaf of the tree a point falls in: an index into the buckets collectLeafOrder() takes. Depends only on the point and the tree, so callers can cache it while neither changes. An empty tree has the one leaf 0.
Parameters
root(object) — result of buildTree(), or invalidx(float)y(float)z(float)
Returns
integer— leaf index, 0 to twice the node count - 1
findBoundsLeaf(
root: object,
x: float,
y: float,
z: float,
points: Array.<BGE.Math.Vector>,
cameraPosition: BGE.Math.Vector,
state: BGE.BSP.LeafState,
): integer
Like findLeaf(), for an object with size: points bound it, and (x, y, z) is its centre. An object wholly on one side of a node's plane goes that side. One that crosses the plane but lies past the quad's edges (a house across the line of a fence down the street) goes on whichever side draws it in the right order from the camera, as a plane at the quad's edge separates them; it then sets state.usesCamera. Otherwise its centre's side is used.
Parameters
root(object) — result of buildTree(), or invalidx(float)y(float)z(float)points(Array.<BGE.Math.Vector>)cameraPosition(BGE.Math.Vector)state(BGE.BSP.LeafState) — set on return
Returns
integer— leaf index, as findLeaf()
separatedOrder(
ax: float,
ay: float,
az: float,
quadMin: float,
quadMax: float,
points: Array.<BGE.Math.Vector>,
cameraPosition: BGE.Math.Vector,
): integer
Along one of a quad's edge directions (ax, ay, az), where the quad spans quadMin to quadMax: 0 if points overlap that span, else 1 if the camera is on the points' side of the gap (they draw after the quad), -1 if on the quad's.
Parameters
ax(float)ay(float)az(float)quadMin(float)quadMax(float)points(Array.<BGE.Math.Vector>)cameraPosition(BGE.Math.Vector)
Returns
integer
collectLeafOrder(
node: object,
cameraPosition: BGE.Math.Vector,
buckets: Array.<object>,
result: Array.<dynamic>,
): void
Walks the tree back-to-front relative to the camera, appending each static item's sceneObj to result, with each leaf's bucket (buckets[findLeaf(...)], already in draw order, or invalid when empty) appended where the leaf falls.
Parameters
node(object) — result of buildTree(), or invalidcameraPosition(BGE.Math.Vector)buckets(Array.<object>) — arrays of payloads by leaf indexresult(Array.<dynamic>) — appended to in draw order
Returns
void
collectNodeOrder(
node: object,
cx: float,
cy: float,
cz: float,
buckets: Array.<object>,
result: Array.<dynamic>,
): void
Parameters
node(object)cx(float)cy(float)cz(float)buckets(Array.<object>)result(Array.<dynamic>)
Returns
void
collectDrawOrder(
node: object,
cameraPosition: BGE.Math.Vector,
dynamicItems: Array.<BGE.BSP.DynamicItem>,
result: Array.<dynamic>,
): void
Walks the tree back-to-front relative to cameraPosition, appending each static item's sceneObj payload to result in the correct draw position, with dynamicItems classified against each node's plane and interleaved at the matching point instead of being split. A leaf (node = invalid) appends its remaining dynamicItems sorted by depthKey ascending.
Parameters
node(object) — result of buildTree(), or invalidcameraPosition(BGE.Math.Vector)dynamicItems(Array.<BGE.BSP.DynamicItem>)result(Array.<dynamic>) — appended to in draw order - not returned, so a caller can reuse one array across a partial traversal if ever needed
Returns
void
Other
StaticItem
One static quad's plane, in world space - planePoint/planeNormal come from the quad's own BGE.Math.CornerPoints.getCenter()/getNormal(). sceneObj is an opaque payload this module never inspects - the caller gets it back unchanged in collectDrawOrder()'s result.
Properties
sceneObj(dynamic)planePoint(BGE.Math.Vector)planeNormal(BGE.Math.Vector)corners(Array.<BGE.Math.Vector>) — The quad's corners (topLeft, topRight, bottomLeft, bottomRight), so findBoundsLeaf() can
DynamicItem
One dynamic (non-static) object being threaded into the BSP walk. depthKey mirrors BGE.SceneObject.negDistanceFromCamera's convention (ascending = farthest from the camera first) - used only to order objects that land in the same leaf, where no more static geometry disambiguates them further.
Properties
sceneObj(dynamic)position(BGE.Math.Vector)depthKey(float)points(Array.<BGE.Math.Vector>) — Points bounding the object, to place it with findBoundsLeaf() instead of findLeaf()
LeafState
Extends: roAssociativeArray
findBoundsLeaf()'s second result
Properties
usesCamera(boolean) — Whether the leaf depends on the camera position as well as the object's