Missing triangles in my triangle mesh

Vino
Posts: 3
Joined: Fri Nov 11, 2011 10:35 am

Missing triangles in my triangle mesh

Post by Vino »

Hello! I love Bullet, it works great, when it works. I'm hoping someone can help me with something I seem to have done wrong. I have a btBvhTriangleMeshShape that I use to do my world collision, but...

Image

A lot of times my collision model is missing triangles. In this case, the triangles on the top half of the right block are missing, as evidence by the lacking green debug lines. Which triangles it is missing depends on how many triangles are in the model in total, but it seems to just cut out after a bit. Whole sections of the model can be missing sometimes. I'm certain that the problem isn't one of not passing my entire triangle array - I've verified that I am passing all of the triangles in. Here's how I create the thing:

Code: Select all

	m_apCollisionMeshes[iModel].m_pIndexVertexArray = new btTriangleIndexVertexArray(iTriangles, aiIndices.data(), sizeof(int)*3, avecVertices.size(), &avecVertices[0].x, sizeof(Vector));
	m_apCollisionMeshes[iModel].m_pCollisionShape = new btBvhTriangleMeshShape(m_apCollisionMeshes[iModel].m_pIndexVertexArray, true);
I've been stepping through the Bullet code with a debugger trying to figure out how it works. I have a hunch that because my models are not always continuous meshes, the code that builds the BVH tree sometimes misses certain triangles. But I haven't had enough time to read and understand that code clearly (the stuff in btQuantizedBvh::buildTree and sortAndCalcSplittingIndex) so I don't know.

Can anybody help? Do I just need to make sure I split non-continuous meshes into different groups? Or I forgot to set some flag or something? Or maybe I should be using a different approach for my world collision?
Vino
Posts: 3
Joined: Fri Nov 11, 2011 10:35 am

Re: Missing triangles in my triangle mesh

Post by Vino »

Update. If I add a separate indexed mesh for each material on my model then it helps.

Code: Select all

	m_apCollisionMeshes[iModel].m_pIndexVertexArray = new btTriangleIndexVertexArray();
	for (size_t i = 0; i < aiIndices.size(); i++)
	{
		if (!aiIndices[i].size())
			continue;

		btIndexedMesh m;
		m.m_numTriangles = aTriangles[i].size()/3;
		m.m_triangleIndexBase = (const unsigned char *)&aiIndices[i][0];
		m.m_triangleIndexStride = sizeof(int)*3;
		m.m_numVertices = avecVertices[i].size();
		m.m_vertexBase = (const unsigned char *)&avecVertices[i][0].x;
		m.m_vertexStride = sizeof(Vector);
		m_apCollisionMeshes[iModel].m_pIndexVertexArray->addIndexedMesh(m, PHY_INTEGER);
	}

	m_apCollisionMeshes[iModel].m_pCollisionShape = new btBvhTriangleMeshShape(m_apCollisionMeshes[iModel].m_pIndexVertexArray, true);
I think that's because the way I model each material is a continuous mesh. I imagine that if I were to do something crazy with a model, I might get missing polygons again, so I don't feel like this is a long-term solution. Any ideas?