I am currently working on detecting collisions with terrain by adjusting the heights of a plane's vertices.
Unfortunately, my Raycaster is only accurately detecting collisions about 10% of the time. To see an example of one of these failed detections, check out this link:
I'm not sure what I might be doing wrong. Can anyone help me troubleshoot?
EDIT: Here is a link to the jsfiddle:
var camera, scene, renderer, gump = 0;
var geometry, material, mesh, map, axis, ray;
init();
animate();
function init() {
//Creating a camera to observe the scene
camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 1, 10000);
camera.position.x = 500;
camera.position.y = 300;
//Initializing the scene
scene = new THREE.Scene();
//Loading terrain material
material = new THREE.MeshBasicMaterial({color: 0xff6666, wireframe: false});
geometry = new THREE.PlaneGeometry(128, 128, 127, 127);
mesh = new THREE.Mesh(geometry, material);
mesh.scale.set(50,50,50);
mesh.rotation.x = -Math.PI/2;
scene.add(mesh);
//Creating axis with the position and rotation of the ray
axis = new THREE.AxisHelper(100);
axis.position = new THREE.Vector3(333.2637, 216.6575, -515.6349);
axis.rotation = new THREE.Vector3(1.6621025025, 0.119175114, -2.2270436357);
axis.scale.z = 10;
scene.add(axis);
//Creating the actual ray using the axis position and rotation
ray = new THREE.Raycaster();
ray.ray.origin.copy(axis.position);
ray.ray.direction.copy(axis.rotation);
//Renderer
renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
}
function animate() {
requestAnimationFrame(animate);
if (mesh) {
var intersections = ray.intersectObject(mesh);
if (intersections.length > 0) {
console.log("OK"); //This never actually happens
}
//Moving the axis so it visibly passes through the mesh object
axis.translateZ(Math.cos(gump) * 2);
gump += 0.01;
//Focusing the camera on the axis
camera.lookAt(axis.position);
}
renderer.render(scene, camera);
}
EDIT: System specs as requested:
Windows 7 64bit
Chrome 26.0.1410
Threejs 57
Graphics card: GTX 560 Ti