I am working on a 3D virtual home project. everything works fine except collision detection. I use PointerLockControls for my camera and movement. but Iām not sure how to deal with the detex in all possible directions. For simplicity, I started with a direct and reverse collision with a simple cube at (0,0,0):
rays = [ new THREE.Vector3(0, 0, 1), new THREE.Vector3(0, 0, -1) ];
Then:
function detectCollision() { var vector; var projector = new THREE.Projector(); for (var i = 0; i < rays.length; i++) { var vector = rays[i].clone(); projector.unprojectVector(vector, camera); var rayCaster = new THREE.Raycaster(controls.getObject().position, vector.sub(controls.getObject().position).normalize()); var intersects = rayCaster.intersectObject(cube, true); if (intersects.length > 0 && intersects[0].distance < 50) { console.log(vector); console.log(i, intersects); $("#status").text("Collision detected @ " + rays[i].x + "," + rays[i].z + "<br \>" + intersects[0].distance); } }
But when I get to my cube, the console shows that both rays hit the cube! So why? Are there any problems with my rays? vector (0,0,1) should be inverse and (0,0, -1) should be forward, right? Please help me before I get lost in 3d! Thanks.
source share