Hello,
I’m making a small web game that the submarine is cleaning up the ocean. However, I’m struggling with collision event. The result I want to show is that when the submarine touch a red box, message on the top will show “Hit”(If the result can be the red box disappear will be great). Therefore, I tried to use Raycaster to detect the boxes, but it doesn’t work. Because I’m a beginner of threejs, I can’t find out the problem with my code. Please help me check my code, thank you!
My game link: http://peterchangsite.com/aau/wnm617/final_project/game2.html
Raycaster event code:
function update(){
var originPoint = cabin.position.clone();
clearText();
for (var vertexIndex = 0; vertexIndex < cabin.geometry.vertices.length; vertexIndex++)
{
var localVertex = cabin.geometry.vertices[vertexIndex].clone();
var globalVertex = localVertex.applyMatrix4( cabin.matrix );
var directionVector = globalVertex.sub( cabin.position );
var ray = new THREE.Raycaster( originPoint, directionVector.clone().normalize() );
var intersects = ray.intersectObjects( collidableMeshList );
if ( intersects.length > 0 && intersects[0].distance < directionVector.length() ) {
appendText(" Hit ");
}
}
}
Reference:
Raycaster collision: https://stackoverflow.com/questions/11473755/how-to-detect-collision-in-three-js