0

I have spent the last few days working on a rubik's cube in Three JS. I am almost done with it, but am having a problem with proper nesting of groups. Basically I have all the cubies made up and when one of the center cubies on a face is clicked, all cubies on that face are added to a group and the group is rotated. The problem is when I try to readd the cubie to the scene they go back to their original location, not the new rotated one. Here is an example if the top face is clicked

else if(cubie.position.x == 0 && cubie.position.y == 12.5 && cubie.position.z == 0){
    for(var i = scene.children.length - 1; i >=0; i--){
        var cubie = scene.children[i];
        if(cubie.position.y == 12.5){
            group.add(cubie);
        }
    }
    group.rotateOnAxis(new THREE.Vector3(0,-1,0), Math.PI/2);
}

and here is where I try to readd the cubie back to the scene

for(var i = group.children.length - 1; i >= 0; i--){
    scene.add(group.children[i]);
}
scene.remove(group);

if I comment out both lines, then one complete rotation appears, but since the cubies are now apart of group I cant rotate them again. If I remove the first line, and leave remove(group) then obviously the row disappears, but if I leave both lines in, then the cube doesn't change at all as if when I readd the cubie to the scene it takes its original place back...I have tried everything I can think of. Any help would be greatly appreciated

I will post the entire code to pastebin if anyone needs it. http://pastebin.com/vj6LgR0r

  • See if this helps you: http://stackoverflow.com/questions/20089098/three-js-adding-and-removing-children-of-rotated-objects – WestLangley Dec 07 '13 at 21:20
  • Thank you very much! it was exactly what I needed. Is there any way to mark a comment as a correct answer so you can get some points? – Andrew Cooper Dec 07 '13 at 23:02
  • You can up-vote the answer that helped you. You can also delete this question if you want, since it is very user-specific. – WestLangley Dec 08 '13 at 01:46

0 Answers0