Simple rectangle hitbox
Exemplo n.º 1
0
        /// <summary>
        /// Collision with another rectangle hitbox (from Platformer 4.0)
        /// </summary>
        /// <param name="anotherHitbox"></param>
        /// <returns></returns>
        public static Vector2 Collide(Hitbox rectHitboxA, Hitbox rectHitboxB)
        {
            // Calculate half sizes.
            float halfWidthA = rectHitboxA.Dimensions.Width / 2.0f;
            float halfHeightA = rectHitboxA.Dimensions.Height / 2.0f;
            float halfWidthB = rectHitboxB.Dimensions.Width / 2.0f;
            float halfHeightB = rectHitboxB.Dimensions.Height / 2.0f;

            // Calculate centers.
            Vector2 centerA = new Vector2(rectHitboxA.EntityLocation.X + halfWidthA, rectHitboxA.EntityLocation.Y + halfHeightA);
            Vector2 centerB = new Vector2(rectHitboxB.EntityLocation.X + halfWidthB, rectHitboxB.EntityLocation.Y + halfHeightB);

            // Calculate current and minimum-non-intersecting distances between centers.
            float distanceX = centerA.X - centerB.X;
            float distanceY = centerA.Y - centerB.Y;
            float minDistanceX = halfWidthA + halfWidthB;
            float minDistanceY = halfHeightA + halfHeightB;

            // If we are not intersecting at all, return (0, 0).
            if (Math.Abs(distanceX) >= minDistanceX || Math.Abs(distanceY) >= minDistanceY)
                return Vector2.Zero;

            // Calculate and return intersection depths.
            float depthX = distanceX > 0 ? minDistanceX - distanceX : -minDistanceX - distanceX;
            float depthY = distanceY > 0 ? minDistanceY - distanceY : -minDistanceY - distanceY;

            Vector2 result = new Vector2(depthX, depthY);

            return result;
        }
Exemplo n.º 2
0
        private void handleEntitiesCollisions()
        {
            // Find collisions between entities
            foreach (Entity worldEnt in WorldEntities)
            {
                foreach (PhysicsEntity regEnt in _registeredEntities)
                {
                    if (worldEnt == regEnt)
                    {
                        continue;
                    }

                    // Look for collisions
                    if ((regEnt.Hitbox != null) && (worldEnt.Hitbox != null))
                    {
                        Vector2 collision = Hitbox.Collide(regEnt.Hitbox, worldEnt.Hitbox);

                        // Raise event
                        if (collision != Vector2.Zero)
                        {
                            if (regEnt.IsInvincible == false)
                            {
                                regEnt.CollisionDetected(worldEnt, collision);
                            }
                        }
                    }

                    // Look for collisions
                    if ((regEnt.AwarenessBox != null) && (worldEnt.Hitbox != null) && worldEnt.IsAlive)
                    {
                        Vector2 collision = Hitbox.Collide(regEnt.AwarenessBox, worldEnt.Hitbox);

                        // Raise event
                        if (collision != Vector2.Zero)
                        {
                            regEnt.EntityDetected(worldEnt, collision);
                        }
                    }
                }
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Tile collisions
        /// </summary>
        /// <param name="hitboxA"></param>
        /// <param name="zoneB"></param>
        /// <returns></returns>
        public static bool Collide(Hitbox rectBox, Rectangle zoneB, out Vector2 depth)
        {
            depth = Vector2.Zero;

            return(rectBox.Dimensions.GetIntersectionDepth(zoneB, out depth));
        }
Exemplo n.º 4
0
 public Chili()
     : base("chili", new Microsoft.Xna.Framework.Rectangle(0, 0, 120, 121), "chili_mod", "chili_col")
 {
     hitbox = new Hitbox(DstRect);
     SetSpriteOriginToMiddle();
 }
Exemplo n.º 5
0
 public Cassoulet()
     : base("cassoulet", new Microsoft.Xna.Framework.Rectangle(0, 0, 92, 128))
 {
     hitbox = new Hitbox(DstRect);
     SetSpriteOriginToMiddle();
 }
Exemplo n.º 6
0
 public Burger()
     : base("burger", new Microsoft.Xna.Framework.Rectangle(0, 0, 120, 121), "burger_mod", "burger_col")
 {
     hitbox = new Hitbox(DstRect);
     SetSpriteOriginToMiddle();
 }
Exemplo n.º 7
0
 public Cacolac()
     : base("cacolac", new Microsoft.Xna.Framework.Rectangle(0, 0, 128, 128))
 {
     hitbox = new Hitbox(DstRect);
     SetSpriteOriginToMiddle();
 }
Exemplo n.º 8
0
        /// <summary>
        /// Tile collisions
        /// </summary>
        /// <param name="hitboxA"></param>
        /// <param name="zoneB"></param>
        /// <returns></returns>
        public static bool Collide(Hitbox rectBox, Rectangle zoneB, out Vector2 depth)
        {
            depth = Vector2.Zero;

            return rectBox.Dimensions.GetIntersectionDepth(zoneB, out depth);
        }
Exemplo n.º 9
0
 public Cassoulet()
     : base("cassoulet", new Microsoft.Xna.Framework.Rectangle(0, 0, 120, 113), "cassoulet_mod", "cassoulet_col")
 {
     hitbox = new Hitbox(DstRect);
     SetSpriteOriginToMiddle();
 }
Exemplo n.º 10
0
 public Pinard()
     : base("pinard", new Microsoft.Xna.Framework.Rectangle(0, 0, 99, 180), "pinard_mod", "pinard_col")
 {
     hitbox = new Hitbox(DstRect);
     SetSpriteOriginToMiddle();
 }
Exemplo n.º 11
0
 public Lessive()
     : base("lessive", new Microsoft.Xna.Framework.Rectangle(0, 0, 120, 135), "lessive_mod", "lessive_col")
 {
     hitbox = new Hitbox(DstRect);
     SetSpriteOriginToMiddle();
 }
Exemplo n.º 12
0
        private bool handleEntityFloorCollisions(PhysicsEntity regEnt, IEnumerable <Floor> floors)
        {
            bool collisionDetected = false;

            foreach (Floor floor in floors)
            {
                Vector2 collision;

                // Do not cross the floor
                if (Hitbox.Collide(regEnt.Hitbox, floor.Rectangle, out collision))
                {
                    float absDepthX = Math.Abs(collision.X);
                    float absDepthY = Math.Abs(collision.Y);

                    // Resolve the collision along the shallow axis.
                    if (absDepthY <= absDepthX)
                    {
                        bool floorOnTop = floor.Rectangle.Y < regEnt.Hitbox.Dimensions.Y;

                        // If we crossed the top of a tile, we are on the ground.
                        if (floorOnTop == false)
                        {
                            // Hack for passable platforms
                            if (floor.IsPassable == false)
                            {
                                regEnt.IsOnGround = true;
                            }
                            else if (regEnt.Velocity.Y >= 0)
                            {
                                regEnt.IsOnGround = true;
                            }
                        }

                        if (floor.IsPassable == false || regEnt.IsOnGround)
                        {
                            regEnt.Location = new Vector2(regEnt.Location.X, regEnt.Location.Y + collision.Y);

                            if (absDepthY != 0)
                            {
                                regEnt.Velocity = new Vector2(regEnt.Velocity.X, 0f);
                            }
                        }
                    }
                    else
                    {
                        regEnt.Location = new Vector2(regEnt.Location.X + collision.X, regEnt.Location.Y);

                        bool passable = (floor.IsPassable == true);

                        if (!passable)
                        {
                            // If going in the direction of the collision
                            if (floor.Rectangle.X > regEnt.Hitbox.Dimensions.X)
                            {
                                regEnt.IsStuckRight = true;
                            }
                            else if (floor.Rectangle.X < regEnt.Hitbox.Dimensions.X)
                            {
                                regEnt.IsStuckLeft = true;
                            }

                            if (absDepthX != 0)
                            {
                                regEnt.Velocity = new Vector2(0f, regEnt.Velocity.Y);
                            }
                        }
                    }

                    regEnt.Hitbox.UpdateBounds();

                    if (regEnt.FloorCollisionDetected != null)
                    {
                        regEnt.FloorCollisionDetected(collision);
                    }

                    if (regEnt.IsAlive == false)
                    {
                        break;
                    }
                    collisionDetected = true;
                }
            }

            return(collisionDetected);
        }