如何解决ThreeJS 如何检测任何 Box/Mesh 是否与另一个碰撞?
如果有人已经问过这个问题,我真的很抱歉,但我已经尝试了很多关于这个问题的搜索。 是否有任何方法可以用于检测 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)
解决方法
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% 正常工作。我不知道为什么没有人推荐这种方法。 顺便说一下,谢谢
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。