# optimizing my quaternion code

• 08-10-2003
confuted
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?
• 08-10-2003
Silvercord
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.
• 08-10-2003
confuted
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.
• 08-10-2003
Silvercord
EDIT:
can u post the sites you read?
• 08-10-2003
confuted
Yup, I remember the conjugates.

http://skal.planet-d.net/demo/matrixfaq.htm
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.
• 08-14-2003
confuted
Sorry to bump, but is there anyone else that knows enough about this to look at this code?
• 08-16-2003
Silvercord
try signing up at gametutorial's forum or gamedev
• 08-16-2003
Perspective
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.

Quote:

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
• 08-16-2003
confuted
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...
• 08-16-2003
confuted
The problem has been solved. I should have paid more attention and given more thought to the sentence "Quaternion multiplication is not communitive" :eek: