CellSpacePartitioning(width, height, depth, cellsX, cellsY, cellsZ)

This class is used for cell-space partitioning, a basic approach for implementing a spatial index. The 3D space is divided up into a number of cells. A cell contains a list of references to all the entities it contains. Compared to other spatial indices like octrees, the division of the 3D space is coarse and often not balanced but the computational overhead for calculating the index of a specific cell based on a position vector is very fast.

new CellSpacePartitioning(width, height, depth, cellsX, cellsY, cellsZ)

Constructs a new spatial index with the given values.

Parameters:
Name Type Description
width Number

The width of the entire spatial index.

height Number

The height of the entire spatial index.

depth Number

The depth of the entire spatial index.

cellsX Number

The amount of cells along the x-axis.

cellsY Number

The amount of cells along the y-axis.

cellsZ Number

The amount of cells along the z-axis.

Author:

Members

cells :Array.<Cell>

The list of partitions.

cellsX :Number

The amount of cells along the x-axis.

cellsY :Number

The amount of cells along the y-axis.

cellsZ :Number

The amount of cells along the z-axis.

depth :Number

The depth of the entire spatial index.

height :Number

The height of the entire spatial index.

width :Number

The width of the entire spatial index.

Methods

addEntityToPartition(entity, index) → {CellSpacePartitioning}

Adds an entity to a specific partition.

Parameters:
Name Type Description
entity GameEntity

The entity to add.

index Number

The partition index.

Returns:
CellSpacePartitioning -

A reference to this spatial index.

addPolygon(polygon) → {CellSpacePartitioning}

Adds a polygon to the spatial index. A polygon is approximated with an AABB.

Parameters:
Name Type Description
polygon Polygon

The polygon to add.

Returns:
CellSpacePartitioning -

A reference to this spatial index.

fromJSON(json) → {CellSpacePartitioning}

Restores this instance from the given JSON object.

Parameters:
Name Type Description
json Object

The JSON object.

Returns:
CellSpacePartitioning -

A reference to this spatial index.

getIndexForPosition(position) → {Number}

Computes the partition index for the given position vector.

Parameters:
Name Type Description
position Vector3

The given position.

Returns:
Number -

The partition index.

makeEmpty() → {CellSpacePartitioning}

Removes all entities from all partitions.

Returns:
CellSpacePartitioning -

A reference to this spatial index.

query(position, radius, result) → {Array.<Any>}

Performs a query to the spatial index according the the given position and radius. The method approximates the query position and radius with an AABB and then performs an intersection test with all non-empty cells in order to determine relevant partitions. Stores the result in the given result array.

Parameters:
Name Type Description
position Vector3

The given query position.

radius Number

The given query radius.

result Array.<Any>

The result array.

Returns:
Array.<Any> -

The result array.

removeEntityFromPartition(entity, index) → {CellSpacePartitioning}

Removes an entity from a specific partition.

Parameters:
Name Type Description
entity GameEntity

The entity to remove.

index Number

The partition index.

Returns:
CellSpacePartitioning -

A reference to this spatial index.

resolveReferences(entities) → {CellSpacePartitioning}

Restores UUIDs with references to GameEntity objects.

Parameters:
Name Type Description
entities Map.<String, GameEntity>

Maps game entities to UUIDs.

Returns:
CellSpacePartitioning -

A reference to this cell space portioning.

toJSON() → {Object}

Transforms this instance into a JSON object.

Returns:
Object -

The JSON object.

updateEntity(entity, currentIndex) → {Number}

Updates the partitioning index of a given game entity.

Parameters:
Name Type Description
entity GameEntity

The entity to update.

currentIndex Number

The current partition index of the entity.

Returns:
Number -

The new partitioning index for the given game entity.