How to properly rotate a quaternion along all axis?

后端 未结 2 1288
感动是毒
感动是毒 2020-12-30 13:28

I want to code a first person camera with its rotation stored in a quaternion. Unfortunately there is something wrong with the rotation.

The following function is re

2条回答
  •  甜味超标
    2020-12-30 14:22

    Matrices and pitch/yaw/roll both having their limitations, I do not use them anymore but use instead quaternions. I rotate the view vector and recalculate first the camera vectors, then the view matrix in regard to the rotated view vector.

    void Camera::rotateViewVector(glm::quat quat) {
    
        glm::quat rotatedViewQuat;
    
        quat = glm::normalize(quat);
        m_viewVector = glm::normalize(m_viewVector);
    
        glm::quat viewQuat(0.0f,
            m_viewVector.x,
            m_viewVector.y,
            m_viewVector.z);
    
        viewQuat = glm::normalize(viewQuat);
    
        rotatedViewQuat = (quat * viewQuat) * glm::conjugate(quat);
        rotatedViewQuat = glm::normalize(rotatedViewQuat);
    
        m_viewVector = glm::normalize(glm::vec3(rotatedViewQuat.x, rotatedViewQuat.y, rotatedViewQuat.z));
        m_rightVector = glm::normalize(glm::cross(glm::vec3(0.0f, 1.0f, 0.0f), m_viewVector));
        m_upVector = glm::normalize(glm::cross(m_viewVector, m_rightVector));
    }
    

提交回复
热议问题