Three.js:获取相机正在查看的方向 [英] Three.js: Get the Direction in which the Camera is Looking

查看:2890
本文介绍了Three.js:获取相机正在查看的方向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我确定这个问题已被重复多次,但是我找不到任何针对我的具体问题的答案!



我使用html5创建游戏, THREE.JS,我有一台相机按照'YXZ'的欧拉顺序旋转。



这样摄像机就像一个旋转的上下左右一样头。在第一人称视图中。



使用此欧拉顺序,camera.rotation中的z属性未使用(始终为0)。 x用于指定以弧度为单位的俯仰或纬度,y用于描述经度。



我有一些以球形方式在用户周围移动的目标,相机一直在这个球体的中心。可以说这个球体的半径为1000.



我的目标是计算相机在哪里看到目标的位置。



我编写了这个计算两个向量之间角度的代码:

pre $ var A = new THREE .Vector3(1,0,0);
var B = new THREE.Vector3(0,1,0);

var theta = Math.acos(A.dot(B)/ A.length()/ B.length());

// theta = PI / 2;

我有目标的矢量(目标[0]。位置)。



事情是我无法找到一种方法来获取相机所在的矢量。



我看过Vector3.applyProjection和applyEuler方法,并且玩了但仍然无法获得任何好的结果。



我认为首先必须将欧拉订单改回'XYZ'之前投影?我试过了,我不知道该怎么做,文档很难理解。



假设相机看起来是正确的,那么我需要在当前旋转的方向上获得距离中心一点距离的向量。



所以我最终得到2个向量,我可以做计算

感谢您的进阶!

解决方案

相机是俯视它的内部负z轴。所以创建一个向下指向负Z轴的向量:

$ $ $ $ $ $ $ $ $ $ $ $ var vector = new THREE.Vector3(0,0,-1 );

现在,将相同的旋转应用于应用于相机的矢量:

  vector.applyQuaternion(camera.quaternion); 

您可以以弧度的形式获得弧度角度,如下所示:

  angle = vector.angleTo(target.position); 






编辑:您现在可以获得方向相机看起来像这样:

  var vector = new THREE.Vector3(); //创建一次并重用它! 
...
camera.getWorldDirection(vector);

注意:通过传入向量要存储结果,每次调用该方法时,该方法都不必实例化新的 THREE.Vector3



已更新至three.js r.79


I'm sure this question has been duplicated many times, however I cannot find any answers for my specific problem!

I'm creating game in using html5 and THREE.js, and I have a camera that rotates with the euler order of 'YXZ'.

This is so the camera rotation up, down, left and right like a head. As in first person view.

With this euler order, the z property in camera.rotation is not used (always 0). x is used to specify pitch or latitude in radians, and y is used depict the longitude.

I have a number of targets moving around the user in a spherical manner, with the camera constantly at the center of this sphere. Lets say the sphere has a radius of 1000.

My aim is calculate the angle between where the camera is LOOKING, and where the target is.

I wrote this code that calculates the angle between 2 vectors:

var A = new THREE.Vector3(1,0,0);
var B = new THREE.Vector3(0,1,0);

var theta = Math.acos( A.dot(B) / A.length() / B.length() );

// theta = PI/2;

I have the vector of the target (targets[0].position).

The thing is I cannot figure out a way to get the vector of where the camera is looking.

I've looked at Vector3.applyProjection and applyEuler methods, and played around but still cannot get any good results.

I believe that the euler order must first be changed back to 'XYZ' before any projections are made? I've tried, and I'm not sure how to do this, the documentation is pretty hard to understand.

Say if the camera is looking directly right, then I need to get a vector that is RADIUS distance away from the center in the direction of the current rotation.

So I will end up with 2 vectors, which I can do calculations on!

Thank you in advanced!

解决方案

The camera is looking down it's internal negative z-axis. So create a vector pointing down the negative z-axis:

var vector = new THREE.Vector3( 0, 0, -1 );

Now, apply the same rotation to the vector that is applied to the camera:

vector.applyQuaternion( camera.quaternion );

You can get the angle in radians to the target like so:

angle = vector.angleTo( target.position );


EDIT: You can now get the direction in which the camera is looking like so:

var vector = new THREE.Vector3(); // create once and reuse it!
...
camera.getWorldDirection( vector );

Note: By passing in the vector in which to store the result, the method will not have to instantiate a new THREE.Vector3 every time the method is called.

Updated to three.js r.79

这篇关于Three.js:获取相机正在查看的方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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