Remove entity map in favor of spatial grid
This commit is contained in:
226
engine/breeze/utils/spatial_grid.c
Normal file
226
engine/breeze/utils/spatial_grid.c
Normal file
@@ -0,0 +1,226 @@
|
||||
#include "spatial_grid.h"
|
||||
|
||||
#include "../core/memory.h"
|
||||
#include "array.h"
|
||||
#include "object_pool.h"
|
||||
|
||||
#include <math.h>
|
||||
|
||||
typedef struct BzSpatialGrid {
|
||||
BzSpatialGridID **cells;
|
||||
BzObjectPool *entriesPool;
|
||||
i32 width;
|
||||
i32 height;
|
||||
i32 cellWidth;
|
||||
i32 cellHeight;
|
||||
i32 userDataSize;
|
||||
i32 queryCount;
|
||||
} BzSpatialGrid;
|
||||
|
||||
static BzSpatialGridID **getCell(const BzSpatialGrid *grid, i32 x, i32 y);
|
||||
|
||||
BzSpatialGrid *bzSpatialGridCreate(const BzSpatialGridDesc *desc) {
|
||||
i32 width = (desc->maxWidth + desc->cellWidth - 1) / desc->cellWidth;
|
||||
i32 height = (desc->maxHeight + desc->cellHeight - 1) / desc->cellHeight;
|
||||
|
||||
size_t cellsNumBytes = width * height * sizeof(BzSpatialGridID *);
|
||||
size_t numBytes = sizeof(BzSpatialGrid) + cellsNumBytes;
|
||||
BzSpatialGrid *grid = bzAlloc(numBytes);
|
||||
|
||||
grid->width = width;
|
||||
grid->height = height;
|
||||
grid->cellWidth = desc->cellWidth;
|
||||
grid->cellHeight = desc->cellHeight;
|
||||
grid->userDataSize = desc->userDataSize;
|
||||
grid->queryCount = 0;
|
||||
grid->cells = (BzSpatialGridID **) (grid + 1);
|
||||
for (i32 y = 0; y < grid->height; y++) {
|
||||
for (i32 x = 0; x < grid->width; x++) {
|
||||
BzSpatialGridID **cell = getCell(grid, x, y);
|
||||
*cell = bzArrayCreate(BzSpatialGridID, 4);
|
||||
}
|
||||
}
|
||||
grid->entriesPool = bzObjectPoolCreate(&(BzObjectPoolDesc) {
|
||||
.objectSize=sizeof(BzSpatialGridEntry) + desc->userDataSize,
|
||||
.numObjects=1024,
|
||||
});
|
||||
return grid;
|
||||
}
|
||||
|
||||
void bzSpatialGridDestroy(BzSpatialGrid *grid) {
|
||||
bzObjectPoolDestroy(grid->entriesPool);
|
||||
grid->entriesPool = NULL;
|
||||
for (i32 y = 0; y < grid->height; y++) {
|
||||
for (i32 x = 0; x < grid->width; x++) {
|
||||
BzSpatialGridID **cell = getCell(grid, x, y);
|
||||
bzArrayDestroy(*cell);
|
||||
*cell = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
grid->cells = NULL;
|
||||
bzFree(grid);
|
||||
}
|
||||
|
||||
void *bzSpatialGridGetData(const BzSpatialGrid *grid, BzSpatialGridID id) {
|
||||
return (u8 *)bzObjectPoolGetObject(grid->entriesPool, id) + sizeof(BzSpatialGridEntry);
|
||||
}
|
||||
|
||||
|
||||
static BzSpatialGridID **getCell(const BzSpatialGrid *grid, i32 x, i32 y) {
|
||||
return grid->cells + y * grid->width + x;
|
||||
}
|
||||
|
||||
static void insertCell(BzSpatialGrid *grid, BzSpatialGridID id, i32 x, i32 y) {
|
||||
BzSpatialGridID **cell = getCell(grid, x, y);
|
||||
bzArrayPush(*cell, id);
|
||||
}
|
||||
static void removeCell(BzSpatialGrid *grid, BzSpatialGridID id, i32 x, i32 y) {
|
||||
BzSpatialGridID **cell = getCell(grid, x, y);
|
||||
i32 i;
|
||||
for (i = 0; i < bzArraySize(*cell); i++) {
|
||||
if (i == (*cell)[i]) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (i < bzArraySize(*cell)) {
|
||||
BzSpatialGridID lastID = bzArrayPop(*cell);
|
||||
(*cell)[id] = lastID;
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
return (BzSpatialGridIndex) {
|
||||
(i16) (minX / grid->width),
|
||||
(i16) (minY / grid->height),
|
||||
(i16) (maxX / grid->width),
|
||||
(i16) (maxY / grid->height),
|
||||
};
|
||||
}
|
||||
|
||||
BzSpatialGridID bzSpatialGridInsert(BzSpatialGrid *grid, void *data, f32 posX, f32 posY, f32 sizeX, f32 sizeY) {
|
||||
BzSpatialGridEntry *entry = bzObjectPool(grid->entriesPool);
|
||||
BZ_ASSERT(entry);
|
||||
|
||||
BzSpatialGridIndex index = calculateGridIndex(grid, posX, posY, sizeX, sizeY);
|
||||
|
||||
BzSpatialGridID id = bzObjectPoolGetIdx(grid->entriesPool, entry);
|
||||
|
||||
for (i32 y = index.minY; y <= index.maxY; y++) {
|
||||
for (i32 x = index.minX; x <= index.maxX; x++) {
|
||||
insertCell(grid, id, x, y);
|
||||
}
|
||||
}
|
||||
|
||||
entry->id = id;
|
||||
entry->queryIdx = 0;
|
||||
entry->index = index;
|
||||
if (grid->userDataSize) {
|
||||
bzMemCpy(entry + 1, data, grid->userDataSize);
|
||||
}
|
||||
|
||||
return id;
|
||||
}
|
||||
BzSpatialGridID bzSpatialGridUpdate(BzSpatialGrid *grid, BzSpatialGridID id, f32 posX, f32 posY, f32 sizeX, f32 sizeY) {
|
||||
BzSpatialGridEntry *entry = bzObjectPoolGetObject(grid->entriesPool, id);
|
||||
BZ_ASSERT(entry && entry->id == id);
|
||||
|
||||
BzSpatialGridIndex index = calculateGridIndex(grid, posX, posY, sizeX, sizeY);
|
||||
if (entry->index.minX == index.minX &&
|
||||
entry->index.minY == index.minY &&
|
||||
entry->index.maxX == index.maxX &&
|
||||
entry->index.maxY == index.maxY) {
|
||||
return id;
|
||||
}
|
||||
|
||||
return id;
|
||||
}
|
||||
void bzSpatialGridRemove(BzSpatialGrid *grid, BzSpatialGridID id) {
|
||||
BzSpatialGridEntry *entry = bzObjectPoolGetObject(grid->entriesPool, id);
|
||||
BZ_ASSERT(entry && entry->id == id);
|
||||
|
||||
BzSpatialGridIndex index = entry->index;
|
||||
for (i32 y = index.minY; y <= index.maxY; y++) {
|
||||
for (i32 x = index.minX; x <= index.maxX; x++) {
|
||||
removeCell(grid, id, x, y);
|
||||
}
|
||||
}
|
||||
bzObjectPoolRelease(grid->entriesPool, entry);
|
||||
}
|
||||
|
||||
BzSpatialGridIter bzSpatialGridIter(BzSpatialGrid *grid, f32 posX, f32 posY, f32 sizeX, f32 sizeY) {
|
||||
BzSpatialGridIndex index = calculateGridIndex(grid, posX, posY, sizeX, sizeY);
|
||||
BzSpatialGridIter it = {
|
||||
.index=index,
|
||||
._grid=grid,
|
||||
._queryIdx=grid->queryCount++,
|
||||
._x=index.minX,
|
||||
._y=index.minY,
|
||||
._cellIdx=0
|
||||
|
||||
};
|
||||
return it;
|
||||
}
|
||||
bool bzSpatialGridQueryNext(BzSpatialGridIter *it) {
|
||||
BzSpatialGridID **cell = getCell(it->_grid, it->_x, it->_y);
|
||||
if (it->_cellIdx >= bzArraySize(*cell)) {
|
||||
it->_cellIdx = 0;
|
||||
it->_x++;
|
||||
if (it->_x > it->index.maxX) {
|
||||
if (it->_y > it->index.maxX) {
|
||||
it->_y++;
|
||||
if (it->_y > it->index.maxY)
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
BzSpatialGridID id = bzArrayGet(*cell, it->_cellIdx);
|
||||
BzSpatialGridEntry *entry = bzObjectPoolGetObject(it->_grid->entriesPool, id);
|
||||
BZ_ASSERT(entry && id == entry->id);
|
||||
|
||||
it->entry = *entry;
|
||||
it->data = (void *) (entry + 1);
|
||||
|
||||
it->_cellIdx++;
|
||||
return true;
|
||||
}
|
||||
|
||||
#include <raylib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
void bzSpatialGridDrawDebugGrid(const BzSpatialGrid *grid) {
|
||||
// Draw lines
|
||||
i32 posX = 0;
|
||||
i32 posY = 0;
|
||||
for (i32 x = 0; x <= grid->width; x++) {
|
||||
DrawLine(posX, 0, posX, grid->height * grid->cellHeight, BLACK);
|
||||
posX += grid->cellWidth;
|
||||
}
|
||||
for (i32 y = 0; y <= grid->height; y++) {
|
||||
DrawLine(0, posY, grid->width * grid->cellWidth, posY, BLACK);
|
||||
posY += grid->cellHeight;
|
||||
}
|
||||
|
||||
posX = 0;
|
||||
posY = 0;
|
||||
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);
|
||||
char buf[8];
|
||||
snprintf(buf, sizeof(buf), "%d", numEntries);
|
||||
DrawText(buf, posX + 1, posY + 1, 6, WHITE);
|
||||
|
||||
posX += grid->cellWidth;
|
||||
}
|
||||
posX = 0;
|
||||
posY += grid->cellHeight;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user