/// <summary> /// Works. /// </summary> /// <param name="point"></param> /// <returns>The node with the highest depth that contains the point or null if the point is outside the QuadTree.</returns> public QuadTreeNode <T> GetSmallestNode(ref Vector2 point) { if (AABB.Contains(point)) // Check if this node contains the point at all { if (!IsLeaf) // If it is divided then check if there is a sub { QuadTreeNode <T> result = null; result = TopLeft.GetSmallestNode(ref point); if (result == null) { result = TopRight.GetSmallestNode(ref point); if (result == null) { result = BottomRight.GetSmallestNode(ref point); if (result == null) { result = BottomLeft.GetSmallestNode(ref point); if (result == null) { throw new Exception("Is not possible. If the node has the point then at least one of the 4 childnodes MUST have the point too."); } } } } return(result); } else { return(this); } } else { return(null); } }