2017-03-03 155 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

是的,無論如何感謝您的幫助,並花時間編輯代碼和製作圖片等! –