Ejemplo n.º 1
0
        QuadTree *AllocTree(LRect rect, QuadTree *tree)
        {
            var ptr = QuadTreeFactory.AllocQuadTree();

            *ptr = new QuadTree(rect, tree);
            return(ptr);
        }
Ejemplo n.º 2
0
        private void Split()
        {
            var hx = _bounds.width / 2;
            var hz = _bounds.height / 2;
            var sz = new LVector2(hx, hz);

            //split a
            var aLoc  = _bounds.position;
            var aRect = new LRect(aLoc, sz);
            //split b
            var bLoc  = new LVector2(_bounds.position.x + hx, _bounds.position.y);
            var bRect = new LRect(bLoc, sz);
            //split c
            var cLoc  = new LVector2(_bounds.position.x + hx, _bounds.position.y + hz);
            var cRect = new LRect(cLoc, sz);
            //split d
            var dLoc  = new LVector2(_bounds.position.x, _bounds.position.y + hz);
            var dRect = new LRect(dLoc, sz);

            fixed(QuadTree *thisPtr = &this)
            {
                _childA = AllocTree(aRect, thisPtr);
                _childB = AllocTree(bRect, thisPtr);
                _childC = AllocTree(cRect, thisPtr);
                _childD = AllocTree(dRect, thisPtr);
            }

            //assign QuadTrees
            for (int i = _bodyCount - 1; i >= 0; i--)
            {
                var child = GetQuadrant(_bodies[i]->pos);
                child->AddBody(_bodies[i]);
                _bodies[i] = null;
            }
        }
Ejemplo n.º 3
0
 private void _GetBodies(LRect rect, List <long> bods)
 {
     //no children
     if (_childA == null)
     {
         for (int i = 0; i < _bodyCount; i++)
         {
             bods.Add((long)(_bodies[i]));
         }
     }
     else
     {
         if (_childA->ContainsRect(rect))
         {
             _childA->_GetBodies(rect, bods);
         }
         if (_childB->ContainsRect(rect))
         {
             _childB->_GetBodies(rect, bods);
         }
         if (_childC->ContainsRect(rect))
         {
             _childC->_GetBodies(rect, bods);
         }
         if (_childD->ContainsRect(rect))
         {
             _childD->_GetBodies(rect, bods);
         }
     }
 }
Ejemplo n.º 4
0
 public QuadTree(LRect bounds, int maxBodiesPerNode = 6, int maxLevel = 6)
 {
     _bounds           = bounds;
     _maxBodiesPerNode = maxBodiesPerNode;
     _maxLevel         = maxLevel;
     _childA           = null;
     _childB           = null;
     _childC           = null;
     _childD           = null;
     _curLevel         = 0;
     _parent           = null;
     _bodyCount        = 0;
     _debugId          = curDebugId++;
     _bodies           = QuadTreeFactory.AllocPtrBlock(_maxBodiesPerNode);
 }
Ejemplo n.º 5
0
 public bool ContainsRect(LRect rect)
 {
     return(_bounds.Overlaps(rect));
 }
Ejemplo n.º 6
0
 public List <long> GetBodies(LRect rect)
 {
     _tempPtrList.Clear();
     _GetBodies(rect, _tempPtrList);
     return(_tempPtrList);
 }
Ejemplo n.º 7
0
 private QuadTree(LRect bounds, QuadTree *parent)
     : this(bounds, parent->_maxBodiesPerNode, parent->_maxLevel)
 {
     _parent   = parent;
     _curLevel = parent->_curLevel + 1;
 }