Example #1
0
        /****************************
        *** Directional Detection ***
        ****************************/

        // Identifies the direction of a collision.
        // WARNING: Heavy use. Only run this AFTER you've tested for if the objects overlap.
        public static DirCardinal GetDirectionOfCollision(GameObject obj, GameObject obj2)
        {
            // If the movement between the objects > the amount overlapped, ignore the overlap.
            // This prevents problems like inaccurate hitboxes from the wrong side.
            int maxOverlapY = CollideDetect.GetMaxOverlapY(obj.physics, obj2.physics);
            int relativeY   = CollideDetect.GetRelativeDY(obj.physics, obj2.physics);
            int overlapY    = CollideDetect.GetOverlapY(obj, obj2, relativeY <= 0);

            if (Math.Abs(overlapY) <= maxOverlapY)
            {
                return(relativeY > 0 ? DirCardinal.Up : DirCardinal.Down);
            }

            // Same as above, but for X coordinates.
            int maxOverlapX = CollideDetect.GetMaxOverlapX(obj.physics, obj2.physics);
            int relativeX   = CollideDetect.GetRelativeDX(obj.physics, obj2.physics);
            int overlapX    = CollideDetect.GetOverlapX(obj, obj2, relativeX <= 0);

            if (Math.Abs(overlapX) <= maxOverlapX)
            {
                return(relativeX > 0 ? DirCardinal.Left : DirCardinal.Right);
            }

            // If we've made it this far, the object is overlapping, but already passed the edge.
            // We return false to avoid unusual behavior, such as 'popping' up on a platform when you're slightly beneath it.
            return(DirCardinal.None);
        }