为什么当试图将鼠标移动360度左右时,鼠标光标在中间是否口吃?

时间:2013-10-23 22:29:50

标签: c# xna xna-4.0

鼠标没有移动鼠标指针/光标卡在中间,口吃/跳舞/摇晃。

这是我用于鼠标输入的代码:

    private void ProcessInputCamera(float amount)
    {
        previousState = currentState;
        currentState = Mouse.GetState();
        MouseState currentMouseState = Mouse.GetState();
        if (currentMouseState != originalMouseState)
        {
            float xDifference = currentMouseState.X - originalMouseState.X;
            float yDifference = currentMouseState.Y - originalMouseState.Y;
            leftrightRot -= rotationSpeed * xDifference * amount;
            updownRot -= rotationSpeed * yDifference * amount;
            Mouse.SetPosition(GraphicsDevice.Viewport.Width / 2, GraphicsDevice.Viewport.Height / 2);
        }

        Vector3 moveVector = new Vector3(0, 0, 0);
        KeyboardState keyState = Keyboard.GetState();
        if (keyState.IsKeyDown(Keys.Up) || keyState.IsKeyDown(Keys.Up))
            moveVector += new Vector3(0, 0, -1);
        if (keyState.IsKeyDown(Keys.Down) || keyState.IsKeyDown(Keys.Down))
            moveVector += new Vector3(0, 0, 1);
        if (keyState.IsKeyDown(Keys.Right) || keyState.IsKeyDown(Keys.Right))
            moveVector += new Vector3(1, 0, 0);
        if (keyState.IsKeyDown(Keys.Left) || keyState.IsKeyDown(Keys.Left))
            moveVector += new Vector3(-1, 0, 0);
        if (keyState.IsKeyDown(Keys.Q))
            moveVector += new Vector3(0, 100, 0);
        if (keyState.IsKeyDown(Keys.Z))
            moveVector += new Vector3(0, -100, 0);
        AddToCameraPosition(moveVector * amount);
    }

    private void AddToCameraPosition(Vector3 vectorToAdd)
    {
        Matrix cameraRotation = Matrix.CreateRotationX(updownRot) * Matrix.CreateRotationY(leftrightRot);
        Vector3 rotatedVector = Vector3.Transform(vectorToAdd, cameraRotation);
        cameraPosition += moveSpeed * rotatedVector;
        UpdateViewMatrix(viewMatrix);
    }

    private void UpdateViewMatrix(Matrix viewMatrix)
    {
        Matrix cameraRotation = Matrix.CreateRotationX(updownRot) * Matrix.CreateRotationY(leftrightRot);

        Vector3 cameraOriginalTarget = new Vector3(0, 0, -1);
        Vector3 cameraRotatedTarget = Vector3.Transform(cameraOriginalTarget, cameraRotation);
        Vector3 cameraFinalTarget = cameraPosition + cameraRotatedTarget;

        Vector3 cameraOriginalUpVector = new Vector3(0, 1, 0);
        Vector3 cameraRotatedUpVector = Vector3.Transform(cameraOriginalUpVector, cameraRotation);

        viewMatrix = Matrix.CreateLookAt(cameraPosition, cameraFinalTarget, cameraRotatedUpVector);
    }

按键工作正在旋转地形。 但是用鼠标我希望能够像第一人称视角一样改变我的视角360度,不要旋转地形而不是旋转物体但让我环顾360c。

问题是这一行:

Mouse.SetPosition(GraphicsDevice.Viewport.Width / 2, GraphicsDevice.Viewport.Height / 2);

出于某种原因,将鼠标光标卡在中间并跳舞/摇晃/口吃。 如果我取消注释这条线没有使用它,那么鼠标移动平滑,但它可以移动任何360度,我可以正常移动鼠标光标。

问题是为什么我不能像第一人称射击游戏一样移动鼠标360c?

这是包含我标记的相机代码的Game1.cs代码。     http://pastebin.com/SF3iiftq

1 个答案:

答案 0 :(得分:2)

Mouse.SetPosition(GraphicsDevice.Viewport.Width / 2, GraphicsDevice.Viewport.Height / 2);

此行将鼠标光标位置设置为屏幕中心。宽度/ 2和高度/ 2是屏幕中心的坐标。这条线在每个游戏循环执行一次,因此它会将光标位置设置回中心。这就是为什么鼠标卡在中心并在你试图移动它时摇晃的原因。