繁体   English   中英

XNA(3D-游戏)中的矩阵用于相机旋转

[英]Matrices in XNA (3D - Game) for Camera rotation

我已经编写了用于旋转和移动相机的代码。
不幸的是,由于我几天前才开始对矩阵和3D编程不熟悉,所以:

plLookAt = new Vector3(plPos.X, plPos.Y, plPos.Z - 20);    
if (kb.IsKeyDown(Keys.W))
        {
            plPos.Z++;
        }
        if (kb.IsKeyDown(Keys.A))
        {
            plPos.X++;
        }
        if (kb.IsKeyDown(Keys.S))
        {
            plPos.Z--;
        }
        if (kb.IsKeyDown(Keys.D))
        {
            plPos.X--;
        }
        view = Matrix.CreateLookAt(new Vector3(0, 0, 0), plLookAt, Vector3.UnitY);
        view = view * Matrix.CreateRotationY(MathHelper.ToRadians(rotations.Y)) * Matrix.CreateRotationX(MathHelper.ToRadians(rotations.X));
        view = view *Matrix.CreateTranslation(plPos);

        if (PrMS.X < ms.X)
        {
            rotations.Y++;
        }
        else if (PrMS.X > ms.X)
        {
            rotations.Y--;
        }
        if (PrMS.Y < ms.Y)
        {
            rotations.X++;
        }
        else if (PrMS.Y > ms.Y)
        {
            rotations.X--;
        }    

plPos是播放器(相机)位置
view是视图矩阵
rotations是保存相机旋转的位置(Vector3)
PrMS是上一帧的MouseState。
这段代码不起作用,我认为这是因为顺序是乘法的顺序,但是我不确定。 旋转相机的最佳方法是什么,这样它才能实际工作,并且我可以朝相机所面对的方向走。 先感谢您!

您的问题不是矩阵乘法的顺序,而是您的旋转必须围绕摄像机局部轴,而您要围绕世界局部轴进行旋转。

我认为您所期望的是应用.CreateRotationX(rotations.X).CreateRotationY(rotationsY)会导致相机改变俯仰并围绕其自身的本地轴偏航。 但是这些旋转总是导致绕着世界轴旋转。 如果您摄像机的局部X轴恰好与世界X轴对齐,并且您执行了.CreateRotationX() ,则它将按预期工作。 但是在您的情况下,您首先要绕Y轴旋转照相机,这会使照相机的局部X轴与世界X轴不对齐,因此下一次旋转(X)将不会如预期那样进行。 即使您先执行X,然后执行Y,X也会将Y甩掉。 尽管矩阵乘法的顺序通常很重要,但是在您的特定情况下,问题是旋转轴。

看来您正在制作的相机位于播放器的位置,但是可以通过鼠标控制向左/向右,向上/向下看。 这是一类,提供了一种不同的方法来处理该标准:

class Camera
{
    public Matrix View { get; set; }
    public Matrix Projection { get; set; }

    Vector2 centerOfScreen;// the current camera's lookAt in screen space (since the mouse coords are also in screen space)
    Vector3 lookAt;

    float cameraRotationSpeed;



    public Camera()
    {
        //initialize Projection matrix here
        //initialize view matrix here 
        //initialize centerOfScreen here like this:  new Vector2(screenWidth/2, screenHeihgt/2); 
        cameraRotationSpeed = 0.01f;//set to taste
    }



    public void Update(Vector3 playerPosition, MouseState currentMouse)
    {
        Matrix cameraWorld = Matrix.Invert(View);

        Vector2 changeThisFrame = new Vector2(currentMouse.X, currentMouse.Y) - centerOfScreen;

        Vector3 axis = (cameraWorld.Right * changeThisFrame.X) + (cameraWorld.Up * changeThisFrame.Y);//builds a rotation axis based on camera's local axis, not world axis

        float angle = axis.Length() * cameraRotationSpeed;
        axis.Normalize();

        //rotate the lookAt around the camera's position
        lookAt = Vector3.Transform(lookAt - playerPosition, Matrix.CreateFromAxisAngle(axis, angle)) + playerPosition;//this line does the typical "translate to origin, rotate, then translate back" routine

        View = Matrix.CreateLookAt(playerPosition, lookAt, Vector3.Up);// your new view matrix that is rotated per the mouse input

    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM