void AKCollisionDetectionEventDrivenLogic2D::setBound(AKBox* bounds, float originX, float originY, float originZ, unsigned int row, unsigned int col, unsigned int range) { _originX = originX; _originY = originY; setSystemBounds(bounds); _cellsCountInCol = col; _cellsCountInRow = row; _cellWidth = (abs(_bounds->center[0]) + _bounds->radius[0]) / _cellsCountInCol; _cellHeight = (abs(_bounds->center[1]) + _bounds->radius[1]) / _cellsCountInRow; _isBoundsAlreadySet = true; // Init cells list unsigned int cellsCount = countOfCell(); _cellList = new AKCellsList(); for (int i = 0; i < cellsCount; i++) { AKCell cell = AKCell((_bounds->is2dDimension) ? 2 : 3); cell.index = i; fillCell(cell, i); fillNeighborsForCell(&cell, i); _cellList->push_back(cell); } fillUnreachableNeighbors(); fillReachableNeighbors(); }
void Grid::fillCell(position2di position, bool occupied, Item* item){ fillCell(position, occupied); fillCell(position, item); }
void Grid::insertOccluder(Polygon3r* occluder) { const vector<Vec3r> vertices = occluder->getVertices(); if (vertices.size() == 0) return; // add this occluder to the grid's occluders list addOccluder(occluder); // find the bbox associated to this polygon Vec3r min, max; occluder->getBBox(min, max); // Retrieve the cell x, y, z cordinates associated with these min and max Vec3u imax, imin; getCellCoordinates(max, imax); getCellCoordinates(min, imin); // We are now going to fill in the cells overlapping with the // polygon bbox. // If the polygon is a triangle (most of cases), we also // check for each of these cells if it is overlapping with // the triangle in order to only fill in the ones really overlapping // the triangle. unsigned i, x, y, z; vector<Vec3r>::const_iterator it; Vec3u coord; if (vertices.size() == 3) { // Triangle case Vec3r triverts[3]; i = 0; for(it = vertices.begin(); it != vertices.end(); it++) { triverts[i] = Vec3r(*it); i++; } Vec3r boxmin, boxmax; for (z = imin[2]; z <= imax[2]; z++) for (y = imin[1]; y <= imax[1]; y++) for (x = imin[0]; x <= imax[0]; x++) { coord[0] = x; coord[1] = y; coord[2] = z; // We retrieve the box coordinates of the current cell getCellBox(coord, boxmin, boxmax); // We check whether the triangle and the box ovewrlap: Vec3r boxcenter((boxmin + boxmax) / 2.0); Vec3r boxhalfsize(_cell_size / 2.0); if (GeomUtils::overlapTriangleBox(boxcenter, boxhalfsize, triverts)) { // We must then create the Cell and add it to the cells list // if it does not exist yet. // We must then add the occluder to the occluders list of this cell. Cell* cell = getCell(coord); if (!cell) { cell = new Cell(boxmin); fillCell(coord, *cell); } cell->addOccluder(occluder); } } } else { // The polygon is not a triangle, we add all the cells overlapping the polygon bbox. for (z = imin[2]; z <= imax[2]; z++) for (y = imin[1]; y <= imax[1]; y++) for (x = imin[0]; x <= imax[0]; x++) { coord[0] = x; coord[1] = y; coord[2] = z; Cell* cell = getCell(coord); if (!cell) { Vec3r orig; getCellOrigin(coord, orig); cell = new Cell(orig); fillCell(coord, *cell); } cell->addOccluder(occluder); } } }