diff --git a/engine/breeze/util/spatial_grid.c b/engine/breeze/util/spatial_grid.c index 2a8230e..ad902a9 100644 --- a/engine/breeze/util/spatial_grid.c +++ b/engine/breeze/util/spatial_grid.c @@ -193,27 +193,36 @@ BzSpatialGridIter bzSpatialGridIter(BzSpatialGrid *grid, f32 posX, f32 posY, f32 } bool bzSpatialGridQueryNext(BzSpatialGridIter *it) { BzSpatialGridID **cell = getCell(it->_grid, it->_x, it->_y); - while (it->_cellIdx >= bzArraySize(*cell)) { - it->_cellIdx = 0; - it->_x++; - if (it->_x > it->index.maxX) { - it->_x = it->index.minX; - it->_y++; - if (it->_y > it->index.maxY) { - return false; - } - } - cell = getCell(it->_grid, it->_x, it->_y); - } - BzSpatialGridID id = bzArrayGet(*cell, it->_cellIdx); - BzSpatialGridEntry *entry = bzObjectPoolGetObject(it->_grid->entriesPool, id); - BZ_ASSERT(entry && id == entry->id); + BzSpatialGridID id; + BzSpatialGridEntry *entry; + bool foundUnprocessed = false; + while (!foundUnprocessed) { + while (it->_cellIdx >= bzArraySize(*cell)) { + it->_cellIdx = 0; + it->_x++; + if (it->_x > it->index.maxX) { + it->_x = it->index.minX; + it->_y++; + if (it->_y > it->index.maxY) { + return false; + } + } + cell = getCell(it->_grid, it->_x, it->_y); + } + id = bzArrayGet(*cell, it->_cellIdx); + entry = bzObjectPoolGetObject(it->_grid->entriesPool, id); + BZ_ASSERT(entry && id == entry->id); + if (entry->queryIdx != it->_queryIdx) { + entry->queryIdx = it->_queryIdx; + foundUnprocessed = true; + } + it->_cellIdx++; + } it->entry = *entry; it->data = (void *) (entry + 1); - it->_cellIdx++; return true; }