Properly clamp spatial index calculation
This commit is contained in:
@@ -102,21 +102,17 @@ static BzSpatialGridIndex calculateGridIndex(BzSpatialGrid *grid, f32 posX, f32
|
||||
(i16) (maxY / grid->cellHeight),
|
||||
};
|
||||
|
||||
if (index.minX > index.maxX) {
|
||||
i32 tmp = index.minX;
|
||||
index.minX = index.maxX;
|
||||
index.maxX = tmp;
|
||||
}
|
||||
if (index.minY > index.maxY) {
|
||||
i32 tmp = index.minY;
|
||||
index.minY = index.maxY;
|
||||
index.maxX = tmp;
|
||||
}
|
||||
index.minX = BZ_MAX(index.minX, 0);
|
||||
index.maxX = BZ_MAX(index.maxX, 0);
|
||||
index.minY = BZ_MAX(index.minY, 0);
|
||||
index.maxY = BZ_MAX(index.maxY, 0);
|
||||
|
||||
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;
|
||||
const i32 maxWidth = grid->width - 1;
|
||||
const i32 maxHeight = grid->height - 1;
|
||||
index.minX = BZ_MIN(index.minX, maxWidth);
|
||||
index.maxX = BZ_MIN(index.maxX, maxWidth);
|
||||
index.minY = BZ_MIN(index.minY, maxHeight);
|
||||
index.maxY = BZ_MIN(index.maxY, maxHeight);
|
||||
|
||||
return index;
|
||||
}
|
||||
@@ -194,6 +190,7 @@ void bzSpatialGridRemove(BzSpatialGrid *grid, BzSpatialGridID id) {
|
||||
|
||||
BzSpatialGridIter bzSpatialGridIter(BzSpatialGrid *grid, f32 posX, f32 posY, f32 sizeX, f32 sizeY) {
|
||||
BzSpatialGridIndex index = calculateGridIndex(grid, posX, posY, sizeX, sizeY);
|
||||
BZ_ASSERT(index.minX < grid->width && index.minY < grid->height);
|
||||
BzSpatialGridIter it = {
|
||||
.index=index,
|
||||
._grid=grid,
|
||||
|
||||
Reference in New Issue
Block a user