【问题标题】: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)
【问题讨论】:
标签:
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% 正常工作。我不知道为什么没有人推荐这种方法。
顺便,谢谢