Mesh is inside X meshes

Hi!

I need some help with BabylonJS, I use it for a while now, but I want to do something I have never done before, and I don’t know how to do it…
I want to be able to get all meshes that contains a choosen mesh, I though that the Mesh.Intersects method could do it, but in fact, in my case, I know the contained mesh, but not the containers…
I don’t know if my explanation clear enough, here is a little example :

  1. big red cube mesh
  2. a green cube mesh, inside the big red cube
  3. a blue sphere, inside the green cube

From the blue sphere, I wish a could know all the meshes that contains this mesh.

Thanks for reading/helping !

Hi Nico, welcome to the new forum.

Let’s make a demo playground, eh?

https://www.babylonjs-playground.com/#1ES3CE#2

IF… the blue sphere… is parented-to the outer containers, then you can check blue sphere parents. See lines 61/62, and JS console on that playground.

If NOT parented… much more work, perhaps. I have turned-ON showBoundingBox for all three mesh, and have output eachmesh.getBoundingInfo().boundingbox to console (lines 58-60). On each boundingbox object seen on console, there are many useful properties. Some are “world-space” measurements (like extendSizeWorld), and some are “local-space” measurements (like extendSize).

I don’t know your goal, but these are tools that COULD be useful. Physical containment of mesh… generally happens on a layer ABOVE BJS library (user-coded)… or possibly with the help of 3rd-party physics engines, which BJS has connectors-for. But, collision-testing on the inside of other mesh… can be challenging, possibly requiring the containment boxes to be assembled from 6 planes.

Ok, stay tuned for more comments. One question: Does the inner blue sphere need to “learn” IF/NOT it is contained? Or does it just need to “discover” its containers, and it WILL ALWAYS have one/some? Give more details about that, if possible. thx.

1 Like

Thanks a lot for your answer Wingnut !

The problem is that the blue sphere is not parented to the outer containers :confused:

Thanks for the demo playground, you rocks, it illustrate exactly what I tried to describe !

I want to use BJS to build a hierarchy using 3d positioning and containment, I would like to do this on huge scenes (more than 10 000 meshes…), the problem is that the 3D file I will use is not generated be me, and I can’t edit it, the container can have another shape than a cubic shape too :confused:

The blue sphere only need to discover its containers, for example by using a method I will call with a prototype like this : MySphere.GetContainers() or Mesh.GetContainers(MySphere).

1 Like

Did you check our octrees? There are using a similar idea: Create containing space to boost meshes selection: Optimize Your Scene with Octrees - Babylon.js Documentation

In the octree code, you can see the code used to detect if a given mesh is intersecting with a box:

This bring us to the all the good function in the BoundingInfo class:

So to your question, you could get the boundingInfo of two meshes and you can see if they intersect.
If they intersect you can then check if boxA is bigger than boxB by checking that min/max of A are respectively smaller/bigger than B

Does it make sense?

1 Like

Thanks you for your answer!
I never player with octree, I am currently playing with this feature, it hope that the intersects method from octree will do what I need (sphereCenter, and sphereRadius parameters let me think it could).

I will let you know if I got some interesting results!

So to your question, you could get the boundingInfo of two meshes and you can see if they intersect.
If they intersect you can then check if boxA is bigger than boxB by checking that min/max of A are respectively smaller/bigger than B

The problem here is that I know the contained mesh, but I don’t know the containers, it will have at least one container, but it could have more than one :confused:
The easy method will be to check boundingInfo from my contained mesh with all other boundingInfo from all other meshes, but with a 10 000+ meshes scene I fear that my browser can’t handle it and crash

if you have to do it per frame yes :slight_smile: but only once can work
and we can be smart by not checking all the meshes based on who is contained where

1 Like