Results 1 to 3 of 3

Thread: Trouble understanding QQuaternions

  1. #1
    Join Date
    Aug 2009
    Posts
    140
    Thanks
    22
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Trouble understanding QQuaternions

    Hi,

    I thought I understood the idea behind quaternions--they have the form <scalar, 3d vector>, where the scalar is an angle. Then where q = <alpha, qv> is a quaternion and v is a 3d vector, q * v * conjugate(q) produces v', which is the rotation of v around qv by angle alpha. The q*v*conj(q) is conveniently encapsulated in QVector3D QQuaternion::rotatedVector(QVector3D).

    Anyway, I clearly do *not* understand quaternions, or at least not QQuaternions. Here is the code I am using to do vector rotations:

    Qt Code:
    1. double vecx, vecy, vecz, angle, quatx, quaty, quatz;
    2. ... //The above arguments are initialized...
    3. QVector3D vpre(vecx, vecy, vecz), vpost;
    4. QQuaternion quat(angle*pi/180.0,quatx,quaty,quatz);
    5. vpost = quat.rotatedVector(vpre);
    To copy to clipboard, switch view to plain text mode 

    Now here is what happens, in pseudocode. NB I use degrees below for convenience, but my code converts them to radians as per above.

    vpre(1,0,0)
    quat(90,0,0,1)
    Expected: vpost = (0,1,0) //Rotation about z axis
    Actual: vpost = (1.4674, pi, 0)

    vpre(1,0,0)
    quat(90,1,0,0)
    Expected: vpost = (1,0,0) //vpre is the unit x vector, so this rotation shouldn't change anything
    Actual: vpost = (3.4674, 0, 0)

    vpre(1,0,0)
    quat(0,0,0,1)
    Expected: vpost = (1,0,0) //angle== 0, so expect no rotation
    Actual: vpost = (-1,0,0) //Rotated 180 degrees

    vpre(1,0,0)
    quat(0,0,0,5)
    Expected: vpost = (1,0,0) or (-1,0,0) given the above //The length of the vector shouldn't matter, just its direction
    Actual: vpost = (-25,0,0)

    vpre(1,0,0)
    quat(360,0,0,1) //Now, rotating in a full circle
    Expected: vpost = (1,0,0) or (-1,0,0) given the above
    Actual: vpost = (38.4784,12.5664,0)
    I've spent about a day trying to crack the system that is being used to calculated the rotation, with no luck. Could someone please please lead me out of my confusion?

    Matt

  2. #2
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Trouble understanding QQuaternions

    Ah, quaternion. Haven't seen that word in many moons. Brings back not very fond memories of my calculus days.

    Anyway I think the following statement is what you are looking for:
    Qt Code:
    1. vpost = QQuaternion::fromAxisAndAngle(quatx,quaty,quatz,angle).rotatedVector(vpre);
    To copy to clipboard, switch view to plain text mode 
    And you don't even have to convert your angle to radians.

  3. The following user says thank you to norobro for this useful post:

    MattPhillips (21st April 2010)

  4. #3
    Join Date
    Aug 2009
    Posts
    140
    Thanks
    22
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Trouble understanding QQuaternions

    Yes, that was exactly it, thank you very much. Thanks also to Jan on the OpenGL forum for also pointing this out to me.

    Best,
    Matt

Similar Threads

  1. Help understanding QWT Contour Plot
    By jwieland in forum Qwt
    Replies: 11
    Last Post: 7th December 2009, 06:47
  2. I need help understanding QGraphicsView
    By aarelovich in forum Qt Programming
    Replies: 13
    Last Post: 22nd July 2009, 20:02
  3. understanding QwtPlot::transform()
    By b.mourat in forum Qwt
    Replies: 1
    Last Post: 25th May 2008, 07:43
  4. Problems understanding QGraphicsScene
    By Bocki in forum Qt Programming
    Replies: 3
    Last Post: 15th February 2008, 12:43
  5. QThread - general understanding
    By soul_rebel in forum Qt Programming
    Replies: 10
    Last Post: 21st August 2007, 23:15

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.