예제 #1
0
 private void _CheckRegionCircle(ColliderProxy obj)
 {
     Debug.Trace($"ColliderProxy _CheckRegionCircle {obj.Id} trans{obj.Transform2D} col{obj.Prefab}");
     if (CollisionHelper.CheckCollision(obj.Prefab, obj.Transform2D, _tempPos, _tempRadius))
     {
         tempCallback(obj);
     }
 }
예제 #2
0
 private void _CheckRegionOBB(ColliderProxy obj)
 {
     Debug.Trace($"ColliderProxy _CheckRegionOBB {obj.Id} trans{obj.Transform2D} col{obj.Prefab}");
     if (CollisionHelper.CheckCollision(obj.Prefab, obj.Transform2D, _tempPos, _tempSize, _tempForward))
     {
         tempCallback(obj);
     }
 }
예제 #3
0
 public static bool CheckCollision(ColliderPrefab col1, CTransform2D trans1, ColliderPrefab col2,
                                   CTransform2D trans2)
 {
     foreach (var part1 in col1.parts)
     {
         foreach (var part2 in col2.parts)
         {
             if (CollisionHelper.CheckCollision(part1.collider, trans1 + part1.transform, part2.collider, trans2 + part2.transform))
             {
                 return(true);
             }
         }
     }
     return(false);
 }
예제 #4
0
        public void OnQuadTreeCollision(ColliderProxy a, ColliderProxy b)
        {
            var pairId = (((long)a.Id) << 32) + b.Id;

            if (_curPairs.Contains(pairId))
            {
                return;
            }
            bool isCollided = CollisionHelper.CheckCollision
                                  (a.Prefab, a.Transform2D, b.Prefab, b.Transform2D);

            if (isCollided)
            {
                _curPairs.Add(pairId);
                var type = _prePairs.Contains(pairId) ? ECollisionEvent.Stay : ECollisionEvent.Enter;
                NotifyCollisionEvent(a, b, type);
            }
        }
예제 #5
0
        //public List<>
        public void DoUpdate(LFloat deltaTime)
        {
            tempLst.Clear();
            //deal layer
            foreach (var pair in BoundsQuadTreeNode.obj2Node)
            {
                var val = pair.Key;
                if (!val.IsStatic)
                {
                    val.DoUpdate(deltaTime);
                    if (val.IsMoved)
                    {
                        val.IsMoved = false;
                        tempLst.Add(val);
                    }
                }
            }

            //Check Collision with static colliders
            var staticTree = GetStaticBoundTree();

            __DebugColProxy1 = null;
            __DebugColProxy2 = null;
            foreach (var proxy in tempLst)
            {
                var lastPos = _id2LastMovePos[proxy.Id];
                var curPos  = proxy.Pos3;
                if (lastPos == curPos)
                {
                    continue;
                }
                staticTree.CheckCollision(proxy, (ColliderProxy obj) => {
                    if (__DebugColProxy1 != null)
                    {
                        return;
                    }
                    if (obj.Id == __DegbuLastCollisionId)
                    {
                    }

                    bool isCollided = CollisionHelper.CheckCollision
                                          (proxy.Prefab, proxy.Transform2D, obj.Prefab, obj.Transform2D);
                    if (isCollided)
                    {
                        __DebugColProxy1       = proxy;
                        __DebugColProxy2       = obj;
                        __DegbuLastCollisionId = obj.Id;
                        //Debug.LogError("碰撞到了静态物体 不让其移动");
                        proxy.Transform2D.Pos3 = lastPos;
                    }
                });
                if (proxy.Transform2D.Pos3 != lastPos)
                {
                    _id2LastMovePos[proxy.Id] = curPos;
                }
            }

            //swap
            var temp = _prePairs;

            _prePairs = _curPairs;
            _curPairs = temp;
            _curPairs.Clear();
            ////class version 1.41ms
            Profiler.BeginSample("UpdateObj");
            foreach (var val in tempLst)
            {
                val.IsMoved = false;
                var bound      = val.GetBounds();
                var boundsTree = GetBoundTree(val.LayerType);
                boundsTree.UpdateObj(val, bound);
            }

            Profiler.EndSample();
            ////0.32~0.42ms
            Profiler.BeginSample("CheckCollision");
            foreach (var val in tempLst)
            {
                val.IsMoved = false;
                var bound = val.GetBounds();
                for (int i = 0; i < LayerCount; i++)
                {
                    if (InterestingMasks[val.LayerType * LayerCount + i])
                    {
                        var boundsTree = GetBoundTree(i);
                        boundsTree.CheckCollision(val, bound);
                    }
                }
            }

            Profiler.EndSample();
            Profiler.BeginSample("CheckLastFrameCollison");
            foreach (var pairId in _curPairs)
            {
                _prePairs.Remove(pairId);
            }

            //check stay leave event
            foreach (var idPair in _prePairs)
            {
                var a = GetCollider((int)(idPair >> 32));
                var b = GetCollider((int)(idPair & 0xffffffff));
                if (a == null || b == null)
                {
                    continue;
                }

                bool isCollided = CollisionHelper.CheckCollision
                                      (a.Prefab, a.Transform2D, b.Prefab, b.Transform2D);
                if (isCollided)
                {
                    _curPairs.Add(idPair);
                    NotifyCollisionEvent(a, b, ECollisionEvent.Stay);
                }
                else
                {
                    NotifyCollisionEvent(a, b, ECollisionEvent.Exit);
                }
            }

            Profiler.EndSample();
        }
예제 #6
0
        //public List<>
        public void DoUpdate()
        {
            tempLst.Clear();
            //deal layer
            foreach (var pair in BoundsQuadTreeNode.obj2Node)
            {
                var val = pair.Key;
                if (!val.IsStatic && val._isMoved)
                {
                    val._isMoved = false;
                    tempLst.Add(val);
                }
            }

            //swap
            var temp = _prePairs;

            _prePairs = _curPairs;
            _curPairs = temp;
            _curPairs.Clear();
            ////class version 1.41ms
            Profiler.BeginSample("UpdateObj");
            foreach (var val in tempLst)
            {
                val._isMoved = false;
                var bound      = val.GetBounds();
                var boundsTree = GetBoundTree(val.LayerType);
                boundsTree.UpdateObj(val, bound);
            }

            Profiler.EndSample();
            ////0.32~0.42ms
            Profiler.BeginSample("CheckCollision");
            foreach (var val in tempLst)
            {
                val._isMoved = false;
                var bound        = val.GetBounds();
                var targetLayers = InterestingMasks[val.LayerType];
                foreach (var layerType in targetLayers)
                {
                    var boundsTree = GetBoundTree(layerType);
                    boundsTree.CheckCollision(val, bound);
                }
            }

            Profiler.EndSample();
            Profiler.BeginSample("CheckLastFrameCollison");
            foreach (var pairId in _curPairs)
            {
                _prePairs.Remove(pairId);
            }

            //check stay leave event
            foreach (var idPair in _prePairs)
            {
                var a = GetCollider((int)(idPair >> 32));
                var b = GetCollider((int)(idPair & 0xffffffff));
                if (a == null || b == null)
                {
                    continue;
                }

                bool isCollided = CollisionHelper.CheckCollision
                                      (a.Prefab, a.Transform2D, b.Prefab, b.Transform2D);
                if (isCollided)
                {
                    _curPairs.Add(idPair);
                    NotifyCollisionEvent(a, b, ECollisionEvent.Stay);
                }
                else
                {
                    NotifyCollisionEvent(a, b, ECollisionEvent.Exit);
                }
            }

            Profiler.EndSample();
        }