Quaternion question

This is a discussion on Quaternion question within the Game Programming forums, part of the General Programming Boards category; I have recently implemented the quaternion tutorial and everything seemed to be rotating ok until: I wanted to rotate about ...

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    5

    Quaternion question

    I have recently implemented the quaternion tutorial and everything seemed to be rotating ok until:
    I wanted to rotate about the X axis and then on the Z axis. But, when I rotate on the X axis the Z axis changes to being the Y axis and my rotation gets messed up (to compensate I would have to rotate on the Y axis).
    I didnt think this was supposed to happen with quaternions? Does anybody know how I can get over this problem as it has been slowly driving me mental.

  2. #2
    Banned
    Join Date
    Jan 2003
    Posts
    1,708
    you have probably implemented something incorrectly. post the math stuff and maybe ill compare it with mine

  3. #3
    Registered User
    Join Date
    Apr 2004
    Posts
    5

    doh!

    ok I am officialy a bit daft, I was going by the tutorial that confuted put up about the quaternions and he either got it wrong or deliberatly put a mistake in so that you would be forced to learn about it.
    Either way the order of multiplication was wrong when calculating the total rotation, I swapped them over and it works like a dream.
    The order of rotation, for how it is specified is:
    totalrot = localrot * totalrot, its actually the other way round!
    There are also a couple of mistakes in the matrix for conversion from quaternions to rotation matrix which I was able to correct through the medium of paper books (gasp!). Thanks anyway but I am sorted now, this has been a very useful site .

  4. #4
    Banned
    Join Date
    Jan 2003
    Posts
    1,708
    i fixed a problem with a quaternion camera tutorial on gamedev.net, it wasn't that site was it? in that case you could blame me

  5. #5
    Registered User
    Join Date
    Apr 2004
    Posts
    5

    Thumbs up Quat? What quat was that?

    no it was the tutorial on cprogramming about rotations, http://www.cprogramming.com/advtutorial.html:
    Rotations in Three Dimensions by Confuted and Silvercord.
    I worked through it from start to finish but found a couple of mistakes here and there that I had to work out, but I have to say it did me good its much better when you have to work out problems for yourself rather than being given the answer on a plate.

  6. #6
    Banned
    Join Date
    Jan 2003
    Posts
    1,708
    Please point out the errors/ inconsistencies so I can fix them.

    Thanks

  7. #7
    Registered User
    Join Date
    Apr 2004
    Posts
    5
    No probs.
    There were a couple of mistakes in the matrix first of all, I cant remember specifically so I will post the matrix that I am using, there were some numbers wrong and some +/- signs in the wrong place (I dont know how to put in superscripts btw so I just put (x*x) for x squared etc ). Also I am using openGL so this is all right hand coordinate system and openGL is column major.:

    M[0] = 1 - 2*(y*y) - 2*(z*z);
    M[1] = (2*x*y) - (2*w*z);
    M[2] = (2*x*z) + (2*w*y);
    M[3] = 0;

    M[4] = (2*x*y) + (2*w*z);
    M[5] = 1 - 2*(x*x) - 2*(z*z);
    M[6] = (2*y*z) - (2*w*x);
    M[7] = 0;

    M[8] = (2*x*z) - (2*w*y);
    M[9] = (2*y*z) + (2*w*x);
    M[10] = 1 - 2*(x*x) - 2*(y*y);
    M[11] = 0;

    M[12] = 0;
    M[13] = 0;
    M[14] = 0;
    M[15] = 1;

    Also I overloaded the * operator like sugested and the actual order became totalrot = totalrot * localrot, but i'm sure that can depend on how you actually perform the multiplication in the first place.

  8. #8
    Banned
    Join Date
    Jan 2003
    Posts
    1,708
    I'm not sure what's going on because I compared the tutorial quaternion to matrix, and gametutorials.com's quaternion to matrix, and they're equivalent. I don't exactly know what is going on. I'd like to take a look at your project. I wouldn't be surprised if something is flipped around somewhere and you don't know about it.

  9. #9
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    just for the sake of comparison here's my quaternion code (without unit quaternion optimizations for reasons unexplained so diagonal entries will obviously be different.) Even without the unit optimization i have different signage than your tut Silver.

    Code:
      matrix[0] = w2 + x2 - y2 - z2;
      matrix[1] = 2*x*y - 2*w*z;
      matrix[2] = 2*x*z + 2*w*y;
      matrix[3] = 0;
      matrix[4] = 2*x*y + 2*w*z;
      matrix[5] = w2 - x2 + y2 - z2;
      matrix[6] = 2*y*z - 2*w*x;
      matrix[7] = 0;
      matrix[8] = 2*x*z - 2*w*y;
      matrix[9] = 2*y*z + 2*w*x;
      matrix[10] = w2 - x2 - y2 + z2;
      matrix[11] = 0;
      matrix[12] = 0;
      matrix[13] = 0;
      matrix[14] = 0;
      matrix[15] = w2 + x2 + y2 + z2;

  10. #10
    Banned
    Join Date
    Jan 2003
    Posts
    1,708
    im actually right about this. mathematics for 3d game programming and computer graphics, first edition, page 72 backs up our tutorial (not technically mine). ours is also the same as gametutorial's code, but the book in my lap suffices. that either means
    1) you guys don't actually have working code
    2) you've flipped something else around somewhere else to compensate
    3) somehow they're equivalent

  11. #11
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    strange.

    someone at berkley does it the same way as me and Q too.
    http://www.cs.berkeley.edu/~laura/cs...uaternion.html

    [light_bulb] perhaps this is why.....
    The difference is weather or not you want to simulate the rotation of an object or the rotation of the camera around an object. They are simply negations of each other mathematicaly even though they are conceptually different

    ex) rotate an object to the right => the object is rotating to the right OR the camera is rotating to the left around the object.

  12. #12
    Banned
    Join Date
    Jan 2003
    Posts
    1,708
    I honestly do not know. weird.

  13. #13
    Registered User
    Join Date
    Apr 2004
    Posts
    5

    ??

    Thats pretty strange, I got my matrix in the end from 3D Game Engine Design by David H. Eberly. I'm not too shabby at maths, but I can only assume that the matrices we are using (assuming we are all using openGL) must be equivalent. All I know, is that the one I am using works, nothing is flipped and the objects rotate.
    I blame Microsoft, what can I say?

  14. #14
    Banned
    Join Date
    Jan 2003
    Posts
    1,708
    haha, why not, those commies!

    (as I am using windows xp )

    good luck, post demos when you get something cool that works.

  15. #15
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Not strange at all.

    p' = qp(q*) OR
    p' = (q*)pq depending on your definition of quaternion multiplication. I've seen two different forms, and the result is that quaternion concatenation takes place in opposite order.

    Firstly, if multiplication is defined as
    Code:
    result.w = w*a.w - x*a.x - y*a.y - z*a.z;
    result.x = w*a.x + x*a.w + z*a.y - y*a.z;
    result.y = w*a.y + y*a.w + x*a.z - z*a.x;
    result.z = w*a.z + z*a.w + y*a.x - x*a.y;
    Then p' = (q*)pq
    Else, if it's defined as
    Code:
    result.w = a.w*w - a.x*x - a.y*y - a.z*z;
    result.x = a.w*x + a.x*w + a.y*z - a.z*y;
    result.y = a.w*y - a.x*z + a.y*w + a.z*x;
    result.z = a.w*z + a.x*y - a.y*x + a.z*w;
    Then p' = qp(q*)

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Confuted/Blackrat: quaternion question
    By Silvercord in forum Game Programming
    Replies: 12
    Last Post: 08-18-2003, 06:02 PM
  4. Camera problem: rolling
    By darksaidin in forum Game Programming
    Replies: 37
    Last Post: 08-15-2003, 08:49 AM
  5. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21