How to read collision mesh data and primitives from a StaticMeshComponent in C++

I found a way to do it.

The collision data is kept within the UBodySetup inside UStaticMesh, which you can get from a StaticMeshComponent via GetStaticMesh method.

Within the UBodySetup there is the data structure AggGeom of type FKAggregateGeom. This appears protected in code. However it is somehow exposed to the external C++ classes.

There are 4 arrays inside this class (BoxElems, SphereElems, SphylElems, ConvexElems) that correspond to each of the four collision body types added to the StaticMesh for collision detection.

Reading the first three (primitive colliders) is trivial. You just need to iterate through the arrays and read parameters (center, radius, orientation, etc) of the primitive colliders.

Reading ConvexElems is a little more complicated. The data for each convex element is kept within the data structures provided by the PhysX API. Namely the PxConvexMesh class.

Accessing the methods of PxConvexMesh class requires addition of the “PhysX” module as a public dependency in the *Build.cs file to the project. And then one should include the “PhysXPublic.h” header file within the cpp file you want to read the mesh data.

This will give you access to the internal index and vertex arrays as well as the faces (polygons) of the convex mesh data (stored within PxConvexMesh).

I guess this answers my own question.

7 Likes