Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/464.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 基于rotation three.js更新相机位置_Javascript_Three.js - Fatal编程技术网

Javascript 基于rotation three.js更新相机位置

Javascript 基于rotation three.js更新相机位置,javascript,three.js,Javascript,Three.js,我正在尝试建立一个快速演示的东西,我需要在我的场景中移动的方向,它是看相机。它类似于指针锁定控件,但是我需要相机能够上下移动,以及向前/向后/左/右。目前,我只是对相机进行旋转,并将其添加到位置,但这并不会向前移动相机(与相机相对),它相对于你似乎朝着一个奇怪的方向移动,而这个方向会根据你看的地方而改变。这是我目前的代码: const rotation=this.internalCamera.rotation.toVector3(); 恒速=1; this.internalCamera.posi

我正在尝试建立一个快速演示的东西,我需要在我的场景中移动的方向,它是看相机。它类似于指针锁定控件,但是我需要相机能够上下移动,以及向前/向后/左/右。目前,我只是对相机进行旋转,并将其添加到位置,但这并不会向前移动相机(与相机相对),它相对于你似乎朝着一个奇怪的方向移动,而这个方向会根据你看的地方而改变。这是我目前的代码:

const rotation=this.internalCamera.rotation.toVector3();
恒速=1;
this.internalCamera.position.add(
新矢量3(
旋转.x*速度,
旋转。y*速度,
旋转速度
)
);

我有一种感觉,这与三层空间系统有关,但我不确定。我搞砸了什么?

你的矢量“旋转”表示角度,而不是坐标测量。您需要生成移动向量,然后将其与摄影机方向的旋转矩阵相乘

//x <- left/right  y <- up/down  z <- forward backward
const vector = new Vector(x, y, z);

//make matrices
const rotx = (new THREE.Matrix4).makeRotationX(camera.rotation.x);
const roty = (new THREE.Matrix4).makeRotationY(camera.rotation.y);
const rotz = (new THREE.Matrix4).makeRotationZ(camera.rotation.z);

//multiply all matrices together
const rotmat = rotx.multiply(roty).multiply(rotx).multiply(rotz);

//multiply vector by matrix
vector.applyMatrix4(rotmat);

//finally add the vector to position
camera.position.add(vector);

//x我想你认为旋转.x.z和.y是坐标测量,当它们是角度时,你需要得到适当的x、y和z分量你的相机方向向量下列任一项:
camera.translateZ(-distance)
camera.translateX(distance)
camera.translateY(distance)
camera.position.y+=距离