使用 boundingSphere 进行碰撞检测

Collision detection with boundingSphere

为每个网格 (THREE.Object3D) Three.js 提供一个非常方便的属性 - boundingSphereboundingSphere 具有 intersectsSphereisIntersectionBox方法。

有了这一切,我想我可以用它来进行简单的碰撞检测,但是当我尝试时,碰撞似乎一直在发生,因为(我试过 boundingSphere)boundingSphere.center 总是在 (0, 0, 0); 因此,如果我想检查 2 个网格之间的碰撞,我应该为每个对象克隆 boundingSphere 对象,然后获取它的世界坐标,然后才使用 intersectsSphere.

像这样:

var bs = component.object.geometry.boundingSphere.clone();
bs.center.setFromMatrixPosition(component.object.matrixWorld);
...
if (_bs.intersectsSphere(bs)){

这是它应该如何使用还是我遗漏了什么并且有更方便的基于 boundingBox/boundingSphere 进行碰撞检测的方法?

如果你想用边界框进行碰撞检测,你需要世界坐标系中的框。网格的 intersectsSphereisIntersectionBox 属性中的边界体积在对象的局部坐标系中。

你可以像你一样做:克隆体积并将它们移动到世界坐标系中的正确位置,这是一个很好的解决方案。

否则你也可以从你的网格中设置一个新的盒子并使用这些盒子进行碰撞。假设你有一个名为 meshTHREE.Mesh 那么你可以这样做:

sphere = new THREE.Sphere.setFromPoints( mesh.vertices );

box = new THREE.Box3.setFromObject( mesh );

小提示。在开发过程中,很高兴看到场景中的边界框,为此您可以使用 the THREE.BoundingBoxHelper:

var helper = new THREE.BoundingBoxHelper( mesh );
scene.add( helper );