简体   繁体   English

如何在 ThreeJS 中围绕其中心旋转一个组?

[英]How to rotate a group in ThreeJS around its center?

I am trying to build a working Rubikscube with ThreeJS .我正在尝试使用ThreeJS构建一个有效的魔方 Now I have a problem with rotating the sides.现在我在旋转侧面时遇到了问题。 At the moment, I am adding the smaller cubes to the Rubikscube and create it like this:目前,我正在将较小的立方体添加到魔方,并像这样创建它:

const rubikscube = new THREE.Group();
for (var i = 0; i < 27; i++) {
  // 3x3x3 (0,0,0) is on the top left corner
  var cube = createPartOfCube(i, scene);
  cube.name = i;
  cube.position.x = (i % 3) * gap;
  cube.position.y = Math.floor(i / 9) * gap;
  cube.position.z = (Math.floor(i / 3) % 3) * gap;
  rubikscube.add(cube);
}
scene.add(rubikscube);

And this all works fine until I try to rotate , eg the right side.这一切都很好,直到我尝试旋转,例如右侧 I select the pieces on the right side and add them to their own group .我将 select 右侧的部分添加到自己的组中。 Now, when I try to rotate the group, it's rotating around the x axes .现在,当我尝试旋转组时,它围绕 x 轴旋转 Here is the method I want to use for moving a side (just ignore the bool and eval part, its just for getting the right pieces):这是我想用来移动一侧的方法(只需忽略 bool 和 eval 部分,它只是为了获得正确的部分):

  function move(direction) {
    var bool = moves[direction];

    var pivot = new THREE.Group();
    pivot.updateMatrixWorld();

    for (var i = 0; i < 27; i++) {
      if (eval(format(bool, i))) {
        pivot.add(scene.getObjectByName(i));
      }
    }
    scene.add(pivot);
    animateMove(pivot);
  }

And animating it:并对其进行动画处理:

  function animateMove(pivot) {
    requestAnimationFrame(() => {
      animateMove(pivot);
    });
    // missing part
    renderer.render(scene, camera);
  }

What I've tried:我试过的:

I have tried different methods, to rotate it the right way but nothing worked and moving the cube is also not the right answer.我尝试了不同的方法,以正确的方式旋转它,但没有任何效果,移动立方体也不是正确的答案。

One thing I tried was on this thread , but when I tried to rotate it this way, the side just moved , so it's still rotating around the x-axes .我尝试过的一件事是在这个线程上,但是当我尝试以这种方式旋转它时,侧面刚刚移动,所以它仍然围绕 x-axes 旋转

I hope that someone understands my issue and helps me to solve it.我希望有人理解我的问题并帮助我解决它。 Thanks!谢谢!

All rotations take place around the object's point of origin.所有旋转都围绕对象的原点进行。 So if your object's position is at (0, 0, 0), then that's going to be the pivot point.因此,如果您的对象的 position 位于 (0, 0, 0),那么这将是 pivot 点。

I recommend you nest your cube pieces into a THREE.Group() each, that way you can keep the pivot point constant at (0, 0, 0) for all 27 pieces.我建议您将立方体块嵌套到一个THREE.Group()中,这样您就可以将所有 27 块的 pivot 点保持在 (0, 0, 0) 不变。 Then you can displace each piece inside its group to the desired position:然后您可以将其组内的每个部分替换为所需的 position:

const geom = new THREE.BoxGeometry(1, 1, 1);
const Piece = new THREE.Mesh(geom, mat);
const Group = new THREE.Group();

// Nest Piece inside of its Group
Group.add(Piece);

// Parent Group position is at (0, 0, 0)
// Inner piece position is at (-1, -1, -1)
Piece.position.set(-1, -1, -1);

// Now we can apply rotations to the parent group 
// and the child will maintain its distance from the center
Group.rotation.set(Math.PI / 2, 0, 0);

You'll want to repeat this for all values of [-1, 0, +1] for X,Y,Z .您需要对X,Y,Z[-1, 0, +1]的所有值重复此操作。 That's 3^3 = 27 pieces.那是 3^3 = 27 件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM