Exemplo n.º 1
0
        public virtual void OnPlayerCollision(Player player)
        {
            Rectangle collisionArea = Rectangle.Intersect(player.AABB, this.AABB);

            float distanceY = collisionArea.Height;
            float distanceX = collisionArea.Left - (collisionArea.Left + collisionArea.Width);

            Vector2 depth = player.AABB.GetIntersectionDepth(this.AABB);

            if (depth != Vector2.Zero)
            {
                float absDepthX = Math.Abs(depth.X);
                float absDepthY = Math.Abs(depth.Y);

                if (absDepthY < absDepthX)
                {
                    var pos = new Vector2(player.Position.X, player.Position.Y + depth.Y + (player.AABB.Center.Y - player.Position.Y));
                    player.Position = pos;
                    player.CollidedWithSurface(false);
                    player.OnGround = true;
                }
                else
                {
                    player.Position = new Vector2(player.Position.X + depth.X, player.Position.Y);
                    player.CollidedWithSurface(true);
                }
            }
        }
        public override void OnPlayerCollision(Player player)
        {
            if (player.RecentlyDamaged)
                return;

            Rectangle collisionArea = Rectangle.Intersect(player.AABB, this.AABB);
            bool yCollision = false;
            if (collisionArea.Width > collisionArea.Height)
                yCollision = true;

            Vector2 depth = player.AABB.GetIntersectionDepth(this.AABB);
            if (yCollision)
            {
                var origin = new Vector2(AABB.Center.X, AABB.Center.Y);
                var destination = new Vector2(AABB.Center.X, AABB.Center.X + (Texture.Height / 2));
                var normal = (destination - origin);
                normal.Normalize();
                var reflected = player.Velocity - 2 * normal * (player.Velocity * normal);
                player.Velocity = reflected;
                player.Damage();
            }
            else
            {
                player.Position = new Vector2(player.Position.X + depth.X, player.Position.Y);
                player.CollidedWithSurface(true);
            }
        }