四元数旋转不作为作品除外 [英] Quaternion rotation do not works as excepted

查看:124
本文介绍了四元数旋转不作为作品除外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在OpenGL ES的1为Android,我有一个魔方,它由27个较小的立方体。我想旋转引起特别的小方块成为正好在观点的前面。所以我需要两个向量。一个是来自对象的原点到一特定立方体的载体。而另一个是来自原点视点向量。然后将它们的叉积带给我的旋转轴的点积带给我的角度。

In OpenGL ES 1 for android, I have a Rubic cube that consists of 27 smaller cubes. i want rotations which cause particular small cube becoming exactly in front of the viewpoint. so I need two vectors. one is the vector that comes from the origin of the object to a particular cube. and another is the vector that comes from origin to the viewpoint. then the cross product of them gives me the axis of the rotation and the dot product gives me the angle.

我转换(0,0,1)哪位是来自原点在世界coordinate-对象坐标的观点出发的载体。这里是code:

I convert the (0,0,1) -which is the vector that comes from the origin to the viewpoint in world coordinate- to object coordinates. here is the code:

    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))));

我用两个四元数的旋转。每次用户选择了轮换应该发生的动作之一。这里是code:

I use two Quaternions for rotations. each time user choose an action one of that rotations should happen. here is the code :

    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);

现在的问题是,它只是正常工作的第一旋转。无论第一旋转会。它运作良好,但下一次轮换,似乎它得到错误轴和学位。

now the problem is that it just works fine for the first rotation. whatever the first rotation would be. it works well but for the next rotations it seems that it gets wrong axis and degree.

例如,如果坐标系统将是

For example if the coordinate system would be


  • X-右(1,0,0)

  • Y-了(0,1,0)

  • Z-IN(0,0,1)

绕X轴90度逆时针(CCW),那么第一个旋转产生

then first rotation around X 90 degrees counter clockwise (CCW) produces


  • X'向右(1,0,0)

  • Y'式(0,0,1)

  • Z'倒(0,-1,0)

和绕Z轴90度逆时针第二旋转产生

and second rotation around Z 90 degrees CCW produces


  • X'-IN(0,1,0)

  • Y'左(-1,0,0)

  • Z'倒(0,-1,0)

但我希望


  • X-了(0,1,0)

  • Y-IN(0,0,1)

  • Z-右(1,0,0)

我认为问题在于resultVector(我使用的是来自原产地朝着观点的第二向量)不能正常转换。任何人都知道我怎么能转换成世界坐标到物体坐标?任何人都知道,我们怎么能确定何时对象轮换对象的坐标?

I think the problem is that the resultVector(the second vector which I used that comes from origin toward the viewpoint) does not convert properly. anyone knows how can I convert the world coordinate to object coordinate? anyone knows how can we determine object coordinates when object have rotated?

推荐答案

会发生什么事是,当你将此转换到模型(旋转你的情况),您也旋转它的基向量。想想看,如果你也想旋转你的坐标系或者如果你是从你的模型的第一人称视角看。每一个改变你做会影响下一个。

What happens is that when you apply this transform to your model (rotation in your case) you also rotate It's base vectors. Think of it as if you would also rotate your coordinate system or as if you were looking from the first person view of your model. Every transform you make will effect the next one.

既然你一般要保持自己的坐标系统,你可能要考虑你的移动摄像机周围的立方体而不是旋转立方体。我相信你可以找到一个的lookAt的方法无论是在你的API或Web的。它应该采取3个向量:cameraPosition,lookAtPoint,upVector。通过这种方法,你可以放置立方体(0,0,0),这也是你的lookAtPoint,第一cameraPosition应该像(0,0,-1)和第一upVector为(0,1,0)。现在的运动(你可能只使用左/右和上/下输入):
去向上/向下(你绕X轴旋转),你接下来要做以下内容:

Since you generally want to keep your own coordinate system you might want to consider moving your camera around the cube rather then rotate the cube. I am sure you can find a "lookAt" method either in your API or on web. It should take 3 vectors: cameraPosition, lookAtPoint, upVector. With this approach you could position the cube to (0,0,0) which is also your "lookAtPoint", first cameraPosition should be something like (0,0,-1) and first upVector to (0,1,0). Now for the movement (You probably only use left/right and up/down as input): To go up/down (your rotation around X) you next to do the following:

originalDistance = (cameraPosition-objectPosition).lenght
leftVector = normalizedVector(crossProduct(camearPosition, upVector))//generaly cameraPosition-objectPosition
camearPosition = cameraPosition + upVector*inputScalar //inputScalar should be a small floating value
cameraPosition = normalizedVector(cameraPosition)*originalDistance //put camera to original distance from object
upVector = normalizedVector(crossProduct(cameraPosition, leftVector))//generaly cameraPosition-objectPosition

去左/右(你绕X轴旋转),你接下来要做以下内容:

To go left/right (your rotation around X) you next to do the following:

originalDistance = (cameraPosition-objectPosition).lenght
leftVector = normalizedVector(crossProduct(camearPosition, upVector))//generaly cameraPosition-objectPosition
camearPosition = cameraPosition + leftVector*inputScalar //inputScalar should be a small floating value
cameraPosition = normalizedVector(cameraPosition)*originalDistance //put camera to original distance from object
leftVector = normalizedVector(crossProduct(cameraPosition, upVector))//generaly cameraPosition-objectPosition
upVector = normalizedVector(crossProduct(cameraPosition, leftVector))//generaly cameraPosition-objectPosition

这通常应该解决的问题。(恳求告诉我,如果我犯了一个错误,因为我硬写本)

This should generally solve the problem.. (pleas tell me if I made a mistake as I am writing this by hard)

至于你本身旋转物体的方法,你应该找出什么是对象自己的坐标系统和周围一个旋转的四元数。如果你有一些数学技能,也是很容易的。其他然后,你也可以只定义2角(X,Y),并直接改变他们通过投入和使用(1,0,0,X)和(0,1,0,Y)四元数,但有可能是有问题这个方法时,Y是90度。

As for your approach of rotating the object itself, you should find out what is your quaternion in object's own coordinate system and rotate it around that one. It is also quite easy if you have some math skills. Other then that you could also just define 2 angles (X,Y) and change them directly through input and use quaternions of (1,0,0,X) and (0,1,0,Y) but there might be problems with this approach when Y is 90 degrees..

我希望这有助于。

这篇关于四元数旋转不作为作品除外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆