/// <summary> /// Check if the specified ray intersects with anything in the tree. See also: GetColliding. /// </summary> /// <param name="checkRay">Ray to check.</param> /// <param name="maxDistance">Distance to check.</param> /// <returns>True if there was a collision.</returns> public bool IsColliding(ref Ray checkRay, float maxDistance = float.PositiveInfinity) { // Is the input ray at least partially in this node? float distance; if (!_bounds.IntersectRay(checkRay, out distance) || distance > maxDistance) { return(false); } // Check against any objects in this node for (int i = 0; i < _objects.Count; i++) { if (_objects[i].Bounds.IntersectRay(checkRay, out distance) && distance <= maxDistance) { return(true); } } // Check children if (_children != null) { for (int i = 0; i < 8; i++) { if (_children[i].IsColliding(ref checkRay, maxDistance)) { return(true); } } } return(false); }
/// <summary> /// Return objects that are within <paramref name="maxDistance"/> of the specified ray. /// </summary> /// <param name="ray">The ray.</param> /// <param name="maxDistance">Maximum distance from the ray to consider.</param> /// <param name="result">List result.</param> /// <returns>Objects within range.</returns> public void GetNearby(ref Ray ray, float maxDistance, List <T> result) { // Does the ray hit this node at all? // Note: Expanding the bounds is not exactly the same as a real distance check, but it's fast. // TODO: Does someone have a fast AND accurate formula to do this check? _bounds.Expand(new Point(maxDistance * 2, maxDistance * 2, maxDistance * 2)); bool intersected = _bounds.IntersectRay(ray); _bounds.Size = _actualBoundsSize; if (!intersected) { return; } // Check against any objects in this node for (int i = 0; i < _objects.Count; i++) { if (SqrDistanceToRay(ray, _objects[i].Pos) <= (maxDistance * maxDistance)) { result.Add(_objects[i].Obj); } } // Check children if (_children != null) { for (int i = 0; i < 8; i++) { _children[i].GetNearby(ref ray, maxDistance, result); } } }