user5041326
user5041326

Reputation:

How do I extract the intersect point (Vector3) of the intersectObjects?

I am trying to find the intersect point between a ray from 'child' and a mesh (child2), using Raycaster:

var raycaster = new THREE.Raycaster();
var meshList = [];
meshList.push(child2);
for (var i = 0; i < child.geometry.vertices.length; i++) {
    var diff = new THREE.Vector3();
    diff.subVectors (child.geometry.vertices[i],child2.position);
    raycaster.set(child.geometry.vertices[i],diff.normalize());
    var intersects = raycaster.intersectObjects( meshList );
    console.log(intersects[0].point);
}

But the above code is giving me error in the last line (console.log(intersects[0].distance)): "TypeError: undefined is not an object (evaluating 'intersects[0].point')".

How can I extract the intersect point between the ray and the 'child2' mesh?

Upvotes: 0

Views: 175

Answers (1)

bjorke
bjorke

Reputation: 3305

Test to ensure there actually were results!

var intersects = raycaster.intersectObjects( meshList );
if (intersects.length) {
   console.log(intersects[0].point);
} else {
   console.log('ha, you missed me');
}

Upvotes: 1

Related Questions