PDA

View Full Version : The most basic quaternion question...



Matt Phillips
04-20-2010, 08:19 AM
Hi,

I'm having difficulty implementing quaternions in Qt using their QQuaternion class, and haven't heard anything back yet from the community boards where I've posted my query. This is my first time trying to use quaternions so I want to see if the issue is just that I don't understand how they're supposed to work. I figured you guys would know.

All I want to do is to rotate a point in 3-space a given angular distance about an axis. So let

vector_pre = (1,0,0);

I.e. the unit vector along the x-axis. Now let's say I want to rotate it 90deg counterclockwise in the x-y plane. To do this, I create the following quaternion:

quat = (90,0,0,1);

The vector component is the unit vector along the z axis, normal to the x-y plane. Then let

vector_post = (quat * vector_pre) * conjugate(quat);

vector_pre gets reinterpreted as a quaternion with scalar part 0, multiplication and conjugacy are as described in Wikipedia. Then my expectation is that

vector_post == (0,1,0);

This is emphatically *not* what I'm getting with the corresponding Qt method (rotatedVector) so I want to see if I am just not understanding things at a basic level. (And it's not a radians vs. degrees issue.) Have I misunderstood the basics? Thank you--

Matt

Jan
04-20-2010, 08:33 AM
I'm definitely no expert on Quaternions and i don't know how Qt implements them, but here's what i think you are doing wrong:


Quaternions have 4 components. These 4 components do NOT correspond to "angle/axis" (e.g. angle/x/y/z). The maths behind Quaternions is "a bit" more complicated.

So when you create a quaternion by setting the 4 components manually, you need to pass quite different values to actually get a Quaternion that represents a rotation around an axis. On a related note, most maths libraries don't work with angles in degrees, but usually in radians, so passing "90" is most likely wrong, as well.

Most Quaternion libs have a function "CreateFromAxisAngle" or something like that. There you can use your intuitive representation of axis/angle and get the (most unintuitive) Quaternion back.

Also most libraries overload the multiplication between quaternion and vector (operator* (Quat, Vec)) and already implement the whole "rotate a vector" stuff in there. So usually you should not need to multiply your vector by the Quaternions conjugate, at all.


Now this is all without knowledge about Qt's Quaternion class and therefore could be entirely wrong. But those are the most likely errors that i can come up with.

Hope that helps,
Jan.

Matt Phillips
04-20-2010, 08:54 AM
Thank you so much, omitting the fromAxisAngle invocation was *exactly* the problem.

Best,
Matt

Jan
04-20-2010, 11:34 AM
Glad to here it :-)