How to convert ASSIMP mesh to physx convex mesh

Started by
6 comments, last by KielanT 1 year, 10 months 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 when the model being read in is a single mesh, but if i want to bring a model that contains multiple meshes the physx mesh is joined together. This isn't an issue with the assimp since my project is rendering the assimp meshes completely fine. How can I 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 a single object looks like (blender but same result as the project)
What the physx object looks like which is correct (physx visual debugger)
What the two object should look like (in blender but same result in project)
What the physx object looks like (Physx visual debugger)

None

Advertisement

KielanT said:
This isn't an issue with the assimp since my project is rendering the assimp meshes completely fine. How can I fix this?

It looks like you export two ‘objects’ to one file, and then PhysX does not know you intend to separate them, so it calculates the convex hull simply over all triangles.
There is nothing wrong with any software. There just is no information identifying multiple objects. But likely you just want to export one file per object.
Eventually you could figure out to use something like multiple materials to separate multiple objects, but not sure if Assimp can use this to delete unwanted triangles. Probably not.
You could implement this on your side, but that's maybe more work than exporting manually and individually.

I have this problem quite often, and then i search for some script for my modeling tool to make the export work less tedious.
Iirc, Blender can export only selected faces of a scene, so probably no script needed to ease up the work even more, assuming something like a double click can quickly select all triangles of a connected piece from the scene.

Though, maybe i got you completely wrong.
If your problem is ‘I want a single rigid body with a shape of two disconnected hulls’, then that's usually possible. In Newton physics engine the feature is called ‘compound collision’. It allows to create a single rigid body from the combination of a box, a cylinder, and a convex hull from a mesh, for example. I'm sure PhysX can do this too, but idk how they call it.

Hi, The main problem isn't exactly with two meshes but one, where I have a loop but it gets filled in, any ideas?@JoeJ

Object in blender
Object in physx visual debugger

None

Ah, i see. Then what you want is a ‘convex decomposition’ of your model. This splits a concave mesh into convex pieces, which a physics engine requires to do collision detection.

I found something related here, so maybe that's part of Apex, but not included into PhysX itself.
Another widely used library is V-HACD, which seems to have some example to use it with PhysX as well.
Pretty sure there are also convex decomposition plugins for Blender, or it's already built in.

However, check the latter link to see those algorithms usually generate an approximated, but not the exact same representation of geometry.
So for a racing track in a racing game it's probably too inaccurate, while it's good enough for all other models like cars, rocks, etc.

For the track you might still need to do something manually / specifically.
E.g., if you could create your track procedurally from a spline, like in this basic example, you could do this so it also generates convex segments to use for physics.

Update to my issue, I got it working by making a PxTriangleMesh instead of a PxConvexMesh

None

KielanT said:
I got it working by making a PxTriangleMesh instead of a PxConvexMesh

Oh sorry - somehow i did not think about this. For static scene that's the usually the best solution.

@JoeJ No problem mate, I appreciate you help

None

This topic is closed to new replies.

Advertisement