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

时间:2015-08-04 19:14:23

标签: c# matrix 3d camera xna

我已编写此代码用于旋转和移动相机 可悲的是,我对矩阵和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是玩家(相机)位置
视图是视图矩阵 旋转是保存摄像机旋转的位置(Vector3)
PrMS是前一帧的MouseState 这段代码不起作用,我认为这是因为乘法所在的顺序,但我不确定。旋转相机的最佳方法是什么,以便实际工作,我可以朝着相机朝向的方向前进。 提前谢谢!

1 个答案:

答案 0 :(得分:2)

您的问题不是矩阵乘法的顺序,而是您的旋转需要围绕摄像机局部轴,并且您正在绕世界轴执行它们。

我认为您所期待的是,应用.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

    }
}