Advertisement
Guest User

Untitled

a guest
May 23rd, 2015
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.68 KB | None | 0 0
  1. #include <btBulletDynamicsCommon.h>
  2. #include "BulletCollision/CollisionDispatch/btGhostObject.h"
  3. #include <iostream>
  4.  
  5. enum CollisionTypes {
  6.     COL_COLLECTIBLE = 64, //<Collide with powerups
  7.     COL_STATIC_ENVIRONMENT = 128,
  8.     COL_PLAYERS = 256
  9. };
  10.  
  11. namespace CollisionGroups{
  12.     static const int environmentCollidesWith = COL_PLAYERS;
  13.     static const int playerCollidesWith = COL_COLLECTIBLE | COL_STATIC_ENVIRONMENT;
  14.     static const int collectibleCollidesWith = COL_PLAYERS;
  15. }
  16.  
  17. //Physics
  18. btAxisSweep3*                       _phyBroadphase;
  19. btDefaultCollisionConfiguration*    _phyCollisionConfig;
  20. btCollisionDispatcher*              _phyDispatcher;
  21. btSequentialImpulseConstraintSolver* _phySolver;
  22. btDynamicsWorld*                    _phyWorld;
  23. btPairCachingGhostObject*           _phyAttackGhostObject;
  24.  
  25.  
  26. void collisionEvent(const btCollisionObject* pBody0, const btCollisionObject * pBody1){
  27.     btCollisionObject* ghostObj = static_cast<btCollisionObject*>(_phyAttackGhostObject);
  28.     btTransform t = _phyAttackGhostObject->getWorldTransform();
  29.     btVector3 v1,v2;
  30.     _phyAttackGhostObject->getCollisionShape()->getAabb(t,v1,v2);
  31.     std::cout << v1.x() << " " << v1.y() << " " << v1.z() << std::endl;
  32.     std::cout << v2.x() << " " << v2.y() << " " << v2.z() << std::endl;
  33.     static int count = 0;
  34.     if (ghostObj == pBody0){
  35.         ++count;
  36.         if (pBody1->getCollisionFlags() != COL_PLAYERS){
  37.             std::cout << count << " Hit non-character" << std::endl << std::endl;
  38.         }
  39.     } else {
  40.         ++count;
  41.         if (pBody0->getCollisionFlags() != COL_PLAYERS){
  42.             std::cout << count << " Hit non-character" << std::endl << std::endl;
  43.         }
  44.     }
  45. }
  46.  
  47. void checkCollisionEvents(){   
  48.     // ATTACK
  49.     btManifoldArray   manifoldArray;
  50.     btBroadphasePairArray& pairArray = _phyAttackGhostObject->getOverlappingPairCache()->getOverlappingPairArray();
  51.     int numPairs = pairArray.size();
  52.     for (int i=0;i<numPairs;i++){
  53.         manifoldArray.clear();
  54.  
  55.         const btBroadphasePair& pair = pairArray[i];
  56.         //unless we manually perform collision detection on this pair, the contacts are in the dynamics world paircache:
  57.         btBroadphasePair* collisionPair = _phyWorld->getPairCache()->findPair(pair.m_pProxy0,pair.m_pProxy1);
  58.         if (!collisionPair)
  59.             continue;
  60.  
  61.         if (collisionPair->m_algorithm)
  62.             collisionPair->m_algorithm->getAllContactManifolds(manifoldArray);
  63.  
  64.         for (int j=0;j<manifoldArray.size();j++){
  65.             btPersistentManifold* manifold = manifoldArray[j];
  66.        
  67.             // TODO: ADD STUFF
  68.             collisionEvent( manifold->getBody0(), manifold->getBody1() );      
  69.  
  70.             btScalar directionSign = manifold->getBody0() == _phyAttackGhostObject ? btScalar(-1.0) : btScalar(1.0);
  71.             for (int p=0;p<manifold->getNumContacts();p++){
  72.                 const btManifoldPoint&pt = manifold->getContactPoint(p);
  73.                
  74.                 if (pt.getDistance()<0.f){
  75.                     const btVector3& ptA = pt.getPositionWorldOnA();
  76.                     const btVector3& ptB = pt.getPositionWorldOnB();
  77.                     const btVector3& normalOnB = pt.m_normalWorldOnB;
  78.                     /// work here
  79.                    
  80.                 }
  81.             }
  82.         }
  83.     }
  84. }
  85.  
  86. void main(){
  87.     // Initialize physics
  88.     _phyBroadphase = new btAxisSweep3(btVector3(-10000,-10000,-10000), btVector3(10000,10000,10000), 1024);
  89.     _phyCollisionConfig = new btDefaultCollisionConfiguration();
  90.     _phyDispatcher = new btCollisionDispatcher(_phyCollisionConfig);
  91.     _phySolver = new btSequentialImpulseConstraintSolver();
  92.     _phyWorld = new btDiscreteDynamicsWorld(_phyDispatcher, _phyBroadphase, _phySolver, _phyCollisionConfig);
  93.     _phyWorld->setGravity(btVector3(0,-9.8,0));
  94.  
  95.     // Static object
  96.     btRigidBody* _phyBody = new btRigidBody(0, new btDefaultMotionState(), new btSphereShape(0.5) );
  97.     _phyBody->setCollisionFlags(COL_STATIC_ENVIRONMENT);
  98.     _phyWorld->addRigidBody(_phyBody, COL_STATIC_ENVIRONMENT, CollisionGroups::environmentCollidesWith);
  99.  
  100.     // Ghost object
  101.     _phyAttackGhostObject = new btPairCachingGhostObject();
  102.     _phyAttackGhostObject->setCollisionShape(  new btSphereShape( 0.5) );
  103.     //_phyAttackGhostObject->setCollisionShape(  new btBoxShape( btVector3(0.5, 0.5, 0.5)) );
  104.     _phyAttackGhostObject->setCollisionFlags(btCollisionObject::CF_NO_CONTACT_RESPONSE | COL_PLAYERS);
  105.     _phyWorld->getBroadphase()->getOverlappingPairCache()->setInternalGhostPairCallback(new btGhostPairCallback());  
  106.  
  107.  
  108.     btTransform hitboxTrans;
  109.     hitboxTrans.setIdentity();
  110.     // #######################################################
  111.     //hitboxTrans.setOrigin(btVector3(1.3, 0, 0)); // no hit
  112.     hitboxTrans.setOrigin(btVector3(1.27, 0, 0)); // hit
  113.     // #######################################################
  114.     _phyAttackGhostObject->setWorldTransform(hitboxTrans);
  115.     _phyWorld->addCollisionObject(_phyAttackGhostObject, COL_PLAYERS, CollisionGroups::playerCollidesWith);
  116.  
  117.     for (int i = 0; i < 300; i++) {
  118.         _phyWorld->stepSimulation(1 / 60.f, 10);
  119.         checkCollisionEvents();
  120.     }
  121.     std::cin.get();
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement