public T[] CheckCollision(QuadtreeLeaf <T> leaf)
    {
        List <T> objs = new List <T>(CheckCollision(leaf.position, leaf.radius));

        objs.Remove(leaf.obj);
        return(objs.ToArray());
    }
 private bool SetLeafToSelf(QuadtreeLeaf <T> leaf)
 {
     _leafs.Add(leaf);
     UpdateMaxRadiusWhenSetLeaf(leaf);
     //Debug.Log("<color=#0040A0>位置在" + _rect.position + "宽高是" + _rect.size + "的树梢节点存入位置在" + leaf.position + "半径是" + leaf.radius + "的叶子,存入后的最大半径是" + _maxRadius + "</color>");
     CheckAndDoSplit();
     return(true);
 }
    private bool RemoveLeafSelf(QuadtreeLeaf <T> leaf)
    {
        bool removeLeafBool = _leafs.Remove(leaf);

        UpdateMaxRadiusWhenRemoveLeaf();
        //Debug.Log("<color=#802030>位置在" + _rect.position + "宽高是" + _rect.size + "的树梢节点移除位置在" + leaf.position + "半径是" + leaf.radius + "的叶子,移除后的最大半径是" + _maxRadius + "</color>");
        return(removeLeafBool);
    }
    void UpdateMaxRadiusWhenSetLeaf(QuadtreeLeaf <T> leaf)
    {
        if (leaf.radius > _maxRadius)       //只有存入的叶子的半径超过了现在节点的最大半径才需要更新最大半径,存入更小的叶子并不会影响到检测。
        {
            _maxRadius = leaf.radius;

            CallParentUpdateMaxRadius();
        }
    }
 public bool SetLeaf(QuadtreeLeaf <T> leaf)
 {
     if (DontHaveChildren())
     {
         return(SetLeafToSelf(leaf));
     }
     else
     {
         return(SetLeafToChildren(leaf));
     }
 }
 public bool RemoveLeaf(QuadtreeLeaf <T> leaf)
 {
     if (DontHaveChildren())
     {
         return(RemoveLeafSelf(leaf));
     }
     else
     {
         return(CallChildrenRemoveLeaf(leaf));
     }
 }
    private bool CallChildrenRemoveLeaf(QuadtreeLeaf <T> leaf)
    {
        //Debug.Log("<color=#802030>位置在" + _rect.position + "宽高是" + _rect.size + "的树枝节点从子节点移除位置在" + leaf.position + "半径是" + leaf.radius + "的叶子</color>");
        if (_upperRightChild._rect.PointToRectDistance(leaf.position) == 0)
        {
            return(_upperRightChild.RemoveLeaf(leaf));
        }
        if (_lowerRightChild._rect.PointToRectDistance(leaf.position) == 0)
        {
            return(_lowerRightChild.RemoveLeaf(leaf));
        }
        if (_lowerLeftChild._rect.PointToRectDistance(leaf.position) == 0)
        {
            return(_lowerLeftChild.RemoveLeaf(leaf));
        }
        if (_upperLeftChild._rect.PointToRectDistance(leaf.position) == 0)
        {
            return(_upperLeftChild.RemoveLeaf(leaf));
        }

        Debug.LogError("位置在" + _rect.position + "宽高是" + _rect.size + "的节点,移除叶子失败,叶子不在任何一个子节点的区域里");
        return(false);
    }
    bool SetLeafToChildren(QuadtreeLeaf <T> leaf)
    {
        //Debug.Log("<color=#0040A0>位置在" + _rect.position + "宽高是" + _rect.size + "的树枝节点向子节点存入位置在" + leaf.position + "半径是" + leaf.radius + "的叶子</color>");
        if (_upperRightChild._rect.PointToRectDistance(leaf.position) == 0)
        {
            return(_upperRightChild.SetLeaf(leaf));
        }
        if (_lowerRightChild._rect.PointToRectDistance(leaf.position) == 0)
        {
            return(_lowerRightChild.SetLeaf(leaf));
        }
        if (_lowerLeftChild._rect.PointToRectDistance(leaf.position) == 0)
        {
            return(_lowerLeftChild.SetLeaf(leaf));
        }
        if (_upperLeftChild._rect.PointToRectDistance(leaf.position) == 0)
        {
            return(_upperLeftChild.SetLeaf(leaf));
        }

        Debug.LogError("向位置在" + _rect.position + "宽高是" + _rect.size + "的节点存入位置在" + leaf.position + "叶子时发生错误:叶子不在所有子节点的范围里。");   //Debug.LogError:在Console面板输出Error,就是红色那种消息
        return(false);
    }
Esempio n. 9
0
        /// <summary>
        /// Recursively draws the quadtree terrain nodes that are in the viewing frustum.
        /// </summary>
        /// <param name="node">The node or leaf to draw.</param>
        protected void DrawQuadtree(QuadtreeNode node)
        {
            if (!(new BoundingFrustum(Matrix.Identity * view * projection)).Intersects(node.boundingBox))
            {
                return;
            }

            if (node.GetType() == typeof(QuadtreeLeaf))
            {
                QuadtreeLeaf leaf = (QuadtreeLeaf)node;

                GraphicsDevice.SetVertexBuffer(leaf.vertexBuffer);
                GraphicsDevice.Indices = leaf.indexBuffer;

                GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleStrip, 0, 0, leaf.indexBuffer.IndexCount - 4);
            }
            else
            {
                DrawQuadtree(node.topLeft);
                DrawQuadtree(node.topRight);
                DrawQuadtree(node.bottomLeft);
                DrawQuadtree(node.bottomRight);
            }
        }
 public static bool RemoveLeaf(QuadtreeLeaf <GameObject> leaf)
 {
     return(_quadtree.RemoveLeaf(leaf));
 }
 public static GameObject[] CheckCollision(QuadtreeLeaf <GameObject> leaf)
 {
     return(_quadtree.CheckCollision(leaf));
 }
 public static bool SetLeaf(QuadtreeLeaf <GameObject> leaf)
 {
     return(_quadtree.SetLeaf(leaf));
 }
 void ResetLeaf(QuadtreeLeaf <T> leaf)
 {
     //Debug.Log("<color=#800080>位置在" + _rect.position + "宽高是" + _rect.size + "的树梢节点移除位置在" + leaf.position + "半径是" + leaf.radius + "的叶子,重新存入树</color>");
     RemoveLeafSelf(leaf);
     _root.SetLeaf(leaf);
 }
Esempio n. 14
0
 private void Awake()
 {
     _transform = transform;
     _leaf      = new QuadtreeLeaf <GameObject>(gameObject, GetLeafPosition(), _radius);
 }