Rigid bodies penetrate the ground

Post Reply
lucky7456969
Posts: 26
Joined: Thu Sep 11, 2014 5:40 am

Rigid bodies penetrate the ground

Post by lucky7456969 »

Code: Select all

void Physics::addStaticMesh(CObjects* pObj) {
	m_pObj = pObj;   
      
	D3DXVECTOR3 vMin, vMax;
	pObj->m_Mesh->GetBoundingBox(vMin, vMax);
	float width = vMax.x - vMin.x;
	float length = vMax.y - vMin.y;
	float height = vMax.z - vMin.z;

	btBoxShape *shape = new btBoxShape(btVector3(width / 2.0f, length / 2.0f, height / 2.0f));

	m_collisionShapes.push_back(shape);
	{
		btScalar mass(0.0f);		

		//rigidbody is dynamic if and only if mass is non zero, otherwise static
		bool isDynamic = (mass != 0.f);

		btVector3 localInertia(0,0,0);
		if (isDynamic)
			shape->calculateLocalInertia(mass,localInertia);
		  
		btTransform trans;
		trans.setIdentity();

		btRigidBody::btRigidBodyConstructionInfo rbInfo(mass,0,shape,localInertia);	
		btRigidBody* body = new btRigidBody(rbInfo);	
		body->setWorldTransform(trans);
		body->setCollisionFlags(body->getCollisionFlags() | btCollisionObject::CF_STATIC_OBJECT);
		 

		//add the body to the dynamics world
		m_dynamicsWorld->addRigidBody(body);

		 
	}
}

void Physics::addMesh(CObjects* pObj) {
   m_pObj = pObj;   
         
   D3DXVECTOR3 vMin, vMax;
   pObj->m_Mesh->GetBoundingBox(vMin, vMax);
   float width = vMax.x - vMin.x;
   float length = vMax.y - vMin.y;
   float height = vMax.z - vMin.z;

   btBoxShape *shape = new btBoxShape(btVector3(width / 2.0f, length / 2.0f, height / 2.0f));   

   m_collisionShapes.push_back(shape);
   {
      btScalar mass(10.0f);     
 
      bool isDynamic = (mass != 0.f);

      btVector3 localInertia(0,0,0);
      if (isDynamic)
         shape->calculateLocalInertia(mass,localInertia);
 
      // Object and MotionState get back pointers both ways
      MyMotionState* ms = new MyMotionState(pObj);
      pObj->SetSimMotionState(ms);   

      btRigidBody::btRigidBodyConstructionInfo rbInfo(mass,ms,shape,localInertia);   
      btRigidBody* body = new btRigidBody(rbInfo);         

      //add the body to the dynamics world
      m_dynamicsWorld->addRigidBody(body);
   }
}
I add background static geometry using the first method, and dynamic objects using the second.
But they don't seem to interact with each other during the simulation steps?
I wonder why this is the case?
Thanks
Jack
Post Reply