mhe*_*ers 5 javascript math 3d three.js
我正在尝试使用threeJS,并且我已经安装了一个摄像头并查看场景的原点(0,0,0).我希望以一定的距离(半径)绕y轴移动相机,同时保持其对原点的聚焦,但我不确定如何设置方程式.目前,我只是旋转物体本身,但我想转动相机.这是移动网格的代码:
function checkRotation(){
if (keyboard.pressed("left")){
mesh.rotation.y += .05;
}
if (keyboard.pressed("right")){
mesh.rotation.y -= .05;
}
}
Run Code Online (Sandbox Code Playgroud)
这里有一些移动相机的例子:
camera.position.x = ??? (移动其x位置的某个方程式)camera.position.z = ??? (移动其z位置的一些方程式)camera.lookAt(mesh.position);
你能提供的任何帮助都会很棒.谢谢!
Isk*_*rak 13
您可以沿以下行手动设置摄像机的位置:
// 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);
Run Code Online (Sandbox Code Playgroud)
有关围绕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
注意:我还没有测试过这个 - 我正在工作.