【问题标题】:Camera not centre on player sprite (monogame)相机不在玩家精灵的中心(单人游戏)
【发布时间】:2020-02-10 10:13:37
【问题描述】:

我正在尝试在 Monogame 中制作游戏并在此处遵循相机教程https://www.youtube.com/watch?v=c_SPRT7DAeM&t=180s(效果很好)唯一的问题是,当它缩放时它会缩放到玩家精灵的角落 见这里:

这是我的代码

camera.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace RpgGameCrossPlatform
{
    class Camera
    {
        private Matrix transform;
        public Matrix Transform
        {
            get { return transform; }
        }

        private Vector2 centre;
        private Viewport viewport;

        private float zoom = 3;
        public float rotation = 0;

        public float CentreX
        {
            get { return centre.X; }
            set { centre.X = value; }
        }

        public float CentreY
        {
            get { return centre.Y; }
            set { centre.Y = value; }
        }

        public float Zoom
        {
            get { return zoom; }
            set { zoom = value; if (zoom < 0.1f) zoom = 0.1f; }
        }


        public Camera (Viewport newViewport)
        {
            viewport = newViewport;
        }

        public void CameraUpdate(Vector2 position)
        {
            centre = new Vector2(position.X, position.Y);

            transform = Matrix.CreateTranslation(new Vector3(-centre.X, -centre.Y, 0)) *                                             
                                                 Matrix.CreateScale(new Vector3(Zoom, Zoom, 0)) *
                                                 Matrix.CreateTranslation(new Vector3(viewport.Width / 2, viewport.Height / 2, 0));
        }
    }
}

player.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace RpgGameCrossPlatform
{
    class Camera
    {
        private Matrix transform;
        public Matrix Transform
        {
            get { return transform; }
        }

        private Vector2 centre;
        private Viewport viewport;

        private float zoom = 3;
        public float rotation = 0;

        public float CentreX
        {
            get { return centre.X; }
            set { centre.X = value; }
        }

        public float CentreY
        {
            get { return centre.Y; }
            set { centre.Y = value; }
        }

        public float Zoom
        {
            get { return zoom; }
            set { zoom = value; if (zoom < 0.1f) zoom = 0.1f; }
        }


        public Camera (Viewport newViewport)
        {
            viewport = newViewport;
        }

        public void CameraUpdate(Vector2 position)
        {
            centre = new Vector2(position.X, position.Y);

            transform = Matrix.CreateTranslation(new Vector3(-centre.X, -centre.Y, 0)) *                                             
                                                 Matrix.CreateScale(new Vector3(Zoom, Zoom, 0)) *
                                                 Matrix.CreateTranslation(new Vector3(viewport.Width / 2, viewport.Height / 2, 0));
        }
    }
}

和game.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace RpgGameCrossPlatform
{
    class Camera
    {
        private Matrix transform;
        public Matrix Transform
        {
            get { return transform; }
        }

        private Vector2 centre;
        private Viewport viewport;

        private float zoom = 3;
        public float rotation = 0;

        public float CentreX
        {
            get { return centre.X; }
            set { centre.X = value; }
        }

        public float CentreY
        {
            get { return centre.Y; }
            set { centre.Y = value; }
        }

        public float Zoom
        {
            get { return zoom; }
            set { zoom = value; if (zoom < 0.1f) zoom = 0.1f; }
        }


        public Camera (Viewport newViewport)
        {
            viewport = newViewport;
        }

        public void CameraUpdate(Vector2 position)
        {
            centre = new Vector2(position.X, position.Y);

            transform = Matrix.CreateTranslation(new Vector3(-centre.X, -centre.Y, 0)) *                                             
                                                 Matrix.CreateScale(new Vector3(Zoom, Zoom, 0)) *
                                                 Matrix.CreateTranslation(new Vector3(viewport.Width / 2, viewport.Height / 2, 0));
        }
    }
}

如果我是新手,我们将不胜感激帮助以及一些一般提示:)

【问题讨论】:

  • 看起来您不小心将 camera.cs 代码放在了所有三个类中。

标签: c# camera xna monogame


【解决方案1】:

这可能意味着相机正聚焦于玩家的默认位置,该位置始终位于左上角。

您应该为播放器设置一个中心位置(称为“原点”),这可以通过在当前默认位置添加播放器宽度(对于 x)和高度(对于 y)的一半来完成。

【讨论】:

    猜你喜欢
    • 2017-03-12
    • 2023-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-22
    • 2020-09-23
    • 1970-01-01
    相关资源
    最近更新 更多