# use of quaternions

This topic is 3668 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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?

##### Share on other sites
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.

##### Share on other sites
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

##### Share on other sites
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??

##### Share on other sites
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 :)

##### Share on other sites
Okay, Thanks. That is simpler that what I thought.

1. 1
2. 2
Rutin
21
3. 3
4. 4
A4L
15
5. 5

• 13
• 26
• 10
• 11
• 9
• ### Forum Statistics

• Total Topics
633737
• Total Posts
3013606
×