Camera flickering with with GLFW_CURSOR_DISABLED

Hi i dont know if its bug or its intentional.

     void Camera::changeCameraPosition(float deltaTime) {
            if ((r8ge::Input::isKeyPressed(r8ge::Key::MBUTTON_RIGHT) && lastoffset != r8ge::Input::getMouseOffset()) ||
                m_firstMouse == true) {
                glfwSetInputMode(Video::getWindowingService()->getWindow(), GLFW_CURSOR, GLFW_CURSOR_DISABLED);
                float velocity = m_MovementSpeed * deltaTime;

                if (r8ge::Input::isKeyPressed(r8ge::Key::W))
                    m_Position += m_Front * velocity;
                if (r8ge::Input::isKeyPressed(r8ge::Key::S))
                    m_Position -= m_Front * velocity;
                if (r8ge::Input::isKeyPressed(r8ge::Key::A))
                    m_Position -= m_Right * velocity;
                if (r8ge::Input::isKeyPressed(r8ge::Key::D))
                    m_Position += m_Right * velocity;


                std::pair<double, double> offset = r8ge::Input::getMouseOffset();

                double xoffset = offset.first;
                double yoffset = offset.second;

                xoffset *= m_MouseSensitivity;
                yoffset *= m_MouseSensitivity;

                m_Yaw += xoffset;
                m_Pitch += yoffset;

                if (m_Pitch > 89.0f)
                    m_Pitch = 89.0f;
                if (m_Pitch < -89.0f)
                    m_Pitch = -89.0f;

                m_firstMouse = false;
                lastoffset = offset;
                updateCameraVectors();
            }
            else {
                glfwSetInputMode(Video::getWindowingService()->getWindow(), GLFW_CURSOR, GLFW_CURSOR_NORMAL);
            }
        }

This cause that when i move my mouse it will force the camera back to the position this doesnt happen if i comment the glfwSetInputMode with GLFW_CURSOR_NORMAL. After using raw input its better but still not optimal
VOD that shows the problem: https://youtu.be/OR0gZzQkXg0

Hi @Karnatour,

Welcome to the GLFW forum.

It’s tricky to debug someone else’s code, particularly when many of the important functions such as getMouseOffset and updateCameraVectors are not shown. However I can spot a few things which I would change.

GLFW mouse input with GLFW_CURSOR_DISABLED can be used for mouse movement, as this video of my app shows.

I’ve not implemented all of your code, but try something like the following:

    /// need to track input mode state in member variables
    bool m_bInputModeOn = false;
    doule m_LastMouseX, m_LastMouseY;

    // ....
    void Camera::changeCameraPosition(float deltaTime) {
        if ( r8ge::Input::isKeyPressed(r8ge::Key::MBUTTON_RIGHT) ) {

            glfwSetInputMode(Video::getWindowingService()->getWindow(), GLFW_CURSOR, GLFW_CURSOR_DISABLED);
            if( !m_bInputModeOn ) {
                // we just changed input mode, reset last mouse pos
                glfwGetCursorPos( Video::getWindowingService()->getWindow(), m_LastMouseX, m_LastMouseY );
            }
            
            // Process inputs here
            m_bInputModeOn = true;
            double mouseX, mouseY;
            glfwGetCursorPos( Video::getWindowingService()->getWindow(), mouseX, mouseY );
            
            // calculate mouse delta since last frame adjusted for frame time
            // note that you may want to cap deltaTime or mouse movement in case of frame hitches
            double mouseMoveX = ( mouseX - m_LastMouseX ) * m_MouseSensitivity * deltaTime;
            double mouseMoveY = ( mouseY - m_LastMouseY ) * m_MouseSensitivity * deltaTime;

            // Add the rest of your movement code here...
            
        } else  {
            glfwSetInputMode(Video::getWindowingService()->getWindow(), GLFW_CURSOR, GLFW_CURSOR_NORMAL);
            m_bInputModeOn = false;
        }
    }

Hi thanks for reply the updateCameraVectors function does this

void Camera::updateCameraVectors() {
            m_Front = glm::normalize(glm::vec3(
                cos(glm::radians(m_Yaw)) * cos(glm::radians(m_Pitch)),
                sin(glm::radians(m_Pitch)),
                sin(glm::radians(m_Yaw)) * cos(glm::radians(m_Pitch))
            ));
            m_Right = glm::normalize(glm::cross(m_Front, m_WorldUp));
            m_Up = glm::normalize(glm::cross(m_Right, m_Front));
        }

I tried to implementing your code this is my result

        void Camera::changeCameraPosition(float deltaTime) {
            if (r8ge::Input::isKeyPressed(r8ge::Key::MBUTTON_RIGHT)) {
                glfwSetInputMode(Video::getWindowingService()->getWindow(), GLFW_CURSOR, GLFW_CURSOR_DISABLED);
                if (!m_bInputModeOn) {
                    glfwGetCursorPos(Video::getWindowingService()->getWindow(), &m_lastX, &m_lastY);
                }
                m_bInputModeOn = true;
                double mouseX, mouseY;
                glfwGetCursorPos(Video::getWindowingService()->getWindow(), &mouseX, &mouseY);

                double mouseMoveX = (mouseX - m_lastX) * m_MouseSensitivity * deltaTime;
                double mouseMoveY = (m_lastY - mouseY) * m_MouseSensitivity * deltaTime;

                float velocity = m_MovementSpeed * deltaTime;

                if (r8ge::Input::isKeyPressed(r8ge::Key::W))
                    m_Position += m_Front * velocity;
                if (r8ge::Input::isKeyPressed(r8ge::Key::S))
                    m_Position -= m_Front * velocity;
                if (r8ge::Input::isKeyPressed(r8ge::Key::A))
                    m_Position -= m_Right * velocity;
                if (r8ge::Input::isKeyPressed(r8ge::Key::D))
                    m_Position += m_Right * velocity;

                m_Yaw += mouseMoveX;
                m_Pitch += mouseMoveY;

                if (m_Pitch > 89.0f)
                    m_Pitch = 89.0f;
                if (m_Pitch < -89.0f)
                    m_Pitch = -89.0f;

                updateCameraVectors();
            } else {
                m_lastX = 0;
                m_lastY = 0;
                glfwSetInputMode(Video::getWindowingService()->getWindow(), GLFW_CURSOR, GLFW_CURSOR_NORMAL);
                m_bInputModeOn = false;
            }
        }

But this gives me problem i encountered before when i stop moving IRL mouse but still holding rightclick the camera will NOT stop moving in game it keeps moving in the direction. My goal is to implement camera from UE5 editor.

Sorry I forgot that you need to update m_lastX and m_lastY after you use them, like this:

                double mouseMoveX = (mouseX - m_lastX) * m_MouseSensitivity * deltaTime;
                double mouseMoveY = (m_lastY - mouseY) * m_MouseSensitivity * deltaTime;
                m_lastX = mouseX;
                m_lastY = mouseY;
1 Like

Thank you so much !!!