Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Problems with QQuaternion
QtWS25 Last Chance

Problems with QQuaternion

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 3.6k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    basil_fawlty
    wrote on last edited by
    #1

    Hello everyboy,

    i want to rotate my object via gui-arows and quaternions. With up and down arrow it should be possible to rotate round the x-axis. with left and right round y and with two other buttons round the z axis.

    I am using opengl in qt. I have achived to rotate the object round every axis via these slot-functions:
    @
    void OpenGLScene::rotate_plus_x()
    {
    OpenGLScene::anglex += 5;
    test->rotation = QQuaternion::fromAxisAndAngle(QVector3D(1,0,0),OpenGLScene::anglex);
    update();

    }

    void OpenGLScene::rotate_minus_x()
    {
    OpenGLScene::anglex -= 5;
    test->rotation = QQuaternion::fromAxisAndAngle(QVector3D(1,0,0),OpenGLScene::anglex);
    update();
    }

    void OpenGLScene::rotate_plus_y()
    {
    OpenGLScene::angley += 5;
    test->rotation = QQuaternion::fromAxisAndAngle(QVector3D(0,1,0),OpenGLScene::angley);
    update();
    }
    @

    In this class my actual rotation is happening.(Where the rotation is multiplied with the modelview matrix)

    @
    QMatrix4x4 matrix;
    matrix.rotate(rotation);
    glMatrixMode (GL_MODELVIEW);
    glLoadIdentity ();

                glPushMatrix();
                light->drawLight(light->light_rotate_x,light->light_rotate_y,light->light_rotate_z);
    
                gluLookAt(   0,   0, -400,  
                             0,   0,    0,   
                             0,   1,    0);  
                qMultMatrix(matrix);
                object->drawObject(Red,Green,Blue);
                draw->drawAxes();
                glPopMatrix();
    

    @

    (The qmultmatrix function is from the pbuffers-example from the qt-examples.)

    My problem is, that the object is always jumping back to the initial position, when i change the rotation-axis. For instance, when i rotate round the x-axis and afterwards round the y-axis the object starts to rotate from the initial position and not from the newest one.

    I think there might be a problem with how i multiply my matrix with the modelview-matrix. I think i should store the last changed matrix and multiply it with the newest one. How to do that is a mystery to me.
    I have tried several things but nothing is working.

    Maybe you could help me.

    Thanks in Advance

    1 Reply Last reply
    0
    • F Offline
      F Offline
      Franzk
      wrote on last edited by
      #2

      You need to do the math. You create a unit matrix and then rotate it according to a certain quaternion -- that is, you're throwing away the previous rotation by assigning them in your slots.

      IIRC the following should provide better results:
      @
      void OpenGLScene::rotate_plus_x()
      {
      OpenGLScene::anglex += 5;
      test->rotation *= QQuaternion::fromAxisAndAngle(QVector3D(1,0,0),OpenGLScene::anglex);
      update();

      }

      void OpenGLScene::rotate_minus_x()
      {
      OpenGLScene::anglex -= 5;
      test->rotation *= QQuaternion::fromAxisAndAngle(QVector3D(1,0,0),OpenGLScene::anglex);
      update();
      }

      void OpenGLScene::rotate_plus_y()
      {
      OpenGLScene::angley += 5;
      test->rotation *= QQuaternion::fromAxisAndAngle(QVector3D(0,1,0),OpenGLScene::angley);
      update();
      }
      @

      "Horse sense is the thing a horse has which keeps it from betting on people." -- W.C. Fields

      http://www.catb.org/~esr/faqs/smart-questions.html

      1 Reply Last reply
      0
      • Z Offline
        Z Offline
        ZapB
        wrote on last edited by
        #3

        As FranzK says you are resetting your rotation matrices in the rotate_*() functions. Instead of replacing the matrix, calculate another matrix from the quarternion and pre-multiply the pre-existing transformation matrix with it.

        I think the order of the matrix multiplication should be opposite to what Franzk said though (but to be fair I've not tried it myself). For e.g.:

        @
        void OpenGLScene::rotate_plus_x()
        {
        OpenGLScene::anglex += 5;
        QMatrix4x4 newRotation = QQuaternion::fromAxisAndAngle(QVector3D(1,0,0),OpenGLScene::anglex);
        test->rotation = newRotation * test->rotation;
        update();
        }
        @

        Nokia Certified Qt Specialist
        Interested in hearing about Qt related work

        1 Reply Last reply
        0
        • Z Offline
          Z Offline
          ZapB
          wrote on last edited by
          #4

          Gah! Sorry ignore my last post I was thinking of matrices not quaternions. Sorry for the confusion. Long day ;-)

          Nokia Certified Qt Specialist
          Interested in hearing about Qt related work

          1 Reply Last reply
          0
          • B Offline
            B Offline
            basil_fawlty
            wrote on last edited by
            #5

            Thanks to both of you. Especially to FranzK. It was exactly the problem.
            Now its working. One thing was wrong as well.
            It should be:

            @
            OpenGLScene::anglex = 5;
            @
            or
            @
            OpenGLScene::anglex = -5;
            @

            Otherwise the rotationangle gets bigger and bigger with every click.
            For those who are interested.

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved