Fix spatial grid not calculating position correctly

This commit is contained in:
2023-11-17 15:06:59 +01:00
parent 90bef40e70
commit 208cdc6b6f
3 changed files with 16 additions and 11 deletions

View File

@@ -54,7 +54,7 @@ void *bzObjectPoolGetObject(BzObjectPool *pool, i32 idx) {
return (void *) ((u8 *) pool->objects + idx * pool->stride);
}
i32 bzObjectPoolGetIdx(BzObjectPool *pool, void *object) {
size_t objectIdx = (size_t) object - (size_t) pool->numObjects;
size_t objectIdx = (size_t) object - (size_t) pool->objects;
return (i32) (objectIdx / pool->stride);
}
void bzObjectPoolRelease(BzObjectPool *pool, void *object) {

View File

@@ -92,13 +92,13 @@ static void removeCell(BzSpatialGrid *grid, BzSpatialGridID id, i32 x, i32 y) {
static BzSpatialGridIndex calculateGridIndex(BzSpatialGrid *grid, f32 posX, f32 posY, f32 sizeX, f32 sizeY) {
i32 minX = (i32) floorf(posX);
i32 minY = (i32) floorf(posY);
i32 maxX = (i32) floorf(posX + sizeX);
i32 maxY = (i32) floorf(posY + sizeY);
i32 maxX = (i32) ceilf(posX + sizeX);
i32 maxY = (i32) ceilf(posY + sizeY);
return (BzSpatialGridIndex) {
(i16) (minX / grid->width),
(i16) (minY / grid->height),
(i16) (maxX / grid->width),
(i16) (maxY / grid->height),
(i16) (minX / grid->cellWidth),
(i16) (minY / grid->cellHeight),
(i16) (maxX / grid->cellWidth),
(i16) (maxY / grid->cellHeight),
};
}
@@ -211,11 +211,16 @@ void bzSpatialGridDrawDebugGrid(const BzSpatialGrid *grid) {
for (i32 y = 0; y < grid->height; y++) {
for (i32 x = 0; x < grid->width; x++) {
BzSpatialGridID **cell = getCell(grid, x, y);
i32 numEntries = 0;
if (*cell) numEntries = bzArraySize(*cell);
i32 numEntries = bzArraySize(*cell);
Color color = WHITE;
i32 tint = numEntries;
if (tint > 10) tint = 10;
tint = 10 - tint;
color.g = 25 * tint;
color.b = 25 * tint;
char buf[8];
snprintf(buf, sizeof(buf), "%d", numEntries);
DrawText(buf, posX + 1, posY + 1, 6, WHITE);
DrawText(buf, posX + 1, posY + 1, 6, color);
posX += grid->cellWidth;
}

View File

@@ -42,7 +42,7 @@ typedef struct BzSpatialGridDesc {
BzSpatialGrid *bzSpatialGridCreate(const BzSpatialGridDesc *desc);
void bzSpatialGridDestroy(BzSpatialGrid *grid);
//void *bzSpatialGridGetData(const BzSpatialGrid *grid, BzSpatialGridID id);
void *bzSpatialGridGetData(const BzSpatialGrid *grid, BzSpatialGridID id);
BzSpatialGridID bzSpatialGridInsert(BzSpatialGrid *grid, void *data, f32 posX, f32 posY, f32 sizeX, f32 sizeY);
BzSpatialGridID bzSpatialGridUpdate(BzSpatialGrid *grid, BzSpatialGridID id, f32 posX, f32 posY, f32 sizeX, f32 sizeY);