简体   繁体   English

3d几何:如何将对象与矢量对齐

[英]3d geometry: how to align an object to a vector

i have an object in 3d space that i want to align according to a vector. 我有一个3d空间中的对象,我想根据向量对齐。 i already got the Y-rotation out by doing an atan2 on the x and z component of the vector. 我已经通过在矢量的x和z分量上执行atan2来获得Y旋转。 but i would also like to have an X-rotation to make the object look downwards or upwards. 但我还希望进行X旋转以使对象向下或向上看。

imagine a plane that does it's pitch yaw roll, just without the roll. 想象一个飞机做它的俯仰偏转滚动,只是没有滚动。

i am using openGL to set the rotations so i will need an Y-angle and an X-angle. 我使用openGL来设置旋转,所以我需要一个Y角和一个X角。

I would not use Euler angles, but rather a Euler axis/angle. 我不会使用欧拉角,而是使用欧拉轴/角度。 For that matter, this is what Opengl glRotate uses as input. 就此而言,这就是Opengl glRotate用作输入的内容。

If all you want is to map a vector to another vector, there are an infinite number of rotations to do that. 如果您想要的只是将矢量映射到另一个矢量,那么可以进行无限次的旋转。 For the shortest one, (the one with the smallest angle of rotation), you can use the vector found by the cross product of your from and to unit vectors. 对于最短的一个(具有最小旋转角度的那个),您可以使用由fromto单位向量的叉积找到的向量。

axis = from X to

from there, the angle of rotation can be found from from.to = cos(theta) (assuming unit vectors) 从那里,旋转角度可以从from.to = cos(theta) (假设单位矢量)

theta = arccos(from.to)

glRotate(axis, theta) will then transform from to to . glRotate(axis, theta)然后将改变fromto

But as I said, this is only one of many rotations that can do the job. 但正如我所说,这只是可以完成这项工作的众多轮换中的一个。 You need a full referencial to define better how you want the transform done. 您需要一个完整的参考来更好地定义您希望如何完成转换。

You should use some form of quaternion interpolation ( Spherical Linear Interpolation ) to animate your object going from its current orientation to this new orientation. 您应该使用某种形式的四元数插值( 球面线性插值 )来动画对象从其当前方向到此新方向的动画。

If you store the orientations using Quaternions (vector space math), then you can get the shortest path between two orientations very easily. 如果使用四元数(向量空间数学)存储方向,则可以非常轻松地获得两个方向之间的最短路径。 For a great article, please read Understanding Slerp, Then Not Using It . 有关一篇好文章,请阅读了解Slerp,然后不使用它

If you use Euler angles, you will be subject to gimbal lock and some really weird edge cases. 如果你使用欧拉角,你将受到万向节锁和一些非常奇怪的边缘情况。

Actually...take a look at this article. 实际上......看看这篇文章。 It describes Euler Angles which I believe is what you want here. 它描述了Euler Angles,我相信这就是你想要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM