How to use user-defined triangle meshes (with inertia)

bunengzaicai
Posts: 15
Joined: Sun Feb 15, 2009 3:42 am

How to use user-defined triangle meshes (with inertia)

Post by bunengzaicai »

I am new to bullet.
What I want to know is how to use my own triangle meshes in bullet?
I have tried btBvhTriangleMeshShape to build a triangle meshes shape but it seems that calculateinertia function are not supported in version 2.74-beta.
Could Anybody tell me how to introduce user-defined triangle meshes in to simulation?
Can I use btTriangleMesh to achieve the goal? In addition, I cannot find how to use btTriangleMesh instruction in demos. :-(

My codes are as follows:
btTriangleIndexVertexArray* indexVertexArrays = new btTriangleIndexVertexArray(totalTriangles,
shred_indices,
indexStride,
totalVerts,(btScalar*) &shred[0].x(),vertStride);

bool useQuantizedAabbCompression = true;
shredShape = new btBvhTriangleMeshShape(indexVertexArrays,useQuantizedAabbCompression);

m_shapes.push_back(shredShape);

btTransform starttransform;
starttransform.setIdentity();

btScalar mass(1.0f);

bool isDynamic=(mass!=0.f);

btVector3 localInertia(0,0,0);

if(isDynamic)
shredShape->calculateLocalInertia(mass,localInertia);<-------It can not work because assert(0);

float start_x=START_POS_X-SIZE_X/2;
float start_y=START_POS_Y*1.5;
float start_z=START_POS_Z-SIZE_Z/2;

starttransform.setOrigin(SCALING*btVector3(start_x*2,40+start_y,start_z));
btDefaultMotionState* mymotionstate=new btDefaultMotionState(starttransform);
btRigidBody::btRigidBodyConstructionInfo rbinfo(mass,mymotionstate,shredShape,localInertia);
btRigidBody* body=new btRigidBody(rbinfo);
body->setActivationState(ISLAND_SLEEPING);
m_dynamicsWorld->addRigidBody(body);
body->setActivationState(ISLAND_SLEEPING);
User avatar
Erwin Coumans
Site Admin
Posts: 4221
Joined: Sun Jun 26, 2005 6:43 pm
Location: California, USA

Re: How to use user-defined triangle meshes (with inertia)

Post by Erwin Coumans »

btBvhTriangleMeshShape cannot be used for moving objects, so its mass / inertia needs to be zero. Check this collision shape decision tree:

Image

You'll find you need to use a btGimpactTriangleMeshShape if you really want moving triangle mesh. Most games simplify triangle meshes, as the decision tree shows.

Hope this helps,
Erwin
bunengzaicai
Posts: 15
Joined: Sun Feb 15, 2009 3:42 am

Re: How to use user-defined triangle meshes (with inertia)

Post by bunengzaicai »

Thank you very much!
But I can not find btGimpactTriangleMeshShape in bullet-2.7.4.
Do you mean btGimpactMeshShape?
bunengzaicai
Posts: 15
Joined: Sun Feb 15, 2009 3:42 am

Re: How to use user-defined triangle meshes (with inertia)

Post by bunengzaicai »

I have tried to use btGImpactConvexDecompositionShape(Got from appGimpactTestDemo) to load user-defined trianglemesh, I can display triangle mesh now. However, there is something weird happens: triangle mesh can collide with other ones but doesn't rotate!
???
some codes related with this problem are as follows:
btTriangleIndexVertexArray* indexVertexArrays = new btTriangleIndexVertexArray(totalTriangles,
shred_indices,
indexStride,
totalVerts,(btScalar*) &shred[0].x(),vertStride);

bool useQuantizedAabbCompression = true;
btGImpactConvexDecompositionShape * trimesh = new btGImpactConvexDecompositionShape(
indexVertexArrays, btVector3(1.f,1.f,1.f),btScalar(0.01));
trimesh->updateBound();

m_trimeshShape=trimesh;

btCollisionDispatcher * dispatcher = static_cast<btCollisionDispatcher *>(m_dynamicsWorld ->getDispatcher());
btGImpactCollisionAlgorithm::registerAlgorithm(dispatcher);

......
float massT=1.0f;
btTransform startTransform;
startTransform.setIdentity();
btVector3 localInertia(0,0,0);
startTransform.setOrigin(btVector3(0,5,5));
startTransform.setRotation(btQuaternion(0,0,3.14159265*0.5));
Body=localCreateRigidBody(massT,startTransform,m_trimeshShape);
Any ideas?
Could you tell me where can I find some sample codes related with gimpacttrianglemesh?
Many thanks.
bunengzaicai
Posts: 15
Joined: Sun Feb 15, 2009 3:42 am

Re: How to use user-defined triangle meshes (with inertia)

Post by bunengzaicai »

It has been solved. I am so stupid, hehe......
I changed to:
btGImpactMeshShape * trimesh = new btGImpactMeshShape(indexVertexArrays);
trimesh->setLocalScaling(btVector3(1.f,1.f,1.f));
trimesh->setMargin(0.07f);
trimesh->updateBound();
m_trimeshShape=trimesh;
Instead of:
btGImpactConvexDecompositionShape * trimesh = new btGImpactConvexDecompositionShape(
indexVertexArrays, btVector3(1.f,1.f,1.f),btScalar(0.01));
trimesh->updateBound();
m_trimeshShape=trimesh;
btCollisionDispatcher * dispatcher = static_cast<btCollisionDispatcher *>(m_dynamicsWorld ->getDispatcher());
btGImpactCollisionAlgorithm::registerAlgorithm(dispatcher);

Thank Erwin Coumans! Thank you for your figure.
razer
Posts: 82
Joined: Sun Apr 04, 2010 10:08 pm

Re: How to use user-defined triangle meshes (with inertia)

Post by razer »

Erwin Coumans wrote: You'll find you need to use a btGimpactTriangleMeshShape if you really want moving triangle mesh. Most games simplify triangle meshes, as the decision tree shows.
bunengzaicai wrote:Thank you very much!
But I can not find btGimpactTriangleMeshShape in bullet-2.7.4.
Do you mean btGimpactMeshShape?

I can't find btGimpactTriangleMeshShape in 2.74 and in latest 2.76 too.


Should I use btGImpactMeshShape and btGImpactConvexDecompositionShape instead ?
Where is it documented. I can't find much information on these classes. and btGimpactTriangleMeshShape is missing

Thanks