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); } } } }