三个js,得到指针锁控制面对的vector3

agm*_*eod 2 javascript math three.js

想知道是否有可能让指针锁控件中的偏航对象面对并将其用作raycaster的方向.我一直在玩代码,试图添加一些基本的射击游戏.我一直在尝试使用指针控件提供的yaw对象的X&Y值在堆栈溢出时找到的一些东西,但没有运气.这是我到目前为止所拥有的:

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);
}
Run Code Online (Sandbox Code Playgroud)

我知道这是错误的,除了它不起作用.将基于旋转计算x和y平移的一些数学混淆,但这显然对目标不起作用.

Wes*_*ley 5

将以下内容添加到您的副本中PointerLockControls(或等待下一个版本的几天):

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;

    }

}();
Run Code Online (Sandbox Code Playgroud)

three.js r.59

  • 不要在紧密循环中使用`clone()`或`new` - 重用`v`. (2认同)