three.js网格避碰

时间:2015-05-05 22:23:20

标签: three.js collision-detection

嗨=)我正在尝试为这些网格添加简单的碰撞避免。 我无法找到有关如何添加避免碰撞的教程 raycaster的intersectObjects()方法。

我正在http://gamingjs.com/ice/

上测试一下
  <body></body>
<script src="http://gamingJS.com/Three.js"></script>
<script src="http://gamingJS.com/ChromeFixes.js"></script>
<script>
  var camera, scene, renderer;
  var geometry, material, mesh;

   init();
   animate();

   function init() {
     scene = new THREE.Scene();

     var aspect = window.innerWidth / window.innerHeight;
     camera = new THREE.PerspectiveCamera(75, aspect, 1, 10000);
   camera.position.x = 0;
  camera.position.y = 700;
  camera.position.z = 150;
  camera.rotation.x = -1;
  camera.rotation.y = 0;
  camera.rotation.z = 0;
  scene.add(camera);

   geometry = new THREE.CubeGeometry(200,200,200);
   material = new THREE.MeshBasicMaterial({color: 0x000000,wireframe:     true,wireframeLinewidth:1});
mesh = new THREE.Mesh(geometry, material);
scene.add(mesh);

  geometry2 = new THREE.CubeGeometry(200,200,200);
    material2 = new THREE.MeshBasicMaterial({color: 0x000ff0,wireframe:    true,wireframeLinewidth:1});
   mesh2 = new THREE.Mesh(geometry2, material2);
   scene.add(mesh2);
   mesh2.position.x = 300;
   mesh2.position.z = -300;

   renderer = new THREE.CanvasRenderer();
   renderer.setClearColorHex(0xffffff);
   renderer.setSize(window.innerWidth, window.innerHeight);

  document.body.style.margin = 0;
   document.body.style.overflow = 'hidden';
   document.body.appendChild(renderer.domElement);
  }

  document.addEventListener('keydown', function(event) {
   var code = event.keyCode;
    if (code == 37) {                                   // left
       mesh.position.x = mesh.position.x-25;
    }
    if (code == 38) {                                   // up
      mesh.position.z = mesh.position.z-25;
     }
    if (code == 39) {                                   // right
      mesh.position.x = mesh.position.x+25;
    }
   if (code == 40) {                                   // down
     mesh.position.z = mesh.position.z+25;
   }

    if (detectCollisions()) {
      if (is_moving_left) mesh.position.x = mesh.position.x+5;
      if (is_moving_right) mesh.position.x = mesh.position.x-5;
      if (is_moving_forward) mesh.position.z = mesh.position.z+5;
      if (is_moving_back) mesh.position.z = mesh.position.z-5;
    }
  });


 document.addEventListener('keyup', function(event) {
    var code = event.keyCode;
   if (code == 37) is_moving_left = false;
   if (code == 38) is_moving_forward = false;
   if (code == 39) is_moving_right = false;
   if (code == 40) is_moving_back = false;
    });


   function detectCollisions() {
   var vector = new THREE.Vector3(0, -1, 0);
   var ray = new THREE.Ray(mesh.position, vector);
    var intersects = ray.intersectObjects(not_allowed);
    if (intersects.length > 0) return true;
    return false;
    }

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





  </script>

0 个答案:

没有答案