Vector to angles position
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.
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,
("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:
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.
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,
|
("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:
|
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.
Graham Rhodes Moderator, Math & Physics forum @ gamedev.net
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement