use of quaternions

Started by
4 comments, last by alta 15 years, 4 months ago
If you have an object that has a location defined by a 3-vector and an orientation defined by a quaternion, how do you change the quanternion so as to rotate the object about an arbitrary axis by some angle? It seems like a staight forward question but I am having a hard time finding an answer. I am not too familiar with quaternions so maybe I am trying to do something that is not possible? Anyone know?
Advertisement
Here is a method that worked for me:

Create a quaternion from the axis of rotation and the angle of rotation. Note that this angle should indicate "how much to rotate", not the "total" rotation from the origin.

If this axis represents an axis in world space, you would do:

objectOrientation = rotationQuat * objectOrientation;

If the axis is in object space, you would do:

objectOrientation *= rotationQuat;

Remember that quaternion multiplication is "reversed" - q1 * q2 is the transformation represented by q2 followed by the transformation represented by q1.

This thread (starting roughly from that post) explains the reasoning behind this entire procedure.
Assuming your arbitrary axis is normalized, the corresponding quaternion for the rotation about the axis for an angle theta is:

w = cos(theta / 2)
x = sin(theta / 2) * axis.x
y = sin(theta / 2) * axis.y
z = sin(theta / 2) * axis.z
So if my object is at the origin with orientation (p1,p2,p3,p4)and I want to rotate it about the vector (x,y,z) an angle theta then I would calculate the following:

q1=cos(theta/2);
q2=sin(theta/2)*x;
q3=sin(theta/2)*y;
q4=sin(theta/2)*z;

p1=p1*q1-p2*q2-p3*q3-p4*q4;
p2=p1*q2+p2*q1+p3*q4-p4*q3;
p3=p1*q3-p2*q4+p3*q1+p4*q2;
p4=p1*q4+p2*q3-p3*q2+p4*q1;


Is that correct??
Quote:p1=p1*q1-p2*q2-p3*q3-p4*q4;
p2=p1*q2+p2*q1+p3*q4-p4*q3;
p3=p1*q3-p2*q4+p3*q1+p4*q2;
p4=p1*q4+p2*q3-p3*q2+p4*q1;
Assuming you've implemented quaternion multiplication correctly and that you're multiplying in the right order, then that should work. (I didn't check your multiplication code for correctness.)

Oh, and I'm guessing the above is just an example, but in your actual code, be sure not to overwrite the values of the original quaternion ('p', in this case) as you go :)
Okay, Thanks. That is simpler that what I thought.

This topic is closed to new replies.

Advertisement