2017-05-21 19 views
1

我在Three.js中創建了一個可玩的迷宮。所有的物體 - 地板,天花板和牆壁 - 都放置在正X軸和Z軸內。相機被放置這樣的:攝像頭控件與他們應該是相反的。爲什麼?

camera = new THREE.PerspectiveCamera(45, 
             window.innerWidth/window.innerHeight, 
             0.1, 
             1000 
    ); 
    camera.position.set(0, 0.25, 0); 

    // The camera is placed pointing to the nearest open path from the 
    // initial position. Check mazeGenerator.js for the reference of 
    // "coordinates". 
    if(grid[1][0] == coordinates.W) 
    { 
     camera.lookAt(new THREE.Vector3(1, 0.25, 0)); 
    } 
    else 
    { 
     camera.lookAt(new THREE.Vector3(0, 0.25, 1)); 
    } 

,並將其移動這樣的:

function keyboardCheck() 
{ 
    var movementSpeed = 0.02; 
    var rotationSpeed = 0.03; 

    if(keyboard.pressed('w') || keyboard.pressed('up')) 
    { 
     camera.translateZ(-movementSpeed); 
    } 

    if(keyboard.pressed('a') || keyboard.pressed('left')) 
    { 
     camera.rotateY(rotationSpeed); 
    } 

    if(keyboard.pressed('d') || keyboard.pressed('right')) 
    { 
     camera.rotateY(-rotationSpeed); 
    } 
} 

如果我把相機沒有lookAt功能,它似乎面臨[0, 0, 0],但當然是我想要的要做的是通過使相機面對最近的開放路徑來幫助玩家一點點。

爲了使它前進,我必須減少Z,因爲否則當你點擊「向前移動」鍵時,它會倒退。這讓我認爲,即使我告訴相機看一個方向,對象的Z軸與世界的Z軸相反。

問題本身就是:爲什麼會發生這種情況?

非常感謝您提前。

回答

1

相機低頭其負面-Z軸,這樣向前移動,這樣做:

camera.translateZ(- distance); 

所有其他對象都被認爲是朝着它們的正z軸方向看,所以爲了向前移動其他物體,您可以使用

object.translateZ(distance); 

three.js r.85

+0

克里斯塔爾清楚。謝謝 :) – MikelAlejoBR