For each mesh (THREE.Object3D
) Three.js provide a very handy properties - boundingSphere
and boundingSphere
that have intersectsSphere
and isIntersectionBox
methods.
With all this I thought I can use it for simple collision detection but when I try it appears that collision happens all the time because (I tried boundingSphere
) boundingSphere.center is always in (0, 0, 0);
So If I want to check collisions between 2 meshes I should for each object - clone boundingSphere
object and then get it world coordinates and only then to use intersectsSphere
.
something like this:
var bs = component.object.geometry.boundingSphere.clone();
bs.center.setFromMatrixPosition(component.object.matrixWorld);
...
if (_bs.intersectsSphere(bs)){
is this how it suppose to be used or am I missing something and there are more convenient way of doing collisions detection based on boundingBox/boundingSphere?
If you want to do collision detection with bounding boxes you need the boxes in the world coordinate system. The bounding volumes in the
intersectsSphere
andisIntersectionBox
properties of the mesh are in the local coordinate system of the object.You can do like you did: clone the volumes and move them to the correct position in the world coordinate system, that is a good solution.
Otherwise you can also set a new box from your meshes and do collision using those boxes. Let's say you have a
THREE.Mesh
calledmesh
then you can do:A little tip. During development it can be nice to see the bounding boxes in your scene, for this you can use the
THREE.BoundingBoxHelper
: