首页 文章

Threejs相机旋转问题

提问于
浏览
1

我知道这是一个记录良好的问题,我已经进行了广泛的搜索,尝试了我找到的所有解决方案,但是,我无法完成最后一步 . 我有一个添加了相机的3D对象,我试图让第一人设置工作(相机的顺序设置为YXZ),当我尝试在x轴上旋转对象时,产生的运动是 . ..奇 . y旋转(即向左/向右看)工作正常 . 如果我在模拟开始时更改x旋转,向上和向下查看也可以正常工作,但只要我向左或向右看,然后尝试向上/向下看,它会以最奇怪的角度旋转,从而使场景成为现实螺旋 . 这是一些代码:

camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 1, 1000 );             

                neck = new THREE.Object3D();
                neck.rotateOnAxis(new THREE.Vector3(1, 0, 0), degInRad(90));
                neck.up = new THREE.Vector3(0, 0, 1);
                neck.position.z = 10;
                neck.position.y = -5;
                neck.add(camera);
                scene.add(neck);

                if (leftPressed) {
                        neck.rotation.y += degInRad(1);  //look left
                } else if (rightPressed) {
                    neck.rotation.y -= degInRad(1);      //look right
                }
                if (upPressed) {
                    neck.rotation.x += degInRad(1);      //look up
                } else if (downPressed) {
                    neck.rotation.x -= degInRad(1);      //look down
                }

edit 我've read a few more github questions on this very problem and understand the problem with rotation with the order. I guess or more pointed version of my question is: how do I change lookAt() (or using any other method) so that change the x rotation doesn' t影响y旋转?

1 回答

  • 0

    如何为颈部创建另一个节点(Object3D) . 这个想法是使用两个分开的坐标进行旋转 . 然后,您可以在y轴旋转新节点并在x轴旋转颈部 . 提示:不要忘记将颈部添加到新的Object3d节点 .

    .......
    var neckNode = new THREE.Object3D();
    neckNode.add(neck);
    scene.add(neckNode);
    .......
    if (leftPressed) {
        neckNode.rotation.y += degInRad(1);  //look left
    } else if (rightPressed) {
        neckNode.rotation.y -= degInRad(1);  //look right
    }
    if (upPressed) {
        neck.rotation.x += degInRad(1);      //look up
    } else if (downPressed) {
        neck.rotation.x -= degInRad(1);      //look down
    }
    

    希望能帮助到你 .

相关问题