问题
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);
}
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.
回答1:
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
回答2:
Well, with the new Version it do not work. I have to add
var v = new THREE.Vector3( 0, 0, -1 );
before
v.copy( direction ).applyEuler( rotation );
because Firefox do not recognize v.
来源:https://stackoverflow.com/questions/18200372/three-js-get-the-vector3-that-pointerlockcontrolsi-s-facing