How to create a bullet collision shape with mesh file

xxh12321
Posts: 7
Joined: Wed Jan 25, 2012 2:27 pm

How to create a bullet collision shape with mesh file

Post by xxh12321 »

I chose to use Ogre+Bullet.
I can create a box but I don't know how to create an object by mesh and skeleton files made from blender?
And, how to detect collision in this case?
kloplop321
Posts: 55
Joined: Sun Jan 01, 2012 7:37 pm

Re: How to create a bullet collision shape with mesh file

Post by kloplop321 »

you take your points, and you shove it into a trimesh, then create a convex or concave object depending on your mesh.
http://bulletphysics.org/Bullet/phpBB3/ ... php?t=4513
winspear
Posts: 77
Joined: Thu Nov 26, 2009 6:32 pm

Re: How to create a bullet collision shape with mesh file

Post by winspear »

Meshes can be displayed as concave dynamic, static and convex-decomposed bodies in Bullet.
For "dynamic Concave meshes" look at btGImpactMeshShape.
For "static mesh objects" , create the mesh using btBvhTriangleMeshShape and use the btCollisionShape to create the btRigidBody. Finally assign zero mass to the btRigidBody.

For dynamic convex-decomposed meshes, you have to decompose the mesh in to convex hulls and then form a compound shape out of all the hulls and use it for collision detection. See example "AppConvexDecompositionDemo" for details.
xxh12321
Posts: 7
Joined: Wed Jan 25, 2012 2:27 pm

Re: How to create a bullet collision shape with mesh file

Post by xxh12321 »

Actually I want to create a Kinematic body which takes a joystick or other input devices as input and I also want to check this Kinematic body whether collides with other dynamic bodys.
So far, I have been able to check the collision between boxes. My task now is to learn how to use my parter's mesh and skeleton in my physics world. Should I ask him to export a .bullet file for me? Does the .bullet file include both mesh and skeleton or What's the relationship between them?
Would you give some suggestions to my problem? Or tell me how to achieve my aim.
Thanks a lot.
winspear
Posts: 77
Joined: Thu Nov 26, 2009 6:32 pm

Re: How to create a bullet collision shape with mesh file

Post by winspear »

Here is how you load a tri mesh in bullet regardless of the format..

Code: Select all

btRigidBody* BulletWrap::addTriangleMesh( btVector3 &p0, btVector3 &p1, btVector3 &p2,btVector3 &position,
									bool useQuantizedBvhTree,btVector3 &inertia, btScalar mass,
									btScalar restitution, bool collision )
{
	btTriangleMesh* trimesh = new btTriangleMesh();
	trimesh->addTriangle( p0, p1, p2 );
	
	btTransform	trans;
	trans.setIdentity();
	trans.setOrigin( position );

	btCollisionShape* trimeshShape  = new btBvhTriangleMeshShape( trimesh, useQuantizedBvhTree );
	trimeshShape->calculateLocalInertia( mass, inertia ); //gives error
	
	btDefaultMotionState* motionstate = new btDefaultMotionState( trans );

	btRigidBody* body= new btRigidBody( mass, motionstate, trimeshShape, inertia );
	body->setRestitution( restitution );
	
	m_trianglemeshs.push_back( trimesh );
	m_triangleMeshBodies.push_back( body );
	m_world->addRigidBody( body );
	
	std::vector< btVector3* > tmp;
	m_vertices.push_back(tmp);
	m_vertices[ m_triangleMeshBodies.size() - 1 ].push_back( &p0 );
	m_vertices[ m_triangleMeshBodies.size() - 1 ].push_back( &p1 );
	m_vertices[ m_triangleMeshBodies.size() - 1 ].push_back( &p2 );
	
	if( collision )
		m_collisionShapes.push_back(trimeshShape);
	
	//return m_triangleMeshBodies.size() - 1;
	return body;
}

void BulletWrap::addTriangleToMesh( int mesh, btVector3 &p0, btVector3 &p1, btVector3 &p2 ){
	if( m_trianglemeshs[mesh] ){
		m_trianglemeshs[mesh]->addTriangle( p0, p1, p2 );
		
		m_vertices[mesh].push_back( &p0 );
		m_vertices[mesh].push_back( &p1 );
		m_vertices[mesh].push_back( &p2 );
	}
	///TODO: error codes...
}

p0 , p1, p2 are the vertices of every triangle.