Point is inside a mesh functionality

Hi,
I wanted to check if the point is inside a mesh. I have used the following sample.

Check if Point is Inside a Mesh | Babylon.js Playground (babylonjs-playground.com)

But when my mesh is rotated, the logic doesnt seem to work. Can someone help me out with this.?
Moreover, when I use it in the workspace, I get an infinite loop.
Check if Point is Inside a Mesh | Babylon.js Playground (babylonjs-playground.com)

You must update the world matrix after you rotate mesh, by calling mesh.computeWorldMatrix().

Regarding the infinite loop, you can fix it by breaking the loop if hitCount is greater than a given value. However, you won’t be able to categorize the point in that case… Maybe you can instead try to use a greater value than 0.00000001 when looking for the next intersection.

2 Likes

Hi @Evgeni_Popov , Sorry for the late reply.
I tried to use mesh.computeWorldMatrix(). but its not working.
Actually, I am trying to find intersecting points between 2 meshes.
two meshes are in different orientation. I am finding each vertex whether it is inside the other mesh geometry using the https://www.babylonjs-playground.com/#XJEG9A#22 sample.
When meshes are not rotated its working properly. In the PG also, mesh is rotated by some degrees and some sphere outside the mesh also showing in red color.

Thanks
Gopinath

You didn’t reuse the corrected PG.

With the correction:

1 Like

Hi @Evgeni_Popov ,
I have two meshes , I need to find the collision points of the mesh with other mesh. So , I am taking vertex data from one mesh and checking whether each vertex is inside other mesh.
In my case, both the meshes can be transformed. I dont want to bake current transformation into the vertices because I might need the transformation matrix later.
I have created a PG to replicate my scenario.

Thanks
Gopinath

Do you simply want to check whether the vertices of mesh A are inside mesh B, or whether the faces of A collide with B? In the latter case, you’ll need a triangle-to-triangle intersection algorithm, as simply checking the vertices won’t suffice (as can be seen in your PG, where none of the cube’s vertices are inside the star, but the cube’s mesh still intersects the star’s mesh).

Hi @Evgeni_Popov,
I need to find whether vertices of mesh A are inside mesh B.
triangle to triangle intersection is not needed.

Thanks.
Gopinath

In fact, there’s a number of problems in the code:

  • 0.00000001 in direction.scale(0.00000001) is too small for the scene, you get the same intersections multiple times. 0.0001 works better
  • the if ((hitCount % 2) === 0 && hitCount > 0) { pointFound = true; } is wrong, it should be removed
  • the point passed to pointIsInside can be modified by the function, so you should clone it when calling this function, to avoid tampering with the sphere position

Once this is fixed, it’s getting to work as expected with the baking:

If you don’t want to bake, you can transform the vertices yourself:

hi @Evgeni_Popov ,
Thanks for your reply. It works fine.
I have made a sample PG with similar data.
Is there any way to improve performance with this workflow.

tooth with rotation and point inside | Babylon.js Playground (babylonjs.com)

Thanks
Gopinath

You have way too many meshes:

image

If you want to show the points, use thin instances to display the spheres in a single draw call:

1 Like