Skip to main content

Spatial Grid Manager

The spatialGridManager provides spatial indexing and collision detection for items placed on floors, walls, and ceilings. It maintains separate grids per level and validates item placement based on geometry overlap.

Import

spatialGridManager

Singleton instance of SpatialGridManager with a default cell size of 0.5 meters.

Methods

handleNodeCreated

Called when a node is created. Registers the node in appropriate spatial grids.
AnyNode
required
The created node
string
required
The level ID containing this node

handleNodeUpdated

Called when a node is updated. Re-indexes the node in spatial grids.
AnyNode
required
The updated node
string
required
The level ID containing this node

handleNodeDeleted

Called when a node is deleted. Removes the node from spatial grids.
string
required
The deleted node ID
string
required
The node type (e.g., ‘wall’, ‘item’, ‘slab’)
string
required
The level ID containing this node
string[]
Array of item IDs that were removed (e.g., when deleting a wall removes all attached items)

canPlaceOnFloor

Checks if an item can be placed on the floor without collisions.
string
required
The level ID to check placement in
[number, number, number]
required
World position [x, y, z]
[number, number, number]
required
Item dimensions [width, height, depth]
[number, number, number]
required
Euler rotation [x, y, z] in radians (typically only Y rotation is used)
string[]
Optional array of item IDs to ignore during collision check (e.g., the item being moved)
{ valid: boolean; conflictIds: string[] }
  • valid: true if placement is allowed, false if there are collisions
  • conflictIds: Array of item IDs that would overlap

canPlaceOnWall

Checks if an item can be placed on a wall without collisions.
string
required
The level ID containing the wall
string
required
The wall ID to place the item on
number
required
X position in wall-local space (distance from wall start in meters)
number
required
Y position (height from floor in meters)
[number, number, number]
required
Item dimensions [width, height, depth]
'wall' | 'wall-side'
default:"'wall'"
  • 'wall': Item needs both sides of the wall (goes through the wall)
  • 'wall-side': Item only needs one side of the wall (e.g., picture frame)
'front' | 'back'
Required if attachType is 'wall-side'. Specifies which side of the wall.
string[]
Optional array of item IDs to ignore during collision check
{ valid: boolean; conflictIds: string[] }
  • valid: true if placement is allowed
  • conflictIds: Array of conflicting item IDs

getSlabElevationAt

Gets the total slab elevation at a given (x, z) position on a level.
string
required
The level ID
number
required
X coordinate in world space
number
required
Z coordinate in world space
number
The highest slab elevation at this point, or 0 if the point is not inside any slab (or is inside a hole)

getSlabElevationForItem

Gets the slab elevation for an item using its full footprint (bounding box).
string
required
The level ID
[number, number, number]
required
Item world position [x, y, z]
[number, number, number]
required
Item dimensions [width, height, depth]
[number, number, number]
required
Item rotation [x, y, z] in radians
number
The highest overlapping slab elevation, or 0 if no overlap

getSlabElevationForWall

Gets the slab elevation for a wall by checking if it overlaps with any slab polygon.
string
required
The level ID
[number, number]
required
Wall start point [x, z]
[number, number]
required
Wall end point [x, z]
number
The highest slab elevation, or 0 if the wall doesn’t overlap any slab

canPlaceOnCeiling

Checks if an item can be placed on a ceiling without collisions.
string
required
The ceiling ID
[number, number, number]
required
Item position in ceiling-local space [x, y, z]
[number, number, number]
required
Item dimensions [width, height, depth]
[number, number, number]
required
Item rotation [x, y, z] in radians
string[]
Optional array of item IDs to ignore during collision check
{ valid: boolean; conflictIds: string[] }
  • valid: true if placement is allowed
  • conflictIds: Array of conflicting item IDs

clearLevel

Clears all spatial grids for a level.
string
required
The level ID to clear

clear

Clears all spatial grids.

Utility Functions

pointInPolygon

Point-in-polygon test using ray casting algorithm.
number
required
Point X coordinate
number
required
Point Z coordinate
Array<[number, number]>
required
Polygon vertices as [x, z] pairs
boolean
true if the point is inside the polygon, false otherwise

Usage Examples

Floor Item Placement

Wall Item Placement

Slab Elevation Query

Moving an Item

Syncing with Scene Store

Performance Characteristics

  • Cell-based spatial indexing: O(1) average case for insertion and queries
  • Separate grids per level: Queries only check relevant level
  • Bounding box culling: Only detailed geometry checks for nearby items
  • Efficient polygon tests: Ray casting algorithm is O(n) where n = polygon vertices

See Also