Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 5 of 5

Thread: [glm] Use quaterions to move the camera direction according to mouse movement

Hybrid View

  1. #1
    Newbie Newbie
    Join Date
    Sep 2013
    Posts
    1

    [glm] Use quaterions to move the camera direction according to mouse movement

    I am trying to use quaterions to move the camera direction vector in the following way.

    This code is perfectly working
    Code :
            glm::quat temp1 = glm::normalize( glm::quat((GLfloat)( -Input1.MouseMove.x  * mouse_sens * time_step), glm::vec3(0.0, 1.0, 0.0)) );
            glm::quat temp2 = glm::normalize( glm::quat((GLfloat)( -Input1.MouseMove.y  * mouse_sens * time_step), dir_norm) );
     
            Camera1.SetCameraDirection(temp2 * (temp1 * Camera1.GetCameraDirection() * glm::inverse(temp1)) * glm::inverse(temp2));
    this code is not
    Code :
            glm::quat temp1 = glm::normalize( glm::quat((GLfloat)( -Input1.MouseMove.x  * mouse_sens * time_step), glm::vec3(0.0, 1.0, 0.0)) );
            glm::quat temp2 = glm::normalize( glm::quat((GLfloat)( -Input1.MouseMove.y  * mouse_sens * time_step), dir_norm) );
     
            glm::quat temp3 = temp2 * temp1;
            Camera1.SetCameraDirection(temp3 * Camera1.GetCameraDirection() * glm::inverse(temp3));
    The two pieces of code, from my understanding of glm, should produce the same result. However, they are not. The first piece of code produce expected result. In the second piece of code when i move the mouse I get extremely small movements in an apparently random direction.

    Why I cannot multiply quaterions successfully? Am I using GLM in a wrong way?

  2. #2
    Member Regular Contributor nickels's Avatar
    Join Date
    Feb 2000
    Location
    Colorado
    Posts
    282
    Try:

    Code :
    glm::quat temp3 = glm::normalize(temp2 * temp1);

    (ab)^-1=(ab)*/||ab||^2=b*a*/||ab||^2
    which is not equal to
    b^-1 a^-1 = b*a* /(||b||^2||a||^2) unless when ||ab||=||a||=||b||=1

  3. #3
    Member Regular Contributor nickels's Avatar
    Join Date
    Feb 2000
    Location
    Colorado
    Posts
    282
    Quote Originally Posted by nickels View Post
    Try:

    Code :
    glm::quat temp3 = glm::normalize(temp2 * temp1);

    (ab)^-1=(ab)*/||ab||^2=b*a*/||ab||^2
    which is not equal to
    b^-1 a^-1 = b*a* /(||b||^2||a||^2) unless when ||ab||=||a||=||b||=1
    Actually, I think this is wrong since ||ab||^2=(ab)(ab)*=abb*a*=-aba*b*=--aa*bb*=||a||^2||b||^2=1x1=1

  4. #4
    Member Regular Contributor nickels's Avatar
    Join Date
    Feb 2000
    Location
    Colorado
    Posts
    282
    I would make sure that Camera1.SetCameraDirection takes a const reference to a quaternion and that Camera1.GetCameraDirection() returns a quaternion by value (weird things could happen if it returns a reference...).
    No more ideas....

  5. #5
    Super Moderator Frequent Contributor Groovounet's Avatar
    Join Date
    Jul 2004
    Posts
    935
    Quote Originally Posted by nickels View Post
    I would make sure that Camera1.SetCameraDirection takes a const reference to a quaternion and that Camera1.GetCameraDirection() returns a quaternion by value (weird things could happen if it returns a reference...).
    No more ideas....
    The issue was that mamo had wrong expectation for the quaternion constructors. It doesn't take an axis and an angle but there is a function for that called angleAxis.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •