Random mesh collision. [Solved]

88Alex88
Posts: 5
Joined: Tue Apr 12, 2011 3:23 am

Random mesh collision. [Solved]

Post by 88Alex88 »

Hi,
im new with Bullet, and i have already made a series of primitive collision shapes collide in my project, like spheres and boxes, but now im trying to have random meshes to take part too, like for example, the bigbunny mesh as a rigid body to collide with rigid spheres. Is this possible with bullet?

Thanx.
Last edited by 88Alex88 on Mon May 30, 2011 6:49 pm, edited 1 time in total.
User avatar
dphil
Posts: 237
Joined: Tue Jun 29, 2010 10:27 pm

Re: Random mesh collision.

Post by dphil »

Create a rigid body using btGImpactMeshShape, which takes in whatever triangle mesh data you want. You also need to make sure to register the gimpact algorithm (for proper mesh collision detection) by calling the following code after you create your world (or at least the dispatcher your world uses):

Code: Select all

btGImpactCollisionAlgorithm::registerAlgorithm(dispatcher)
88Alex88
Posts: 5
Joined: Tue Apr 12, 2011 3:23 am

Re: Random mesh collision.

Post by 88Alex88 »

Thanx for the fast reply, i have read about btGImpactMeshShape, and i guess i have to provide my mesh to it in the constructor, by giving a filled btStridingMeshInterface, but i dont really know how to do this. Can anyone help me with that? if I have my data into

Vector3* vertex;
unsigned long* indices;

How can i fill a btGImpactMeshShape with it?

Thanx
winspear
Posts: 77
Joined: Thu Nov 26, 2009 6:32 pm

Re: Random mesh collision.

Post by winspear »

Just get the pointer to vertex indices and pointer to vertices and pass it to the following function. m_world is nothing but pointer to btDynamicsWorld.
Hope this helps.

Code: Select all

btRigidBody* addConcaveMesh(int numTriangles, int *indices, int numVertices, float *verts,
									btVector3 &position, btVector3 &inertia, btScalar mass, btScalar restitution, bool collision)
{
	// create trimesh
	btTriangleIndexVertexArray* indexVertexArrays = new btTriangleIndexVertexArray(numTriangles,
		indices,
		3*sizeof(int),
		numVertices, verts, 3*sizeof(float));

	btGImpactMeshShape * trimesh = new btGImpactMeshShape(indexVertexArrays);
	trimesh->setLocalScaling(btVector3(1.f,1.f,1.f));
	trimesh->updateBound();
	btCollisionShape *m_trimeshShape = trimesh;

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

	/////
	btTransform trans;
	trans.setIdentity();
	if (!mass)
		trans.setRotation(btQuaternion(btVector3(1,0.0,0.0),3.14));

	trans.setOrigin( position );


	inertia.setZero();

	m_trimeshShape->calculateLocalInertia( mass, inertia );

	btDefaultMotionState* motionstate = new btDefaultMotionState( trans );
	btRigidBody* body = new btRigidBody( mass, motionstate, m_trimeshShape, inertia );
//	body->setRestitution( restitution );

	m_world->addRigidBody( body );
	//	body->setActivationState(ACTIVE_TAG);
//	body->setDamping(0.1,0.5);

	if( collision )
		m_collisionShapes.push_back(m_trimeshShape);

	return body;

}
88Alex88
Posts: 5
Joined: Tue Apr 12, 2011 3:23 am

Re: Random mesh collision.

Post by 88Alex88 »

That totally did the job. Thanx a lot winspear and dphil :)