Esempio n. 1
0
        /****************************
        *** Directional Detection ***
        ****************************/

        // Identifies the direction of a collision relative to a rectangle.
        // WARNING: Heavy process. Only run this AFTER you've tested for if the rectangle overlaps.
        public static DirCardinal GetDirectionOfCollision(GameObject obj, int x1, int x2, int y1, int y2)
        {
            // If the movement between the objects > the amount overlapped, ignore the overlap.
            // This prevents problems like inaccurate hitboxes from the wrong side.
            int maxOverlapY = Math.Abs(obj.physics.AmountMovedY);
            int relativeY   = 0 - obj.physics.AmountMovedY;
            int overlapY    = CollideRect.GetOverlapY(obj, y1, y2, relativeY <= 0);

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

            // Same as above, but for X coordinates.
            int maxOverlapX = Math.Abs(obj.physics.AmountMovedX);
            int relativeX   = 0 - obj.physics.AmountMovedX;
            int overlapX    = CollideRect.GetOverlapX(obj, x1, x2, relativeX <= 0);

            if (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);
        }
Esempio n. 2
0
        public virtual void BounceUp(int midX, sbyte strengthMod = 4, byte maxX = 2, sbyte relativeMult = 3)
        {
            this.physics.velocity.Y = FInt.Create(-strengthMod);

            if (maxX > 0)
            {
                short xDiff   = CollideRect.GetRelativeX(this, midX);
                FInt  xAdjust = FInt.Create(Math.Min(maxX, Math.Abs(xDiff / relativeMult)));

                if (xDiff > 0)
                {
                    this.physics.velocity.X += xAdjust;
                }
                else
                {
                    this.physics.velocity.X -= xAdjust;
                }
            }
        }