Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Qt 5.9 Qt OpenGL Hello GL2 Example Calculate object rotation angle from mouse movement



  • In the Qt 5.9 Qt OpenGL Hello GL2 Example, to implement the rotation of the objects, there is a degree normalization function when calculate the rotation from the movement of the mouse. I just don't get it 1) why it needs dx and dy time 8 to get the mouse movement distance? and 2) then 360 times 16 here to normalize the angle, why 16? And 3) when set up the rotation in render by dividing 16? Thanks.

    void GLWidget::mouseMoveEvent(QMouseEvent *event)
    {
        int dx = event->x() - m_lastPos.x();
        int dy = event->y() - m_lastPos.y();
    
        if (event->buttons() & Qt::RightButton) {
            ....
            setZRotation(m_zRot + 8 * dx);
        }
        m_lastPos = event->pos();
    }
    
    static void qNormalizeAngle(int &angle) {
        while (angle < 0)
            angle += 360 * 16;
        while (angle > 360 * 16)
            angle -= 360 * 16;
    }
    
    void GLWidget::setZRotation(int angle)
    {
        qNormalizeAngle(angle);
        if (angle != m_zRot) {
            m_zRot = angle;
            .....
            update();
        }
    }
    
    void GLWidget::paintGL() {
        ....
        m_world.rotate(m_zRot / 16.0f, 0, 0, 1);
        ....
    }
    

Log in to reply