Skip to main content
The spatial grid system provides efficient collision detection and placement validation for your 3D scene. It tracks items, walls, slabs, and ceilings in optimized spatial data structures.

Understanding the Spatial Grid

The spatialGridManager 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

Return value:
Use the ignoreIds parameter when validating movement of an existing item to avoid self-collision.

Wall Placement

Validate if an item can be placed on a wall:
Parameters explained:
  • localX - Position along the wall from its start point
  • localY - Height above the floor
  • attachType:
    • '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:
  1. The item footprint is within the ceiling polygon
  2. The item is not in a ceiling hole
  3. No collision with other ceiling items

Elevation Queries

Get Slab Elevation at Point

Find the floor elevation at a specific (x, z) coordinate:
How it works:
  • Returns the highest slab elevation if the point is inside a slab polygon
  • Returns 0 if the point is not on any slab
  • Respects holes in slabs (returns 0 if point is in a hole)

Get Slab Elevation for Item

Find the slab elevation for an item based on its full footprint:
Use case: This is used by the ItemSystem to automatically elevate floor items to sit on slabs:

Get Slab Elevation for Wall

Find the slab elevation for a wall segment:
This is useful for elevating walls when they sit on raised slabs or platforms.

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 is 0.5 meters:
The cell size is automatically configured. You don’t need to adjust it for typical use cases.

Efficient Queries

Batch Operations

When placing multiple items, validate all positions first:

Best Practices

Always Validate Before Creating

Use Scaled Dimensions

Always use getScaledDimensions() when querying the spatial grid to account for item scaling.

Ignore Self in Movement Validation

Next Steps

Creating Nodes

Create validated nodes using spatial queries

Event Handling

Use spatial queries in event handlers