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);
    }

键正在工作,它们正在旋转地形。但是使用鼠标,我希望能够像第一人称视角一样将我的视角更改为 360c 度,而不是旋转地形,而不是旋转对象,而是让我环顾四周 360c。

问题是这一行:

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

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

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

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

4

1 回答 1

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

此行将鼠标光标位置设置为屏幕中心。Width / 2 和Height / 2 是屏幕中心的坐标。此行在每个游戏循环中执行一次,因此它不断将光标位置设置回中心。这就是为什么当您尝试移动鼠标时,鼠标会卡在中间并晃动。

于 2013-10-24T00:59:24.230 回答