What is the easiest way to align the z axis with a vector?

Given a point like (0, 0, 0) and a vector like (x, y, z). What is the easiest way to align the negative Z axis centered at (0, 0, 0) in the direction of this vector? OpenGL examples are welcome, but not required.

+3
source share
5 answers

There are many ways to rotate a coordinate frame to a point in a given direction; they all leave the z axis directed in the direction you need, but with variations in the orientation of the x and y landmarks.

The following is a brief rotation, which may or may not be what you want.

vec3 target_dir = normalise( vector ); float rot_angle = acos( dot_product(target_dir,z_axis) ); if( fabs(rot_angle) > a_very_small_number ) { vec3 rot_axis = normalise( cross_product(target_dir,z_axis) ); glRotatef( rot_angle, rot_axis.x, rot_axis.y, rot_axis.z ); } 
+13
source

To answer my own question, the best answer I came up with is this:

Divide the vector into “components." Component x represents the offset along the x axis. If we turn to trigonometry, then we have cos (α) = x / vector_magnitude. If we calculate RHS, then we can get alpha, which is the sum by which we have to rotate around the y axis.

Then the coordinate system can be aligned with the vector by a series of calls to glRotatef ()

+1
source

You probably want to see Diana Gruber's article

+1
source

There are many resources related to the rotation of your coordinates (or rotating objects, which is the same thing). I learned a lot from this site about how to program in several dimensions, and especially how to manipulate vectors

0
source

The page here contains a section called “Transformations for moving a vector to the z axis,” which looks like what you want, or possibly vice versa.

0
source

All Articles