0

I am really sorry if anyone already have asked this question, but I have tried to search a lot regarding this. Is there any method that can be use to detect if one Box in Threejs is colliding with one or many other objects in the scene?

let allObjects = getAllObjectsArray(); //returns all Boxes of the scene
let newBox = getNewBox(); //Adding new box;

let collidingObjs = allCollidingObjects(newBox);//I want a logic that will return array of all objects collide with "newBox"
console.log(collidingObjs)

USMANHEART
  • 93
  • 1
  • 9
  • You need to add a physics engine like [cannonJS](https://schteppe.github.io/cannon.js/) or [ammoJS](https://github.com/kripken/ammo.js/) if you dont want to do all the heavy lifting yourself – marks Mar 19 '21 at 11:52

1 Answers1

0
function allCollidingObjects(mesh:Object3D) {
    let fromMeshes = getAllObjectsArray();//returns all Boxes of the scene
    let boundary = new Box3().setFromObject(mesh);
    let collidingObjs:Object3D[]=[];
    fromMeshes.forEach((meshNow:Object3D)=>
    {
        let otherBounds = new Box3().setFromObject(meshNow);
        if(boundary.intersectsBox(otherBounds))
        {
            collidingObjs.push(meshNow)
        }
    });
    return collidingObjs;
}

Very simple, working 100% fine. I don't know why no one is recommending this method. By the way, thanks

USMANHEART
  • 93
  • 1
  • 9