Exemplo n.º 1
0
 void Start()
 {
     quadtreeSize = new Rect(-100, -100, 200, 200);
     sphereGO     = GameObject.CreatePrimitive(PrimitiveType.Sphere);
     sphereGO.transform.localScale = new Vector3(4, 4, 1);
     sphereQuadTree = new QuadTreeN2 <SphereObj>(0, quadtreeSize);
 }
Exemplo n.º 2
0
    public void Insert(T insertedObject)                                        //Insert function
    {
        if (cells[0] != null)                                                   //If the first cell has been created exe
        {
            int iCell = GetCellToInsertObject(insertedObject.GetPosition());    //
            if (iCell > -1)                                                     //
            {
                cells[iCell].Insert(insertedObject);                            //
            }
            return;                                                             //Return = get out of this scope & continue
        }

        m_storedObjects.Add(insertedObject);
        if (m_storedObjects.Count > m_maxObjectCount)                           //Check if the amount of stored objects exceed the maximum object count to then start dividing
        {
            //Start Dividing Into 4 Squares
            if (cells[0] == null)
            {
                float subWidth  = (m_bounds.width / 2f);                                                         // div width by 2
                float subHeight = (m_bounds.height / 2f);                                                        // div height by 2
                float x         = m_bounds.x;                                                                    // get x pos
                float y         = m_bounds.y;                                                                    // get y pos
                cells[0] = new QuadTreeN2 <T>(m_maxObjectCount, new Rect(x + subWidth, y, subWidth, subHeight)); //Rect = (x,y,width,height)
                cells[1] = new QuadTreeN2 <T>(m_maxObjectCount, new Rect(x, y, subWidth, subHeight));            // x = x pos / y = y pos / width & height = scale of rect
                cells[2] = new QuadTreeN2 <T>(m_maxObjectCount, new Rect(x, y + subHeight, subWidth, subHeight));
                cells[3] = new QuadTreeN2 <T>(m_maxObjectCount, new Rect(x + subWidth, y + subHeight, subWidth, subHeight));
            }
        }
    }