/** * Query from root quadtree and nodes with in query shape * **/ public List<IQuadtreeAgent> QueryRangeFromRoot(IQuadtreeQuery query){ //If not root then find root and query if (parentNode != null) return rootQuadtree ().QueryRange (query); return QueryRange (query); }
/** * Query elements within or contact with query shape * * You can create your own query with any kind of convex shape * * Return list of elements contact with or within query shape * * Param upwardSearch true query will find the node that query shape complete fit in in first * place then start search downward from that node. It is recommend to leave value as true for * more accurate result. **/ public List<IQuadtreeAgent> QueryRange(IQuadtreeQuery query, bool upwardSearch = true){ if (query == null) { #if DEBUG Debug.LogError("Can't query range, given query is null"); #endif return null; } /** * Upward search from this node * * This will go up until the node wihch query shape complete fit in **/ if (upwardSearch) { //If this is the node query shape complete fit in CollisionResult cResult = query.GetShape().IntersectWithShape(boundary); //we found the node then start query from this node if (cResult == CollisionResult.Fit) { //downward search from parent if there is parent node //the reason from parent node is that parent node's overlap element //might be contact query shape if (parentNode != null) { //from parent node we start downward search return parentNode.QueryRange (query, false); } } else {//continue search upward //If there is a parent node otherwsie this is root node and start from //here downward search if (parentNode != null) { //Continue finding upward until the node query shape can totally fit in return parentNode.QueryRange (query, true); } } } /** * Downward search from this node * * Downward search only search elements in child node include this node **/ List<IQuadtreeAgent> result = new List<IQuadtreeAgent> (); CollisionResult r = query.GetShape ().IntersectWithShape (boundary); //if not contact with query shape which is not in range if (r == CollisionResult.None) return result; //search child nodes if (nodes != null) { IEnumerator er = nodes.GetEnumerator (); while (er.MoveNext ()) { result.AddRange ((er.Current as QuadtreeNode).QueryRange (query, false)); } } //add this node's elements result.AddRange (elements); result.AddRange (overlapElements); //Check if any of element contact query shape List<IQuadtreeAgent> filterResult = new List<IQuadtreeAgent>(); List<IQuadtreeAgent>.Enumerator resultEr = result.GetEnumerator(); while(resultEr.MoveNext()){ if(resultEr.Current.GetShape().IntersectWithShape(query.GetShape()) != CollisionResult.None){ filterResult.Add(resultEr.Current); } } return filterResult; }