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 (
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