I’ve actually been pulling my hair out for the past hour because I can’t get this working in general, but there’s one error in particular that is throwing me for a loop.
Here’s my code (i’ve actually pushed all my code here if you need any additional context)
export function CollisionCheck(obj) {
// Raycaster
const raycaster = new THREE.Raycaster(obj);
// For each of the objects in our scene
for (let i = 0; i < m.objects.length; i++) {
let sobj = m.objects[i]; // The object we're searching
label: try {
let objDir = sobj.position.clone().sub(obj.position).normalize(); // The direction between it and the other object.
sobj.updateMatrixWorld(); // (I don't remember why this is here, however I think I remember it fixing something)
console.log(obj.position.x);
raycaster.set(obj.position, objDir); // Set up the raycaster
let intersects = raycaster.intersectObject(sobj, true);
if(intersects[0].distance > 0) {
console.log(intersects[0].distance);
}
break label;
} catch (ex) {console.log(ex)}
}
}
Note how I log the variable obj.position.x
and then instantly pass it to raycaster.set
.
I get this flooding the console (because I’m executing this on a loop)
> 0
> collision.js:22 TypeError: Cannot read property 'x' of undefined
at Vector3.copy (three.js:2052)
at Group.copy (three.js:4336)
at Ray.set (three.js:2836)
at Raycaster.set (three.js:26162)
at Module.CollisionCheck (collision.js:15)
at Module.ballUpdate (ball.js:61)
at animate (animate.js:7)
obj.position
is undefined despite the fact that I can not only log it to the console, but I can literally log the exact variable that can’t be found…? I’m completely lost here, how does this even happen and how do I fix it?