Thread: optimizing my quaternion code

  1. #1
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916

    optimizing my quaternion code

    I got quaternions working correctly now, but the code seems awfully inefficient, even after I cleaned it up quite a bit.

    What it does:
    It accepts an amount to change a rotation on the x,y, or z axis. It then generates a temporary quaternion which represents just the new rotation. The temp quaternion is multiplied by the quaternion representing the cumulative effect of all the rotations on every axis. Then a rotation matrix is built from the temp quaternion to rotate the axes, and the axes are rotated with this matrix. (This keeps all the axes in object space) Anyway, it just seems like a lot of computation for each and every rotation. Can some people have a look at my transformation class and see if you can think of a better way to do that?
    Away.

  2. #2
    Banned
    Join Date
    Jan 2003
    Posts
    1,708
    I downloaded and looked at the code. To be perfectly honest the real reason I downloaded it was to get a better understanding of quaternions for myself (I've barely touched upon them) but it seems as though d3d handles all of the quaternion math behind the scenes. If I had any questions would you be able to answer them in PM for me?

    Anyway, it seems like you are doing way too much just for simple rotations. I would definitely suggest just learning the equation for doing polar coordinates on an arbitrary plane (I posted it in darksaidin's post). I've never had any problems with it to be completely honest, plus it seems easier to use than quaternions.

  3. #3
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    I can try to answer any questions you have about quaternions, and I can direct you to some of the stuff I just read about them, but you know - no guarantees about me being able to explain it all. I should be able to do it though.
    Away.

  4. #4
    Banned
    Join Date
    Jan 2003
    Posts
    1,708
    do you remember reading about conjugates when doing quaternions?
    EDIT:
    can u post the sites you read?
    Last edited by Silvercord; 08-10-2003 at 09:53 PM.

  5. #5
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    Yup, I remember the conjugates.

    Here are a couple links:
    http://skal.planet-d.net/demo/matrixfaq.htm
    http://www.magic-software.com/Docume...uaternions.pdf (that's a bit advanced)
    http://www.gamedev.net/reference/art...rticle1095.asp (quite good)

    Something which none of the tutorials I found mentioned was that you have to update your axes yourself. (This explanation might not make sense - look at my code) If you rotate on the X axis, and then want to rotate on the Y axis, you need to have kept track of which direction the Y axis is pointing if you're using quaternions. If you just rotate it on <0,1,0> the axes won't rotate with your model, which is a bit strange. Also, when you initialize your quaternion, before multiplying it by any rotation quaternions (again, look at my code), you'll have to initialize it to (1,0,0,0) (w,x,y,z) Ummm... I can't find the other things I printed to give you the urls to those, and I read a lot of stuff online and didn't save bookmarks, so if you want me to try to explain some stuff, just ask.
    Away.

  6. #6
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    Sorry to bump, but is there anyone else that knows enough about this to look at this code?
    Away.

  7. #7
    Banned
    Join Date
    Jan 2003
    Posts
    1,708
    try signing up at gametutorial's forum or gamedev

  8. #8
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    it seems in your code you are just doing rotations about the x, y, and z axis. The purpose of a quaternion is to represent the cumulative rotation matrices as a single rotation about a single axis. I dont know that much about quaternions yet but here is a definition that may help.


    If we consider all rotation matrices to represent a rotation of angle THETA about axis A(X, Y, Z), then the quaternion for the rotation would be:
    Q = (sX, sY, sZ, c)
    where:
    s = sin(THETA)
    c = cos(THETA)

    --GPG 3

  9. #9
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    Sure enough, Perspective. My code's doing that... but I have to keep the axes rotated. It really really seems like there should be a better way. If you're not sure what the program is doing right now, take a look at the link in my signature. I could (and did) do the rotations over <1,0,0>, <0,1,0> and <0,0,1> for the different axes, but then the axes didn't move with the cube...
    Away.

  10. #10
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    The problem has been solved. I should have paid more attention and given more thought to the sentence "Quaternion multiplication is not communitive"
    Away.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Proposal: Code colouring
    By Perspective in forum A Brief History of Cprogramming.com
    Replies: 28
    Last Post: 05-14-2007, 07:23 AM
  2. Optimizing my code
    By Lina in forum C Programming
    Replies: 30
    Last Post: 10-22-2006, 01:31 PM
  3. Explain this C code in english
    By soadlink in forum C Programming
    Replies: 16
    Last Post: 08-31-2006, 12:48 AM
  4. Updated sound engine code
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 11-18-2004, 12:38 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM