Given a point such as (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) to point in the direction of this vector? Examples using OpenGL would be welcome, but not neccessary.
相关问题
- Extract matrix elements using a vector of column i
- Is GLFW designed to use without LWJGL (in java)?
- Reshape matrix by rows
- glDrawElements only draws half a quad
- Non-Conformable Arrays when Doing Matrix Multiplic
相关文章
- Numpy matrix of coordinates
- Converting glm::lookat matrix to quaternion and ba
- Algorithm for partially filling a polygonal mesh
- How do I get characters common to two vectors in C
- Robust polygon normal calculation
- Behavior of uniforms after glUseProgram() and spee
- Keep constant number of visible circles in 3D anim
- How can I unpack (destructure) elements from a vec
You probably want to have a look at Diana Gruber's article
There are lots of resources out there about rotating your coordinates (or rotating objects, which amounts to the same thing). I learnt a lot from this site, both about how to program in multiple dimensions and especially how to manipulate vectors
To answer my own question, the best answer I've come up with is this:
Divide the vector up into "components". The x component is the displacement along the x axis. If we turn to trigonometry, we have that cos(alpha) = x / vector_magnitude. If we compute the RHS then we can derive alpha, which is the amount by which we'd have to rotate around the y axis.
Then the coordinate system can be aligned to the vector by a series of calls to glRotatef()
There's lots of different ways to rotate a coordinate-frame to point in a given direction; they'll all leave the z-axis pointed in the direction you want, but with variations in how the x- and y-axes are oriented.
The following gets you the shortest rotation, which may or may not be what you want.
The page here has a section "Transformations for moving a vector to the z-axis" that seems to be what you want, or perhaps the inverse of it.