旋转三维空间中的向量向量、空间

2023-09-08 10:23:00 作者:我在未来等你

我正在在OpenGL ES中使用加速度计来计算改变特定轴的机器人项目,我的目标是我的旋转飞船状物体的运动矢量。问题是,我不明白背后的旋转矩阵数学。缺省运动矢量是0,1,0,装置+ Y,所以对象看起来向上在开始。我试图转动它的运动矢量,所以我可以将它指向的对象。我可以聚集在手机的旋转变化。 x轴:旋转[0],Y轴:旋转[1],Z轴:旋转[2]。使用如何旋转我的运动矢量旋转矩阵?

I am making an android project in opengl es that uses accelerometer to calculate change in specific axes and my aim is to rotate my spacecraft-like object's movement vector. The problem is that i can't understand the math behind rotation matrices. Default movement vector is 0,1,0 , means +y, so the object looks upward in the beginning. and i am trying to rotate its movement vector so i can move the object where it points. I can gather rotation changes in phone. x-axis : rotate[0], y-axis : rotate[1], z-axis : rotate[2]. How can i rotate my movement vector using rotation matrix ?

推荐答案

如果您要旋转,你应该建立什么是所谓的的旋转矩阵。

If you want to rotate a vector you should construct what is known as a rotation matrix.

假设你要旋转向量或点按θ,然后三角指出,新坐标

Say you want to rotate a vector or a point by θ, then trigonometry states that the new coordinates are


    x' = x cos θ - y sin θ
    y' = x sin θ + y cos θ

要演示这一点,让我们的X和Y的基数轴;当我们旋转X轴90°逆时针,我们应该结束了X轴转换成Y轴。 X轴作为一个单元,载体是

To demo this, let's take the cardinal axes of X and Y; when we rotate the X-axis 90° counter-clockwise, we should end up with the X-axis transformed into Y-axis. X-axis as a unit-vector is


    unit vector along X axis = <1, 0>
    x' = 1 cos 90 - 0 sin 90 = 0
    y' = 1 sin 90 + 0 cos 90 = 1
    New coordinates of the vector = <x', y'> = <0, 1> => Y-axis.

当你理解这一点,创建一个矩阵来做到这一点变得简单。甲矩阵仅仅是一个数学工具在一个舒适的,广义的方式执行此以便像旋转,缩放和平移(移动)各种变换可以组合并在一个单一步骤中进行,使用一种通用的方法。从线性代数,旋转在2D点或向量,矩阵待建是

When you understand this, creating a matrix to do this becomes simple. A matrix is just a mathematical tool to perform this in a comfortable, generalized way so that various transformations like rotation, scale and translation (moving) can be combined and performed in a single step, using one common method. From linear algebra, to rotate a point or vector in 2D, the matrix to be built is


    |cos θ   -sin θ| |x| = |x cos θ - y sin θ| = |x'|
    |sin θ    cos θ| |y|   |x sin θ + y cos θ|   |y'|

旋转的3D

这是工作在2D,而在3D领域,我们需要采取在考虑到第三轴。旋转围绕原点(一个点)在2D只是意味着旋转它绕Z轴(线)的三维矢量;由于我们绕Z轴的坐标应保持不变,即0°(在三维的XY平面上的旋转发生)。在三维旋转绕Z轴将

Rotation in 3D

That works in 2D, while in 3D we need to take in to account the third axis. Rotating a vector around the origin (a point) in 2D simply means rotating it around the Z-axis (a line) in 3D; since we're rotating around Z-axis its coordinates should kept constant i.e. 0° (the rotation happens on the XY plane in 3D). In 3D rotating around the Z-axis would be


    |cos θ   -sin θ   0| |x|   |x cos θ - y sin θ|   |x'|
    |sin θ    cos θ   0| |y| = |x sin θ + y cos θ| = |y'|
    |  0       0      1| |z|   |        z        |   |z'|

绕Y轴将


    | cos θ    0   sin θ| |x|   | x cos θ + z sin θ|   |x'|
    |   0      1       0| |y| = |         y        | = |y'|
    |-sin θ    0   cos θ| |z|   |-x sin θ + z cos θ|   |z'|

绕X轴将


    |1     0           0| |x|   |        x        |   |x'|
    |0   cos θ    -sin θ| |y| = |y cos θ - z sin θ| = |y'|
    |0   sin θ     cos θ| |z|   |y sin θ + z cos θ|   |z'|

请注意,该轴绕其旋转完成没有正弦或余弦的矩阵元素。我希望这是旋转的情况下清楚了。

Note that the axis around which rotation is done has no sin or cos elements in the matrix. I hope this makes the rotation case clear.

上述矩阵旋转对象仿佛对象是在从原点距离r =√(X 2 + Y 2);查找极坐标来知道为什么。该转动将是相对于所述世界空间原点。一般,我们需要旋转对象绕其自身的帧/枢轴,而不是周围的世界。因为不是所有的对象都在世界原点,利用这些矩阵不会给周围的对象自己的帧旋转的所需的结果旋转。因此,你需要了解的翻译了​​。你第一平移(移动)的目的是世界原点(以便在对象的原点将对准世界上,从而使使r = 0),与一种这些矩阵(或多个)执行的旋转,然后再次转换回其previous位置。该顺序的转换应用于事项的。

The aforementioned matrices rotate an object as if the object is at a distance r = √(x² + y²) from the origin; lookup polar coordinates to know why. This rotation will be with respect to the world space origin. Usually we need to rotate an object around its own frame/pivot and not around the world's. Since not all objects are at the world origin, rotating using these matrices will not give the desired result of rotating around the object's own frame. Hence you need to learn about translation too. You'd first translate (move) the object to world origin (so that the object's origin would align with the world's, thereby making r = 0), perform the rotation with one (or more) of these matrices and then translate it back again to its previous location. The order in which the transforms are applied matters.

我希望你了解线性和仿射变换和它们的成分在一杆执行多个转换,在code转换播放前。如果没有理解它背后的基本数学,调试转换将是一场噩梦。我发现这段视频是一个非常好的教程。

I urge you to read about linear and affine transformations and their composition to perform multiple transformations in one shot, before playing with transformations in code. Without understanding the basic maths behind it, debugging transformations would be a nightmare. I found this video to be a very good tutorial.

注意:执行回转这种方法符合欧拉角轮换制,这是简单的传授和掌握。这工作完全正常的2D和简单的3D案件;但是,当旋转需要围绕所有三个轴的同时要执行则欧拉角不足以此由于在本系统中它表现为万向锁。人诉诸四元的S在这种情况下,这是更高级的比这但从不会受到正确使用。当万向锁

Note: This method of performing rotations follows the Euler angle rotation system, which is simpler to teach and to grasp. This works perfectly fine for 2D and for simple 3D cases; but when rotation needs to be performed around all three axes at the same time then Euler angles are not sufficient for this due to an inherent deficiency in this system which manifests itself as Gimbal lock. People resort to Quaternions in such situations, which is more advanced than this but doesn't suffer from Gimbal locks when used correctly.