btDbvtBroadphase.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. Bullet Continuous Collision Detection and Physics Library
  3. Copyright (c) 2003-2009 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. ///btDbvtBroadphase implementation by Nathanael Presson
  14. #ifndef BT_DBVT_BROADPHASE_H
  15. #define BT_DBVT_BROADPHASE_H
  16. #include "BulletCollision/BroadphaseCollision/btDbvt.h"
  17. #include "BulletCollision/BroadphaseCollision/btOverlappingPairCache.h"
  18. //
  19. // Compile time config
  20. //
  21. #define DBVT_BP_PROFILE 0
  22. //#define DBVT_BP_SORTPAIRS 1
  23. #define DBVT_BP_PREVENTFALSEUPDATE 0
  24. #define DBVT_BP_ACCURATESLEEPING 0
  25. #define DBVT_BP_ENABLE_BENCHMARK 0
  26. //#define DBVT_BP_MARGIN (btScalar)0.05
  27. extern btScalar gDbvtMargin;
  28. #if DBVT_BP_PROFILE
  29. #define DBVT_BP_PROFILING_RATE 256
  30. #include "LinearMath/btQuickprof.h"
  31. #endif
  32. //
  33. // btDbvtProxy
  34. //
  35. struct btDbvtProxy : btBroadphaseProxy
  36. {
  37. /* Fields */
  38. //btDbvtAabbMm aabb;
  39. btDbvtNode* leaf;
  40. btDbvtProxy* links[2];
  41. int stage;
  42. /* ctor */
  43. btDbvtProxy(const btVector3& aabbMin, const btVector3& aabbMax, void* userPtr, int collisionFilterGroup, int collisionFilterMask) : btBroadphaseProxy(aabbMin, aabbMax, userPtr, collisionFilterGroup, collisionFilterMask)
  44. {
  45. links[0] = links[1] = 0;
  46. }
  47. };
  48. typedef btAlignedObjectArray<btDbvtProxy*> btDbvtProxyArray;
  49. ///The btDbvtBroadphase implements a broadphase using two dynamic AABB bounding volume hierarchies/trees (see btDbvt).
  50. ///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.
  51. ///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 btAxisSweep3 and bt32BitAxisSweep3.
  52. struct btDbvtBroadphase : btBroadphaseInterface
  53. {
  54. /* Config */
  55. enum
  56. {
  57. DYNAMIC_SET = 0, /* Dynamic set index */
  58. FIXED_SET = 1, /* Fixed set index */
  59. STAGECOUNT = 2 /* Number of stages */
  60. };
  61. /* Fields */
  62. btDbvt m_sets[2]; // Dbvt sets
  63. btDbvtProxy* m_stageRoots[STAGECOUNT + 1]; // Stages list
  64. btOverlappingPairCache* m_paircache; // Pair cache
  65. btScalar m_prediction; // Velocity prediction
  66. int m_stageCurrent; // Current stage
  67. int m_fupdates; // % of fixed updates per frame
  68. int m_dupdates; // % of dynamic updates per frame
  69. int m_cupdates; // % of cleanup updates per frame
  70. int m_newpairs; // Number of pairs created
  71. int m_fixedleft; // Fixed optimization left
  72. unsigned m_updates_call; // Number of updates call
  73. unsigned m_updates_done; // Number of updates done
  74. btScalar m_updates_ratio; // m_updates_done/m_updates_call
  75. int m_pid; // Parse id
  76. int m_cid; // Cleanup index
  77. int m_gid; // Gen id
  78. bool m_releasepaircache; // Release pair cache on delete
  79. bool m_deferedcollide; // Defere dynamic/static collision to collide call
  80. bool m_needcleanup; // Need to run cleanup?
  81. btAlignedObjectArray<btAlignedObjectArray<const btDbvtNode*> > m_rayTestStacks;
  82. #if DBVT_BP_PROFILE
  83. btClock m_clock;
  84. struct
  85. {
  86. unsigned long m_total;
  87. unsigned long m_ddcollide;
  88. unsigned long m_fdcollide;
  89. unsigned long m_cleanup;
  90. unsigned long m_jobcount;
  91. } m_profiling;
  92. #endif
  93. /* Methods */
  94. btDbvtBroadphase(btOverlappingPairCache* paircache = 0);
  95. ~btDbvtBroadphase();
  96. void collide(btDispatcher* dispatcher);
  97. void optimize();
  98. /* btBroadphaseInterface Implementation */
  99. btBroadphaseProxy* createProxy(const btVector3& aabbMin, const btVector3& aabbMax, int shapeType, void* userPtr, int collisionFilterGroup, int collisionFilterMask, btDispatcher* dispatcher);
  100. virtual void destroyProxy(btBroadphaseProxy* proxy, btDispatcher* dispatcher);
  101. virtual void setAabb(btBroadphaseProxy* proxy, const btVector3& aabbMin, const btVector3& aabbMax, btDispatcher* dispatcher);
  102. virtual void rayTest(const btVector3& rayFrom, const btVector3& rayTo, btBroadphaseRayCallback& rayCallback, const btVector3& aabbMin = btVector3(0, 0, 0), const btVector3& aabbMax = btVector3(0, 0, 0));
  103. virtual void aabbTest(const btVector3& aabbMin, const btVector3& aabbMax, btBroadphaseAabbCallback& callback);
  104. virtual void getAabb(btBroadphaseProxy* proxy, btVector3& aabbMin, btVector3& aabbMax) const;
  105. virtual void calculateOverlappingPairs(btDispatcher* dispatcher);
  106. virtual btOverlappingPairCache* getOverlappingPairCache();
  107. virtual const btOverlappingPairCache* getOverlappingPairCache() const;
  108. virtual void getBroadphaseAabb(btVector3& aabbMin, btVector3& aabbMax) const;
  109. virtual void printStats();
  110. ///reset broadphase internal structures, to ensure determinism/reproducability
  111. virtual void resetPool(btDispatcher* dispatcher);
  112. void performDeferredRemoval(btDispatcher* dispatcher);
  113. void setVelocityPrediction(btScalar prediction)
  114. {
  115. m_prediction = prediction;
  116. }
  117. btScalar getVelocityPrediction() const
  118. {
  119. return m_prediction;
  120. }
  121. ///this setAabbForceUpdate is similar to setAabb but always forces the aabb update.
  122. ///it is not part of the btBroadphaseInterface but specific to btDbvtBroadphase.
  123. ///it bypasses certain optimizations that prevent aabb updates (when the aabb shrinks), see
  124. ///http://code.google.com/p/bullet/issues/detail?id=223
  125. void setAabbForceUpdate(btBroadphaseProxy* absproxy, const btVector3& aabbMin, const btVector3& aabbMax, btDispatcher* /*dispatcher*/);
  126. static void benchmark(btBroadphaseInterface*);
  127. };
  128. #endif