Three.js立方体-旋转到任意一侧

Three.js Cube - Spin to random side

本文关键字:任意一 旋转 js 立方体 Three      更新时间:2023-10-26

我有一个有六个不同边的Three.js立方体。

我写了一个随机数生成器,shich生成1-6之间的数字。单击按钮,我想将立方体旋转到一边(数字)。

史泰龙的小提琴(http://jsfiddle.net/majman/6vrH8/)看起来不错,但对我来说不起作用,因为我不知道我必须把立方体吼到特定的一边。。。。

我的尝试是:

   mesh.rotation.x =0;
     mesh.rotation.y =0;
    //face2
     mesh.rotation.x =0;
     mesh.rotation.y =1.571;
    //face3
     mesh.rotation.x =0;
     mesh.rotation.y =3.142;
     ...
     mesh.rotation.x =0;
     mesh.rotation.y =4.714;
     ...
     mesh.rotation.x =1.571;
     mesh.rotation.y =0;
     ...
     mesh.rotation.x =-1.571;
     mesh.rotation.y =0;

然后我想+=或-=旋转到上面的数字。问题是,几轮之后,mesh.rotation.x/y太高(100+),需要很长时间才能到达面。

我能做什么?小提琴在这儿:https://jsfiddle.net/crinca15/2o8n3xox/

更新:我认为存在误解。。。单击"开始"按钮,立方体开始旋转。在停止按钮上,立方体应该从1-6(随机)转到一侧。我在上面写的面部表情。

现在我可以用于(mesh.rotation.x == wanted.rotation.x; mesh.rotation.x -= 0.02)

问题是,如果网格旋转值很高,立方体需要沿着时间和多次旋转才能旋转到位置。。。

根据这一点,

看起来是正确的,但如果值不断增加,听起来就像是在循环中重复调用此代码。确保仅在需要时应用一次旋转。

还要确保x和y值保持在0 <= value < Math.PI * 2范围内。