At runtime, you can indeed use
create_trimesh_collision() on the
MeshInstance: MeshInstance — Godot Engine (3.1) documentation in English
It creates a child static body with the mesh as collision shape. How do you know it doesn’t work? It might be a bit impractical to customize though.
If you want to setup the node hierarchy yourself, you can instead generate only the
Shape using the method of the same name from the
Mesh resource directly: Mesh — Godot Engine (3.1) documentation in English
This returns a
Shape which you can assign to a
CollisionShape node, which you can itself place under a physics body of your choice.
Note: if your body is not static, you may want a convex shape, otherwise it won’t work.
Although, why do you need to create collisions at runtime? You can make them in the editor and turn the shapes on/off at runtime.
If you do it at runtime from an already-built
Mesh, be aware that Godot actually downloads it back from the graphics card, builds a BVH, gives it to Bullet, which builds a BVH again. This is fine in editor but in game it’s 2 to 3 times slower than it should be.
This may be fine for a low amount of objects, but if you plan doing this on hundreds of meshes and experience performance issues, consider baking them in editor beforehands anyways.
If they are procedurally generated, there is a way to bypass the overhead but it’s only efficiently done using C++: Creating physics shapes is extremely slow · Issue #54 · Zylann/godot_voxel · GitHub
I think they can if you select a
MeshInstance, a menu should appear in the viewport toolbar with that option in it.
If you imported your model from Blender, you could also make collision shapes from there and exploit the importer by naming them with the
*_col suffix: Importing 3D scenes — Godot Engine (3.1) documentation in English
However if you reaaaaally want to create them by hand in the editor, the menu doesn’t seem to work for multiple selection.
Zylann | 2019-09-17 17:27