0

目前我正在开发一个带有three.js 和pointerlockcontrols 的FPS。

使用下面的代码,我可以向任何水平方向射击:

var direction = new THREE.Vector3( 0, 0, -1 );
var rotation = new THREE.Euler( 0, 0, 0, "XYZ" );
var cameraDirection = new THREE.Vector3(this.game.usermodel.root.children[0].position.x, this.game.usermodel.root.children[0].rotation._x, this.game.usermodel.root.children[0].position.z);
cameraDirection.copy( direction ).applyEuler( this.game.user.rotation );

var raycaster = new THREE.Raycaster(this.game.usermodel.root.children[0].position, cameraDirection); 

但我的代码没有考虑 y 轴。下面的行包含俯仰旋转:

this.game.usermodel.root.children[0].rotation._x

如何应用此值以便我也可以沿 y 轴(垂直于任何方向)拍摄?目前,子弹正沿着一条直线前进。

提前感谢你的帮助。

4

2 回答 2

1

如果您正在使用PointerLockControls并且想要设置光线投射器,则可以使用以下模式:

var direction = new THREE.Vector3();
var raycaster = new THREE.Raycaster(); // create once and reuse
...

controls.getDirection( direction );
raycater.set( controls.getObject().position, direction );

如果您正在使用 ,请勿直接设置相机位置或旋转PointerLockControls

三.js r.71

于 2015-03-28T01:52:50.720 回答
0

对此进行了更多调查,我终于自己想出了一个解决方法。这可能不是做到这一点的完美方式,但它确实有效。

它现在的工作方式如下:我得到基本的网格旋转并应用欧拉,然后添加俯仰旋转。通过这种方式,我将水平和垂直旋转传递给光线投射器。

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

direction.copy( direction ).applyEuler( this.game.user.rotation );
direction.y = this.game.usermodel.root.children[0].rotation._x;

var raycaster = new THREE.Raycaster(this.game.usermodel.root.children[0].position, direction);

仍然欢迎大家对此发表评论或提出更优雅的解决方案。

于 2015-03-28T14:05:30.467 回答