How to Copy Collision Mesh from one Static Mesh to another Static Mesh

Hello friends,

I am trying to copy the Collision Meshes (in my case the convex decomposition collision meshes) from one StaticMesh to another StaticMesh.

After some looking around I read that the Collision Meshes are stored in the BodySetup and so I tired something like this:

UBodySetup* OldBodySetup = MyOldStaticMeshActor->GetStaticMeshComponent()->GetBodySetup();
FKAggregateGeom OldAggregateGeom = OldBodySetup->AggGeom;

MyNewStaticMeshActor->GetStaticMeshComponent()->GetBodySetup()->AddCollisionFrom(OldAggregateGeom);

This works fine for simple collsion but with convex decomposition meshes I encounter a strange thing:

If I open the “receiver” static mesh (MyNewStaticMeshActor) in the editor, I can see in the collision section that there are indeed 10 Convex Elements, so the copy process worked but the collisions are not shown (see picture 1) and also not working.

But here comes the strange thing: As soon as I change ANY collision setting with the mouse pointer (like “Double Sided Geometry”, the collision meshes show up! (see picture 2).

So it seems that changing any setting in the collisions section in the editor “refreshes” something and that makes the collision meshes appear and also work.

Does anyone have a clue what to add to the code (the refreshing part) ???

Thanks, Alex

1 Like

Hi,

I solved it! I knew the meshes appear if I change the material (that triggers a refresh) so I checked what the change material does. The critical lines in the code are:

MyNewStaticMeshActor->GetStaticMeshComponent()->GetBodySetup()->InvalidatePhysicsData();
MyNewStaticMeshActor->GetStaticMeshComponent()->GetBodySetup()->CreatePhysicsMeshes();

so the whole code to copy convex decomposed collision is:

UBodySetup* OldBodySetup = MyOldStaticMeshActor->GetStaticMeshComponent()->GetBodySetup();

FKAggregateGeom OldAggregateGeom = OldBodySetup->AggGeom;
 
UBodySetup* NewBodySetup = MyNewStaticMeshActor->GetStaticMeshComponent()->GetBodySetup(); 

NewBodySetup ->AddCollisionFrom(OldAggregateGeom);
NewBodySetup ->InvalidatePhysicsData();
NewBodySetup ->CreatePhysicsMeshes();
1 Like