Beispiel #1
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;
            }
        }
Beispiel #2
0
        QuadTree *AllocTree(LRect rect, QuadTree *tree)
        {
            var ptr = QuadTreeFactory.AllocQuadTree();

            *ptr = new QuadTree(rect, tree);
            return(ptr);
        }
Beispiel #3
0
 public Circle(int typeid, int id, LVector2 pos, LFloat radius)
 {
     this._TypeId = typeid;
     this.Id      = id;
     this.pos     = pos;
     this.radius  = radius;
     ParentNode   = null;
     _debugId     = 0;
 }
Beispiel #4
0
 void FreeTree(ref QuadTree *ptr)
 {
     if (ptr != null)
     {
         ptr->Clear();
         QuadTreeFactory.FreeQuadTree(ptr);
         ptr = null;
     }
 }
 private void OnDestroy()
 {
     Debug.Log("Collision Quit :OnDestroy");
     if (_quadTree != null)
     {
         _quadTree->Clear();
         QuadTreeFactory.FreeQuadTree(_quadTree);
         _quadTree = null;
     }
     NativeFactory.Clear();
     Debug.Log($"RemainMemSize: NativeHelper.MemSize {NativeHelper.MemSize}");
     Debug.Assert(NativeHelper.MemSize == 0, $"NativeHelper.MemSize {NativeHelper.MemSize}");
 }
        private void OnStart()
        {
            random    = new System.Random(0);
            _quadTree = QuadTreeFactory.AllocQuadTree();
            *_quadTree = new QuadTree(new LRect(LFloat.zero, LFloat.zero,
                                                WorldSize.x.ToLFloat(), WorldSize.y.ToLFloat()), BodiesPerNode, MaxSplits);
            _collisionSystem = new CollisionSystemQuadTree(_quadTree);
            var tempLst = new List <PhysicsBody>();

            RandomMove.border = WorldSize;
            for (int i = 0; i < MaxBodies; i++)
            {
                var body = GameObject.Instantiate <PhysicsBody>(DemoPhysicsBody);
                body.transform.position = new Vector3(
                    random.Next(0, (int)(WorldSize.x * 1000)) * 0.001f, 0,
                    random.Next(0, (int)(WorldSize.y * 1000)) * 0.001f);
                if (i % (int)(1 / RandomMovePercent) == 0)
                {
                    body.gameObject.AddComponent <RandomMove>();
                }

                tempLst.Add(body);
            }

            GameObject.Destroy(DemoPhysicsBody.gameObject);
            //raw  35.43ms 38.52ms 39.05ms
            //LMath 40.7ms 38.9ms
            //UnsafeLMath 8.6ms 8.7ms
            Profiler.BeginSample("QuadInit");
            foreach (var body in tempLst)
            {
                var config = body.ColliderConfig;
                foreach (var collider in config.allColliders)
                {
                    var type = (EShape2D)collider.TypeId;
                    switch (type)
                    {
                    case EShape2D.AABB: {
                        AABB2D *boxPtr = CollisionFactory.AllocAABB();
                        var     shape  = ((ShapeWrapAABB)collider).shape;
                        body.RefId  = _collisionSystem.AddBody(body, boxPtr, shape.pos, shape.size);
                        body.ColPtr = (Circle *)boxPtr;
                        _quadTree->AddBody(boxPtr);     // add body to QuadTree
                        break;
                    }
                    }
                }
            }

            Profiler.EndSample();
        }
Beispiel #7
0
 public void Clear()
 {
     CheckDebugInfo("Clear");
     _parent    = null;
     _bodyCount = 0;
     FreeTree(ref _childA);
     FreeTree(ref _childB);
     FreeTree(ref _childC);
     FreeTree(ref _childD);
     if (_bodies != null)
     {
         QuadTreeFactory.FreePtrBlock(_bodies, _maxBodiesPerNode);
         _bodies = null;
     }
 }
Beispiel #8
0
 public static void FreeQuadTree(QuadTree *treePtr)
 {
     _treePool.Return(treePtr);
 }
Beispiel #9
0
        ///// Constructor /////

        public CollisionSystemQuadTree(QuadTree *tree)
        {
            _quadTree = tree;
        }
Beispiel #10
0
 private QuadTree(LRect bounds, QuadTree *parent)
     : this(bounds, parent->_maxBodiesPerNode, parent->_maxLevel)
 {
     _parent   = parent;
     _curLevel = parent->_curLevel + 1;
 }