0

如果有人已经问过这个问题,我真的很抱歉,但我已经尝试过很多关于这个的搜索。是否有任何方法可用于检测 Threejs 中的一个 Box 是否与场景中的一个或多个其他对象发生碰撞?

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)

4

1 回答 1

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;
}

非常简单,100% 正常工作。我不知道为什么没有人推荐这种方法。顺便谢谢

于 2021-03-20T02:29:11.977 回答