Download

Determine whether a point is inside a mesh of any shape

I currently have a room system (3D) in my level and I need to determine which room my player is in. To avoid being in 2 rooms at the same time, I only look at the “center” of my player. However, I can’t find any documentation to know if a point is in a mesh. I only found Is Point In Box but that is not enough for me.

Here is my blueprint :

I have read that there is a method called Point in Polygon which counts the number of intersections between a vector from the point and the mesh (Point in polygon - Wikipedia). However I have no idea how to do this in blueprint.
https://en.wikipedia.org/wiki/Point_in_polygon#/media/File:RecursiveEvenPolygon.svg

Just use sphere trace with a low radius, like 1

Why not just:
a. Trace down to the floor (or use “Current Floor”) and get the room through the actor name, tag, or material.
b. Have triggers at the doors that set the current room the player is in.

1 Like

Unfortunately I can’t, my scene is not predefined and I import my assets from a file (.obj). I can extract the information of the role I gave to my mesh but not more. I need to have an adaptive system that recognizes the volume my actor is in.
Also, my actor can fly and walk through walls, so he can arrive from any place.

I finally figured out how to make a function that solves my problem. The code is quite big and not necessarily pretty so I’ll explain it below:

As a reminder, I was trying to find out if a (3D) point was in a mesh of any shape. After some research, I started from the definition of wikipedia which says the following (Point in polygon - Wikipedia) :

One simple way of finding whether the point is inside or outside a simple polygon is to test how many times a ray, starting from the point and going in any fixed direction, intersects the edges of the polygon. If the point is on the outside of the polygon the ray will intersect its edge an even number of times. If the point is on the inside of the polygon then it will intersect the edge an odd number of times.

This can be applied to mesh as well.
So I have this very simple function that tells me if my point is in the mesh :


The transformation allows the mesh to be transposed into the world (otherwise it is only in local space so the function cannot work)

I couldn’t find any blueprint function to count the number of intersections between a ray and a mesh, so I decided to do it myself :

The function looks messy but I will explain it step by step. The general idea is to cut my mesh into triangles and check if the ray intersects one of these triangles. If it does, the number of intersections increases by 1.

To do this, I use the “Get Section from Static Mesh” function ( :warning: When importing the mesh, enable “Allow CPU Access” to allow BP to access the mesh details). This returns the vertices and normals of the mesh and the indices of these. For each group of 3 indices I have a triangle.

In the loop, I first look to see if the triangle has a chance of being intersected, i.e. if the ray is not orthogonal to the normal of the triangle (the top part). Then I use the Möller-Trumbore intersection algorithm (Möller–Trumbore intersection algorithm - Wikipedia) to check if the ray intersects the given triangle. If it does, I increase the number by 1.

Here is my implementation of the algorithm in c++:


I couldn’t manage to use FCustomMeshTriangle (include problem) so I used a 3D point array

Finally, to transform a vector into world coordinates, I use this very simple macro:

Thank you for those who helped me, hopefully it will help others. Don’t hesitate if you have any recommendations

1 Like