public void Insert(IBox2DCollider objectBounds) { //var cX = objectBounds.MinX; //var cY = objectBounds.MinY; var cX = objectBounds.MinX + 0.5f * (objectBounds.MaxX - objectBounds.MinX); var cY = objectBounds.MinY + 0.5f * (objectBounds.MaxY - objectBounds.MinY); // Convert to integer grid coordinates. int minX = Math.Max((int)Math.Floor((cX - Bounds.MinX) / CellSize.X), 0); int minY = Math.Max((int)Math.Floor((cY - Bounds.MinY) / CellSize.Y), 0); cells[minX, minY].Add(objectBounds); }
public void Insert(IBox2DCollider objectBounds) { // Convert the object's AABB to integer grid coordinates. // Objects outside of the grid are clamped to the edge. int minX = Math.Max((int)Math.Floor((objectBounds.MinX - Bounds.X) / CellSize.X), 0); int maxX = Math.Min((int)Math.Floor((objectBounds.MaxX - Bounds.X) / CellSize.X), CellCountX - 1); int minY = Math.Max((int)Math.Floor((objectBounds.MinY - Bounds.Y) / CellSize.Y), 0); int maxY = Math.Min((int)Math.Floor((objectBounds.MaxY - Bounds.Y) / CellSize.Y), CellCountY - 1); // Loop over the cells the object overlaps and insert the object. for (int y = minY; y <= maxY; ++y) { for (int x = minX; x <= maxX; ++x) { cells[x, y].Add(objectBounds); } } }
public static bool IntersectsX(this IBox2DCollider a, IBox2DCollider b) { bool noXintersect = (a.MaxX <= b.MinX) || (a.MinX >= b.MaxX); return(!noXintersect); }
public static bool IntersectsY(this IBox2DCollider a, IBox2DCollider b) { bool noYintersect = (a.MaxY <= b.MinY) || (a.MinY >= b.MaxY); return(!noYintersect); }