【问题标题】:XNA Smooth Camera RotationXNA 平滑相机旋转
【发布时间】:2018-10-18 17:36:54
【问题描述】:

我有一个问题 - 我说问题,它更像是一种极端的不便,让玩起来很烦人。

我正在使用 XNA Monogame 制作一个 3D 游戏(这是一项痛苦的工作,在统一中会更容易,但由于我懒得进入的原因,它不是。)我的相机是用来设置玩家的将鼠标移到窗口/视口的中心,然后在该帧和下一帧之间获取鼠标移动的变化,这将用于旋转相机......它正在工作......有点。但是现在我已经将这个项目转移到了个人电脑上,而不是我正在使用的笔记本电脑上,相机在移动时变得非常不稳定。我不太确定如何修复它,我一直在弄乱我将发布的代码 sn-p 中的一些值,但它不再流畅了。如果这可能是一个问题,这台电脑是使用 3 个屏幕设置的吗?

            if (moveVector != Vector3.Zero)
        {
            //Normalise the vector - to stop us moving faster diagonally
            moveVector.Normalize();
            //Add smooth and speed
            moveVector *= deltaTime * cameraSpeed;

            //Move camera
            Move(moveVector);

        }

        //Handle mouse movement
        float deltaX;
        float deltaY;

        if (currentMouseState != previousMouseState)
        {
            //Save mouse location
            deltaX = currentMouseState.X - (Game.GraphicsDevice.PresentationParameters.BackBufferWidth / 2);
            deltaY = currentMouseState.Y - (Game.GraphicsDevice.PresentationParameters.BackBufferHeight / 2);

            //Determines the speed of the rotation 0.1 is pretty quick and looks just about right
            mouseRotationBuffer.X -= 0.1f * deltaX * deltaTime;
            mouseRotationBuffer.Y -= 0.1f * deltaY * deltaTime;

            if (mouseRotationBuffer.Y < MathHelper.ToRadians(-75.0f))
            {
                mouseRotationBuffer.Y = mouseRotationBuffer.Y - (mouseRotationBuffer.Y - MathHelper.ToRadians(-75.0f));
            }
            if (mouseRotationBuffer.Y > MathHelper.ToRadians(75.0f))
            {
                mouseRotationBuffer.Y = mouseRotationBuffer.Y - (mouseRotationBuffer.Y - MathHelper.ToRadians(75.0f));
            }

            Rotation = new Vector3(-MathHelper.Clamp(mouseRotationBuffer.Y, MathHelper.ToRadians(-75.0f), MathHelper.ToRadians(75.0f)),
                MathHelper.WrapAngle(mouseRotationBuffer.X), 0);

            deltaX = 0;
            deltaY = 0;
        }

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

在任何人说之前,我也将这个发布到 gamedev,但我想我也会在这里尝试一下,以防有人知道一些事情。

【问题讨论】:

    标签: 3d rotation xna monogame xna-4.0


    【解决方案1】:

    您的 sn-p 看起来不错,但是,当您想要平滑旋转相机时,可以调整以使值随着新鼠标位置的增加或减少而变得平滑。

    Unity 在鼠标平移及其变换属性期间简化了这一点,您所要做的就是根据鼠标移动速度调整鼠标的最后位置和新位置的值。

    只需一点点 sudeo 代码即可演示相机的波动旋转。

    // This is a potential Unity fix for solving camera translations in general 
    // for smooth movement
    void Update() {
            Vector3 msNewPoint = Mouse.Position;
            Vector3 adjustedPoint = msOldPoint - msNewPoint;
            Vector3 slerpedValue = Vector3.Slerp(msOldPoint, adjustedPoint, 0.2f);
    
            gameObject.Transform.Rotation *= slerpedValue;
    }
    

    我在多个屏幕上偶然发现了同样的问题,当我将鼠标夹在游戏本身的视口上时,这对我来说是一个简单的解决方法。

    // Again this is sudo code to demonstrate clamping the mouse to prevent multi
    // monitor dpi (IF you are using spanned displays. 
    // Ie nVidia surround, or AMD Eyefinity.
    private Rectangle oldClipRect;
    private bool oldClipSet;
    void clampMouse(Control control) {
            Rectangle screenRect = control.RectangleToScreen(control);
    
            // Initialize this only once to prevent the clip being altered
            // when the size of the screen changes
            if (!oldClipSet) {
                oldClipRect = Cursor.Clip;
                oldClipSet = true;
            }
    
            // If the control is focused bind the mouse to the game screen,
            // otherwise release it.
            if (control.Focused)
                Cursor.Clip = screenRect;
            else Cursor.Clip = oldClipRect;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-08-12
      • 1970-01-01
      • 2013-06-07
      • 1970-01-01
      • 1970-01-01
      • 2017-02-12
      • 2015-02-28
      • 2023-01-11
      相关资源
      最近更新 更多