In my ThreeJS project, I have planes (Object3D) flying inside a sphere (Mesh).
My goal is to detect when a plane collides with the border of the sphere so that I can remove it and respawn it in a different location within the sphere.
I am wondering how I can accurately determine when one object exits another object?
This is the current code I am using:
detectCollision(plane, sphere) {
var boxPlane = new THREE.Box3().setFromObject(plane);
boxPlane.applyMatrix4(plane.matrixWorld);
var boxSphere = new THREE.Box3().setFromObject(sphere);
boxSphere.applyMatrix4(sphere.matrixWorld);
return boxPlane.intersectsBox(boxSphere);
}
Within my render function:
var collision = this.detectCollision(plane, this.radar)
if (collision == true) {
console.log("the plane is inside the sphere")
}
else {
console.log("the plane is outside the sphere")
}
})
The issue I am facing is that when the planes are inside the sphere, I consistently receive both true and false results until all planes have exited the sphere. At that point, I only get false and no more true indications.