Unexpected results when using raycast.GetCollisionFaceIndex with the MeshDataTool

Godot Version

Godot Engine v4.4.1.stable.mono.official

Question

Hi all!,

I’m using raycast.GetCollisionFaceIndex and sending that to the MeshDataTool to get information from the hit. The problem is, I seem to be getting unexpected results. In this example I’ve tried to highlight the points that make up the collided face but it doesn’t always work (as in 2 and 4).
output

Here is the basic script.

    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;
            }
        }
    }

I think the problem might be that the collision mesh has a different order to the mesh I’m giving to the meshdata tool but I’m not sure.

Any ideas?
Thanks!
Pete

1 Like