btGhostObject.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. Bullet Continuous Collision Detection and Physics Library
  3. Copyright (c) 2003-2008 Erwin Coumans http://bulletphysics.com
  4. This software is provided 'as-is', without any express or implied warranty.
  5. In no event will the authors be held liable for any damages arising from the use of this software.
  6. Permission is granted to anyone to use this software for any purpose,
  7. including commercial applications, and to alter it and redistribute it freely,
  8. subject to the following restrictions:
  9. 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
  10. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  11. 3. This notice may not be removed or altered from any source distribution.
  12. */
  13. #ifndef BT_GHOST_OBJECT_H
  14. #define BT_GHOST_OBJECT_H
  15. #include "btCollisionObject.h"
  16. #include "bullet/BulletCollision//BroadphaseCollision/btOverlappingPairCallback.h"
  17. #include "bullet/LinearMath/btAlignedAllocator.h"
  18. #include "bullet/BulletCollision//BroadphaseCollision/btOverlappingPairCache.h"
  19. #include "btCollisionWorld.h"
  20. class btConvexShape;
  21. class btDispatcher;
  22. ///The btGhostObject can keep track of all objects that are overlapping
  23. ///By default, this overlap is based on the AABB
  24. ///This is useful for creating a character controller, collision sensors/triggers, explosions etc.
  25. ///We plan on adding rayTest and other queries for the btGhostObject
  26. ATTRIBUTE_ALIGNED16(class) btGhostObject : public btCollisionObject
  27. {
  28. protected:
  29. btAlignedObjectArray<btCollisionObject*> m_overlappingObjects;
  30. public:
  31. btGhostObject();
  32. virtual ~btGhostObject();
  33. void convexSweepTest(const class btConvexShape* castShape, const btTransform& convexFromWorld, const btTransform& convexToWorld, btCollisionWorld::ConvexResultCallback& resultCallback, btScalar allowedCcdPenetration = 0.f) const;
  34. void rayTest(const btVector3& rayFromWorld, const btVector3& rayToWorld, btCollisionWorld::RayResultCallback& resultCallback) const;
  35. ///this method is mainly for expert/internal use only.
  36. virtual void addOverlappingObjectInternal(btBroadphaseProxy* otherProxy, btBroadphaseProxy* thisProxy=0);
  37. ///this method is mainly for expert/internal use only.
  38. virtual void removeOverlappingObjectInternal(btBroadphaseProxy* otherProxy,btDispatcher* dispatcher,btBroadphaseProxy* thisProxy=0);
  39. int getNumOverlappingObjects() const
  40. {
  41. return m_overlappingObjects.size();
  42. }
  43. btCollisionObject* getOverlappingObject(int index)
  44. {
  45. return m_overlappingObjects[index];
  46. }
  47. const btCollisionObject* getOverlappingObject(int index) const
  48. {
  49. return m_overlappingObjects[index];
  50. }
  51. btAlignedObjectArray<btCollisionObject*>& getOverlappingPairs()
  52. {
  53. return m_overlappingObjects;
  54. }
  55. const btAlignedObjectArray<btCollisionObject*> getOverlappingPairs() const
  56. {
  57. return m_overlappingObjects;
  58. }
  59. //
  60. // internal cast
  61. //
  62. static const btGhostObject* upcast(const btCollisionObject* colObj)
  63. {
  64. if (colObj->getInternalType()==CO_GHOST_OBJECT)
  65. return (const btGhostObject*)colObj;
  66. return 0;
  67. }
  68. static btGhostObject* upcast(btCollisionObject* colObj)
  69. {
  70. if (colObj->getInternalType()==CO_GHOST_OBJECT)
  71. return (btGhostObject*)colObj;
  72. return 0;
  73. }
  74. };
  75. class btPairCachingGhostObject : public btGhostObject
  76. {
  77. btHashedOverlappingPairCache* m_hashPairCache;
  78. public:
  79. btPairCachingGhostObject();
  80. virtual ~btPairCachingGhostObject();
  81. ///this method is mainly for expert/internal use only.
  82. virtual void addOverlappingObjectInternal(btBroadphaseProxy* otherProxy, btBroadphaseProxy* thisProxy=0);
  83. virtual void removeOverlappingObjectInternal(btBroadphaseProxy* otherProxy,btDispatcher* dispatcher,btBroadphaseProxy* thisProxy=0);
  84. btHashedOverlappingPairCache* getOverlappingPairCache()
  85. {
  86. return m_hashPairCache;
  87. }
  88. };
  89. ///The btGhostPairCallback interfaces and forwards adding and removal of overlapping pairs from the btBroadphaseInterface to btGhostObject.
  90. class btGhostPairCallback : public btOverlappingPairCallback
  91. {
  92. public:
  93. btGhostPairCallback()
  94. {
  95. }
  96. virtual ~btGhostPairCallback()
  97. {
  98. }
  99. virtual btBroadphasePair* addOverlappingPair(btBroadphaseProxy* proxy0,btBroadphaseProxy* proxy1)
  100. {
  101. btCollisionObject* colObj0 = (btCollisionObject*) proxy0->m_clientObject;
  102. btCollisionObject* colObj1 = (btCollisionObject*) proxy1->m_clientObject;
  103. btGhostObject* ghost0 = btGhostObject::upcast(colObj0);
  104. btGhostObject* ghost1 = btGhostObject::upcast(colObj1);
  105. if (ghost0)
  106. ghost0->addOverlappingObjectInternal(proxy1, proxy0);
  107. if (ghost1)
  108. ghost1->addOverlappingObjectInternal(proxy0, proxy1);
  109. return 0;
  110. }
  111. virtual void* removeOverlappingPair(btBroadphaseProxy* proxy0,btBroadphaseProxy* proxy1,btDispatcher* dispatcher)
  112. {
  113. btCollisionObject* colObj0 = (btCollisionObject*) proxy0->m_clientObject;
  114. btCollisionObject* colObj1 = (btCollisionObject*) proxy1->m_clientObject;
  115. btGhostObject* ghost0 = btGhostObject::upcast(colObj0);
  116. btGhostObject* ghost1 = btGhostObject::upcast(colObj1);
  117. if (ghost0)
  118. ghost0->removeOverlappingObjectInternal(proxy1,dispatcher,proxy0);
  119. if (ghost1)
  120. ghost1->removeOverlappingObjectInternal(proxy0,dispatcher,proxy1);
  121. return 0;
  122. }
  123. virtual void removeOverlappingPairsContainingProxy(btBroadphaseProxy* /*proxy0*/,btDispatcher* /*dispatcher*/)
  124. {
  125. btAssert(0);
  126. //need to keep track of all ghost objects and call them here
  127. //m_hashPairCache->removeOverlappingPairsContainingProxy(proxy0,dispatcher);
  128. }
  129. };
  130. #endif