Code to generate and add convex shape to scene:
Code: Select all
btRigidBody* CBulletFactory::addConvex(CMesh* mesh, const btVector3& pos, float mass)
{
btTransform startTrans;
startTrans.setIdentity();
startTrans.setOrigin(pos);
btMotionState* ms = new btDefaultMotionState(startTrans);
vMotionStates.push_back(ms);// vector of motion states for later cleanup
// This vertex format is correct for my meshes!!!
struct VERTEX
{
D3DXVECTOR3 p;
D3DXVECTOR3 n;
D3DXVECTOR2 t;
};
DWORD numVertices = mesh->getMesh()->GetNumVertices();
VERTEX* pVertices;
btVector3* btVertices = new btVector3[numVertices];
mesh->getMesh()->LockVertexBuffer(0, (LPVOID*)&pVertices);
for(DWORD i = 0; i < numVertices; ++i)
{
btVertices[i] = btVector3(pVertices[i].p.x, pVertices[i].p.y, pVertices[i].p.z);
}
mesh->getMesh()->UnlockVertexBuffer();
btConvexHullShape* cs = new btConvexHullShape((btScalar*)btVertices, numVertices, sizeof(btVector3));
vCollisionShapes.push_back(cs);// vector of collision shapes for later cleanup
btVector3 localInertia(0.0f, 0.0f, 0.0f);
cs->calculateLocalInertia(mass, localInertia);
btRigidBody* body = new btRigidBody(mass, ms, cs, localInertia);
phyEngine->getPhysicsWorld()->addRigidBody(body);
vRigidBodies.push_back(body);// vector of rigid bodies for later cleanup
#if 0
int maxSerializeBufferSize = 1024*1024*5;
btDefaultSerializer* serializer = new btDefaultSerializer(maxSerializeBufferSize);
serializer->startSerialization();
cs->serializeSingleShape(serializer);
serializer->finishSerialization();
FILE* f = fopen("bottle.bullet","wb");
fwrite(serializer->getBufferPointer(), serializer->getCurrentBufferSize(), 1, f);
fclose(f);
#endif
return body;
}
wireframe debug display:
But doesn't for bottle mesh :
wireframe debug display:
apart from bottle wireframe, collision is all wrong for it too.
Can Bullet generate approximate convex shape for this kind of meshes?
Thank you for your time.
EDIT:
Debug display with NewtonGD physics.