【问题标题】:Why when trying to move the mouse around 360c degrees the mouse cursor is stuttering in the middle?为什么尝试将鼠标移动 360c 度左右时,鼠标光标在中间卡顿?
【发布时间】:2013-10-24 08:59:24
【问题描述】:

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

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

    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

【问题讨论】:

    标签: c# xna xna-4.0


    【解决方案1】:
    Mouse.SetPosition(GraphicsDevice.Viewport.Width / 2, GraphicsDevice.Viewport.Height / 2);
    

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

    【讨论】:

    • 好的,所以我删除了这一行,并在 LoadContent 方法中只用这一行更新了一次。现在为什么当我左右移动鼠标时它根本不会移动 360c 度?我看到鼠标光标移动正常?它完成了 ProcessInputCamera 方法的所有部分,但鼠标不像第一人称射击游戏那样将我的观点 360c 电影。就像在这个演示中一样:dhpoware.com/demos/xnaFirstPersonCamera.html
    • 查看代码的作用后,该行应该在那里。鼠标应该重置到屏幕的中心。除了这些,我无能为力了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-19
    • 2011-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多