How to make a programmatically generated mesh collidable?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By IcyDiamond
:warning: Old Version Published before Godot 3 was released.

Hello!

So in my project, I’m creating a mesh using SurfaceTool and then assigning it to a MeshInstance. But what I’d like to know is what I need to do in order to be able to make my player collide with it.

My player is a KinematicBody with a simple capsule mesh and CollisionShape.

Do I need to generate a CollisionShape for my mesh to be collidable as well? The documentation says that CollisionShape is an “Editor-only helper”, does that mean that I’m not supposed to create it programmatically?

Please excuse me if this is a stupid question, I’m just getting started with Godot and I ran into a wall here and couldn’t find anything online.

I’m using Godot 3.0-rc3

:bust_in_silhouette: Reply From: davidoc

I used this code to add a box, the floor level was 2 units down:

	var box_sape = BoxShape.new()
	var extents = Vector3(2.0,2.0,2.0)
	var static_body = StaticBody.new()
	msh.add_child(static_body)
	static_body.set_translation(Vector3(0,-2,0))
	static_body.set_owner(root)
	var coll_shape = CollisionShape.new()
	static_body.add_child(coll_shape)
	coll_shape.set_owner(root)
	box_sape.set_extents(extents)
	coll_shape.set_shape(box_sape)

I hope it has not changed for version 3.0

:bust_in_silhouette: Reply From: IcyDiamond

Figured it out!

I added a StaticBody and a CollisionShape to the MeshInstance and I generate a Convex Polygon Shape for the collisions

var col_shape = ConcavePolygonShape.new()
col_shape.set_faces(mesh.get_faces())
col.set_shape(col_shape)

Another note: This only seems to work with PRIMITIVE_TRIANGLES

Thanks to Tapio Pyrhönen on Facebook for helping me out!