How to find orientation of rubik cube?

I'm trying to make a rubik cube game in webgl using three.js (you can try it here). And I have problems to detect on witch axis I have to rotate my cube according the rotation of the cube. For instance, if the cube is in original position/rotation, if I want to rotate the left layer from down to up, I must make a rotation on the Y axis. But I rotate my cube 90 degrees on Y, I will have to rotate It on the Z axis to rotate my left layer from down to up.

I'm trying to find a way to get the correct rotation axis according the orientation of the cube. For the moment I check witch vector of the axis of the rotation matrix of the cube is most parallel with the vector(0,1,0) if I want to move a front layer from down to up. But it do not works in edge cases like this for instance :

立方体方向

I guess there is some simple way to do that, but I'm not good enough in matrix and mathematical stuff :)


An AxisHelper can show the aixs of the scene which you could determine the orientation with.

var axishelper = new THREE.AxisHelper(40);
axishelper.position.y = 300;
scene.add(axishelper);

You could also log your cube and check the position and rotation properties with Chrome Developer Tools or Firebug.


You can store the orientation of each cube in its own 4x4 matrix (ie a "model" matrix) that tells you how to get from the cube's local coordinates to the world's coordinates. Now, since you want to rotate the cube around to an axis (ie vector) in world coordinates, you need to translate the axis into cube coordinates. This is exactly what the inverse of the model matrix yields.

链接地址: http://www.djcxy.com/p/81810.html

上一篇: 在3D空间中旋转矢量

下一篇: 如何找到魔方的方向?