Procedural Mesh only detect entering collision

Hello,

I’m using procedural mesh to get overlapping actor of custom shapes. As I’m quite new to procedural meshes, I might have done something wrong. The problem is that my procedural mesh does not detect collisions, if I move it into my enemies (in unpossesed mode), but if I move an enemy into my procedural mesh, it detects the collision.

Here is the code of my procedural mesh :

ACone::ACone() 
{
	mesh = CreateDefaultSubobject<UProceduralMeshComponent>(TEXT("GeneratedMesh"));  
	RootComponent = mesh;

	precalcPoints = TArray<FVector>();    
	for (int i = 0;i<360; i+=10)    
	{
		precalcPoints.Add(FVector(FMath::Cos(FMath::DegreesToRadians(i)), FMath::Sin(FMath::DegreesToRadians(i)) , 0.0f));
	}
	// New in UE 4.17, multi-threaded PhysX cooking.
	mesh->bUseAsyncCooking = true;
	mesh->SetMobility(EComponentMobility::Movable);
	mesh->SetCollisionEnabled(ECollisionEnabled::QueryAndPhysics);
	mesh->SetCollisionProfileName("OverlapAll");
	mesh->OnComponentBeginOverlap.AddDynamic(this, &ACone::OnPickup);
	SetActorEnableCollision(true);
}

void ACone::CreateCone()
{
	float angleInRad = FMath::DegreesToRadians(_angle / 2.0f);
	TArray<FVector> vertices;
	vertices.Add(FVector(0, 0, 0));//start
	float _currentAngle = -_angle / 2.0f;
	vertices.Add(FVector(FMath::Cos(-angleInRad), FMath::Sin(-angleInRad), 0)*_length);
	_currentAngle -= (int)_currentAngle % 10;
	for (int i = _currentAngle; i < _angle / 2.0f; i += 10)
	{
		int index = i / 10;
		if (index < 0)
		{
			index = index % 36;
			index += 36;
		}
		else
		{
			index = index % 36;
		}

		vertices.Add(precalcPoints[index] * _length);
	}
	if (_currentAngle != _angle / 2.0f)
		vertices.Add(FVector(FMath::Cos(angleInRad), FMath::Sin(angleInRad), 0)*_length);


	TArray<int32> Triangles;
	for (int i = 0; i < vertices.Num() - 1; i++)
	{
		Triangles.Add(0);
		Triangles.Add(i + 2);
		Triangles.Add(i + 1);
	}

	mesh->CreateMeshSection_LinearColor(0, vertices, Triangles, {}, {}, {}, {}, true);

	// Enable collision data
	mesh->ContainsPhysicsTriMeshData(true);
}

I’ve found the solution ! The problem was that I created the procedural mesh like this :

mesh->CreateMeshSection_LinearColor(0, vertices, Triangles, {}, {}, {}, {}, true);
mesh->SetCollisionConvexMeshes({ vertices });
mesh->bUseComplexAsSimpleCollision = false;

By digging into the source code, I’ve found that when the overlap is updated, It looks at the collisionFlag, which is bUseComplexAsSimpleCollision = true, by default for procedural mesh components. The problem is that changing the status of bUseComplexAsSimpleCollision does not update the collision. So all my collisions are build as complex and in FBodyInstance::OverlapTestForBodiesImpl, which is called on movement, spawn, etc… As the procedural mesh is handled as a complex collision, it has a ShapeType of PxGeometryType::eTRIANGLEMESH.
The problem is that PxGeometryType::eTRIANGLEMESH are not handled by FBodyInstance::OverlapPhysX_AssumesLocked so they will just ignore the overlap

if(ShapeType == PxGeometryType::eHEIGHTFIELD || ShapeType == PxGeometryType::eTRIANGLEMESH)
{
	continue;	//we skip complex shapes - should this respect ComplexAsSimple?
}

And by executing the collision creation of my procedural mesh like this, I was able to get overlaps with the convex collision :

 mesh->bUseComplexAsSimpleCollision = false;
 mesh->CreateMeshSection_LinearColor(0, vertices, Triangles, {}, {}, {}, {}, true);
 mesh->SetCollisionConvexMeshes({ vertices });

This set the procedural mesh as a simple collision before creating the collisions, which will handle it as a convexMesh which is supported by FBodyInstance::OverlapPhysX_AssumesLocked

So maybe Epic forgot to support PxGeometryType::eTRIANGLEMESH on overlap, or it is not quite possible (not really a pro in physics), but I think that bUseComplexAsSimpleCollision modification should update the collision of the mesh as it is a quite important change.

1 Like

Me too struggling with procedural mesh. I can’t be more thankful that I stumbled upon your post.
Now I am able to trigger overlap events. I have been searching this for straight 4 days.

Thank you so much for your detailed investigation. Really appreciate that you captured the issue :slight_smile: