Graphics Reference
In-Depth Information
3. Now that we've got the object to move around and the objects to detect the
collisions with, we can add the code to detect collisions. In the render loop,
we need to add the following:
// reset the opacity at the beginning of
the loop
cubes.forEach(function(cube){
cube.material.transparent = false;
cube.material.opacity = 1.0;
});
var cube = scene.getObjectByName('cube');
var originPoint = cube.position.clone();
for (var vertexIndex = 0;
vertexIndex <
cube.geometry.vertices.length;
vertexIndex++) {
var localVertex = cube.geometry.
vertices[vertexIndex].clone();
var globalVertex =
localVertex.applyMatrix4(
cube.matrix);
var directionVector =
globalVertex.sub(
cube.position);
var ray = new THREE.Raycaster(
originPoint,
directionVector.clone().normalize() );
var collisionResults =
ray.intersectObjects( cubes );
if ( collisionResults.length > 0
&&
collisionResults[0].distance <
Search WWH ::




Custom Search