btBroadphaseProxy.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /*
  2. Bullet Continuous Collision Detection and Physics Library
  3. Copyright (c) 2003-2006 Erwin Coumans http://continuousphysics.com/Bullet/
  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_BROADPHASE_PROXY_H
  14. #define BT_BROADPHASE_PROXY_H
  15. #include "bullet/LinearMath/btScalar.h" //for SIMD_FORCE_INLINE
  16. #include "bullet/LinearMath/btVector3.h"
  17. #include "bullet/LinearMath/btAlignedAllocator.h"
  18. /// btDispatcher uses these types
  19. /// IMPORTANT NOTE:The types are ordered polyhedral, implicit convex and concave
  20. /// to facilitate type checking
  21. /// CUSTOM_POLYHEDRAL_SHAPE_TYPE,CUSTOM_CONVEX_SHAPE_TYPE and CUSTOM_CONCAVE_SHAPE_TYPE can be used to extend Bullet without modifying source code
  22. enum BroadphaseNativeTypes
  23. {
  24. // polyhedral convex shapes
  25. BOX_SHAPE_PROXYTYPE,
  26. TRIANGLE_SHAPE_PROXYTYPE,
  27. TETRAHEDRAL_SHAPE_PROXYTYPE,
  28. CONVEX_TRIANGLEMESH_SHAPE_PROXYTYPE,
  29. CONVEX_HULL_SHAPE_PROXYTYPE,
  30. CONVEX_POINT_CLOUD_SHAPE_PROXYTYPE,
  31. CUSTOM_POLYHEDRAL_SHAPE_TYPE,
  32. //implicit convex shapes
  33. IMPLICIT_CONVEX_SHAPES_START_HERE,
  34. SPHERE_SHAPE_PROXYTYPE,
  35. MULTI_SPHERE_SHAPE_PROXYTYPE,
  36. CAPSULE_SHAPE_PROXYTYPE,
  37. CONE_SHAPE_PROXYTYPE,
  38. CONVEX_SHAPE_PROXYTYPE,
  39. CYLINDER_SHAPE_PROXYTYPE,
  40. UNIFORM_SCALING_SHAPE_PROXYTYPE,
  41. MINKOWSKI_SUM_SHAPE_PROXYTYPE,
  42. MINKOWSKI_DIFFERENCE_SHAPE_PROXYTYPE,
  43. BOX_2D_SHAPE_PROXYTYPE,
  44. CONVEX_2D_SHAPE_PROXYTYPE,
  45. CUSTOM_CONVEX_SHAPE_TYPE,
  46. //concave shapes
  47. CONCAVE_SHAPES_START_HERE,
  48. //keep all the convex shapetype below here, for the check IsConvexShape in broadphase proxy!
  49. TRIANGLE_MESH_SHAPE_PROXYTYPE,
  50. SCALED_TRIANGLE_MESH_SHAPE_PROXYTYPE,
  51. ///used for demo integration FAST/Swift collision library and Bullet
  52. FAST_CONCAVE_MESH_PROXYTYPE,
  53. //terrain
  54. TERRAIN_SHAPE_PROXYTYPE,
  55. ///Used for GIMPACT Trimesh integration
  56. GIMPACT_SHAPE_PROXYTYPE,
  57. ///Multimaterial mesh
  58. MULTIMATERIAL_TRIANGLE_MESH_PROXYTYPE,
  59. EMPTY_SHAPE_PROXYTYPE,
  60. STATIC_PLANE_PROXYTYPE,
  61. CUSTOM_CONCAVE_SHAPE_TYPE,
  62. CONCAVE_SHAPES_END_HERE,
  63. COMPOUND_SHAPE_PROXYTYPE,
  64. SOFTBODY_SHAPE_PROXYTYPE,
  65. HFFLUID_SHAPE_PROXYTYPE,
  66. HFFLUID_BUOYANT_CONVEX_SHAPE_PROXYTYPE,
  67. INVALID_SHAPE_PROXYTYPE,
  68. MAX_BROADPHASE_COLLISION_TYPES
  69. };
  70. ///The btBroadphaseProxy is the main class that can be used with the Bullet broadphases.
  71. ///It stores collision shape type information, collision filter information and a client object, typically a btCollisionObject or btRigidBody.
  72. ATTRIBUTE_ALIGNED16(struct) btBroadphaseProxy
  73. {
  74. BT_DECLARE_ALIGNED_ALLOCATOR();
  75. ///optional filtering to cull potential collisions
  76. enum CollisionFilterGroups
  77. {
  78. DefaultFilter = 1,
  79. StaticFilter = 2,
  80. KinematicFilter = 4,
  81. DebrisFilter = 8,
  82. SensorTrigger = 16,
  83. CharacterFilter = 32,
  84. AllFilter = -1 //all bits sets: DefaultFilter | StaticFilter | KinematicFilter | DebrisFilter | SensorTrigger
  85. };
  86. //Usually the client btCollisionObject or Rigidbody class
  87. void* m_clientObject;
  88. short int m_collisionFilterGroup;
  89. short int m_collisionFilterMask;
  90. void* m_multiSapParentProxy;
  91. int m_uniqueId;//m_uniqueId is introduced for paircache. could get rid of this, by calculating the address offset etc.
  92. btVector3 m_aabbMin;
  93. btVector3 m_aabbMax;
  94. SIMD_FORCE_INLINE int getUid() const
  95. {
  96. return m_uniqueId;
  97. }
  98. //used for memory pools
  99. btBroadphaseProxy() :m_clientObject(0),m_multiSapParentProxy(0)
  100. {
  101. }
  102. btBroadphaseProxy(const btVector3& aabbMin,const btVector3& aabbMax,void* userPtr,short int collisionFilterGroup, short int collisionFilterMask,void* multiSapParentProxy=0)
  103. :m_clientObject(userPtr),
  104. m_collisionFilterGroup(collisionFilterGroup),
  105. m_collisionFilterMask(collisionFilterMask),
  106. m_aabbMin(aabbMin),
  107. m_aabbMax(aabbMax)
  108. {
  109. m_multiSapParentProxy = multiSapParentProxy;
  110. }
  111. static SIMD_FORCE_INLINE bool isPolyhedral(int proxyType)
  112. {
  113. return (proxyType < IMPLICIT_CONVEX_SHAPES_START_HERE);
  114. }
  115. static SIMD_FORCE_INLINE bool isConvex(int proxyType)
  116. {
  117. return (proxyType < CONCAVE_SHAPES_START_HERE);
  118. }
  119. static SIMD_FORCE_INLINE bool isNonMoving(int proxyType)
  120. {
  121. return (isConcave(proxyType) && !(proxyType==GIMPACT_SHAPE_PROXYTYPE));
  122. }
  123. static SIMD_FORCE_INLINE bool isConcave(int proxyType)
  124. {
  125. return ((proxyType > CONCAVE_SHAPES_START_HERE) &&
  126. (proxyType < CONCAVE_SHAPES_END_HERE));
  127. }
  128. static SIMD_FORCE_INLINE bool isCompound(int proxyType)
  129. {
  130. return (proxyType == COMPOUND_SHAPE_PROXYTYPE);
  131. }
  132. static SIMD_FORCE_INLINE bool isSoftBody(int proxyType)
  133. {
  134. return (proxyType == SOFTBODY_SHAPE_PROXYTYPE);
  135. }
  136. static SIMD_FORCE_INLINE bool isInfinite(int proxyType)
  137. {
  138. return (proxyType == STATIC_PLANE_PROXYTYPE);
  139. }
  140. static SIMD_FORCE_INLINE bool isConvex2d(int proxyType)
  141. {
  142. return (proxyType == BOX_2D_SHAPE_PROXYTYPE) || (proxyType == CONVEX_2D_SHAPE_PROXYTYPE);
  143. }
  144. }
  145. ;
  146. class btCollisionAlgorithm;
  147. struct btBroadphaseProxy;
  148. ///The btBroadphasePair class contains a pair of aabb-overlapping objects.
  149. ///A btDispatcher can search a btCollisionAlgorithm that performs exact/narrowphase collision detection on the actual collision shapes.
  150. ATTRIBUTE_ALIGNED16(struct) btBroadphasePair
  151. {
  152. btBroadphasePair ()
  153. :
  154. m_pProxy0(0),
  155. m_pProxy1(0),
  156. m_algorithm(0),
  157. m_internalInfo1(0)
  158. {
  159. }
  160. BT_DECLARE_ALIGNED_ALLOCATOR();
  161. btBroadphasePair(const btBroadphasePair& other)
  162. : m_pProxy0(other.m_pProxy0),
  163. m_pProxy1(other.m_pProxy1),
  164. m_algorithm(other.m_algorithm),
  165. m_internalInfo1(other.m_internalInfo1)
  166. {
  167. }
  168. btBroadphasePair(btBroadphaseProxy& proxy0,btBroadphaseProxy& proxy1)
  169. {
  170. //keep them sorted, so the std::set operations work
  171. if (proxy0.m_uniqueId < proxy1.m_uniqueId)
  172. {
  173. m_pProxy0 = &proxy0;
  174. m_pProxy1 = &proxy1;
  175. }
  176. else
  177. {
  178. m_pProxy0 = &proxy1;
  179. m_pProxy1 = &proxy0;
  180. }
  181. m_algorithm = 0;
  182. m_internalInfo1 = 0;
  183. }
  184. btBroadphaseProxy* m_pProxy0;
  185. btBroadphaseProxy* m_pProxy1;
  186. mutable btCollisionAlgorithm* m_algorithm;
  187. union { void* m_internalInfo1; int m_internalTmpValue;};//don't use this data, it will be removed in future version.
  188. };
  189. /*
  190. //comparison for set operation, see Solid DT_Encounter
  191. SIMD_FORCE_INLINE bool operator<(const btBroadphasePair& a, const btBroadphasePair& b)
  192. {
  193. return a.m_pProxy0 < b.m_pProxy0 ||
  194. (a.m_pProxy0 == b.m_pProxy0 && a.m_pProxy1 < b.m_pProxy1);
  195. }
  196. */
  197. class btBroadphasePairSortPredicate
  198. {
  199. public:
  200. bool operator() ( const btBroadphasePair& a, const btBroadphasePair& b ) const
  201. {
  202. const int uidA0 = a.m_pProxy0 ? a.m_pProxy0->m_uniqueId : -1;
  203. const int uidB0 = b.m_pProxy0 ? b.m_pProxy0->m_uniqueId : -1;
  204. const int uidA1 = a.m_pProxy1 ? a.m_pProxy1->m_uniqueId : -1;
  205. const int uidB1 = b.m_pProxy1 ? b.m_pProxy1->m_uniqueId : -1;
  206. return uidA0 > uidB0 ||
  207. (a.m_pProxy0 == b.m_pProxy0 && uidA1 > uidB1) ||
  208. (a.m_pProxy0 == b.m_pProxy0 && a.m_pProxy1 == b.m_pProxy1 && a.m_algorithm > b.m_algorithm);
  209. }
  210. };
  211. SIMD_FORCE_INLINE bool operator==(const btBroadphasePair& a, const btBroadphasePair& b)
  212. {
  213. return (a.m_pProxy0 == b.m_pProxy0) && (a.m_pProxy1 == b.m_pProxy1);
  214. }
  215. #endif //BT_BROADPHASE_PROXY_H