This topic is now archived and is closed to further replies.

Vector to angles position

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

There has to be an easy way to do this: I have an arbituary vector called vec(X,Y,Z). I have an unit vector vecUp(X = 0, Y = 1, Z = 0). How do I calculate the rotation angles so that the vecUp will equal to vec(X,Y,Z)? Code example would be the best. Thank you.

Share this post

Link to post
Share on other sites
The rotation angle is easy, using dot products:

dotproduct = vec.x * vecUp.x + vec.y * vecUp.y + vec.z * vecUp.z;
angle = acos(dotproduct);

That angle will be between 0 and 180 degrees I believe.

To rotate vecUp into vec, you have to rotate about an axis that is perpendicular to both vec and vecUp. Find the axis by calculating the cross product between vec and vecUp:

axis_of_rotation = aor = vecUp cross vec;

or, calculating,

[ i j k ]
aor = det| vecUp.x vecUp.y vecUp.z |
[ vec.x vec.y vec.z ]

("det" = determinant of matrix)

The "i" part of the result is the x component. the "j" part is the y component, and the "k" component is the z part.

This ends up being:

aor.x = vecUp.y*vec.z - vecUp.z*vec.y);
aor.y = vecUp.z*vec.x - vecUp.x*vec.z);
aor.z = vecUp.x*vec.y - vecUp.y*vec.x);

So then, rotate vecUp about the aor axis by angle (which is in radians) should give the result you want. You can turn that into a quaternion if you want. Otherwise, you''d really have to construct a full 3x3 rotation matrix to actually do the rotation.

Graham Rhodes
Senior Scientist
Applied Research Associates, Inc.

Share this post

Link to post
Share on other sites