Static Mesh created from MeshDescription doesn't have a collision

I’m making a procedurally generated terrain using a MeshDescription:

void ATerrain::createTerrainMesh() {
	
	
	//Describes the mesh to create
	FMeshDescription terrainDescriptor;


	FStaticMeshAttributes attributes(terrainDescriptor);
	attributes.Register();
	
	//creates static mesh
	UStaticMesh *terrain = NewObject<UStaticMesh>(this, FName(TEXT("ProceduralStaticMesh")));

	//the class that builds the mesh
	FMeshDescriptionBuilder meshDescBuilder;
	meshDescBuilder.SetMeshDescription(&terrainDescriptor);
	meshDescBuilder.EnablePolyGroups();
	meshDescBuilder.SetNumUVLayers(1);

	// Array to store all the vertices
	TArray< FVertexInstanceID > vertexInsts;

	TArray< FVertexID > vertexIDs; vertexIDs.SetNum(vertexNum*vertexNum);

	float dist = terrainSize / vertexNum; //distance between each vertex

	// Create vertices
	for (int i = 0; i < vertexNum; i++) { //x
		for (int j = 0; j < vertexNum; j++) { //y

			vertexIDs[cti(i,j)] = meshDescBuilder.AppendVertex(FVector(dist * i, dist * j, getVertexHeight(i,j)));

		}

	}

	//add istances
	FVertexInstanceID instance;
	for (int i = 0; i < vertexNum; i++) { //x
		for (int j = 0; j < vertexNum; j++) { //y
			instance = meshDescBuilder.AppendInstance(vertexIDs[cti(i, j)]);

			meshDescBuilder.SetInstanceNormal(instance, FVector(0, 0,1));
			meshDescBuilder.SetInstanceUV(instance, FVector2D((uvScale/ (vertexNum - 1))*i, (uvScale/ (vertexNum - 1))*j), 0 );
			meshDescBuilder.SetInstanceColor(instance, FVector4(1.0f, 1.0f, 1.0f, 1.0f));
			//meshDescBuilder.SetAllEdgesHardness(true);
			vertexInsts.Add(instance);
		}

	}

	//create triangles
	FPolygonGroupID polygonGroup = meshDescBuilder.AppendPolygonGroup();
	for (int i = 0; i < vertexNum - 1; i++) { //x
		for (int j = 0; j < vertexNum - 1; j++) { //y

			meshDescBuilder.AppendTriangle(vertexInsts[cti(i, j+1)], vertexInsts[cti(i+1, j)], vertexInsts[cti(i, j)], polygonGroup);
			meshDescBuilder.AppendTriangle(vertexInsts[cti(i, j + 1)], vertexInsts[cti(i + 1, j + 1)], vertexInsts[cti(i+1, j)], polygonGroup);
		}

	}

	// Build static mesh
	TArray<const FMeshDescription*> meshDescPtrs;
	meshDescPtrs.Emplace(&terrainDescriptor);

	terrain->BuildFromMeshDescriptions(meshDescPtrs, false);
	mesh->SetStaticMesh(terrain);
	mesh->bDrawMeshCollisionIfComplex = true;
	mesh->SetCollisionObjectType(ECollisionChannel::ECC_WorldStatic);
	mesh->SetCollisionEnabled(ECollisionEnabled::QueryAndPhysics);
	mesh->bUseDefaultCollision = true;
	mesh->SetCollisionResponseToChannel(ECollisionChannel::ECC_Pawn, ECollisionResponse::ECR_Block);
	mesh->SetSimulatePhysics(true);
	mesh->SetEnableGravity(false);
}

I don’t undestand why this is not working. Because I set bDrawMeshCollisionIfComplex to true when I run the game I can see some cyan lines indicating the collision. When in the editor I go into “view player collision mode” the terrain colors purple, everything is hinting at the collision being there but it doesn’t work.

First I tried with my player controller then I placed a cube with physics and they both fell through the terrain.

1 Like

Do you know how to assign material for this mesh?