Package com.tenio.engine.physic.utility
Class CellSpacePartition<E extends BaseGameEntity>
- java.lang.Object
-
- com.tenio.engine.physic.utility.CellSpacePartition<E>
-
- Type Parameters:
E
- the game entity template
- All Implemented Interfaces:
IRender
public class CellSpacePartition<E extends BaseGameEntity> extends java.lang.Object implements IRender
This class is used to divide a 2D space into a grid of cells each of which may contain a number of entities. Once created and initialized with entities, fast proximity queries can be made by calling the CalculateNeighbors method with a position and proximity radius. If an entity is capable of moving, and therefore capable of moving between cells, the Update method should be called each update-cycle to synchronize the entity and the cell space it occupies- Author:
- sallyx (https://www.sallyx.org/sally/en/game-ai/)
-
-
Constructor Summary
Constructors Constructor Description CellSpacePartition(float width, float height, int cellsX, int cellsY, int maxEntities)
Create a new instance
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addEntity(E entity)
Used to add the entities to the data structure adds entities to the class by allocating them to the appropriate cellvoid
calculateNeighbors(Vector2 targetPos, float queryRadius)
This must be called to create the vector of neighbors.This method examines each cell within range of the target, If the cells contain entities then they are tested to see if they are situated within the target's neighborhood region.void
clearCells()
Clears the cells of all entitiesE
getFrontOfNeighbor()
E
getNextOfNeighbor()
boolean
isEndOfNeighbors()
void
render(Paint paint)
To render objects to screenvoid
updateEntity(E entity, Vector2 oldPosition)
Update an entity's cell by calling this from your entity's Update method Checks to see if an entity has moved cells.
-
-
-
Constructor Detail
-
CellSpacePartition
public CellSpacePartition(float width, float height, int cellsX, int cellsY, int maxEntities)
Create a new instance- Parameters:
width
- width of 2D spaceheight
- height of 2D spacecellsX
- number of divisions horizontallycellsY
- number of divisions verticallymaxEntities
- maximum number of entities to partition
-
-
Method Detail
-
addEntity
public void addEntity(E entity)
Used to add the entities to the data structure adds entities to the class by allocating them to the appropriate cell- Parameters:
entity
- an entity
-
updateEntity
public void updateEntity(E entity, Vector2 oldPosition)
Update an entity's cell by calling this from your entity's Update method Checks to see if an entity has moved cells. If so the data structure is updated accordingly- Parameters:
entity
- an entityoldPosition
- seeVector2
-
calculateNeighbors
public void calculateNeighbors(Vector2 targetPos, float queryRadius)
This must be called to create the vector of neighbors.This method examines each cell within range of the target, If the cells contain entities then they are tested to see if they are situated within the target's neighborhood region. If they are they are added to neighbor list this method stores a target's neighbors in the neighbor vector. After you have called this method use the begin, next and end methods to iterate through the vector.- Parameters:
targetPos
- seeVector2
queryRadius
- radius value
-
getFrontOfNeighbor
public E getFrontOfNeighbor()
- Returns:
- a reference to the entity at the front of the neighbor vector
-
getNextOfNeighbor
public E getNextOfNeighbor()
- Returns:
- the next entity in the neighbor vector
-
isEndOfNeighbors
public boolean isEndOfNeighbors()
- Returns:
- true if the end of the vector is found (a zero value marks the end)
-
clearCells
public void clearCells()
Clears the cells of all entities
-
-