【问题标题】:ThreeJS How to detect if any Box/Mesh collide with another?ThreeJS如何检测是否有任何Box / Mesh与另一个碰撞?
【发布时间】:2021-03-19 11:50:53
【问题描述】:

如果有人已经问过这个问题,我真的很抱歉,但我已经尝试了很多关于这个的搜索。 是否有任何方法可用于检测 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)

【问题讨论】:

  • 如果你不想自己做所有繁重的工作,你需要添加一个物理引擎,如 cannonJSammoJS

标签: javascript 3d shapes pythreejs


【解决方案1】:
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% 正常工作。我不知道为什么没有人推荐这种方法。 顺便,谢谢

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多