Exemplo n.º 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);
        }
Exemplo n.º 2
0
        // TODO HIGH PRIORITY: Might not need this if we rebuild GetDirectionOfCollision, due to no longer needing floats, etc.
        // Forces a direction to be assumed in a collision based on relative motion of the objects.
        public static DirCardinal AssumeDirectionOfCollision(Physics obj1Phys, Physics obj2Phys)
        {
            int relativeY = CollideDetect.GetRelativeDY(obj1Phys, obj2Phys);
            int relativeX = CollideDetect.GetRelativeDX(obj1Phys, obj2Phys);

            // If the relative motion of Y is greater than the relative motion of X, return a vertical collision.
            if (relativeY > relativeX)
            {
                return(relativeY > 0 ? DirCardinal.Up : DirCardinal.Down);
            }

            return(relativeX > 0 ? DirCardinal.Left : DirCardinal.Right);
        }