问题描述
想知道是否有可能获得指针锁定控件中偏航对象的朝向,并将其用作光线投射器的方向.我一直在玩弄代码,试图向其中添加一些基本的射击游戏内容.我一直在尝试使用X&在堆栈溢出中发现的一些东西.指针控件提供的偏航对象的Y值,但没有运气.这是我到目前为止的内容:
Wondering if it's possible to get the way the yaw object in pointer lock controls is facing and use that as a direction for a raycaster. I've been toying around with the code, trying to add some basic shooter stuff to it. I've been trying a few things i've found on stack overflow with the X & Y values from the yaw object the pointer controls provides, but no luck. Here's what I have so far:
setupTarget: function() {
//var vector = new THREE.Vector3(game.mouse.x, game.mouse.y, 0);
var yaw = game.controls.getObject();
var pitch = game.controls.pitchObject;
var vector = new THREE.Vector3(
yaw.position.x * Math.cos(pitch.rotation.x),
yaw.position.y * Math.sin(yaw.rotation.y),
0
);
var dir = vector.sub(yaw.position).normalize();
this.ray = new THREE.Raycaster(yaw.position.clone(), dir);
}
我知道这是不对的,除了它不起作用.混淆了一些用于计算x& amp;的数学运算y是基于旋转的平移,但显然不适用于目标.
I know that's wrong aside from it's not working. Was confusing some math for calculating x & y translation based on rotation, but that doesnt work for the target obviously.
推荐答案
将以下内容添加到PointerLockControls
的副本中(或等待几天才能发布下一个版本):
Add the following to your copy of PointerLockControls
( or wait a few days for the next release):
this.getDirection = function() {
// assumes the camera itself is not rotated
var direction = new THREE.Vector3( 0, 0, -1 );
var rotation = new THREE.Euler( 0, 0, 0, "YXZ" );
return function( v ) {
rotation.set( pitchObject.rotation.x, yawObject.rotation.y, 0 );
v.copy( direction ).applyEuler( rotation );
return v;
}
}();
three.js r.59
three.js r.59
这篇关于三个js,获取pointerlockcontrolsi面对的vector3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!