Game Development Reference
In-Depth Information
int
i = 0;
int
cellId = -1;
while(i
(i <= 3 && (cellId = cellIds[i++]) != -1) {
staticCells[cellId].add(obj);
}
}
public void
insertDynamicObject(GameObject obj) {
int
[] cellIds = getCellIds(obj);
int
i = 0;
int
cellId = -1;
while(i
(i <= 3 && (cellId = cellIds[i++]) != -1) {
dynamicCells[cellId].add(obj);
}
}
Next up are the methods
insertStaticObject()
and
insertDynamicObject()
. They calculate
the IDs of the cells in which the object is contained, via a call to
getCellIds()
, and insert the
object into the appropriate list accordingly. The
getCellIds()
method will actually fill the
cellIds[i++])
member array.
public void
removeObject(GameObject obj) {
int
[] cellIds = getCellIds(obj);
int
i = 0;
int
cellId = -1;
while(i
(i <= 3 && (cellId = cellIds[i++]) != -1) {
dynamicCells[cellId].remove(obj);
staticCells[cellId].remove(obj);
}
}
We also have a
removeObject()
method, which we can use to figure out which cells the object
is in, and then delete it from the dynamic or static lists accordingly. This will be needed when a
game object dies, for example.
public void
clearDynamicCells(GameObject obj) {
int
len = dynamicCells.length;
for
(
int
i = 0; i < len; i++) {
dynamicCells[i].clear();
}
}
The
clearDynamicCells()
method will be used to clear all dynamic cell lists. We need to call this
each frame before we reinsert the dynamic objects, as discussed earlier.
public
List<GameObject> getPotentialColliders(GameObject obj) {
foundObjects.clear();
int
[] cellIds = getCellIds(obj);
int
i = 0;
int
cellId = -1;
while(i
(i <= 3 && (cellId = cellIds[i++]) != -1) {