public WorldObject[] QueryBounds(WorldObjectBounds bounds, params string[] ignoreTags) { // Removing ToRectangle() does not give us accurate overlaps. return(QueryCells(bounds).Where(other => bounds.ToRectangle().Intersects(other.Bounds.ToRectangle()) && !ignoreTags.Any(tag => other.Tags.Contains(tag))).ToArray()); }
public Vector2 GetIntersectionDepth(WorldObjectBounds aabb2) { // Calculate half sizes. float halfWidthA = Width / 2f; float halfHeightA = Height / 2f; float halfWidthB = aabb2.Width / 2f; float halfHeightB = aabb2.Height / 2f; // Calculate centers. Vector2 centerA = new Vector2(Left + halfWidthA, Top + halfHeightA); Vector2 centerB = new Vector2(aabb2.Left + halfWidthB, aabb2.Top + halfHeightB); // Calculate current and minimum-non-intersecting distances between centers. float distanceX = centerA.X - centerB.X; float distanceY = centerA.Y - centerB.Y; float minDistanceX = halfWidthA + halfWidthB; float minDistanceY = halfHeightA + halfHeightB; // If we are not intersecting at all, return (0, 0). if (Math.Abs(distanceX) >= minDistanceX || Math.Abs(distanceY) >= minDistanceY) { return(Vector2.Zero); } // Calculate and return intersection depths. float depthX = distanceX > 0 ? minDistanceX - distanceX : -minDistanceX - distanceX; float depthY = distanceY > 0 ? minDistanceY - distanceY : -minDistanceY - distanceY; return(new Vector2(depthX, depthY)); }
private IEnumerable <Point> GetBoundPointCells(WorldObjectBounds bounds) { if (TryGetCellPosition(new Vector2(bounds.X, bounds.Top), out int topLeftCol, out int topLeftRow)) { yield return(new Point(topLeftCol, topLeftRow)); } if (TryGetCellPosition(new Vector2(bounds.Right, bounds.Top), out int topRightCol, out int topRightRow)) { yield return(new Point(topRightCol, topRightRow)); } if (TryGetCellPosition(new Vector2(bounds.X, bounds.Bottom), out int bottomLeftCol, out int bottomLeftRow)) { yield return(new Point(bottomLeftCol, bottomLeftRow)); } if (TryGetCellPosition(new Vector2(bounds.Right, bounds.Bottom), out int bottomRightCol, out int bottomRightRow)) { yield return(new Point(bottomRightCol, bottomRightRow)); } }
private IEnumerable <Point> GetOccupyingCells(WorldObjectBounds bounds) { return(GetBoundPointCells(bounds).Distinct()); }
private IEnumerable <WorldObject> QueryCells(WorldObjectBounds bounds) { return(GetOccupyingCells(bounds).SelectMany(cell => _cells[cell.X, cell.Y].WorldObjects).Distinct()); }
public WorldObject[] QueryBounds(WorldObjectBounds bounds) { // Removing ToRectangle() does not give us accurate overlaps. return(QueryCells(bounds).Where(other => bounds.ToRectangle().Intersects(other.Bounds.ToRectangle())).ToArray()); }
public bool Intersects(WorldObjectBounds aabb2) { return(!(aabb2.Left > Right || aabb2.Right < Left || aabb2.Top > Bottom || aabb2.Bottom < Top)); }