Advertisement
Guest User

Untitled

a guest
Feb 15th, 2012
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.02 KB | None | 0 0
  1. ///The setup code for using btGhostObjects
  2. //create the ghost object.
  3.     Ogre::Vector3 pos = node->getPosition();
  4.     body->setShape(node, _sphereShapeInstance, bodyRes, bodyFriction, bodyMass, pos);
  5.  
  6.     btPairCachingGhostObject* ghostObject = new btPairCachingGhostObject();
  7.     ghostObject->setCollisionShape(_sphereShapeInstance->getBulletShape());
  8.    
  9.     //create the debug node
  10.     Ogre::SceneNode* debugNode = _geoMgr->createCube(Ogre::Vector3(10.0f, 10.0f, 10.0f), "DebugNode" + sphereIdStr);
  11.     PhysicsDestructionComponent* phyDestructionComp = new PhysicsDestructionComponent(ghostObject, body, debugNode);
  12.     _world->getBulletDynamicsWorld()->addCollisionObject(ghostObject, btBroadphaseProxy::DefaultFilter, btBroadphaseProxy::StaticFilter);
  13.     _world->getBulletDynamicsWorld()->addAction(phyDestructionComp);
  14.     _actionComps.push_back(phyDestructionComp);
  15.  
  16. ///below is the updateAction method where you will find the ghost object code.
  17.  
  18. /**
  19. * This method implements the updateAction interface from bullet.
  20. *
  21. * This method will find the max penetration and assume that to be the impact point. The current assumption is that we
  22. *don't care about penetration because this impact point is used in the inelastic collision model.
  23. *
  24. * \precondition We assume this object is valid: ghostobject and target object exists and is valid.
  25. * \postcondition the impact point is computed.
  26. **/
  27. void
  28.     PhysicsDestructionComponent::updateAction(btCollisionWorld* collisionWorld, btScalar deltaTime)
  29. {
  30.     //do collision detection specific to static objects.
  31.     btManifoldArray manifoldArray;
  32.     btBroadphasePairArray& pairArray = _ghostObject->getOverlappingPairCache()->getOverlappingPairArray();
  33.     bool collision = false;
  34.     for(size_t i = 0; i < pairArray.size(); ++i)
  35.     {
  36.         manifoldArray.clear();
  37.  
  38.         const btBroadphasePair& pair = pairArray[i];
  39.  
  40.         btBroadphasePair* collisionPair = collisionWorld->getPairCache()->findPair(pair.m_pProxy0, pair.m_pProxy1);
  41.         if(!collisionPair)
  42.             continue;
  43.         btCollisionObject* collisionObject = _targetObject->getBulletObject();
  44.         btCollisionObject* proxyObject = static_cast<btCollisionObject*>(collisionPair->m_pProxy1->m_clientObject);
  45.         if(proxyObject == collisionObject)
  46.         {
  47.             continue;
  48.         }
  49.  
  50.         if(collisionPair->m_algorithm)
  51.             collisionPair->m_algorithm->getAllContactManifolds(manifoldArray);
  52.  
  53.        
  54.         for(size_t j = 0; j < manifoldArray.size(); ++j)
  55.         {
  56.             btPersistentManifold* manifold = manifoldArray[j];
  57.             btScalar directionSign = manifold->getBody0() == _ghostObject ? btScalar(-1.0) : btScalar(1.0);
  58.  
  59.             for(int p = 0; p < manifold->getNumContacts(); ++p)
  60.             {
  61.                 const btManifoldPoint &pt = manifold->getContactPoint(p);
  62.  
  63.                 btScalar dist = pt.getDistance();
  64.                 if(dist < 0.0) //pentration and thus collision
  65.                 {
  66.                     collision = true;
  67.                 }
  68.             }
  69.         }
  70.     }
  71.     auto worldTransform = _targetObject->getBulletObject()->getWorldTransform();
  72.     _ghostObject->setWorldTransform(worldTransform);
  73.     if(collision)
  74.         _debugNode->setPosition(OgreBulletCollisions::BtOgreConverter::to(worldTransform.getOrigin()));
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement