Game Development Reference
In-Depth Information
if (x1 >= 0 && x1 < cellsPerRow) {
if (y1 >= 0 && y1 < cellsPerCol)
cellIds[i++] = x1 + y1 * cellsPerRow;
if (y2 >= 0 && y2 < cellsPerCol)
cellIds[i++] = x1 + y2 * cellsPerRow;
}
while(i (i <= 3) cellIds[i++] = -1;
}
else if (y1 == y2) {
int i = 0;
if (y1 >= 0 && y1 < cellsPerCol) {
if (x1 >= 0 && x1 < cellsPerRow)
cellIds[i++] = x1 + y1 * cellsPerRow;
if (x2 >= 0 && x2 < cellsPerRow)
cellIds[i++] = x2 + y1 * cellsPerRow;
}
while(i (i <= 3) cellIds[i++] = -1;
}
else {
int i = 0;
int y1CellsPerRow = y1 * cellsPerRow;
int y2CellsPerRow = y2 * cellsPerRow;
if (x1 >= 0 && x1 < cellsPerRow && y1 >= 0 && y1 < cellsPerCol)
cellIds[i++] = x1 + y1CellsPerRow;
if (x2 >= 0 && x2 < cellsPerRow && y1 >= 0 && y1 < cellsPerCol)
cellIds[i++] = x2 + y1CellsPerRow;
if (x2 >= 0 && x2 < cellsPerRow && y2 >= 0 && y2 < cellsPerCol)
cellIds[i++] = x2 + y2CellsPerRow;
if (x1 >= 0 && x1 < cellsPerRow && y2 >= 0 && y2 < cellsPerCol)
cellIds[i++] = x1 + y2CellsPerRow;
while(i (i <= 3) cellIds[i++] = -1;
}
return cellIds;
}
}
The first four lines of this method calculate the cell coordinates of the bottom-left and top-
right corners of the object's bounding rectangle. This calculation was discussed earlier. To
understand the rest of this method, think about how an object can overlap grid cells. There are
four possibilities:
The object is contained in a single cell. The bottom-left and top-right
ï?®
corners of the bounding rectangle thus have the same cell coordinates.
The object overlaps two cells horizontally. The bottom-left corner is in one
ï?®
cell, and the top-right corner is in the cell to the right.
The object overlaps two cells vertically. The bottom-left corner is in one cell,
ï?®
and the top-right corner is in the cell above.
The object overlaps four cells. The bottom-left corner is in one cell, the
ï?®
bottom-right corner is in the cell to the right, the top-right corner is in the
cell above that, and the top-left corner is in the cell above the first cell.
 
Search WWH ::




Custom Search