碰撞检测代码:
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(线框立方体)
提前致谢