public static CollisionDirection CheckCollision(Rectangle collider, Rectangle obstacle, CollisionDirection direction, bool isInside = false)
        {
            CollisionDirection result = CollisionDirection.None;

            if (isBetween(collider.Top, obstacle.Top, obstacle.Bottom) || isBetween(collider.Bottom, obstacle.Top, obstacle.Bottom))
            {
                if (direction.HasFlag(CollisionDirection.Left))
                {
                    if ((!isInside && isBetween(collider.Right, obstacle.Left, obstacle.Right)) || (isInside && isBetween(collider.Left, obstacle.Left - collider.Width, obstacle.Left)))
                    {
                        result |= CollisionDirection.Left;
                    }
                }

                if (direction.HasFlag(CollisionDirection.Right))
                {
                    if ((!isInside && isBetween(collider.Left, obstacle.Left, obstacle.Right)) || (isInside && isBetween(collider.Right, obstacle.Right, collider.Width + obstacle.Right)))
                    {
                        result |= CollisionDirection.Right;
                    }
                }
            }

            if (isBetween(collider.Left, obstacle.Left, obstacle.Right) || isBetween(collider.Right, obstacle.Left, obstacle.Right))
            {
                if (direction.HasFlag(CollisionDirection.Top))
                {
                    if ((!isInside && isBetween(collider.Bottom, obstacle.Top, obstacle.Bottom)) || (isInside && isBetween(collider.Top, obstacle.Top - collider.Width, obstacle.Top)))
                    {
                        result |= CollisionDirection.Top;
                    }
                }

                if (direction.HasFlag(CollisionDirection.Bottom))
                {
                    if ((!isInside && isBetween(collider.Top, obstacle.Top, obstacle.Bottom)) || (isInside && isBetween(collider.Bottom, obstacle.Bottom, collider.Height + collider.Bottom)))
                    {
                        result |= CollisionDirection.Bottom;
                    }
                }
            }
            return(result);
        }
Exemple #2
0
 public bool IsEntityOnEdge(Rectangle entity, Rectangle obstacle, CollisionDirection direction, bool inside)
 {
     if (direction.HasFlag(CollisionDirection.Left))
     {
         if (!inside)
         {
             return(Collisions.isBetween(entity.Right, obstacle.Left, obstacle.Left + Collisions.CollisionOffset));
         }
         return(Collisions.isBetween(entity.Left, obstacle.Left, obstacle.Left + Collisions.CollisionOffset));
     }
     if (direction.HasFlag(CollisionDirection.Right))
     {
         if (!inside)
         {
             return(Collisions.isBetween(entity.Left, obstacle.Right - Collisions.CollisionOffset, obstacle.Right));
         }
         return(Collisions.isBetween(entity.Right, obstacle.Right - Collisions.CollisionOffset, obstacle.Right));
     }
     return(false);
 }
        public static bool isInLimits(Rectangle collider, Rectangle obstacle, CollisionDirection direction)
        {
            if (direction.HasFlag(CollisionDirection.Left) && isBetween(collider.Right, obstacle.Left, obstacle.Left + CollisionOffset))
            {
                return(true);
            }

            if (direction.HasFlag(CollisionDirection.Right) && isBetween(collider.Left, obstacle.Right - CollisionOffset, obstacle.Right))
            {
                return(true);
            }

            if (direction.HasFlag(CollisionDirection.Top) && isBetween(collider.Bottom, obstacle.Top, obstacle.Top + CollisionOffset))
            {
                return(true);
            }

            if (direction.HasFlag(CollisionDirection.Bottom) && isBetween(collider.Top, obstacle.Bottom - CollisionOffset, obstacle.Bottom))
            {
                return(true);
            }
            return(false);
        }
Exemple #4
0
        void PutEntityOnEdge(Entity e, Rectangle bounds, CollisionDirection direction, bool bounce = false, bool inside = true)
        {
            int horizontalOffset = 0;
            int verticalOffset   = 0;

            if (bounce)
            {
                horizontalOffset = Collisions.CollisionOffset;
                verticalOffset   = Collisions.CollisionOffset;
            }
            if (!inside)
            {
                if (direction.HasFlag(CollisionDirection.Left) || direction.HasFlag(CollisionDirection.Right))
                {
                    horizontalOffset += e.Bounds.Width;
                }
                if (direction.HasFlag(CollisionDirection.Top) || direction.HasFlag(CollisionDirection.Bottom))
                {
                    verticalOffset += e.Bounds.Height;
                }
            }
            Point p = new Point(e.Position.X, e.Position.Y);

            if (!Collisions.isInLimits(e.Bounds, bounds, direction))
            {
                return;
            }
            if (direction.HasFlag(CollisionDirection.Left))
            {
                p.X = bounds.Left - horizontalOffset;
            }
            if (direction.HasFlag(CollisionDirection.Right))
            {
                p.X = bounds.Right + (horizontalOffset - e.Width);
            }

            if (direction.HasFlag(CollisionDirection.Top))
            {
                p.Y = bounds.Top - verticalOffset;
            }

            e.Position = p;
        }