将 Z 轴与向量对齐的最简单方法是什么?
给定一个点(如 (0, 0, 0))和一个向量(如 (x, y, z))。 对齐以 (0, 0, 0) 为中心的负 Z 轴以指向该向量的方向的最简单方法是什么? 使用 OpenGL 的示例将受到欢迎,但不是必需的。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
有很多不同的方法可以旋转坐标系以指向给定方向; 它们都会使 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.
您可能想看看Diana Gruber 的文章
You probably want to have a look at Diana Gruber's article
为了回答我自己的问题,我想出的最佳答案是:
将向量划分为“分量”。 x 分量是沿 x 轴的位移。 如果我们转向三角学,我们有 cos(alpha) = x / vector_magnitude。 如果我们计算 RHS,那么我们就可以推导出 alpha,它是我们必须绕 y 轴旋转的量。
然后可以通过一系列调用 glRotatef() 将坐标系与向量对齐
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 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
页面此处有一个部分“将向量移动到 z 的变换” - 轴”
似乎是你想要的,或者也许是相反的。
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.