btTriangleMeshShape Read Access Violation

Post Reply
ahzeke
Posts: 1
Joined: Thu Dec 15, 2022 5:36 am

btTriangleMeshShape Read Access Violation

Post by ahzeke »

Recently implemented the btTriangleMeshShape into a game engine I've been working on. For some reason I get this error:

"Exception thrown: read access violation.
graphicsbase was 0x23176382370."

But not on start up/creation rather when a dynamic body collides with it. Looking through the docs "graphicsbase" seems to have something to do with indexing the triangle array. Line 133 of btBvhTriangleMeshShape.cpp:

Code: Select all

for (int j = 2; j >= 0; j--)
		{
			int graphicsindex = indicestype == PHY_SHORT ? ((unsigned short*)gfxbase)[j] : gfxbase[j];
			
			if (type == PHY_FLOAT)
			{
				float* graphicsbase = (float*)(vertexbase + graphicsindex * stride);
			
				m_triangle[j] = btVector3(graphicsbase[0] * meshScaling.getX(), graphicsbase[1] * meshScaling.getY(), graphicsbase[2] * meshScaling.getZ());
			}
			else
			{
				double* graphicsbase = (double*)(vertexbase + graphicsindex * stride);
			
			    m_triangle[j] = btVector3(btScalar(graphicsbase[0]) * meshScaling.getX(), btScalar(graphicsbase[1]) * meshScaling.getY(), btScalar(graphicsbase[2]) * meshScaling.getZ());
			}
		}
My guess would that it has something to do with the striding but I don't think I'm doing anything wrong. Here's how I create the shape:

Code: Select all

// btTrangleIndexVertexArray constructor arguments
		int triangleCount = (meshComponent.vertexData.indices[1] - meshComponent.vertexData.indices[0]) / 3.0f; //  Triangle stored as 3 indices, so num of indices / 3
		int* indexData = (int*)const_cast<INDEX_TYPE*>(meshComponent.vertexData.pIndexData + meshComponent.vertexData.indices[0]); // The offset of the index data into the global index buffer
		int triangleSize = sizeof(INDEX_TYPE) * 3;
		int vertexCount = meshComponent.vertexData.vertexCount;
		btScalar* vertexData = (btScalar*)const_cast<Vertex*>(meshComponent.vertexData.pVertexData); // pointer magic to remove const
		int vertexSize = sizeof(Vertex);

		meshComponent.pTriangleArray = new btTriangleIndexVertexArray(triangleCount, indexData, triangleSize, vertexCount, vertexData, vertexSize);

		// Create the collision shape using the loaded triangle array
		meshComponent.pTriangleMeshShape = new btBvhTriangleMeshShape(meshComponent.pTriangleArray, true, true);
Any help is greatly apprectiated.
Post Reply