/** * Query an AABB for overlapping proxies. The callback class * is called for each proxy that overlaps the supplied AABB. */ public void Query(BroadPhaseQueryCallback callback, b2AABB aabb) { List <float> lowerValues = new List <float>(); List <float> upperValues = new List <float>(); ComputeBounds(lowerValues, upperValues, aabb); uint lowerIndex = 0; uint upperIndex = 0; List <uint> lowerIndexOut = new List <uint>(); lowerIndexOut.Add(lowerIndex); List <uint> upperIndexOut = new List <uint>(); upperIndexOut.Add(upperIndex); QueryAxis(lowerIndexOut, upperIndexOut, (uint)lowerValues[0], (uint)upperValues[0], m_bounds[0], (uint)(2 * m_proxyCount), 0); QueryAxis(lowerIndexOut, upperIndexOut, (uint)lowerValues[1], (uint)upperValues[1], m_bounds[1], (uint)(2 * m_proxyCount), 1); //b2Settings.b2Assert(m_queryResultCount < b2Settings.b2_maxProxies); // TODO: Don't be lazy, transform QueryAxis to directly call callback for (int i = 0; i < m_queryResultCount; ++i) { b2Proxy proxy = (b2Proxy)m_queryResults[i]; //b2Settings.b2Assert(proxy.IsValid()); if (!callback(proxy)) { break; } } // Prepare for next query. m_queryResultCount = 0; IncrementTimeStamp(); }
/// <summary> /// Query an AABB for overlapping proxies. The callback class /// is called for each proxy that overlaps the supplied AABB. /// </summary> /// <param name="callback">The callback.</param> /// <param name="aabb">The aabb.</param> public void Query(BroadPhaseQueryCallback callback, ref AABB aabb) { _queryStack.Clear(); _queryStack.Push(_root); while (_queryStack.Count > 0) { int nodeId = _queryStack.Pop(); if (nodeId == NullNode) { continue; } //TreeNode<T>* node = &_nodes[nodeId]; if (AABB.TestOverlap(ref _nodes[nodeId].AABB, ref aabb)) { if (_nodes[nodeId].IsLeaf()) { bool proceed = callback(nodeId); if (proceed == false) { return; } } else { _queryStack.Push(_nodes[nodeId].Child1); _queryStack.Push(_nodes[nodeId].Child2); } } } }
/// <summary> /// Constructs a new broad phase based on the dynamic tree implementation /// </summary> public DynamicTreeBroadPhase() { _queryCallbackCache = new BroadPhaseQueryCallback(QueryCallback); _proxyCount = 0; _pairCapacity = 16; _pairCount = 0; _pairBuffer = new Pair[_pairCapacity]; _moveCapacity = 16; _moveCount = 0; _moveBuffer = new int[_moveCapacity]; }
/** * Query an AABB for overlapping proxies. The callback * is called for each proxy that overlaps the supplied AABB. * The callback should match function signature * <code>fuction callback(proxy:b2DynamicTreeNode):Boolean</code> * and should return false to trigger premature termination. */ public void Query(BroadPhaseQueryCallback callback, b2AABB aabb) { if (m_root == null) { return; } Dictionary <int, b2DynamicTreeNode> stack = new Dictionary <int, b2DynamicTreeNode>(); int count = 0; stack.Add(count++, m_root); while (count > 0) { b2DynamicTreeNode node = stack[--count]; if (node.aabb.TestOverlap(aabb)) { if (node.IsLeaf()) { bool proceed = callback(node); if (!proceed) { return; } } else { // No stack limit, so no assert int key; key = count++; if (stack.ContainsKey(key)) { stack.Remove(key); } stack.Add(key, node.child1); key = count++; if (stack.ContainsKey(key)) { stack.Remove(key); } stack.Add(key, node.child2); } } } }
/// <summary> /// Query an AABB for overlapping proxies. The callback class /// is called for each proxy that overlaps the supplied AABB. /// </summary> /// <param name="callback">The callback.</param> /// <param name="aabb">The aabb.</param> public void Query(BroadPhaseQueryCallback callback, ref AABB aabb) { _tree.Query(callback, ref aabb); }
private Func <Element <FixtureProxy>, bool> TransformPredicate(BroadPhaseQueryCallback idPredicate) { Func <Element <FixtureProxy>, bool> qtPred = qtnode => idPredicate(qtnode.Value.ProxyId); return(qtPred); }
public void Query(BroadPhaseQueryCallback callback, ref AABB query) { _quadTree.QueryAABB(TransformPredicate(callback), ref query); }
/** * Update the pairs. This results in pair callbacks. This can only add pairs. */ public void UpdatePairs(Action <object, object> callback) { m_pairCount = 0; // Perform tree queries for all moving queries foreach (b2DynamicTreeNode queryProxy in m_moveBuffer) { /*bool QueryCallback(b2DynamicTreeNode proxy) * { * // A proxy cannot form a pair with itself. * if (proxy == queryProxy) * return true; * * // Grow the pair buffer as needed * if (m_pairCount == m_pairBuffer.Count) * { * m_pairBuffer[m_pairCount] = new b2DynamicTreePair(); * } * * b2DynamicTreePair pair = m_pairBuffer[m_pairCount]; * pair.proxyA = proxy < queryProxy?proxy:queryProxy; * pair.proxyB = proxy >= queryProxy?proxy:queryProxy; ++m_pairCount; * * return true; * }*/ BroadPhaseQueryCallback QueryCallback = delegate(object proxy){ // A proxy cannot form a pair with itself. if (proxy == queryProxy) { return(true); } // Grow the pair buffer as needed if (m_pairCount == m_pairBuffer.Count) { m_pairBuffer.Add(new b2DynamicTreePair()); } b2DynamicTreePair pair = m_pairBuffer[m_pairCount]; //pair.proxyA = proxy < queryProxy?proxy:queryProxy; //pair.proxyB = proxy >= queryProxy?proxy:queryProxy; //改 pair.proxyA = queryProxy; pair.proxyB = (b2DynamicTreeNode)proxy; ++m_pairCount; return(true); }; // We have to query the tree with the fat AABB so that // we don't fail to create a pair that may touch later. b2AABB fatAABB = m_tree.GetFatAABB(queryProxy); m_tree.Query(QueryCallback, fatAABB); } // Reset move buffer //m_moveBuffer.length = 0; m_moveBuffer.RemoveRange(0, m_moveBuffer.Count); // Sort the pair buffer to expose duplicates. // TODO: Something more sensible //m_pairBuffer.sort(ComparePairs); // Send the pair buffer for (int i = 0; i < m_pairCount;) { b2DynamicTreePair primaryPair = m_pairBuffer[i]; object userDataA = m_tree.GetUserData(primaryPair.proxyA); object userDataB = m_tree.GetUserData(primaryPair.proxyB); callback(userDataA, userDataB); ++i; // Skip any duplicate pairs while (i < m_pairCount) { b2DynamicTreePair pair = m_pairBuffer[i]; if (pair.proxyA != primaryPair.proxyA || pair.proxyB != primaryPair.proxyB) { break; } ++i; } } }
/** * @inheritDoc */ public void Query(BroadPhaseQueryCallback callback, b2AABB aabb) { m_tree.Query(callback, aabb); }