Clamp spatial index to grid size

This commit is contained in:
2024-02-10 11:06:41 +01:00
parent 0e6e1d5a2d
commit 504b5b5f9b

View File

@@ -68,6 +68,7 @@ void *bzSpatialGridGetData(const BzSpatialGrid *grid, BzSpatialGridID id) {
static BzSpatialGridID **getCell(const BzSpatialGrid *grid, i32 x, i32 y) {
BZ_ASSERT(x >= 0 && y >= 0 && x < grid->width && y < grid->height);
return grid->cells + y * grid->width + x;
}
@@ -100,10 +101,6 @@ static BzSpatialGridIndex calculateGridIndex(BzSpatialGrid *grid, f32 posX, f32
(i16) (maxX / grid->cellWidth),
(i16) (maxY / grid->cellHeight),
};
if (index.minX < 0) index.minX = 0;
if (index.minY < 0) index.minY = 0;
if (index.maxX >= grid->width) index.maxX = grid->width - 1;
if (index.maxY >= grid->height) index.maxY = grid->height - 1;
if (index.minX > index.maxX) {
i32 tmp = index.minX;
@@ -115,6 +112,12 @@ static BzSpatialGridIndex calculateGridIndex(BzSpatialGrid *grid, f32 posX, f32
index.minY = index.maxY;
index.maxX = tmp;
}
if (index.minX < 0) index.minX = 0;
if (index.minY < 0) index.minY = 0;
if (index.maxX >= grid->width) index.maxX = grid->width - 1;
if (index.maxY >= grid->height) index.maxY = grid->height - 1;
return index;
}