Understanding the Spatial Grid
ThespatialGridManager is a singleton that maintains spatial indices for:
- Floor items - Items placed on the floor/slabs
- Wall items - Items attached to walls
- Ceiling items - Items attached to ceilings
- Slabs - Floor polygons with elevation data
- Walls - Wall segments for attachment validation
The spatial grid is automatically synchronized with the scene state. You don’t need to manually update it when creating or moving nodes.
Importing the Spatial Grid Manager
Placement Validation
Floor Placement
Validate if an item can be placed on the floor without collisions:1
Get the current level ID
2
Call canPlaceOnFloor
Wall Placement
Validate if an item can be placed on a wall:localX- Position along the wall from its start pointlocalY- Height above the floorattachType:'wall'- Item spans both sides (requires both sides free)'wall-side'- Item on one side only (e.g., wall-mounted TV)
side- Which wall side for'wall-side'items
Ceiling Placement
Validate if an item can be placed on a ceiling:The ceiling placement check validates:
- The item footprint is within the ceiling polygon
- The item is not in a ceiling hole
- No collision with other ceiling items
Elevation Queries
Get Slab Elevation at Point
Find the floor elevation at a specific (x, z) coordinate:- Returns the highest slab elevation if the point is inside a slab polygon
- Returns
0if the point is not on any slab - Respects holes in slabs (returns
0if point is in a hole)
Get Slab Elevation for Item
Find the slab elevation for an item based on its full footprint:Get Slab Elevation for Wall
Find the slab elevation for a wall segment:Collision Detection Workflow
Here’s a complete example of validating item placement with real-time feedback:1
Track mouse position
2
Validate placement in real-time
3
Visualize validity
4
Place item on click
Advanced Use Cases
Finding Available Space
Iterate through positions to find the first available spot:Snapping to Walls
Find the nearest valid wall attachment point:Checking Item Overlaps
Find all items that overlap with a given area:Performance Considerations
Spatial Grid Cell Size
The spatial grid divides space into cells for efficient lookup. The default cell size is0.5 meters:
Efficient Queries
Batch Operations
When placing multiple items, validate all positions first:Best Practices
Always Validate Before Creating
Use Scaled Dimensions
Ignore Self in Movement Validation
Next Steps
Creating Nodes
Create validated nodes using spatial queries
Event Handling
Use spatial queries in event handlers