【发布时间】:2012-07-13 09:40:31
【问题描述】:
在 OpenGL ES 1 for android 中,我有一个由 27 个较小的立方体组成的 Rubic 立方体。我想要导致特定小立方体正好在视点前面的旋转。所以我需要两个向量。一个是从对象的原点到特定立方体的向量。另一个是从原点到视点的向量。然后它们的叉积给我旋转轴,点积给我角度。
我将 (0,0,1)(从原点到世界坐标中的视点的向量)转换为对象坐标。这是代码:
matrixGrabber.getCurrentModelView(gl);
temporaryMatrix.set(matrixGrabber.mModelView);
inputVector[0] = 0f;
inputVector[1] = 0f;
inputVector[2] = 1f;
inputVector[3] = 1f;
Matrix.multiplyMV(resultVector, 0, temporaryMatrix.InvertMatrix(), 0, inputVector,0);
resultVector[0]/=resultVector[3];
resultVector[1]/=resultVector[3];
resultVector[2]/=resultVector[3];
inputVector = ..... // appropriate vector due to user-selection
axis = Vector.normalized(Vector.crossProduct(Vector.normalized(inputVector), Vector.normalized(resultVector)));
degree = (float)Math.toDegrees(Math.acos(Vector.dot(Vector.normalized(inputVector), Vector.normalized(resultVector))));
我使用两个四元数进行旋转。每次用户选择一个动作时,应该发生一个旋转。这是代码:
Quaternion currentRotation = new Quaternion();
Quaternion temporaryRotation = new Quaternion();
.
.
.
currentRotation = (currentRotation).mulLeft(temporaryRotation.set(axis, degree));
currentRotation.toMatrix(matrix);
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glMultMatrixf(matrix, 0);
现在的问题是它在第一次旋转时效果很好。无论第一次轮换是什么。它运作良好,但对于下一次旋转,它似乎得到了错误的轴和度数。
例如,如果坐标系是
- X-right (1,0,0)
- 向上 (0,1,0)
- Z-in (0,0,1)
然后第一次绕 X 90 度逆时针 (CCW) 旋转产生
- X'-右 (1,0,0)
- Y'-in (0,0,1)
- Z'-down (0,-1,0)
第二次绕 Z 旋转 90 度 CCW 产生
- X'-in (0,1,0)
- Y'-左 (-1,0,0)
- Z'-down (0,-1,0)
但我期待
- 向上 (0,1,0)
- Y-in (0,0,1)
- Z-right(1,0,0)
我认为问题在于 resultVector(我使用的第二个从原点到视点的向量)没有正确转换。任何人都知道如何将世界坐标转换为对象坐标?有谁知道物体旋转后如何确定物体坐标?
【问题讨论】:
-
我不能说我完全理解你的问题并且你没有发布任何输出。告诉我这是否是你的场景(尝试制作它):如果你的坐标系是 X-right (1,0 ,0), Y-up(0,1,0), Z-in(0,0,1) 然后首先应用围绕 X 90 度逆时针 (CCW) 的旋转结果应该是 X'-right(1,0 ,0), Y'-in(0,0,1), Z'-down(0,-1,0) 然后围绕 Z 90 度逆时针旋转现在你得到的是 X'-in(0,1, 0), Y'-左(-1,0,0), Z'-下(0,-1,0)。而你期望 X-up(0,1,0), Y-in(0,0,1), Z-right(1,0,0) 这是否描述了发生了什么?
-
@MaticOblak 是的,是的,你完全正确。这准确地描述了发生的事情。我该怎么办?为什么会这样?
-
@MaticOblak 我编辑了问题并在其中添加了您的示例。
-
@RaminZahedi 非常古老的问题,但无论如何我用我的方法添加了答案,希望它可以帮助某人......