RayCast Not Detecting Hits Inside Mesh When Distance is Large

Godot Version : 4.3

Hey Godot devs :waving_hand:

I wrote a script to detect if a point is inside a closed mesh. To do this, I fire 6 raycasts (+X, -X, +Y, -Y, +Z, -Z). If all of them hit properly, I assume the point is inside.

The issue: sometimes, even though the point is clearly inside the object (see image below), some of the raycasts fail and return no hit :sob:

After testing a bit, I realized that the bigger the query distance, the more likely I am to get false negatives. It’s like large queries break the detection somehow. And honestly, I’d rather not manually tweak the ray length for every mesh I test.

So my questions are:

Why does a larger query make it unreliable?

Is there a solid method to make the detection work regardless of ray size?

Thanks in advance, y’all rock :black_heart:

PS: yes the photo is inside an organ

Is the raycast long enough to detect a collision, but doesn’t because the length is too long? Or is the raycast too short to actually hit the walls of the organ? What numbers are you using? I can’t really tell from the screenshot.

Are you sure the mesh is watertight?

The raycast length I’m using is 1000.0, but my mesh is relatively small — for reference, the point radius I’m working with is 0.001. The most accurate raycast I can currently achieve is with a length of 10. However, I would like to be able to use any raycast length I want, so it can adapt to different mesh sizes.

I’ve already cleaned the mesh in Blender beforehand and verified there are no issues with the model.

I found the solution: the problem actually came from Godot’s physics engine. If you switch to the Jolt physics engine, the raycast issue disappears.