# Get the name of the mesh(es) a mesh is intersecting

Hi!

A little while back I had a question on whether you could detect if a mesh is intersecting ANYTHING. @JohnK cam forward with the solution to use

``````for (var i = 0; i < scene.meshes.length; i++) {
if (sphere !== scene.meshes[i]) {
sphere.intersectsMesh(scene.meshes[i], false);
}
}
``````

to find what mesh the mesh is intersecting. But with a lot of meshes, this can be QUIT () the performance hit. All triggers and boolean functions involve a mesh and detecting for a specific mesh.() I’m wondering if there is a non-performance-heavy way to detect what mesh a mesh is intersecting.

Thanks for any ideas, and I hope this is possible.

This function is already doing some optimization for you by doing a bounding sphere test first

You could do some work by filtering out impossible options. Everything too far away is an impossible option.

I would divide the map into a logical grid. Calculate at which cell each mesh is. Say mesh A in cell [2,3] And check everything 1 cell around it. Whenever a mesh changes position it also updates its position in the grid. This way you have some custom culling implemented.

That function of yours would check for intersection only the cell where the sphere is or all the 1 other cells around it.

1 Like

That’s a great idea! Thanks! I’ll try to implement it. Maybe first by having meshes disappear, then with the physics.

P.S. Nice name

Ah. ok. So it can’t get more optimized without me doing something. I think.

Thanks, I like my name too .

Let me know if it works out well for you.

So far I’ve had a coupe ideas on how to make it work.
The first is a crap-ton of conditionals checking the players condition. Not super efficient, but getting everything the isn’t near the player to cull is something I’m not sure of.
The second is have a bunch of 100x100x100 cubes placed around the map. use Those to check where the player is, so then the grid is already there. I could have them invisible, but not sure how to cull.
The third is to divide the map up into different pieces, and use the cube method. then I could cull the models instead of trying to figure out what submesh is where.
Another thing I could do is have a big cube around the player, like 25x25x25 or so, with backface culling off. Then it’ll cull everything, but I’m not sure how physics impostors could be turned on and off.
The last is just find a different map, of make this one smaller. That’s the easiest.

All of the above deal with rendering the map, witch drops frames. I get around 30 fps when inside the map, but that is without physics impostos. Everything needs and impostor, and that’s where I get the massive fps hit.

I will try to make it easier.

Create an array which will act as a grid. Imagine the grid being composed of 4x1x4 cells. One player is 1x1x1 lets say. What you would do would be to calculate the player position in the grid. How? By calculating the players x and z. If your map starts from [-50 , 0 , -50] and ends in [50 , 0 , 50], this would mean you have 12.5 * 12.5 = 1562,5 cells by my calculations. First cell would start at [-50, 0 ,50] and end in [-46, 0, 46 ] ( upper left corner ) . If your player positions x is between -50 and -46 and your players positions z is between 50 and 46, the player is in the first cell. What you have to do next is check for collisions in the meshes in the adjacent cells. Get it? Whenever a mesh changes position recalculate it in the grid. Then get all the meshes in the mesh cell and adjacent cells. I hope I made it clear enough. Honestly, I tried but I doubt I can do better than this

1 Like

This means I’m gonna have to get the positions of all of the sub-meshes in the map mesh, aren’t I? Nice explanation, I haven’t really used arrays.

Of all the meshes you need to test collision with. This can be done on creation if the mesh is static or whenever it moves. Store it as a variable or as an attribute ( if you are using classes ) and you can find where it is in the grid. What I would also do, in the array of cells which I mentioned before, I would save another array with the meshes inside it. Whenever a mesh changes position, update the array of meshes in the cells. So this would make it super easy to retrieve whats there. I get this is a little bit advanced but this is sure to work, I have implemented it identically in one of my games.

The mesh will always be static. I just noticed that I gave no pg link. https://playground.babylonjs.com/#LJ4W85#16

uncomment line 81 for collision. left click to fire a ball to test collision, and right click to get the name of the mesh with an alert. (uses camera.get forward ray)

1 Like

I am afraid I can not help with an implementation. You will have to figure it out yourself. let me know if there is anything you still do not understand.

1 Like

Ok. Thanks for the basis of it! I’ll try to get it working. If you notice, I already have a sub-mesh being disposed when the mesh is being loaded. This is the CS:S respawn area.