There’s a misunderstanding here,
octree.intersects returns an array of objects that (may) collide with the bounding sphere passed in parameter (center + radius). That is, it finds the blocks of the octree that collide with the sphere and it returns the meshes that are in these blocks. So,
length is simply the length of the mesh array, not a measure of the “collisionness”.
You should probably not pass
true for the 3rd parameter of
octree.intersects, as doing so will duplicate several times the same mesh in the result array, as a mesh can intersect multiple blocks of an octree.
Also, 0.1 is not valid for the
maxDepth parameter of the octree constructor, as it is a maximum recursive depth: it should be an integer number. Passing 0.1 means that the octree will never be subdivided and will always have only 8 blocks, which defeats the purpose of using an octree.
Another important thing is that
octree.update(worldExtends.min, worldExtends.max, ); should be called with an up-to-date
worldExtends object, i.e. some extends that encompasses all the objects of the scene you want to put in the octree. So, you should do it after you create the meshes in the scene, not before.
Lastly, a block of the octree is subdivided into 8 smaller blocks only if the number of entries (meshes in your case) is greater than the max capacity value you passed at construction time.
You have a 0 length because you computed
worldExtends before adding the ground and the box to the scene, so they are not in the octree extends. If you fix the code, then the result array is not empty anymore:
Again, there’s a misunderstanding of what the Octree structure will do. It won’t tell you if a mesh intersects with another mesh, it will tell you which meshes are contained in the block(s) of the octree structure that intersect a given bounding sphere, given the sphere center and radius.
It’s an acceleration structure that helps finding meshes which are located around an area in space (defined by a bounding sphere). Note that’s not because a mesh is returned by the
intersects method that it really intersects the bounding sphere you passed to
intersects! The blocks in the octree are cube blocks, so it’s possible that a bounding sphere intersects a cube without intersecting one or multiple meshes that are contained in this block.
The octree described above will not allow you to do this, as I hope the previous comments have helped you understand.
There’s also the possibility to use an octree for collision and picking detection, as explained in the doc: Optimizing With Octrees | Babylon.js Documentation
It is applicable to a single mesh and should be used only if your mesh is really complex, to avoid checking every faces when looking for an intersection, and instead relying on the octree to eliminate faces that are too far from the testing point to collide. But I’m not sure it is your use case.
You should look for this forum if you want some pointers on how to optimize collisions, there have been a number of threads on the subject I think.
I hope this clarifies things a bit.