问题描述
给定一个点,如(0,0,0)和类似的(X,Y,Z)的载体。什么是对准负Z轴为中心的(0,0,0),以在该矢量的方向指向的最简单的方法是什么?使用OpenGL的例子会受到欢迎,但不是neccessary。
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.
推荐答案
有许多不同的方式来旋转的坐标帧,以给定的方向指向;他们将全部离开z轴指向你想要的方向,但变化是如何的X轴和Y轴取向。
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.
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 );
}
这篇关于什么是一个矢量对准Z轴的最简单的方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!