0
\$\begingroup\$

I am working on a 3D editor and for that I would like to implement orbiting camera. The camera works well, but when the camera's direction vector gets parallel to the world up axis, it gets stuck. At the moment I simply do not let user get totally parallel to up axis, but I am not happy with this solution, as I would like to be able to rotate all the way around, for infinitely long time. Here is the code:

void updateRight()
    {
        right_ = glm::normalize(glm::cross(worldUp_, direction_));
    }

void updateUp()
{
    up_ = glm::normalize(glm::cross(right_, direction_));
}

void ProcessMouseMovement(float xoffset, float yoffset)
{
    //Apply Mouse sensitivity and invert
    xoffset *= MouseSensitivity;
    if(invertX)
    {
        xoffset *= -1;
    }
    yoffset *= MouseSensitivity;
    if(invertY)
    {
        yoffset *= -1;
    }

    //Rotate y axis
    glm::quat rotateYAxis =  glm::angleAxis(glm::radians(xoffset), up_);
    direction_ = direction_ * rotateYAxis;

    //Rotate x axis, prevent camera from being pararell to worldUp
    if (direction_.y > cameraDistance_ * (-0.98) && direction_.y < cameraDistance_*(0.98))
    {
        glm::quat rotateXAxis = glm::angleAxis(glm::radians(yoffset), -right_);
        direction_ = direction_ * rotateXAxis;
    }

    //Get new camera position
    position_ = target_ - direction_;

    //Update camera's right and up vectors
    updateUp();
    updateRight();

}

Thanks for help!

\$\endgroup\$

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.