public bool IsInBorders(Collidable c) { int x = ((int)c.GetPosition().X / ZoneWidth) + 1; int y = ((int)c.GetPosition().Y / ZoneHeight) + 1; if (x >= 0 && x < GRID_WIDTH && y >= 0 && y < GRID_HEIGHT) return true; else return false; }
//returns a list of all of the zones a collidable object intersects public List<Coordinate> GetAllZones(Collidable c) { List<Coordinate> retList = new List<Coordinate>(); Coordinate pos = GetPrimaryZone(c); retList.Add(pos); Vector2 p = c.GetPosition(); int radius = c.GetHitRadius(); bool up, down, left, right; up = down = left = right = false; if (p.X % ZoneWidth <= radius) { retList.Add(new Coordinate(pos.X - 1, pos.Y)); left = true; } else if ((p.X % ZoneWidth) + radius >= ZoneWidth) { retList.Add(new Coordinate(pos.X + 1, pos.Y)); right = true; } //Check overlap on vertical zones if (p.Y % ZoneHeight <= radius) { retList.Add(new Coordinate(pos.X, pos.Y - 1)); up = true; } else if((p.Y % ZoneHeight) + radius >= ZoneHeight) { retList.Add(new Coordinate(pos.X, pos.Y + 1)); down = true; } //Checking corner overlaps if (left) { if (up) retList.Add(new Coordinate(pos.X - 1, pos.Y - 1)); else if (down) retList.Add(new Coordinate(pos.X - 1, pos.Y + 1)); } else if (right) { if (up) retList.Add(new Coordinate(pos.X + 1, pos.Y - 1)); else if (down) retList.Add(new Coordinate(pos.X + 1, pos.Y + 1)); } return retList; }
//given a collidable returns the primary zone it resides in public Coordinate GetPrimaryZone(Collidable c) { int row = ((int)c.GetPosition().X / ZoneWidth) + 1; int column = ((int)c.GetPosition().Y / ZoneHeight) + 1; return new Coordinate(row, column); }
//checks if two Collidables collide public bool DoesCollide(Collidable a, Collidable b) { int radius = a.GetHitRadius() + b.GetHitRadius(); int dX = (int)b.GetPosition().X - (int)a.GetPosition().X; int dY = (int)b.GetPosition().Y - (int)a.GetPosition().Y; if (radius * radius < dX * dX + dY * dY) return false; else return true; }