private void AddCollidableObject(ICutlassCollidable collidableObject) { //Want to extend collisions slightly outside visible screen. BoundingRectangle collisionSpace = BoundingRectangle.Scale(_ViewScreen.VisibleArea, 1.1f, _ViewScreen.VisibleArea.Center); BoundingRectangle intersection = BoundingRectangle.Intersection(collidableObject.CurrentFrameBoundingRect, collisionSpace); int minHorizontalGroup = -1, maxHorizontalGroup = -1, minVerticalGroup = -1, maxVerticalGroup = -1; if (!intersection.IsZero) { float groupWidth = collisionSpace.Width / _HorizontalGroups; float groupHeight = collisionSpace.Height / _VerticalGroups; minHorizontalGroup = Math.Max(0, (int)((intersection.Left - collisionSpace.Left) / groupWidth)); maxHorizontalGroup = Math.Min(_HorizontalGroups - 1, (int)((intersection.Right - collisionSpace.Left) / groupWidth)); minVerticalGroup = Math.Max(0, (int)((intersection.Top - collisionSpace.Top) / groupHeight)); maxVerticalGroup = Math.Min(_VerticalGroups - 1, (int)((intersection.Bottom - collisionSpace.Top) / groupHeight)); for (int i = minHorizontalGroup; i <= maxHorizontalGroup; i++) { for (int j = minVerticalGroup; j <= maxVerticalGroup; j++) { if (_CurrentCollidableObjects[i, j] == null) { _CurrentCollidableObjects[i, j] = new List <ICutlassCollidable>(); } _CurrentCollidableObjects[i, j].Add(collidableObject); } } } }
public void AssertThat_Intersection_ReturnsNothing_WhenNotOverlappingWithGapTopLeft() { var a = new BoundingRectangle(new Vector2(0, 0), new Vector2(10, 10)); var b = new BoundingRectangle(new Vector2(-5, 15), new Vector2(-25, 25)); // ReSharper disable once PossibleInvalidOperationException Assert.IsFalse(a.Intersection(b).HasValue); }
public void AssertThat_Intersection_ReturnsIntersectingArea() { var a = new BoundingRectangle(new Vector2(0, 0), new Vector2(10, 10)); var b = new BoundingRectangle(new Vector2(5, 5), new Vector2(15, 15)); // ReSharper disable once PossibleInvalidOperationException var m = a.Intersection(b).Value; Assert.AreEqual(new Vector2(5, 5), m.Min); Assert.AreEqual(new Vector2(10, 10), m.Max); }
public void Intersection(BoundingRectangle boundingRectangle1, BoundingRectangle boundingRectangle2, BoundingRectangle?expectedBoundingRectangle) { Assert.AreEqual(expectedBoundingRectangle, boundingRectangle1.Intersection(boundingRectangle2)); Assert.AreEqual(expectedBoundingRectangle, BoundingRectangle.Intersection(boundingRectangle1, boundingRectangle2)); }