コード例 #1
0
        void CacheCollision(CPhysics a, CPhysics b)
        {
            var intersection = IntersectionSystem.CheckIntersection(a.Collider, b.Collider);

            if (intersection.Collided)
            {
                // Calculating some collision data right away, since it will be reused multiple times.
                var manifold = intersection.GenerateManifold();

                var i = _collisionsPool.Take();


                _collisionsPool[i].A            = a;
                _collisionsPool[i].B            = b;
                _collisionsPool[i].Intersection = intersection;
                _collisionsPool[i].Manifold     = manifold;
                _collisionsPool[i].InvMassSum   = a.InverseMass + b.InverseMass;

                _collisionsPool[i].ElasticityDirection = (1 + Math.Min(a.Elasticity, b.Elasticity)) * manifold.Direction
                                                         // Secret sauce that makes platformer stacking work. Should point in the gravity direction.
                                                         + Vector2.Min(a.DirectionalElasticity, b.DirectionalElasticity) * manifold.Direction;

                a.HadCollision = true;
                b.HadCollision = true;
            }
        }
コード例 #2
0
        /// <summary>
        /// Returns a list of all physics bodies which intersect with given
        /// collider and are not the specified owner.
        /// </summary>
        public static List <CPhysics> GetAllCollisions(ICollider collider, CPhysics owner = null)
        {
            var topLeft     = collider.Position - collider.HalfSize;
            var bottomRight = collider.Position + collider.HalfSize;

            var cells  = Grid.GetFilledCellsInRange(topLeft, bottomRight);
            var leaves = new List <QuadTreeNode>();

            for (var i = 0; i < cells.Count; i += 1)
            {
                cells[i].GetLeavesInRange(leaves, topLeft, bottomRight);
            }

            var collisions = new List <CPhysics>();

            for (var i = 0; i < leaves.Count; i += 1)
            {
                for (var k = 0; k < leaves[i].ItemsCount; k += 1)
                {
                    var physics = leaves[i].GetItem(k);

                    if (physics != owner && IntersectionSystem.CheckIntersection(collider, physics.Collider).Collided)
                    {
                        collisions.Add(physics);
                    }
                }

                for (var k = 0; k < leaves[i].ImmovableItemsCount; k += 1)
                {
                    var physics = leaves[i].GetImmovableItem(k);

                    if (physics != owner && IntersectionSystem.CheckIntersection(collider, physics.Collider).Collided)
                    {
                        collisions.Add(physics);
                    }
                }
            }

            return(collisions);
        }
コード例 #3
0
 /// <summary>
 /// Returns a list of all physics bodies which intersect with given physics body.
 /// </summary>
 public static List <CPhysics> GetAllCollisions(CPhysics owner) =>
 GetAllCollisions(owner.Collider, owner);
コード例 #4
0
 /// <summary>
 /// Returns the first physics body which intersects with given physics body.
 /// </summary>
 public static CPhysics GetCollision(CPhysics owner) =>
 GetCollision(owner.Collider, owner);