NVIDIAGameWorks / PhysX

NVIDIA PhysX SDK
Other
3.16k stars 802 forks source link

How to convert ASSIMP mesh to physx convex mesh #578

Closed KielanT closed 2 years ago

KielanT commented 2 years ago

Hi, I'm trying to convert a mesh from an assimp object to a physx convex mesh. The code I have done works fine for object such as a vehicle, however for a mesh that is a loop the physx mesh gets filled in. (Images below). Any ideas on how I can fix this?

` // Create the mesh static physx::PxConvexMesh CreateConvexMesh(const physx::PxVec3 verts, const physx::PxU32 numVerts, physx::PxPhysics physics, physx::PxCooking cooking) { // Create descriptor for convex mesh physx::PxConvexMeshDesc convexDesc; convexDesc.points.count = numVerts; convexDesc.points.stride = sizeof(physx::PxVec3); convexDesc.points.data = verts; convexDesc.flags = physx::PxConvexFlag::eCOMPUTE_CONVEX;

    physx::PxConvexMesh* convexMesh = NULL;
    physx::PxDefaultMemoryOutputStream buf;
    if (cooking->cookConvexMesh(convexDesc, buf))
    {
        physx::PxDefaultMemoryInputData id(buf.getData(), buf.getSize());
        convexMesh = physics->createConvexMesh(id);
    }

    return convexMesh;
}

// Creates the object out of the assimp mesh
physx::PxConvexMesh* class::MakeObject(int index, Entity* entity)
{
    physx::PxU32 vertexCount;
    std::vector<physx::PxVec3> vertices;
    if (entity != nullptr)
    {

        if (mesh)
        {

            vertexCount = mesh->GetNumberOfVertices(index);

            std::vector<CVector3> meshVertices = mesh->GetVertices();
            // Copy from cvector array to PxVec3 array
            for (int i = 0; i < vertexCount; i++)
            {
                vertices.push_back(physx::PxVec3(trackVertices[i].x, trackVertices[i].y, trackVertices[i].z));
            }

            physx::PxVec3* v = vertices.data();

            return CreateConvexMesh(v, vertexCount, m_PhysicsSystem->GetPhysics(), m_PhysicsSystem->GetCooking());
        }
    }
    return nullptr;
}

`

What the mesh looks like (In blender but looks like this in my project) LoopRendered

What the mesh looks like in the physx visual debugger Loop

Zeblote commented 2 years ago

It's a convex mesh, so this is the expected result. If you need it to be hollow, try creating a triangle mesh collider instead.

KielanT commented 2 years ago

It's a convex mesh, so this is the expected result. If you need it to be hollow, try creating a triangle mesh collider instead.

This solved my issue thank you