ProceduralMeshComponent doesn't collide with anything

I’m trying to create procedural objects that collide with the world, but I can’t make it work.

I’m creating a simple box, with physics disabled; I’m using the First Person C++ template as testing room.

This is the code, executed on an Actor when the game starts:

TArray mVertices;
TArray mIndices;
TArray mNormals;
TArray mUvs;
TArray mTangents;
TArray mVertexColors;

mMesh->bUseComplexAsSimpleCollision = false;


mMesh->SetCollisionEnabled(ECollisionEnabled::QueryAndPhysics);
mMesh->SetCollisionResponseToAllChannels(ECollisionResponse::ECR_Block);
mMesh->SetCollisionObjectType(ECollisionChannel::ECC_PhysicsBody);

UKismetProceduralMeshLibrary::GenerateBoxMesh(FVector(100, 100, 100), mVertices, mIndices, mNormals, mUvs, mTangents);
mMesh->CreateMeshSection(0, mVertices, mIndices, mNormals, mUvs, mVertexColors, mTangents, true);

mMesh->RegisterComponent();

Then I’m grabbing pre-made cubes that are in the scene and I move them so that they should hit the procedural cube; except that they don’t, they pass right through it.

If I grab the procedural cube and move it around, it doesn’t hit anything. If I enable “simulate physics” on the procedural cube, it just fall through the ground.

I have not verified this assumption, but here is what I think: You set bUseComplexAdSimpleCollision = false, which means you are telling the engine to not use the visible mesh as collision, but instead some special collision geometry, that is made up of primitives. But you don’t provide that. You only provide the geometry, but you tell the engine not to use it for collision. So the engine is left with nothing to calculate collision. If you set this to true, it might work.

1 Like

Actually you’re right, if I set it to true then if I grab pre-made cubes and throw them to the procedural cube they get blocked!

But there is a reason if I set it to false: if I try to simulate physics on the procedural cube, then it throws me this error and it doesn’t simulate anything:

PIE:Warning: Warning Trying to simulate physics on ‘’/Game/FirstPersonCPP/Maps/UEDPIE_0_FirstPersonExampleMap.FirstPersonExampleMap:PersistentLevel.BP_MyActor.UCustomProceduralComponent’’ but it has ComplexAsSimple collision.
LogPhysics:Error: PHYSX: (d:\build++ue4+release-4.14+physx_compile\sync\engine\source\thirdparty\physx\physx_3.4\source\physx\src\NpRigidBodyTemplate.h 479) eINVALID_PARAMETER : RigidBody::setRigidBodyFlag: dynamic meshes/planes/heightfields are not supported!

How can I solve this so that I can simulate physics AND have the procedural cube collide with other things?

A second wild guess :smiley: Simulating physics only works when the physics engine knows about a mass of an object. Since you just provided vertecies and triangles the engine may not know what mass your object has. Have you tried to use the mass override in the details panel of your mesh istance, and just set it to 100 or whatever?

Actually I discovered a method of UProceduralMeshComponent called “AddCollisionConvexMesh”, which takes the list of vertices that compose the collision shell basically. That plus the bUseComplexAsSimpleCollision boolean set to false will make the procedural objects behave like they should.

To make the cube behave like any object now I just call that method after creating the section and I pass the vertices of the section to it.

Like this:

mesh->bUseComplexAsSimpleCollision = false;
mMesh->CreateMeshSection(0, mVertices, mIndices, mNormals, mUvs, mVertexColors, mTangents, true);
AddCollisionConvexMesh(mVertices);

It seems to work fine (the errors are gone), even with more complex procedural meshes! :smiley:

Thanks for the help pulp_user :slight_smile:

1 Like

Glad I could help :slight_smile:

Although it still interests me if the above would work. The AddCollisionConvexMesh probably provides a primitive, of which the mass is known. Can you just try out if it works with the mass override (I may need that in the future myself :D)

Sure! I tried but it doesn’t work, it keeps throwing me the errors I mentioned above.

The thing goes probably like you said: with bUseComplexAsSimpleCollision set to false the engine needs to know the shape of the object, and that’s what AddCollisionConvexMesh() is for. With the boolean set to true it works for simple collisions only (meaning that other objects can collide with the cube), while it’s not enough to simulate physics.

One more thing: right now I’m having another issue regarding physics (physics handles in particular). I described the problem here: https://forums.unrealengine.com/showthread.php?138646-PhysicsHandleComponent-doesn-t-move-when-SetTargetLocation()-is-called-in-C&p=675951#post675951

Maybe you can look at it, when you have time? Since you know your way with physics objects :slight_smile: