使用ThreeJS将摄像机朝向其朝向的方向移动 [英] Moving the camera in the direction it's facing, with ThreeJS

查看:5008
本文介绍了使用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 类的 getWorldDirection 方法:


它返回一个向量,表示相机在世界空间中的查找方向。

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

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

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的.org / docs / #api / en / math / Vector3.multiplyScalarrel =nofollow noreferrer> multiplyScalar 方法 class 并乘以所需距离。

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天全站免登陆