Trimesh Collision Sibling

Godot Version

4.2.1

Question

When having a MeshInstance3D you can “Create Trimesh Collision Sibling” in the Editor, which creates a CollisionShape3D but i cant recreate it by code.

There is the create_trimesh_collision() Method, which creates a StaticBody3D with a child node CollisionShape3D, which not behave like the editor version of it.

parent_node.create_trimesh_collision()
	var staticbody = parent_node.get_child(0)
	var collisionshape = staticbody.get_child(0)
	staticbody.reparent(parent_node,false)
	#collisionshape.transform.origin = parent_node.transform.origin

What i want is the collisionshape, which i need to reparent and have its position, which it should have by my settings. Last line is just what i tried.

Any ideas?

Mesh.create_trimesh_shape()

1 Like

I was scrolling over that method, but not sure how to use it

You just call it on the mesh resource of your MeshInstance3D and it will give you the collision shape.

1 Like

Im not sure what to do with this, i tried to assign the output of create_trimesh_shape() to a CollisionShape but nothing changed to that what i tried in the code above

If i print out the editor version of mesh and the shape of new created CollisionShape3D from this approach i get different id’s

<ConcavePolygonShape3D#-9223372012544588576>
<ConcavePolygonShape3D#-9223372012041272076>

Dont know if it means something, i just thought they should be the same, if it would be the same mesh/shape

Mesh::create_trimesh_shape() creates a new collision shape so the object id will always be different.

There are not more steps to the procedural than:

var instance: MeshInstance3D = # wherever your node is
var cshape: CollisionShape3D = CollisionShape3D.new()
cshape.shape =  instance.mesh.create_trimesh_shape()
var body: StaticBody3D = StaticBody3D.new()
body.add_child(cshape)
instance.add_child(body)

That is everything the editor function does.

1 Like

Thank you for your time! When i attach body to a BoneAttachment it wont work anymore, even if i reparent later.

(I try to make meshes of animated 3d model selectable, if there is a better or more easy way, please tell me)

Use simple box / sphere collision shapes for your bone attachments and only if you have to capsule shapes.

Trimesh is the worst physic shape that you can pick for this for both performance and quality reasons. I know everyone wants “perfect” mesh collision but that is not how those things are cooked in games especially not when animated.

Depending on your games frame rate and how quick your animated movement is you might need to set your animation processing to physics else your visuals and your collision will not match up at all bone attachment or not.

1 Like