btBroadphaseProxy.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*
  2. Bullet Continuous Collision Detection and Physics Library
  3. Copyright (c) 2003-2006 Erwin Coumans https://bulletphysics.org
  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 "LinearMath/btScalar.h" //for SIMD_FORCE_INLINE
  16. #include "LinearMath/btVector3.h"
  17. #include "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. SDF_SHAPE_PROXYTYPE = CUSTOM_CONCAVE_SHAPE_TYPE,
  63. CONCAVE_SHAPES_END_HERE,
  64. COMPOUND_SHAPE_PROXYTYPE,
  65. SOFTBODY_SHAPE_PROXYTYPE,
  66. HFFLUID_SHAPE_PROXYTYPE,
  67. HFFLUID_BUOYANT_CONVEX_SHAPE_PROXYTYPE,
  68. INVALID_SHAPE_PROXYTYPE,
  69. MAX_BROADPHASE_COLLISION_TYPES
  70. };
  71. ///The btBroadphaseProxy is the main class that can be used with the Bullet broadphases.
  72. ///It stores collision shape type information, collision filter information and a client object, typically a btCollisionObject or btRigidBody.
  73. ATTRIBUTE_ALIGNED16(struct)
  74. btBroadphaseProxy
  75. {
  76. BT_DECLARE_ALIGNED_ALLOCATOR();
  77. ///optional filtering to cull potential collisions
  78. enum CollisionFilterGroups
  79. {
  80. DefaultFilter = 1,
  81. StaticFilter = 2,
  82. KinematicFilter = 4,
  83. DebrisFilter = 8,
  84. SensorTrigger = 16,
  85. CharacterFilter = 32,
  86. AllFilter = -1 //all bits sets: DefaultFilter | StaticFilter | KinematicFilter | DebrisFilter | SensorTrigger
  87. };
  88. //Usually the client btCollisionObject or Rigidbody class
  89. void* m_clientObject;
  90. int m_collisionFilterGroup;
  91. int m_collisionFilterMask;
  92. int m_uniqueId; //m_uniqueId is introduced for paircache. could get rid of this, by calculating the address offset etc.
  93. btVector3 m_aabbMin;
  94. btVector3 m_aabbMax;
  95. SIMD_FORCE_INLINE int getUid() const
  96. {
  97. return m_uniqueId;
  98. }
  99. //used for memory pools
  100. btBroadphaseProxy() : m_clientObject(0)
  101. {
  102. }
  103. btBroadphaseProxy(const btVector3& aabbMin, const btVector3& aabbMax, void* userPtr, int collisionFilterGroup, int collisionFilterMask)
  104. : m_clientObject(userPtr),
  105. m_collisionFilterGroup(collisionFilterGroup),
  106. m_collisionFilterMask(collisionFilterMask),
  107. m_aabbMin(aabbMin),
  108. m_aabbMax(aabbMax)
  109. {
  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. class btCollisionAlgorithm;
  146. struct btBroadphaseProxy;
  147. ///The btBroadphasePair class contains a pair of aabb-overlapping objects.
  148. ///A btDispatcher can search a btCollisionAlgorithm that performs exact/narrowphase collision detection on the actual collision shapes.
  149. ATTRIBUTE_ALIGNED16(struct)
  150. btBroadphasePair
  151. {
  152. btBroadphasePair()
  153. : m_pProxy0(0),
  154. m_pProxy1(0),
  155. m_algorithm(0),
  156. m_internalInfo1(0)
  157. {
  158. }
  159. BT_DECLARE_ALIGNED_ALLOCATOR();
  160. btBroadphasePair(btBroadphaseProxy & proxy0, btBroadphaseProxy & proxy1)
  161. {
  162. //keep them sorted, so the std::set operations work
  163. if (proxy0.m_uniqueId < proxy1.m_uniqueId)
  164. {
  165. m_pProxy0 = &proxy0;
  166. m_pProxy1 = &proxy1;
  167. }
  168. else
  169. {
  170. m_pProxy0 = &proxy1;
  171. m_pProxy1 = &proxy0;
  172. }
  173. m_algorithm = 0;
  174. m_internalInfo1 = 0;
  175. }
  176. btBroadphaseProxy* m_pProxy0;
  177. btBroadphaseProxy* m_pProxy1;
  178. mutable btCollisionAlgorithm* m_algorithm;
  179. union {
  180. void* m_internalInfo1;
  181. int m_internalTmpValue;
  182. }; //don't use this data, it will be removed in future version.
  183. };
  184. /*
  185. //comparison for set operation, see Solid DT_Encounter
  186. SIMD_FORCE_INLINE bool operator<(const btBroadphasePair& a, const btBroadphasePair& b)
  187. {
  188. return a.m_pProxy0 < b.m_pProxy0 ||
  189. (a.m_pProxy0 == b.m_pProxy0 && a.m_pProxy1 < b.m_pProxy1);
  190. }
  191. */
  192. class btBroadphasePairSortPredicate
  193. {
  194. public:
  195. bool operator()(const btBroadphasePair& a, const btBroadphasePair& b) const
  196. {
  197. const int uidA0 = a.m_pProxy0 ? a.m_pProxy0->m_uniqueId : -1;
  198. const int uidB0 = b.m_pProxy0 ? b.m_pProxy0->m_uniqueId : -1;
  199. const int uidA1 = a.m_pProxy1 ? a.m_pProxy1->m_uniqueId : -1;
  200. const int uidB1 = b.m_pProxy1 ? b.m_pProxy1->m_uniqueId : -1;
  201. return uidA0 > uidB0 ||
  202. (a.m_pProxy0 == b.m_pProxy0 && uidA1 > uidB1) ||
  203. (a.m_pProxy0 == b.m_pProxy0 && a.m_pProxy1 == b.m_pProxy1 && a.m_algorithm > b.m_algorithm);
  204. }
  205. };
  206. SIMD_FORCE_INLINE bool operator==(const btBroadphasePair& a, const btBroadphasePair& b)
  207. {
  208. return (a.m_pProxy0 == b.m_pProxy0) && (a.m_pProxy1 == b.m_pProxy1);
  209. }
  210. #endif //BT_BROADPHASE_PROXY_H