碰撞中的准确性ThreeJS

时间:2017-12-29 14:58:51

标签: three.js collision-detection collision

我正在做一个有飞机飞行的游戏和你需要通过移动平面来避开它们的一些岩石。为了使用碰撞检测,我制作了一个与我的物体位置相同的立方体,但准确度不均匀50%。有时当我的物体碰撞时它不会告诉我,有时当物体离开时它会显示它发生碰撞的警报。

Wrong Collision Detection Picture

碰撞检测代码:

   if(collisiondetection === true) {
   var originPoint = obcube.position.clone();

    clearText();

    for (var vertexIndex = 0; vertexIndex < obcube.geometry.vertices.length; vertexIndex++)
    {
        var localVertex = obcube.geometry.vertices[vertexIndex].clone();
        var globalVertex = localVertex.applyMatrix4( obcube.matrix );
        var directionVector = globalVertex.sub( obcube.position );

        var ray = new THREE.Raycaster( originPoint, directionVector.clone().normalize() );

        var collisionResults = ray.intersectObjects( collisionlist );
        if ( collisionResults.length > 0 && collisionResults[0].distance < directionVector.length() ) {
            if(alerted === false) {
                alerted = true;
 GameOver();
            }
        }

    }
    }

十二面体(碰撞的立方体)

      var cubegeo = new THREE.DodecahedronGeometry(2,0);
      var cubemat = new THREE.MeshPhongMaterial({
      color:0x29CB1C,
        map: new THREE.TextureLoader().load('rock.png'),   
 transparent:true,
  depthWrite:false,
   blending: THREE.AdditiveBlending
     });
  var cube1 = new THREE.Mesh(cubegeo,cubemat);

  cube1.position.z = -60;
  cube1.position.x = randomnumx1 ;
  cube1.position.y = randomnumy1;
  scene.add(cube1);
  cube1.name = "cuves";
  makeme=false;
   mycubes = scene.getObjectByName('cuves');
    collisionlist.push(cube1);

立方体有Z旋转和位置变化,没有x,y和i推入阵列碰撞列表! 平面可以改变位置x,y,z “collisiondetection === true”用于确保所有立方体和平面都在场景中。 我将立方体和平面放在一个组中,但在碰撞检测中我使用了obcube(线框立方体)

提前致谢

0 个答案:

没有答案