Ejemplo n.º 1
0
        protected List <FBCollision <T> > GetAllPossibleCollisions(List <T> bodies, FBSpatialHash <T> bodiesHashed)
        {
            List <FBCollision <T> > collisions = new List <FBCollision <T> >();

            var allPotentialCollisionPairs = GetPotentialCollisionPairs(bodies, bodiesHashed);

            foreach (var pair in allPotentialCollisionPairs)
            {
                if (pair.BodyA.Collider.WillCollideWith(pair.BodyA.MovementThisFrame, pair.BodyB.Collider, pair.BodyB.MovementThisFrame, out var bodyAMovementInfo, out var bodyBMovementInfo, true))
                {
                    var collision = new FBCollision <T>();
                    collision.BodyA     = pair.BodyA;
                    collision.BodyB     = pair.BodyB;
                    collision.AMovement = bodyAMovementInfo;
                    collision.BMovement = bodyBMovementInfo;

                    collisions.Add(collision);
                }
            }

            return(collisions);
        }
        public static bool CollidesWith(this FBCollider a, FBCollider b, Action <FBCollision> onCollision)
        {
            var axes = a.CollisionAxes(b);

            axes.AddRange(b.CollisionAxes(a));

            Vector2 mtv         = Vector2.Zero;
            float   mtvDistance = float.MaxValue;

            foreach (var axis in axes)
            {
                float aMin, aMax, bMin, bMax;
                a.Project(axis, out aMin, out aMax);
                b.Project(axis, out bMin, out bMax);

                var intervalDistance = IntervalDistance(aMin, aMax, bMin, bMax);
                if (intervalDistance >= 0)
                {
                    return(false);
                }

                if ((intervalDistance = Math.Abs(intervalDistance)) < mtvDistance)
                {
                    mtv         = axis;
                    mtvDistance = intervalDistance;
                }
            }

            var collision = new FBCollision()
            {
                seperatingVector = mtv * mtvDistance,
                collidedWith     = b
            };

            onCollision?.Invoke(collision);

            return(true);
        }