我正在试验 threeJS,我已经定位了一个相机并查看场景的原点 (0,0,0)。我想以设定的距离(半径)围绕 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);
参见 http://en.wikipedia.org/wiki/Rotation_matrix#In_three_dimensions对于绕 x、y 和 z 轴旋转的等效矩阵。
您也可以调整 TrackballControls 以使用键盘而不是鼠标。
默认轨迹球行为:http://mrdoob.github.com/three.js/examples/misc_camera_trackball.html
注意:我还没有测试这个——我在工作。
关于javascript/threejs - 围绕中心 y 轴(在 3D 空间中)在圆中移动对象的方程式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10341581/