Since you are using the position
and the geometry
of the object, you can use the built in ThreeJS function intersectsBox()
. You can clone the original geometrys’ bounding boxes, apply their object’s transform matrix to them and check for intersections between them.
An example of this is discussed here:
With the relevant code below
function detectCollisionCubes(object1, object2){
object1.geometry.computeBoundingBox(); //not needed if its already calculated
object2.geometry.computeBoundingBox();
object1.updateMatrixWorld();
object2.updateMatrixWorld();
var box1 = object1.geometry.boundingBox.clone();
box1.applyMatrix4(object1.matrixWorld);
var box2 = object2.geometry.boundingBox.clone();
box2.applyMatrix4(object2.matrixWorld);
return box1.intersectsBox(box2);
}