2017-03-03 131 views
0

我对这个问题没有丝毫的理解,因为如果你看看我制作的下面的视频,它是自我解释的。3D摄像头X轴旋转

无角 - https://www.youtube.com/watch?v=R_GtaXWpP9c&feature=youtu.be

角度 - https://www.youtube.com/watch?v=RoaZMccGYGo&feature=youtu.be

正如你可以在任何角度版本中看到,您可以旋转Y轴完全正常的,但是当你控制X轴(如中所看到的'角度'视频),你以一种奇怪的方式旋转,这是有道理的。我想,正如你可能已经猜到的那样,X轴保持不变,而Y轴从左到右继续移动,我不希望Y轴从正向负向下降。

我希望这是有道理的,希望有一个很好的解释,我可能错过了一个拥有所有公式的网站,但我不知道这个'问题'被称为什么。

我旋转脚本的一些代码:

var pressed = [0,0,0,0,0,0,0,0]; 
 
function update() { 
 
    $('#w').html(pressed); 
 
} 
 
setInterval(update, 100); 
 
$(document).keydown(function(evt) { 
 
    if (evt.which == 87) { 
 
     pressed[0] = 1; 
 
    } 
 
    if(evt.which == 68) { 
 
     pressed[1] = 1; 
 
    } 
 
    if(evt.which == 65) { 
 
     pressed[2] = 1; 
 
    } 
 
    if(evt.which == 83) { 
 
     pressed[3] = 1; 
 
    } 
 
    if(evt.which == 39) { 
 
     pressed[4] = 1; 
 
    } 
 
    if(evt.which == 37) { 
 
     pressed[5] = 1; 
 
    } 
 
    if(evt.which == 38) { 
 
     pressed[6] = 1; 
 
    } 
 
    if(evt.which == 40) { 
 
     pressed[7] = 1; 
 
    } 
 
    //console.log(evt.which); 
 
}); 
 
$(document).keyup(function(evt) { 
 
    if (evt.which == 87) { 
 
     pressed[0] = 0; 
 
    } 
 
    if(evt.which == 68) { 
 
     pressed[1] = 0; 
 
    } 
 
    if(evt.which == 65) { 
 
     pressed[2] = 0; 
 
    } 
 
    if(evt.which == 83) { 
 
     pressed[3] = 0; 
 
    } 
 
    if(evt.which == 39) { 
 
     pressed[4] = 0; 
 
    } 
 
    if(evt.which == 37) { 
 
     pressed[5] = 0; 
 
    } 
 
    if(evt.which == 38) { 
 
     pressed[6] = 0; 
 
    } 
 
    if(evt.which == 40) { 
 
     pressed[7] = 0; 
 
    } 
 
}); 
 
function check_pressed() { 
 
    if(pressed[0] == 1){ 
 
     camera.position.z = camera.position.z - 0.1; 
 
    } 
 
    if(pressed[1] == 1){ 
 
     camera.position.x = camera.position.x + 0.1; 
 
    } 
 
    if(pressed[2] == 1){ 
 
     camera.position.x = camera.position.x - 0.1; 
 
    } 
 
    if(pressed[3] == 1){ 
 
     camera.position.z = camera.position.z + 0.1; 
 
    } 
 
    if(pressed[4] == 1){ 
 
     if(camera.rotation.y <= Math.PI * -2) { 
 
      camera.rotation.y = 0; 
 
     } else { 
 
      camera.rotation.y -= 0.03; 
 
     } 
 
    } 
 
    if(pressed[5] == 1){ 
 
     if(camera.rotation.y >= Math.PI * 2) { 
 
      camera.rotation.y = 0; 
 
     } else { 
 
      camera.rotation.y += 0.03; 
 
     } 
 
    } 
 
    if(pressed[6] == 1){ 
 
     if(camera.rotation.x >= 0.5) { 
 
      camera.rotation.x = 0.5; 
 
     } else { 
 
      camera.rotation.x += 0.01; 
 
     } 
 
    } 
 
    if(pressed[7] == 1){ 
 
     if(camera.rotation.x <= 0) { 
 
      camera.rotation.x = 0; 
 
     } else { 
 
      camera.rotation.x -= 0.01; 
 
     } 
 
    } 
 

 
} 
 
setInterval(check_pressed, 20);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<html> 
 
\t <head> 
 
\t \t <title>My first three.js app</title> 
 
\t \t <style> 
 
\t \t \t body { margin: 0; } 
 
\t \t \t canvas { width: 100%; height: 100% } 
 
\t \t </style> 
 
\t </head> 
 
\t <body> 
 
\t \t <script src="https://threejs.org/build/three.js"></script> 
 
\t \t <script src="js/jquery-3.1.1.min.js"></script> 
 
\t \t <script src="js/movement.js"></script> 
 
\t \t <script> 
 
\t \t \t var cubes = []; 
 
      var geometry = new THREE.BoxGeometry(1, 1, 1); 
 
      //var material = new THREE.MeshBasicMaterial({ color: 0x00ff00 }); 
 

 
\t \t \t var scene = new THREE.Scene(); 
 
\t \t \t var camera = new THREE.PerspectiveCamera(75, window.innerWidth/window.innerHeight, 0.1, 1000); 
 

 
\t \t \t var renderer = new THREE.WebGLRenderer(); 
 
\t \t \t renderer.setSize(window.innerWidth, window.innerHeight); 
 
\t \t \t document.body.appendChild(renderer.domElement); 
 

 
      for(i = 0; i < 4; i++) { 
 
       if(i == 0) { 
 
        material = new THREE.MeshBasicMaterial({ color: 0x63AEDB }); 
 
\t \t \t \t } else { 
 
        material = new THREE.MeshBasicMaterial({ color: 0x00ff00 }); 
 
\t \t \t \t } 
 
       cubes[i] = new THREE.Mesh(geometry, material); 
 
       scene.add(cubes[i]); 
 
      } 
 
      cubes[1].position.x = -2; 
 
      cubes[1].position.z = 2; 
 
      cubes[2].position.x = 2; 
 
      cubes[2].position.z = 2; 
 
      cubes[3].position.z = 4; 
 
\t \t \t camera.position.z = 2; 
 
\t \t \t camera.rotation.y = 0; 
 
\t \t \t //console.log(camera.rotation.x); 
 
\t \t \t var render = function() { 
 
\t \t \t \t requestAnimationFrame(render); 
 

 
\t \t \t \t //cubes[1].rotation.x += 0.00; 
 
\t \t \t \t //cube.rotation.y += 0.01; 
 

 
\t \t \t \t renderer.render(scene, camera); 
 
\t \t \t \t 
 
\t \t \t }; 
 
\t \t \t render(); 
 
\t \t </script> 
 
\t </body> 
 
</html>

+0

你如何旋转相机?你有一些代码要显示吗? – neeh

+0

你走了,有完整的代码。我做了一个小系统,它检查你是否按住按钮,这就是为什么这么小的系统有这么多的代码......我也对你可以走多远等做了一些限制。 –

+0

请参阅http: //stackoverflow.com/questions/32157515/threejs-x-rotation-behaving-unexpectedly/32158005#32158005。试验,直到你明白欧拉角如何在three.js中工作。 – WestLangley

回答

1

枢轴不是必需的。您的问题的简单解决方案是设置

camera.rotation.order = 'YXZ'; // the default is 'XYZ' 

然后,相机旋转将按照您的预期行事。

请参阅this answer了解更多信息。

更新后的提琴:https://jsfiddle.net/07g07uLa/1/

注意的小提琴,没​​有支点,而且也没有必要将相机添加到场景。

three.js r.84

+0

也谢谢你,我给了你最好的答案(对其他人没有冒犯性)。 –

+0

我不知道欧拉角的'订单'属性,谢谢! :) – neeh

+0

是的,@neeh提供了很好的答案! – WestLangley

1

你旋转相机在自己的坐标系

camera.rotation.x(或z)不为0,则ÿ轴变换,而不是向上指向了。这就解释了为什么轮转怪异。

Coordinate system of the camera modified

如果你希望你的Ÿ轴是世界的坐标系中,你可以添加一个THREE.Object3D充当支点为您camera的父母。

Camera rotation in world coordinates through a pivot

var pivot = new THREE.Object3D(); 
scene.add(pivot); 
pivot.add(camera); 

现在,您的相机的rotation.ypivot控制:

pivot.rotation.y += 0.03; 

...等是position

pivot.position.z = 2.0; 

我更新您的代码在this fiddle

+0

谢谢,很好的解释,你花时间修复,这是真棒!我肯定会自己做这个修复,所以我可以从中学习:D –

+0

谢谢,但是你应该选择[WestLangley](http://stackoverflow.com/users/1461008/westlangley)提供的解决方案, ;) – neeh

+0

是的,无论如何感谢您的帮助,并花时间编辑代码和制作图片等! –