PDA

View Full Version : Trouble understanding QQuaternions



MattPhillips
20th April 2010, 03:15
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:


double vecx, vecy, vecz, angle, quatx, quaty, quatz;
... //The above arguments are initialized...
QVector3D vpre(vecx, vecy, vecz), vpost;
QQuaternion quat(angle*pi/180.0,quatx,quaty,quatz);
vpost = quat.rotatedVector(vpre);


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

norobro
21st April 2010, 01:46
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:
vpost = QQuaternion::fromAxisAndAngle(quatx,quaty,quatz,an gle).rotatedVector(vpre);
And you don't even have to convert your angle to radians.

MattPhillips
21st April 2010, 02:40
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