2 Bullet Continuous Collision Detection and Physics Library
3 Copyright (c) 2003-2009 Erwin Coumans http://bulletphysics.org
5 This software is provided 'as-is', without any express or implied warranty.
6 In no event will the authors be held liable for any damages arising from the use of this software.
7 Permission is granted to anyone to use this software for any purpose,
8 including commercial applications, and to alter it and redistribute it freely,
9 subject to the following restrictions:
11 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.
12 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
13 3. This notice may not be removed or altered from any source distribution.
16 ///btDbvtBroadphase implementation by Nathanael Presson
17 #ifndef BT_DBVT_BROADPHASE_H
18 #define BT_DBVT_BROADPHASE_H
20 #include "BulletCollision/BroadphaseCollision/btDbvt.h"
21 #include "BulletCollision/BroadphaseCollision/btOverlappingPairCache.h"
24 // Compile time config
27 #define DBVT_BP_PROFILE 0
28 //#define DBVT_BP_SORTPAIRS 1
29 #define DBVT_BP_PREVENTFALSEUPDATE 0
30 #define DBVT_BP_ACCURATESLEEPING 0
31 #define DBVT_BP_ENABLE_BENCHMARK 0
32 //#define DBVT_BP_MARGIN (btScalar)0.05
33 extern btScalar gDbvtMargin;
36 #define DBVT_BP_PROFILING_RATE 256
37 #include "LinearMath/btQuickprof.h"
43 struct btDbvtProxy : btBroadphaseProxy
48 btDbvtProxy* links[2];
51 btDbvtProxy(const btVector3& aabbMin, const btVector3& aabbMax, void* userPtr, int collisionFilterGroup, int collisionFilterMask) : btBroadphaseProxy(aabbMin, aabbMax, userPtr, collisionFilterGroup, collisionFilterMask)
53 links[0] = links[1] = 0;
57 typedef btAlignedObjectArray<btDbvtProxy*> btDbvtProxyArray;
59 ///The btDbvtBroadphase implements a broadphase using two dynamic AABB bounding volume hierarchies/trees (see btDbvt).
60 ///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.
61 ///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.
62 struct btDbvtBroadphase : btBroadphaseInterface
67 DYNAMIC_SET = 0, /* Dynamic set index */
68 FIXED_SET = 1, /* Fixed set index */
69 STAGECOUNT = 2 /* Number of stages */
72 btDbvt m_sets[2]; // Dbvt sets
73 btDbvtProxy* m_stageRoots[STAGECOUNT + 1]; // Stages list
74 btOverlappingPairCache* m_paircache; // Pair cache
75 btScalar m_prediction; // Velocity prediction
76 int m_stageCurrent; // Current stage
77 int m_fupdates; // % of fixed updates per frame
78 int m_dupdates; // % of dynamic updates per frame
79 int m_cupdates; // % of cleanup updates per frame
80 int m_newpairs; // Number of pairs created
81 int m_fixedleft; // Fixed optimization left
82 unsigned m_updates_call; // Number of updates call
83 unsigned m_updates_done; // Number of updates done
84 btScalar m_updates_ratio; // m_updates_done/m_updates_call
85 int m_pid; // Parse id
86 int m_cid; // Cleanup index
88 bool m_releasepaircache; // Release pair cache on delete
89 bool m_deferedcollide; // Defere dynamic/static collision to collide call
90 bool m_needcleanup; // Need to run cleanup?
91 btAlignedObjectArray<btAlignedObjectArray<const btDbvtNode*> > m_rayTestStacks;
96 unsigned long m_total;
97 unsigned long m_ddcollide;
98 unsigned long m_fdcollide;
99 unsigned long m_cleanup;
100 unsigned long m_jobcount;
104 btDbvtBroadphase(btOverlappingPairCache* paircache = 0);
106 void collide(btDispatcher* dispatcher);
109 /* btBroadphaseInterface Implementation */
110 btBroadphaseProxy* createProxy(const btVector3& aabbMin, const btVector3& aabbMax, int shapeType, void* userPtr, int collisionFilterGroup, int collisionFilterMask, btDispatcher* dispatcher);
111 virtual void destroyProxy(btBroadphaseProxy* proxy, btDispatcher* dispatcher);
112 virtual void setAabb(btBroadphaseProxy* proxy, const btVector3& aabbMin, const btVector3& aabbMax, btDispatcher* dispatcher);
113 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));
114 virtual void aabbTest(const btVector3& aabbMin, const btVector3& aabbMax, btBroadphaseAabbCallback& callback);
116 virtual void getAabb(btBroadphaseProxy* proxy, btVector3& aabbMin, btVector3& aabbMax) const;
117 virtual void calculateOverlappingPairs(btDispatcher* dispatcher);
118 virtual btOverlappingPairCache* getOverlappingPairCache();
119 virtual const btOverlappingPairCache* getOverlappingPairCache() const;
120 virtual void getBroadphaseAabb(btVector3& aabbMin, btVector3& aabbMax) const;
121 virtual void printStats();
123 ///reset broadphase internal structures, to ensure determinism/reproducability
124 virtual void resetPool(btDispatcher* dispatcher);
126 void performDeferredRemoval(btDispatcher* dispatcher);
128 void setVelocityPrediction(btScalar prediction)
130 m_prediction = prediction;
132 btScalar getVelocityPrediction() const
137 ///this setAabbForceUpdate is similar to setAabb but always forces the aabb update.
138 ///it is not part of the btBroadphaseInterface but specific to btDbvtBroadphase.
139 ///it bypasses certain optimizations that prevent aabb updates (when the aabb shrinks), see
140 ///http://code.google.com/p/bullet/issues/detail?id=223
141 void setAabbForceUpdate(btBroadphaseProxy* absproxy, const btVector3& aabbMin, const btVector3& aabbMax, btDispatcher* /*dispatcher*/);
143 static void benchmark(btBroadphaseInterface*);