javascript - 第一人称射击游戏用 three.js 控制

标签 javascript 3d three.js

我对 three.js 和 3D 完全陌生。我正在尝试制作一个非常简单的第一人称射击游戏。我找到了很多例子,但它们看起来都很复杂。我想在使用之前了解代码。我遇到的麻烦是相机旋转。其他一切都很好。我的方法不太奏效。它似乎在 z 轴上旋转,即使我将其设置为 0。这是我的所有代码(125 行)

var width    = window.innerWidth, height = window.innerHeight,
    scene    = new THREE.Scene(),
    camera   = new THREE.PerspectiveCamera(75, width / height, 0.1, 1000),
    renderer = new THREE.WebGLRenderer(),
    mouse = {
      x: 0,
      y: 0,
      movedThisFrame: false
    };
renderer.setSize(width, height);
document.body.appendChild(renderer.domElement);

var geometry = new THREE.BoxGeometry(1, 1, 1);
var material = new THREE.MeshBasicMaterial({
  color: 0x00ff00
});
var cube = new THREE.Mesh(geometry, material);
scene.add(cube);
var floorgeometry = new THREE.BoxGeometry(100,0.1,100);
var floormaterial = new THREE.MeshBasicMaterial({
  color: 0xff0000
});
var floor = new THREE.Mesh(floorgeometry, floormaterial);
floor.position.y = -1;
scene.add(floor);

var keys = {
  w: false,
  a: false,
  s: false,
  d: false
};

camera.position.z = 5;

function radDeg(radians) {
  return radians * 180 / Math.PI;
}

function degRad(degrees) {
  return degrees * Math.PI / 180;
}

function rotateCam() {
  if (!mouse.movedThisFrame) {
    mouse.x = 0;
    mouse.y = 0;
  }
  /*

  What am I doing wrong here?

  */
  camera.rotation.x -= mouse.y * 0.001;
  camera.rotation.y -= mouse.x * 0.001;
  camera.rotation.z = 0;

  mouse.movedThisFrame = false;
}

function moveCam() {
  var rotation = camera.rotation.y % (Math.PI * 2), motion = [0,0];
  if (keys.w) {
    motion[0] += 0.1 * Math.cos(rotation);
    motion[1] += 0.1 * Math.sin(rotation);
  }
  if (keys.a) {
    motion[0] += 0.1 * Math.cos(rotation + degRad(90));
    motion[1] += 0.1 * Math.sin(rotation + degRad(90));
  }
  if (keys.s) {
    motion[0] += 0.1 * Math.cos(rotation - degRad(180));
    motion[1] += 0.1 * Math.sin(rotation - degRad(180));
  }
  if (keys.d) {
    motion[0] += 0.1 * Math.cos(rotation - degRad(90));
    motion[1] += 0.1 * Math.sin(rotation - degRad(90));
  }

  camera.position.z -= motion[0];
  camera.position.x -= motion[1];
}

window.onload = function() {
  renderer.domElement.onclick = function() {
    console.log('requested pointer lock');
    renderer.domElement.requestPointerLock();
  };

  renderer.domElement.onmousemove = function(e) {
    if (!mouse.movedThisFrame) {
      mouse.x = e.movementX;
      mouse.y = e.movementY;
      mouse.movedThisFrame = true;
    }
  };

  document.onkeydown = function(e) {
    var char = String.fromCharCode(e.keyCode);
    if (char == 'W')
      keys.w = true;
    else if (char == 'A')
      keys.a = true;
    else if (char == 'S')
      keys.s = true;
    else if (char == 'D')
      keys.d = true;
  };

  document.onkeyup = function(e) {
    var char = String.fromCharCode(e.keyCode);
    if (char == 'W')
      keys.w = false;
    else if (char == 'A')
      keys.a = false;
    else if (char == 'S')
      keys.s = false;
    else if (char == 'D')
      keys.d = false;
  };

  function animate() {
    requestAnimationFrame(animate);
    rotateCam();
    moveCam();
    renderer.render(scene, camera);
  }
  animate();
};

问题出在 rotateCam 函数中。它不太管用,我真的不知道为什么。

我还尝试使用 this question 上的代码但它没有用。

最佳答案

第一人称控制比您想象的要复杂。即使您算出了 Angular 数学,当指针未锁定时,鼠标也会碰到窗口边缘并停止转动。

我建议您从指针锁定示例 ( http://threejs.org/examples/#misc_controls_pointerlock ) 开始,这是 3js 的第一人称控件示例。

关于javascript - 第一人称射击游戏用 three.js 控制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36524712/

相关文章:

javascript - 如何使用 Three.js 创建网格

javascript - 50 === 50 : false. 50 == 50 : true?

javascript - 为什么 CoffeeScript 使用 %% 代替标准的 javascript 运算符 % 来表示模数

c++ - 具有部分颜色写入的 OpenGL 全深度

iphone - 使用 OpenGL ES 2.0 显示/处理 iPhone 相机帧

javascript - 突出显示 Three.js 中的 THREE.Line

javascript - 使用 Javascript 中的值从多维数组中删除元素

javascript - Nodejs获取不到返回值?

javascript - 带轴和 Angular 3D 旋转

javascript - 如何在 3D json 资源上实现基本的 LOD 机制