我正在尝试ThreeJS,并且已经放置了一个摄像头,并查看了场景的原点(0,0,0)。我想在设定焦点(原点)的同时绕着y轴以一定的距离绕y轴移动一圈,但仍不确定如何建立方程式。目前,我只是旋转对象本身,但我想改为旋转相机。这是我移动网格的代码:

function checkRotation(){
    if (keyboard.pressed("left")){
        mesh.rotation.y += .05;
    }

    if (keyboard.pressed("right")){
        mesh.rotation.y -= .05;
    }
}

这是移动摄像机的一些示例:

camera.position.x = ??? (一些方程式可移动其x位置)
camera.position.z = ??? (一些方程式可移动其z位置)
camera.lookAt(mesh.position);

您可以提供的任何帮助都会很棒。谢谢!

最佳答案

您可以沿着以下几行手动设置摄像机的位置:

// let theta be the amount you want to rotate by
var x = camera.position.x;
var z = camera.position.z;

camera.position.x = x * Math.cos(theta) + z * Math.sin(theta);
camera.position.z = z * Math.cos(theta) - x * Math.sin(theta);
camera.lookAt(mesh.position); // or camera.lookAt(0, 0, 0);

有关绕x,y和z轴旋转的矩阵等效项,请参见http://en.wikipedia.org/wiki/Rotation_matrix#In_three_dimensions

您也许还可以使TrackballControls适应使用键盘而不是鼠标。

默认轨迹球行为:http://mrdoob.github.com/three.js/examples/misc_camera_trackball.html

注意:我尚未对此进行测试-我正在工作。

10-08 08:28
查看更多