Is there a way to get AbstractMesh next to/nearby picked mesh?

So i have made a model and playground for basic reconstruction of what i want to achieve.
I want to pick a mesh and also get the mesh next to it.
Example:
image
Like if i pick this circle in here i also want this mesh:
image

Like it does not have to be picked, i just need it in the background for myself.
Playground: https://playground.babylonjs.com/#R6297F#30

Per default there is nostructure for your scene based on the positions of your meshes. What I mean is that there is no way to know what mesh is “next” to another mesh, without checking that or pre-populating some structure that defines it. There is also a question of the definition of “next”. Are you talking about the position of the mesh? Or which one “borders” with the other mesh? The first is quite simple to check, especially if the scene is static - check once all the distances between each mesh and the rest of the meshes in the scene (of course, there is a large room for optimization here), but I am not sure this is what you are trying to achieve. the position is (usually) the center of the element, which means that bordering meshes might appear far, even though their bounding box is very close.
Another idea you might be able to play with is sending rays from the center of the mesh you are checking in 4 directions and see what it hits and what is the distance to this hit. You can also check for intersections, but again - it very much depends on what exactly you are trying to achieve.

1 Like

Ok, thank you. Now i atleast know that i don’t have to search for this and maybe just missing the right search. I am trying to get the mesh that borders with the other one…

My personal favorite here is ray intersection. Send a ray from the mesh you have selected in 6 directions (all axes negative and positive) and check those results (especially the distance result). The lowest distance is your nearest mesh.

Of course, this is not the best in all cases . meshes that intersect themselves might return the wrong result. In this case you could first check if a mesh intersects with another mesh before sending those rays.

And another of course - it also depends on the shape of your meshes. The scene you have shared is quite “quadratic”, so this method fits here.

Never mind how you do that, it will hurt performance if done in real time. If you have a static scene (or if you are able to provide this information yourself) - generate it yourself, or before the scene starts.

1 Like

If you want a generic way of doing that, you’ll have to delve into computational geometry a bit and data structures to represent space, such as a BVH Bounding volume hierarchy - Wikipedia