绕枢轴旋转(C#)

时间:2020-03-20 19:26:37

标签: c# rotation xna monogame

我正在尝试将相机围绕自身旋转,这会给我的游戏带来非常酷的效果。问题是我不知道怎么做。

我正在使用“矩阵”素材进行轮换。即使我以2D模式工作,我仍使用Vector3,因为这样可以更好地控制矩阵。

基本上,相机的旋转轴位于左上方,而不是中央。这是我的代码以及我尝试过的代码(我正在使用XNA / MonoGame):

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 Showdown
{
    public class Camera
    {
        public Matrix transform;
        public Vector2 folllow;
        public Viewport view;
        public Vector2 position;
        public float rotation;
        public float zoom;
        public float maxRotation = 0.1f;
        public float rotateSpeed = 0.05f;
        public float rotateDirection = -1f;

        bool follow = false;

        public Camera(Viewport _view)
        {
            view = _view;
        }

        public void Update(Vector2 _follow)
        {
            GetRotation();

            if (follow) // true if there's something to follow
            {
                position = _follow;
            }
            else
            {
                position = Vector2.Zero;
            }
            transform = Matrix.CreateScale(new Vector3(zoom, zoom, 0f));
            transform = Matrix.CreateTranslation(new Vector3(position.X, position.Y, 0f));
            transform.Translation = Vector3.Transform(Vector3.Zero, Matrix.CreateRotationZ(rotation));
        }

        void GetRotation()
        {
            rotation += rotateSpeed * rotateDirection * (float)Globals.gameTime.ElapsedGameTime.TotalSeconds;

            if (rotateDirection < 0f && rotation <= -maxRotation)
            {
                rotateDirection = 1f;
            }
            if (rotateDirection > 0f && rotation >= maxRotation)
            {
                rotateDirection = -1f;
            }
        }
    }
}

0 个答案:

没有答案