I have three meshes in the shape of targets, for which I want each to have their mesh as the collision shape. How would I go about this?
Can anyone provide some sort of code to help me through this? Thanks in advance

Code: Select all
std::vector<unsigned short> targetIndices;
std::vector<glm::vec3> targetVertices;
std::vector<glm::vec2> targetUvs;
std::vector<glm::vec3> targetNormals;
res = loadAssImp("target2.obj", targetIndices, targetVertices, targetUvs, targetNormals);
// first create a btTriangleIndexVertexArray
// NOTE: we must track this pointer and delete it when all shapes are done with it!
btTriangleIndexVertexArray* data = new btTriangleIndexVertexArray;
// add an empty mesh (data makes a copy)
btIndexedMesh tempMesh;
data->addIndexedMesh(tempMesh, PHY_FLOAT);
// get a reference to internal copy of the empty mesh
btIndexedMesh& mesh = data->getIndexedMeshArray()[0];
// allocate memory for the mesh
const int32_t VERTICES_PER_TRIANGLE = 3;
size_t numIndices = targetIndices.size();
mesh.m_numTriangles = numIndices / VERTICES_PER_TRIANGLE;
if (numIndices < std::numeric_limits<int16_t>::max()) {
// we can use 16-bit indices
mesh.m_triangleIndexBase = new unsigned char[sizeof(int16_t) * (size_t)numIndices];
mesh.m_indexType = PHY_SHORT;
mesh.m_triangleIndexStride = VERTICES_PER_TRIANGLE * sizeof(int16_t);
} else {
// we need 32-bit indices
mesh.m_triangleIndexBase = new unsigned char[sizeof(int32_t) * (size_t)numIndices];
mesh.m_indexType = PHY_INTEGER;
mesh.m_triangleIndexStride = VERTICES_PER_TRIANGLE * sizeof(int32_t);
}
mesh.m_numVertices = targetVertices.size();
mesh.m_vertexBase = new unsigned char[VERTICES_PER_TRIANGLE * sizeof(btScalar) * (size_t)mesh.m_numVertices];
mesh.m_vertexStride = VERTICES_PER_TRIANGLE * sizeof(btScalar);
// copy vertices into mesh
btScalar* vertexData = static_cast<btScalar*>((void*)(mesh.m_vertexBase));
for (int32_t i = 0; i < mesh.m_numVertices; ++i) {
int32_t j = i * VERTICES_PER_TRIANGLE;
const glm::vec3& point = targetVertices[i];
vertexData[j] = point.x;
vertexData[j + 1] = point.y;
vertexData[j + 2] = point.z;
}
// copy indices into mesh
if (numIndices < std::numeric_limits<int16_t>::max()) {
// 16-bit case
int16_t* indices = static_cast<int16_t*>((void*)(mesh.m_triangleIndexBase));
for (int32_t i = 0; i < numIndices; ++i) {
indices[i] = (int16_t)targetIndices[i];
}
} else {
// 32-bit case
int32_t* indices = static_cast<int32_t*>((void*)(mesh.m_triangleIndexBase));
for (int32_t i = 0; i < numIndices; ++i) {
indices[i] = targetIndices[i];
}
}
// create the shape
// NOTE: we must track this pointer and delete it when all btCollisionObjects that use it are done with it!
const bool USE_QUANTIZED_AABB_COMPRESSION = true;
btBvhTriangleMeshShape* shape = new btBvhTriangleMeshShape(data, USE_QUANTIZED_AABB_COMPRESSION);