create_trimesh_collision not working for ArrayMesh?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By AiTechEye

When im using create_trimesh_collision to any ArrayMesh i just gets this error

create_trimesh_collision: Condition "!static_body" is true.
scene/3d/mesh_instance.cpp:571 @ create_trimesh_collision()

but create_convex_collision works fine.

i aloso tried create_trimesh_shape it gives no error but just returns null

any idea?

test code:

func _ready():
#planemesh - works fine
	var m = MeshInstance.new()
	m.mesh = PlaneMesh.new()
	m.transform.origin = Vector3(4,1,0)
	add_child(m)
	m.create_trimesh_collision()

#arraymesh - error
	var vertices = PoolVector3Array()
	vertices.push_back(Vector3(1, 0, 0))
	vertices.push_back(Vector3(1, 0, 1))
	vertices.push_back(Vector3(0, 0, 1))
	vertices.push_back(Vector3(0, 0, 0))

	var arr_mesh = ArrayMesh.new()
	var arrays = []
	arrays.resize(ArrayMesh.ARRAY_MAX)
	arrays[ArrayMesh.ARRAY_VERTEX] = vertices
	arr_mesh.add_surface_from_arrays(Mesh.PRIMITIVE_TRIANGLE_FAN, arrays)
	var m2 = MeshInstance.new()
	m2.mesh = arr_mesh
	m2.transform.origin = Vector3(0,1,0)
	add_child(m2)
	m2.create_trimesh_collision()
:bust_in_silhouette: Reply From: cascas

Not sure why but create_trimesh_collision() only works when using GLES3. A workaround is using a StaticBody with a ConcavePolygonShape3D to which you add the faces.

:bust_in_silhouette: Reply From: AiTechEye

Finally i found the solution, the trimesh doesnt work with Mesh.PRIMITIVE_TRIANGLE_FAN, but with Mesh.PRIMITIVE_TRIANGLES and add_triangle_fan()

so here is a working example

func _ready():
	var mesh = Mesh.new()
	var vertices = [Vector3(1,0,0), Vector3(1,0,1), Vector3(0,0,1),Vector3(0,0,0)]
	var UVs = [Vector2(0,0), Vector2(0,1), Vector2(1,1), Vector2(1,0)]
	var st = SurfaceTool.new()
	st.begin(Mesh.PRIMITIVE_TRIANGLES)
	st.add_triangle_fan(vertices,UVs)
	st.commit(mesh)
	
	$MeshInstance.mesh = mesh
	$MeshInstance/StaticBody/CollisionShape.shape = mesh.create_trimesh_shape()