Example #1
0
    /// <summary>
    /// 插入
    /// </summary>
    /// <param name="_tree"></param>
    public void Insert(CollisionObject pBounds)
    {
        int index;

        //如果存在子节点,直接加入
        if (childNodes.Count > 0)
        {
            index = GetIndex(pBounds);

            if (-1 != index)
            {
                childNodes[index].Insert(pBounds);
                return;
            }
        }

        //加入根节点list
        objectsList.Add(pBounds);
        pBounds.SetCurrentTree(this);
        pBounds.SetDepth(this.depth);

        //分离的前提是子物体大于了最大容量,而且深度不为最深
        if (objectsList.Count > MAX_Objects && depth < MAX_Depth)
        {
            if (childNodes.Count == 0)
            {
                Split();
                //必须在操作List时使用倒序遍历,否则会出现索引的问题
                for (int i = objectsList.Count - 1; i >= 0; i--)
                {
                    index = GetIndex(objectsList[i]);
                    if (-1 != index)
                    {
                        CollisionObject co = objectsList[i];
                        objectsList.Remove(objectsList[i]);
                        childNodes[index].Insert(co);
                    }
                }
            }
        }
    }