水平和垂直旋转相机three.js

时间:2016-08-24 22:52:17

标签: javascript three.js rotation

我有以下代码,当我按下一个键时,我的相机会垂直旋转(这很好用):

var cameraOrginX = 0, cameraOrginY = 0, cameraOrginZ = 0;
var cameraEndX = 0, cameraEndY = -707, cameraEndZ = 707;
var angle = 1;

function onDocumentKeyDown(event){
    var keyCode = event.which;
    console.log(keyCode);

    if(keyCode == 87 || keyCode == 83){
        if (keyCode == 87) {
            angle = 1;
            console.log("forward  " + angle);
        }
        else if (keyCode == 83) {
            angle = -1;
            console.log("backward  " + angle);
        }
    }
    if(keyCode == 68 || keyCode == 65){
        if (keyCode == 68) {
            angle = 1;
            console.log("right  " + angle);
        }
        else if (keyCode == 65) {
            angle = -1;
            console.log("left  " + angle);
        }
    }

    //rotate vertically
    var radiansVert = angle * Math.PI / 180.0;
    var radiansHori = angle * Math.PI / 180.0;

    var cosTheta = Math.cos(radiansVert);
    var sinTheta = Math.sin(radiansVert);

    var aVert = cosTheta * (cameraEndY-cameraOrginY);
    var bVert = sinTheta * (cameraEndZ-cameraOrginZ);
    var cVert = sinTheta * (cameraEndY-cameraOrginY);
    var dVert = cosTheta * (cameraEndZ-cameraOrginZ);

    cameraEndY = aVert - bVert + cameraOrginY;
    cameraEndZ = cVert + dVert + cameraOrginZ; 

    camera.position.x = cameraOrginX + cameraEndX;
    camera.position.y = cameraOrginY + cameraEndY;
    camera.position.z = cameraOrginZ + cameraEndZ;
    //
}

但现在我想让我的相机也水平旋转...我试着复制并粘贴注释之间的代码并更改变量名称,以便它可以在x和y轴上工作,但这给了我一些奇怪的结果是什么我真的不是在寻找;)有人可以给我一个提示或指出我正确的方向如何做到这一点?我还在学习,任何帮助都会受到很大关注:)

如果需要更多代码让我的问题清楚,请告诉我如果你愿意,我也可以制作一个jsfiddle!

0 个答案:

没有答案