btBvhTriangleMeshShape and collision

whiplash
Posts: 16
Joined: Sun Dec 13, 2009 3:21 pm

btBvhTriangleMeshShape and collision

Post by whiplash »

I create "btBvhTriangleMeshShape" collision shape, but my shape don't collising at other collision shape's. My code:

Code: Select all

btTriangleIndexVertexArray* indexVertexArrays = new btTriangleIndexVertexArray(
	m_iTriangles, m_iPoint, 3 * sizeof(int),
	m_iVertices, m_vPoint, 3 * sizeof(float));
btBvhTriangleMeshShape* trimesh = new btBvhTriangleMeshShape(indexVertexArrays, true);

btVector3 inertia(0.0, 0.0, 0.0);
//trimesh->calculateLocalInertia(mass, inertia);

btVector3 pos(0, 300, 0);
btQuaternion rot(0, 0, 0, 1);
btTransform trans(rot, pos);
btTransform centerOfMass = btTransform::getIdentity();	
btDefaultMotionState* mtState = new btDefaultMotionState(trans, centerOfMass);

btRigidBody::btRigidBodyConstructionInfo rbInfo(mass, mtState, trimesh, inertia);
rbInfo.m_additionalDamping = true;
btRigidBody* body = new btRigidBody(rbInfo);
My physic world initialize:

Code: Select all

int maxProxies = 1024;
btVector3 worldAabbMin(-100000.0, -100000.0, -100000.0);
btVector3 worldAabbMax(100000.0, 100000.0, 100000.0);
btAxisSweep3* broadphase = new btAxisSweep3(worldAabbMin, worldAabbMax, maxProxies);
btDefaultCollisionConfiguration* collisionConfiguration = new btDefaultCollisionConfiguration();
btCollisionDispatcher* dispatcher = new btCollisionDispatcher(collisionConfiguration);
btSequentialImpulseConstraintSolver* solver = new btSequentialImpulseConstraintSolver;
dynamicsWorld = new btDiscreteDynamicsWorld(dispatcher, broadphase, solver, collisionConfiguration);

// Ground shape
dynamicsWorld->setGravity(btVector3(0, -9.8, 0));
btCollisionShape* groundShape = new btStaticPlaneShape(btVector3(0, 1, 0), 1);
btDefaultMotionState* groundMotionState = new btDefaultMotionState(btTransform(btQuaternion(0, 0, 0, 1),
	btVector3(0, -1, 0)));
btRigidBody::btRigidBodyConstructionInfo groundRigidBodyCI(0, groundMotionState, groundShape, btVector3(0,0,0));
btRigidBody* groundRigidBody = new btRigidBody(groundRigidBodyCI);
groundRigidBody->setCollisionFlags(groundRigidBody->getCollisionFlags() | btCollisionObject::CF_CUSTOM_MATERIAL_CALLBACK);
groundRigidBody->setFriction(5.0);
groundRigidBody->setRestitution(0.6);
dynamicsWorld->addRigidBody(groundRigidBody);
I need a creating special "btCollisionDispatcher"?
User avatar
Erwin Coumans
Site Admin
Posts: 4221
Joined: Sun Jun 26, 2005 6:43 pm
Location: California, USA

Re: btBvhTriangleMeshShape and collision

Post by Erwin Coumans »

btBvhTriangleMeshShape can only be used for static (non-moving, mass=0) world geometry, and there is no collision detection among static artwork.

Generally it is not recommended to use moving concave triangle meshes, but approximate the moving shapes using convex shapes or compounds of convex shapes (btCompoundShape). If you still want to use moving concave triangle meshes, please check out Bullet/Demos/MovingConcaveDemo, and use btGImpactMeshShape.

Thanks,
Erwin
whiplash
Posts: 16
Joined: Sun Dec 13, 2009 3:21 pm

Re: btBvhTriangleMeshShape and collision

Post by whiplash »

Thanks! I try it.
whiplash
Posts: 16
Joined: Sun Dec 13, 2009 3:21 pm

Re: btBvhTriangleMeshShape and collision

Post by whiplash »

I use btGImpactMeshShape. Collision "btGImpactMeshShape"-"ground" is normal (all position) but collision "btGImpactMeshShape"-"btCapsuleShape" is fail (some position). :(
fail_col.JPG
Create btGImpactMeshShape:

Code: Select all

const int c_vertex = 8;
const int c_tri = 12;
float vertex[c_vertex * 3] = {
	-20, -50, 20,
	-20, -50, -20,
	20, -50, -20,
	20, -50, 20,
	-20, 50, 20,
	-20, 50, -20,
	20, 50, -20,
	20, 50, 20};
int idx[c_tri * 3] = {
	1, 5, 8,
	1, 8, 4,
	4, 8, 7,
	4, 7, 3,
	3, 7, 6,
	3, 6, 2,
	2, 6, 5,
	2, 5, 1,
	5, 6, 7,
	5, 7, 8,
	2, 1, 4,
	2, 4, 3};

	btTriangleIndexVertexArray* indexVertexArrays = new btTriangleIndexVertexArray(
		c_tri, idx, 3 * sizeof(int),
		c_vertex, vertex, 3 * sizeof(float));
	btGImpactMeshShape* trimesh = new btGImpactMeshShape(indexVertexArrays);
	trimesh->updateBound();

	btScalar mass = 60.0;
	btVector3 inertia(0.0, 0.0, 0.0);
	trimesh->calculateLocalInertia(mass, inertia);

	Vector3 entPos = ph_entities[2]->getParentSceneNode()->_getDerivedPosition();
	Quaternion entRot = ph_entities[2]->getParentSceneNode()->_getDerivedOrientation();

	btVector3 pos(entPos.x, entPos.y, entPos.z);
	btQuaternion rot(entRot.x, entRot.y, entRot.z, entRot.w);
	btTransform trans(rot, pos);
	btTransform centerOfMass = btTransform::getIdentity();			
	btDefaultMotionState* mtState = new btDefaultMotionState(trans, centerOfMass);

	btRigidBody::btRigidBodyConstructionInfo rbInfo(mass, mtState, trimesh, inertia);
	rbInfo.m_additionalDamping = true;

	btRigidBody* body = new btRigidBody(rbInfo);
	dynamicsWorld->addRigidBody(body);
Create btCapsuleShape:

Code: Select all

btCollisionShape* shape = new btCapsuleShape(10.0, 80.0);
	btScalar mass = 10.0;
	btVector3 inertia(0.0, 0.0, 0.0);
	shape->calculateLocalInertia(mass, inertia);

	Vector3 entPos = ph_entities[0]->getParentSceneNode()->_getDerivedPosition();
	Quaternion entRot = ph_entities[0]->getParentSceneNode()->_getDerivedOrientation();

	btVector3 pos(entPos.x, entPos.y, entPos.z);
	btQuaternion rot(entRot.x, entRot.y, entRot.z, entRot.w);
	btTransform trans(rot, pos);
	btDefaultMotionState* mtState = new btDefaultMotionState(trans);

	btRigidBody::btRigidBodyConstructionInfo rbInfo(mass, mtState, shape, inertia);
	rbInfo.m_additionalDamping = true;
	btRigidBody* body = new btRigidBody(rbInfo);

	body->setActivationState(DISABLE_DEACTIVATION);
	body->setCollisionFlags(body->getCollisionFlags() | btCollisionObject::CF_CUSTOM_MATERIAL_CALLBACK);
	//body->setCollisionFlags(body->getCollisionFlags() | btCollisionObject::CF_STATIC_OBJECT);
	body->setFriction(5.0);
	body->setRestitution(0.5);
	dynamicsWorld->addRigidBody(body);
Init physic:

Code: Select all

int maxProxies = 1024;
	btVector3 worldAabbMin(-100000.0, -100000.0, -100000.0);
	btVector3 worldAabbMax(100000.0, 100000.0, 100000.0);
	btAxisSweep3* broadphase = new btAxisSweep3(worldAabbMin, worldAabbMax, maxProxies);
	btDefaultCollisionConfiguration* collisionConfiguration = new btDefaultCollisionConfiguration();
	btCollisionDispatcher* dispatcher = new btCollisionDispatcher(collisionConfiguration);
	dispatcher->setNearCallback(MyNearCallback);
	btSequentialImpulseConstraintSolver* solver = new btSequentialImpulseConstraintSolver;
	dynamicsWorld = new btDiscreteDynamicsWorld(dispatcher, broadphase, solver, collisionConfiguration);

	btCollisionDispatcher* dispatcherGI = static_cast<btCollisionDispatcher*>(dynamicsWorld->getDispatcher());
	btGImpactCollisionAlgorithm::registerAlgorithm(dispatcher);
	
	dynamicsWorld->setGravity(btVector3(0, -9.8, 0));
	btCollisionShape* groundShape = new btStaticPlaneShape(btVector3(0, 1, 0), 1);
	btDefaultMotionState* groundMotionState = new btDefaultMotionState(btTransform(btQuaternion(0, 0, 0, 1),
		btVector3(0, -1, 0)));
	btRigidBody::btRigidBodyConstructionInfo groundRigidBodyCI(0, groundMotionState, groundShape, btVector3(0,0,0));
	btRigidBody* groundRigidBody = new btRigidBody(groundRigidBodyCI);
	groundRigidBody->setCollisionFlags(groundRigidBody->getCollisionFlags() | btCollisionObject::CF_CUSTOM_MATERIAL_CALLBACK);
	groundRigidBody->setFriction(5.0);
	groundRigidBody->setRestitution(0.6);
	dynamicsWorld->addRigidBody(groundRigidBody);
You do not have the required permissions to view the files attached to this post.