How to set a mesh as a collision shape

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

I have the individual vertices to create a mesh and can create one using the surface tool, however, what I need is to be able to set that mesh as the collision shape. I tried using the CollisionShape node however is says in the docs that it is only used as an editor helper so I should not use it for this purpose. Without it, how can I accomplish this?

:bust_in_silhouette: Reply From: Jowan-Spooner

Follow this steps:

  1. Create an new node of type KinematicBody, RigidBody or StaticBody, depending on what you will use the object for.
  2. Add your mesh as a child of this node. To test it you can add MeshInstance node and create a CubeMesh.
  3. Having the mesh node selected you will see a button appear on top of the 3d viewport saying ‘Mesh’.
  4. Click it.
  5. Select create Convex Collision Sibling.

Now you objects should collide.

If not please ask again. Good luck.

What I want is to do this in gdscript though. I am procedurally generating the mesh using gdscript but need to also then procedurally generate the collision mesh as well. I am just confused as to how to assign the mesh I generate as the shape for the Static Body.

orosmatthew | 2019-05-14 13:38

Oh I understand. Have you tried to use the make_convex_from_brothers() function of a collision shape node? Don’t know if that works.

Jowan-Spooner | 2019-05-14 13:45

Oh! I found a setter for the CollisionShape set_shape(). It is exactly what I need because I just need to pass in the vertices. Thanks for your help anyway. :wink:

orosmatthew | 2019-05-14 19:20

and how can you do this from GDSCRIPT I can’t find a parameter to instantiate a convect collision sibling from GDScript?

extends Spatial

var NodosHijos =  null#variable que va a guardar una lista de objetos

func _ready():
  NodosHijos = get_children()#toma todos los hijos

  for i in NodosHijos:
	  #print(i.name)
	   var mesh = i.get_child(0)#crea las colsiones y las mallas
	  print(mesh.name)
	  mesh.create_trimesh_collision() #no work

ariel | 2019-09-15 13:27

I use c# so I’m not sure what the exact gdscript function name is, but there should be a mesh method called something like “createconvexcollision”.

wooshuwu | 2022-02-22 08:50

:bust_in_silhouette: Reply From: orosmatthew

CollisionShape has a set_shape() which takes in vertices.