使用 ThreeJS 将相机朝着它所面对的方向移动 [英] Moving the camera in the direction it's facing, with ThreeJS

查看:23
本文介绍了使用 ThreeJS 将相机朝着它所面对的方向移动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个摄像头:

{
  x: 10,
  y: 30,
  z: 20
}

它的旋转是:

{
  x: 0.1,
  y: 0.2,
  z: 0.3
}

我想将摄像机从当前位置向其面向的方向移动 1 个单位.

I want to move the camera 1 unit from it's current position in the direction it's facing.

如何计算摄像机的新位置?

How do I do calculate the cameras new position?

我正在使用 ThreeJS.

I'm using ThreeJS.

推荐答案

THREE.Camera 类扩展了 THREE.Object3D 类.您可以使用 getWorldDirection 方法获取相机方向来自基类:

The THREE.Camera class extends the THREE.Object3D class. You can get the camera direction with the getWorldDirection method from the base class:

它返回一个向量,表示相机在世界空间中的观察方向.

It returns a vector representing the direction in which the camera is looking, in world space.

当你有这个 direction 向量时,你可以用它来将相机移动到所需的方向:

When you have this direction vector you can use it to move the camera in the desired direction:

var direction = new THREE.Vector3();
camera.getWorldDirection( direction );

要在那个方向只移动 1,你可以简单地做:

To move only 1 in that direction you can simply do:

camera.position.add( direction );

如果你想移动更多,你可以使用THREE.Vector3 类中的 >multiplyScalar 方法并乘以所需的距离.

If you want to move more you could for example use the multiplyScalar method from the THREE.Vector3 class and multiply with the desired distance.

distance = 10;
camera.position.add( direction.multiplyScalar(distance) );

这篇关于使用 ThreeJS 将相机朝着它所面对的方向移动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆