• ### Announcements

#### Archived

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

# Vector to angles position

## 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 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.

• ### Forum Statistics

• Total Topics
627731
• Total Posts
2978831

• 10
• 9
• 21
• 14
• 12