b3DynamicBvhBroadphase.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. Bullet Continuous Collision Detection and Physics Library
  3. Copyright (c) 2003-2013 Erwin Coumans http://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. ///b3DynamicBvhBroadphase implementation by Nathanael Presson
  14. #ifndef B3_DBVT_BROADPHASE_H
  15. #define B3_DBVT_BROADPHASE_H
  16. #include "Bullet3Collision/BroadPhaseCollision/b3DynamicBvh.h"
  17. #include "Bullet3Collision/BroadPhaseCollision/b3OverlappingPairCache.h"
  18. #include "Bullet3Common/b3AlignedObjectArray.h"
  19. #include "b3BroadphaseCallback.h"
  20. //
  21. // Compile time config
  22. //
  23. #define B3_DBVT_BP_PROFILE 0
  24. //#define B3_DBVT_BP_SORTPAIRS 1
  25. #define B3_DBVT_BP_PREVENTFALSEUPDATE 0
  26. #define B3_DBVT_BP_ACCURATESLEEPING 0
  27. #define B3_DBVT_BP_ENABLE_BENCHMARK 0
  28. #define B3_DBVT_BP_MARGIN (b3Scalar)0.05
  29. #if B3_DBVT_BP_PROFILE
  30. #define B3_DBVT_BP_PROFILING_RATE 256
  31. #endif
  32. B3_ATTRIBUTE_ALIGNED16(struct) b3BroadphaseProxy
  33. {
  34. B3_DECLARE_ALIGNED_ALLOCATOR();
  35. ///optional filtering to cull potential collisions
  36. enum CollisionFilterGroups
  37. {
  38. DefaultFilter = 1,
  39. StaticFilter = 2,
  40. KinematicFilter = 4,
  41. DebrisFilter = 8,
  42. SensorTrigger = 16,
  43. CharacterFilter = 32,
  44. AllFilter = -1 //all bits sets: DefaultFilter | StaticFilter | KinematicFilter | DebrisFilter | SensorTrigger
  45. };
  46. //Usually the client b3CollisionObject or Rigidbody class
  47. void* m_clientObject;
  48. short int m_collisionFilterGroup;
  49. short int m_collisionFilterMask;
  50. void* m_multiSapParentProxy;
  51. int m_uniqueId;//m_uniqueId is introduced for paircache. could get rid of this, by calculating the address offset etc.
  52. b3Vector3 m_aabbMin;
  53. b3Vector3 m_aabbMax;
  54. B3_FORCE_INLINE int getUid() const
  55. {
  56. return m_uniqueId;
  57. }
  58. //used for memory pools
  59. b3BroadphaseProxy() :m_clientObject(0),m_multiSapParentProxy(0)
  60. {
  61. }
  62. b3BroadphaseProxy(const b3Vector3& aabbMin,const b3Vector3& aabbMax,void* userPtr,short int collisionFilterGroup, short int collisionFilterMask,void* multiSapParentProxy=0)
  63. :m_clientObject(userPtr),
  64. m_collisionFilterGroup(collisionFilterGroup),
  65. m_collisionFilterMask(collisionFilterMask),
  66. m_aabbMin(aabbMin),
  67. m_aabbMax(aabbMax)
  68. {
  69. m_multiSapParentProxy = multiSapParentProxy;
  70. }
  71. };
  72. //
  73. // b3DbvtProxy
  74. //
  75. struct b3DbvtProxy : b3BroadphaseProxy
  76. {
  77. /* Fields */
  78. //b3DbvtAabbMm aabb;
  79. b3DbvtNode* leaf;
  80. b3DbvtProxy* links[2];
  81. int stage;
  82. /* ctor */
  83. explicit b3DbvtProxy() {}
  84. b3DbvtProxy(const b3Vector3& aabbMin,const b3Vector3& aabbMax,void* userPtr,short int collisionFilterGroup, short int collisionFilterMask) :
  85. b3BroadphaseProxy(aabbMin,aabbMax,userPtr,collisionFilterGroup,collisionFilterMask)
  86. {
  87. links[0]=links[1]=0;
  88. }
  89. };
  90. typedef b3AlignedObjectArray<b3DbvtProxy*> b3DbvtProxyArray;
  91. ///The b3DynamicBvhBroadphase implements a broadphase using two dynamic AABB bounding volume hierarchies/trees (see b3DynamicBvh).
  92. ///One tree is used for static/non-moving objects, and another tree is used for dynamic objects. Objects can move from one tree to the other.
  93. ///This is a very fast broadphase, especially for very dynamic worlds where many objects are moving. Its insert/add and remove of objects is generally faster than the sweep and prune broadphases b3AxisSweep3 and b332BitAxisSweep3.
  94. struct b3DynamicBvhBroadphase
  95. {
  96. /* Config */
  97. enum {
  98. DYNAMIC_SET = 0, /* Dynamic set index */
  99. FIXED_SET = 1, /* Fixed set index */
  100. STAGECOUNT = 2 /* Number of stages */
  101. };
  102. /* Fields */
  103. b3DynamicBvh m_sets[2]; // Dbvt sets
  104. b3DbvtProxy* m_stageRoots[STAGECOUNT+1]; // Stages list
  105. b3AlignedObjectArray<b3DbvtProxy> m_proxies;
  106. b3OverlappingPairCache* m_paircache; // Pair cache
  107. b3Scalar m_prediction; // Velocity prediction
  108. int m_stageCurrent; // Current stage
  109. int m_fupdates; // % of fixed updates per frame
  110. int m_dupdates; // % of dynamic updates per frame
  111. int m_cupdates; // % of cleanup updates per frame
  112. int m_newpairs; // Number of pairs created
  113. int m_fixedleft; // Fixed optimization left
  114. unsigned m_updates_call; // Number of updates call
  115. unsigned m_updates_done; // Number of updates done
  116. b3Scalar m_updates_ratio; // m_updates_done/m_updates_call
  117. int m_pid; // Parse id
  118. int m_cid; // Cleanup index
  119. bool m_releasepaircache; // Release pair cache on delete
  120. bool m_deferedcollide; // Defere dynamic/static collision to collide call
  121. bool m_needcleanup; // Need to run cleanup?
  122. #if B3_DBVT_BP_PROFILE
  123. b3Clock m_clock;
  124. struct {
  125. unsigned long m_total;
  126. unsigned long m_ddcollide;
  127. unsigned long m_fdcollide;
  128. unsigned long m_cleanup;
  129. unsigned long m_jobcount;
  130. } m_profiling;
  131. #endif
  132. /* Methods */
  133. b3DynamicBvhBroadphase(int proxyCapacity, b3OverlappingPairCache* paircache=0);
  134. ~b3DynamicBvhBroadphase();
  135. void collide(b3Dispatcher* dispatcher);
  136. void optimize();
  137. /* b3BroadphaseInterface Implementation */
  138. b3BroadphaseProxy* createProxy(const b3Vector3& aabbMin,const b3Vector3& aabbMax,int objectIndex,void* userPtr,short int collisionFilterGroup,short int collisionFilterMask);
  139. virtual void destroyProxy(b3BroadphaseProxy* proxy,b3Dispatcher* dispatcher);
  140. virtual void setAabb(int objectId,const b3Vector3& aabbMin,const b3Vector3& aabbMax,b3Dispatcher* dispatcher);
  141. virtual void rayTest(const b3Vector3& rayFrom,const b3Vector3& rayTo, b3BroadphaseRayCallback& rayCallback, const b3Vector3& aabbMin=b3MakeVector3(0,0,0), const b3Vector3& aabbMax = b3MakeVector3(0,0,0));
  142. virtual void aabbTest(const b3Vector3& aabbMin, const b3Vector3& aabbMax, b3BroadphaseAabbCallback& callback);
  143. //virtual void getAabb(b3BroadphaseProxy* proxy,b3Vector3& aabbMin, b3Vector3& aabbMax ) const;
  144. virtual void getAabb(int objectId,b3Vector3& aabbMin, b3Vector3& aabbMax ) const;
  145. virtual void calculateOverlappingPairs(b3Dispatcher* dispatcher=0);
  146. virtual b3OverlappingPairCache* getOverlappingPairCache();
  147. virtual const b3OverlappingPairCache* getOverlappingPairCache() const;
  148. virtual void getBroadphaseAabb(b3Vector3& aabbMin,b3Vector3& aabbMax) const;
  149. virtual void printStats();
  150. ///reset broadphase internal structures, to ensure determinism/reproducability
  151. virtual void resetPool(b3Dispatcher* dispatcher);
  152. void performDeferredRemoval(b3Dispatcher* dispatcher);
  153. void setVelocityPrediction(b3Scalar prediction)
  154. {
  155. m_prediction = prediction;
  156. }
  157. b3Scalar getVelocityPrediction() const
  158. {
  159. return m_prediction;
  160. }
  161. ///this setAabbForceUpdate is similar to setAabb but always forces the aabb update.
  162. ///it is not part of the b3BroadphaseInterface but specific to b3DynamicBvhBroadphase.
  163. ///it bypasses certain optimizations that prevent aabb updates (when the aabb shrinks), see
  164. ///http://code.google.com/p/bullet/issues/detail?id=223
  165. void setAabbForceUpdate( b3BroadphaseProxy* absproxy,const b3Vector3& aabbMin,const b3Vector3& aabbMax,b3Dispatcher* /*dispatcher*/);
  166. //static void benchmark(b3BroadphaseInterface*);
  167. };
  168. #endif