Unexpected results from Raycast.GetCollisionFaceIndex on Concave Mesh
Tested versions
- Reproducible in Godot Engine v4.4.1 mono
System information
MacOS 15.5
Issue description
When using Raycast.GetCollisionFaceIndex in conjunction with the MeshDataTool's meshDataTool.GetFaceVertex, the resulting points are sometimes from a different face.
In some cases it works fine (as in 1 and 3), could this be to do with the way Godot creates concave collision meshes internally? I'm not sure where the problem is coming from but there seems to be some difference between the face index from the raycast and the face index from the meshdatatool.
Steps to reproduce
Here is the basic script I've been using in my tests, I've also included a simplified project demonstrating the issue.
public override void _PhysicsProcess(double delta)
{
base._PhysicsProcess(delta);
// Move raycast point
float time = Time.GetTicksMsec();
float offset = Mathf.Sin((time) / (1000) );
raycast.Position = new Vector3(offset * 4, raycast.Position.Y, 0);
// Update collision data
if (raycast.IsColliding())
{
CollisionObject3D other = (CollisionObject3D)raycast.GetCollider();
meshInstance3D = other.GetParent().GetNode<MeshInstance3D>("Mesh");
meshDataTool.CreateFromSurface((ArrayMesh)meshInstance3D.Mesh, 0);
// Highlight points that make up the collision face
int collisionFaceIndex = raycast.GetCollisionFaceIndex();
for (var i = 0; i < 3; i++)
{
var index = meshDataTool.GetFaceVertex(collisionFaceIndex, i);
var vertex = meshInstance3D.ToGlobal(meshDataTool.GetVertex(index));
debugSpheres[i].GlobalPosition = vertex;
}
}
}
Minimal reproduction project (MRP)
Can reproduce this (also in 4.5.1):
Godot v4.6.dev (25203e24c) - Windows 11 (build 26100) - Multi-window, 2 monitors - Direct3D 12 (Mobile) - dedicated NVIDIA GeForce RTX 4080 SUPER (NVIDIA; 32.0.15.8180) - AMD Ryzen 7 7800X3D 8-Core Processor (16 threads) - 63.11 GiB memory
Also I couldn't run the MRP but I recreated it in GDScript
Note: Issue exists both with Jolt and GodotPhysics but for Jolt a extra option needs to be enabled for get_collision_face_index to work