1 | /* |
---|
2 | Bullet Continuous Collision Detection and Physics Library |
---|
3 | Copyright (c) 2003-2006 Erwin Coumans http://continuousphysics.com/Bullet/ |
---|
4 | |
---|
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: |
---|
10 | |
---|
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. |
---|
14 | */ |
---|
15 | |
---|
16 | |
---|
17 | /** |
---|
18 | * @mainpage Bullet Documentation |
---|
19 | * |
---|
20 | * @section intro_sec Introduction |
---|
21 | * Bullet Collision Detection & Physics SDK |
---|
22 | * |
---|
23 | * Bullet is a Collision Detection and Rigid Body Dynamics Library. The Library is Open Source and free for commercial use, under the ZLib license ( http://opensource.org/licenses/zlib-license.php ). |
---|
24 | * |
---|
25 | * There is the Physics Forum for Feedback and bteral Collision Detection and Physics discussions. |
---|
26 | * Please visit http://www.continuousphysics.com/Bullet/phpBB2/index.php |
---|
27 | * |
---|
28 | * @section install_sec Installation |
---|
29 | * |
---|
30 | * @subsection step1 Step 1: Download |
---|
31 | * You can download the Bullet Physics Library from our website: http://www.continuousphysics.com/Bullet/ |
---|
32 | * @subsection step2 Step 2: Building |
---|
33 | * Bullet comes with autogenerated Project Files for Microsoft Visual Studio 6, 7, 7.1 and 8. |
---|
34 | * The main Workspace/Solution is located in Bullet/msvc/8/wksbullet.sln (replace 8 with your version). |
---|
35 | * |
---|
36 | * Under other platforms, like Linux or Mac OS-X, Bullet can be build using either using make, cmake, http://www.cmake.org, or jam, http://www.perforce.com/jam/jam.html . cmake can autogenerate Xcode, KDevelop, MSVC and other build systems. just run cmake . in the root of Bullet. |
---|
37 | * So if you are not using MSVC or cmake, you can run ./autogen.sh ./configure to create both Makefile and Jamfile and then run make or jam. |
---|
38 | * Jam is a build system that can build the library, demos and also autogenerate the MSVC Project Files. |
---|
39 | * If you don't have jam installed, you can make jam from the included jam-2.5 sources, or download jam from ftp://ftp.perforce.com/pub/jam/ |
---|
40 | * |
---|
41 | * @subsection step3 Step 3: Testing demos |
---|
42 | * Try to run and experiment with BasicDemo executable as a starting point. |
---|
43 | * Bullet can be used in several ways, as Full Rigid Body simulation, as Collision Detector Library or Low Level / Snippets like the GJK Closest Point calculation. |
---|
44 | * The Dependencies can be seen in this documentation under Directories |
---|
45 | * |
---|
46 | * @subsection step4 Step 4: Integrating in your application, full Rigid Body and Soft Body simulation |
---|
47 | * Check out BasicDemo how to create a btDynamicsWorld, btRigidBody and btCollisionShape, Stepping the simulation and synchronizing your graphics object transform. |
---|
48 | * Check out SoftDemo how to use soft body dynamics, using btSoftRigidDynamicsWorld. |
---|
49 | * @subsection step5 Step 5 : Integrate the Collision Detection Library (without Dynamics and other Extras) |
---|
50 | * Bullet Collision Detection can also be used without the Dynamics/Extras. |
---|
51 | * Check out btCollisionWorld and btCollisionObject, and the CollisionInterfaceDemo. |
---|
52 | * @subsection step6 Step 6 : Use Snippets like the GJK Closest Point calculation. |
---|
53 | * Bullet has been designed in a modular way keeping dependencies to a minimum. The ConvexHullDistance demo demonstrates direct use of btGjkPairDetector. |
---|
54 | * |
---|
55 | * @section copyright Copyright |
---|
56 | * Copyright (C) 2005-2008 Erwin Coumans, some contributions Copyright Gino van den Bergen, Christer Ericson, Simon Hobbs, Ricardo Padrela, F Richter(res), Stephane Redon |
---|
57 | * Special thanks to all visitors of the Bullet Physics forum, and in particular above contributors, John McCutchan, Nathanael Presson, Dave Eberle, Dirk Gregorius, Erin Catto, Dave Eberle, Adam Moravanszky, |
---|
58 | * Pierre Terdiman, Kenny Erleben, Russell Smith, Oliver Strunk, Jan Paul van Waveren, Marten Svanfeldt. |
---|
59 | * |
---|
60 | */ |
---|
61 | |
---|
62 | |
---|
63 | |
---|
64 | #ifndef COLLISION_WORLD_H |
---|
65 | #define COLLISION_WORLD_H |
---|
66 | |
---|
67 | class btStackAlloc; |
---|
68 | class btCollisionShape; |
---|
69 | class btConvexShape; |
---|
70 | class btBroadphaseInterface; |
---|
71 | #include "LinearMath/btVector3.h" |
---|
72 | #include "LinearMath/btTransform.h" |
---|
73 | #include "btCollisionObject.h" |
---|
74 | #include "btCollisionDispatcher.h" //for definition of btCollisionObjectArray |
---|
75 | #include "BulletCollision/BroadphaseCollision/btOverlappingPairCache.h" |
---|
76 | #include "LinearMath/btAlignedObjectArray.h" |
---|
77 | |
---|
78 | ///CollisionWorld is interface and container for the collision detection |
---|
79 | class btCollisionWorld |
---|
80 | { |
---|
81 | |
---|
82 | |
---|
83 | protected: |
---|
84 | |
---|
85 | btAlignedObjectArray<btCollisionObject*> m_collisionObjects; |
---|
86 | |
---|
87 | btDispatcher* m_dispatcher1; |
---|
88 | |
---|
89 | btDispatcherInfo m_dispatchInfo; |
---|
90 | |
---|
91 | btStackAlloc* m_stackAlloc; |
---|
92 | |
---|
93 | btBroadphaseInterface* m_broadphasePairCache; |
---|
94 | |
---|
95 | btIDebugDraw* m_debugDrawer; |
---|
96 | |
---|
97 | |
---|
98 | public: |
---|
99 | |
---|
100 | //this constructor doesn't own the dispatcher and paircache/broadphase |
---|
101 | btCollisionWorld(btDispatcher* dispatcher,btBroadphaseInterface* broadphasePairCache, btCollisionConfiguration* collisionConfiguration); |
---|
102 | |
---|
103 | virtual ~btCollisionWorld(); |
---|
104 | |
---|
105 | void setBroadphase(btBroadphaseInterface* pairCache) |
---|
106 | { |
---|
107 | m_broadphasePairCache = pairCache; |
---|
108 | } |
---|
109 | |
---|
110 | btBroadphaseInterface* getBroadphase() |
---|
111 | { |
---|
112 | return m_broadphasePairCache; |
---|
113 | } |
---|
114 | |
---|
115 | btOverlappingPairCache* getPairCache() |
---|
116 | { |
---|
117 | return m_broadphasePairCache->getOverlappingPairCache(); |
---|
118 | } |
---|
119 | |
---|
120 | |
---|
121 | btDispatcher* getDispatcher() |
---|
122 | { |
---|
123 | return m_dispatcher1; |
---|
124 | } |
---|
125 | |
---|
126 | const btDispatcher* getDispatcher() const |
---|
127 | { |
---|
128 | return m_dispatcher1; |
---|
129 | } |
---|
130 | |
---|
131 | virtual void updateAabbs(); |
---|
132 | |
---|
133 | virtual void setDebugDrawer(btIDebugDraw* debugDrawer) |
---|
134 | { |
---|
135 | m_debugDrawer = debugDrawer; |
---|
136 | } |
---|
137 | |
---|
138 | virtual btIDebugDraw* getDebugDrawer() |
---|
139 | { |
---|
140 | return m_debugDrawer; |
---|
141 | } |
---|
142 | |
---|
143 | |
---|
144 | ///LocalShapeInfo gives extra information for complex shapes |
---|
145 | ///Currently, only btTriangleMeshShape is available, so it just contains triangleIndex and subpart |
---|
146 | struct LocalShapeInfo |
---|
147 | { |
---|
148 | int m_shapePart; |
---|
149 | int m_triangleIndex; |
---|
150 | |
---|
151 | //const btCollisionShape* m_shapeTemp; |
---|
152 | //const btTransform* m_shapeLocalTransform; |
---|
153 | }; |
---|
154 | |
---|
155 | struct LocalRayResult |
---|
156 | { |
---|
157 | LocalRayResult(btCollisionObject* collisionObject, |
---|
158 | LocalShapeInfo* localShapeInfo, |
---|
159 | const btVector3& hitNormalLocal, |
---|
160 | btScalar hitFraction) |
---|
161 | :m_collisionObject(collisionObject), |
---|
162 | m_localShapeInfo(localShapeInfo), |
---|
163 | m_hitNormalLocal(hitNormalLocal), |
---|
164 | m_hitFraction(hitFraction) |
---|
165 | { |
---|
166 | } |
---|
167 | |
---|
168 | btCollisionObject* m_collisionObject; |
---|
169 | LocalShapeInfo* m_localShapeInfo; |
---|
170 | btVector3 m_hitNormalLocal; |
---|
171 | btScalar m_hitFraction; |
---|
172 | |
---|
173 | }; |
---|
174 | |
---|
175 | ///RayResultCallback is used to report new raycast results |
---|
176 | struct RayResultCallback |
---|
177 | { |
---|
178 | btScalar m_closestHitFraction; |
---|
179 | btCollisionObject* m_collisionObject; |
---|
180 | short int m_collisionFilterGroup; |
---|
181 | short int m_collisionFilterMask; |
---|
182 | |
---|
183 | virtual ~RayResultCallback() |
---|
184 | { |
---|
185 | } |
---|
186 | bool hasHit() const |
---|
187 | { |
---|
188 | return (m_collisionObject != 0); |
---|
189 | } |
---|
190 | |
---|
191 | RayResultCallback() |
---|
192 | :m_closestHitFraction(btScalar(1.)), |
---|
193 | m_collisionObject(0), |
---|
194 | m_collisionFilterGroup(btBroadphaseProxy::DefaultFilter), |
---|
195 | m_collisionFilterMask(btBroadphaseProxy::AllFilter) |
---|
196 | { |
---|
197 | } |
---|
198 | |
---|
199 | virtual bool needsCollision(btBroadphaseProxy* proxy0) const |
---|
200 | { |
---|
201 | bool collides = (proxy0->m_collisionFilterGroup & m_collisionFilterMask) != 0; |
---|
202 | collides = collides && (m_collisionFilterGroup & proxy0->m_collisionFilterMask); |
---|
203 | return collides; |
---|
204 | } |
---|
205 | |
---|
206 | |
---|
207 | virtual btScalar addSingleResult(LocalRayResult& rayResult,bool normalInWorldSpace) = 0; |
---|
208 | }; |
---|
209 | |
---|
210 | struct ClosestRayResultCallback : public RayResultCallback |
---|
211 | { |
---|
212 | ClosestRayResultCallback(const btVector3& rayFromWorld,const btVector3& rayToWorld) |
---|
213 | :m_rayFromWorld(rayFromWorld), |
---|
214 | m_rayToWorld(rayToWorld) |
---|
215 | { |
---|
216 | } |
---|
217 | |
---|
218 | btVector3 m_rayFromWorld;//used to calculate hitPointWorld from hitFraction |
---|
219 | btVector3 m_rayToWorld; |
---|
220 | |
---|
221 | btVector3 m_hitNormalWorld; |
---|
222 | btVector3 m_hitPointWorld; |
---|
223 | |
---|
224 | virtual btScalar addSingleResult(LocalRayResult& rayResult,bool normalInWorldSpace) |
---|
225 | { |
---|
226 | //caller already does the filter on the m_closestHitFraction |
---|
227 | btAssert(rayResult.m_hitFraction <= m_closestHitFraction); |
---|
228 | |
---|
229 | m_closestHitFraction = rayResult.m_hitFraction; |
---|
230 | m_collisionObject = rayResult.m_collisionObject; |
---|
231 | if (normalInWorldSpace) |
---|
232 | { |
---|
233 | m_hitNormalWorld = rayResult.m_hitNormalLocal; |
---|
234 | } else |
---|
235 | { |
---|
236 | ///need to transform normal into worldspace |
---|
237 | m_hitNormalWorld = m_collisionObject->getWorldTransform().getBasis()*rayResult.m_hitNormalLocal; |
---|
238 | } |
---|
239 | m_hitPointWorld.setInterpolate3(m_rayFromWorld,m_rayToWorld,rayResult.m_hitFraction); |
---|
240 | return rayResult.m_hitFraction; |
---|
241 | } |
---|
242 | }; |
---|
243 | |
---|
244 | |
---|
245 | struct LocalConvexResult |
---|
246 | { |
---|
247 | LocalConvexResult(btCollisionObject* hitCollisionObject, |
---|
248 | LocalShapeInfo* localShapeInfo, |
---|
249 | const btVector3& hitNormalLocal, |
---|
250 | const btVector3& hitPointLocal, |
---|
251 | btScalar hitFraction |
---|
252 | ) |
---|
253 | :m_hitCollisionObject(hitCollisionObject), |
---|
254 | m_localShapeInfo(localShapeInfo), |
---|
255 | m_hitNormalLocal(hitNormalLocal), |
---|
256 | m_hitPointLocal(hitPointLocal), |
---|
257 | m_hitFraction(hitFraction) |
---|
258 | { |
---|
259 | } |
---|
260 | |
---|
261 | btCollisionObject* m_hitCollisionObject; |
---|
262 | LocalShapeInfo* m_localShapeInfo; |
---|
263 | btVector3 m_hitNormalLocal; |
---|
264 | btVector3 m_hitPointLocal; |
---|
265 | btScalar m_hitFraction; |
---|
266 | }; |
---|
267 | |
---|
268 | ///RayResultCallback is used to report new raycast results |
---|
269 | struct ConvexResultCallback |
---|
270 | { |
---|
271 | btScalar m_closestHitFraction; |
---|
272 | short int m_collisionFilterGroup; |
---|
273 | short int m_collisionFilterMask; |
---|
274 | |
---|
275 | ConvexResultCallback() |
---|
276 | :m_closestHitFraction(btScalar(1.)), |
---|
277 | m_collisionFilterGroup(btBroadphaseProxy::DefaultFilter), |
---|
278 | m_collisionFilterMask(btBroadphaseProxy::AllFilter) |
---|
279 | { |
---|
280 | } |
---|
281 | |
---|
282 | virtual ~ConvexResultCallback() |
---|
283 | { |
---|
284 | } |
---|
285 | |
---|
286 | bool hasHit() const |
---|
287 | { |
---|
288 | return (m_closestHitFraction < btScalar(1.)); |
---|
289 | } |
---|
290 | |
---|
291 | |
---|
292 | |
---|
293 | virtual bool needsCollision(btBroadphaseProxy* proxy0) const |
---|
294 | { |
---|
295 | bool collides = (proxy0->m_collisionFilterGroup & m_collisionFilterMask) != 0; |
---|
296 | collides = collides && (m_collisionFilterGroup & proxy0->m_collisionFilterMask); |
---|
297 | return collides; |
---|
298 | } |
---|
299 | |
---|
300 | virtual btScalar addSingleResult(LocalConvexResult& convexResult,bool normalInWorldSpace) = 0; |
---|
301 | }; |
---|
302 | |
---|
303 | struct ClosestConvexResultCallback : public ConvexResultCallback |
---|
304 | { |
---|
305 | ClosestConvexResultCallback(const btVector3& convexFromWorld,const btVector3& convexToWorld) |
---|
306 | :m_convexFromWorld(convexFromWorld), |
---|
307 | m_convexToWorld(convexToWorld), |
---|
308 | m_hitCollisionObject(0) |
---|
309 | { |
---|
310 | } |
---|
311 | |
---|
312 | btVector3 m_convexFromWorld;//used to calculate hitPointWorld from hitFraction |
---|
313 | btVector3 m_convexToWorld; |
---|
314 | |
---|
315 | btVector3 m_hitNormalWorld; |
---|
316 | btVector3 m_hitPointWorld; |
---|
317 | btCollisionObject* m_hitCollisionObject; |
---|
318 | |
---|
319 | virtual btScalar addSingleResult(LocalConvexResult& convexResult,bool normalInWorldSpace) |
---|
320 | { |
---|
321 | //caller already does the filter on the m_closestHitFraction |
---|
322 | btAssert(convexResult.m_hitFraction <= m_closestHitFraction); |
---|
323 | |
---|
324 | m_closestHitFraction = convexResult.m_hitFraction; |
---|
325 | m_hitCollisionObject = convexResult.m_hitCollisionObject; |
---|
326 | if (normalInWorldSpace) |
---|
327 | { |
---|
328 | m_hitNormalWorld = convexResult.m_hitNormalLocal; |
---|
329 | } else |
---|
330 | { |
---|
331 | ///need to transform normal into worldspace |
---|
332 | m_hitNormalWorld = m_hitCollisionObject->getWorldTransform().getBasis()*convexResult.m_hitNormalLocal; |
---|
333 | } |
---|
334 | m_hitPointWorld = convexResult.m_hitPointLocal; |
---|
335 | return convexResult.m_hitFraction; |
---|
336 | } |
---|
337 | }; |
---|
338 | |
---|
339 | int getNumCollisionObjects() const |
---|
340 | { |
---|
341 | return int(m_collisionObjects.size()); |
---|
342 | } |
---|
343 | |
---|
344 | /// rayTest performs a raycast on all objects in the btCollisionWorld, and calls the resultCallback |
---|
345 | /// This allows for several queries: first hit, all hits, any hit, dependent on the value returned by the callback. |
---|
346 | void rayTest(const btVector3& rayFromWorld, const btVector3& rayToWorld, RayResultCallback& resultCallback) const; |
---|
347 | |
---|
348 | // convexTest performs a swept convex cast on all objects in the btCollisionWorld, and calls the resultCallback |
---|
349 | // This allows for several queries: first hit, all hits, any hit, dependent on the value return by the callback. |
---|
350 | void convexSweepTest (const btConvexShape* castShape, const btTransform& from, const btTransform& to, ConvexResultCallback& resultCallback) const; |
---|
351 | |
---|
352 | |
---|
353 | /// rayTestSingle performs a raycast call and calls the resultCallback. It is used internally by rayTest. |
---|
354 | /// In a future implementation, we consider moving the ray test as a virtual method in btCollisionShape. |
---|
355 | /// This allows more customization. |
---|
356 | static void rayTestSingle(const btTransform& rayFromTrans,const btTransform& rayToTrans, |
---|
357 | btCollisionObject* collisionObject, |
---|
358 | const btCollisionShape* collisionShape, |
---|
359 | const btTransform& colObjWorldTransform, |
---|
360 | RayResultCallback& resultCallback); |
---|
361 | |
---|
362 | /// objectQuerySingle performs a collision detection query and calls the resultCallback. It is used internally by rayTest. |
---|
363 | static void objectQuerySingle(const btConvexShape* castShape, const btTransform& rayFromTrans,const btTransform& rayToTrans, |
---|
364 | btCollisionObject* collisionObject, |
---|
365 | const btCollisionShape* collisionShape, |
---|
366 | const btTransform& colObjWorldTransform, |
---|
367 | ConvexResultCallback& resultCallback, btScalar allowedPenetration); |
---|
368 | |
---|
369 | void addCollisionObject(btCollisionObject* collisionObject,short int collisionFilterGroup=btBroadphaseProxy::DefaultFilter,short int collisionFilterMask=btBroadphaseProxy::AllFilter); |
---|
370 | |
---|
371 | btCollisionObjectArray& getCollisionObjectArray() |
---|
372 | { |
---|
373 | return m_collisionObjects; |
---|
374 | } |
---|
375 | |
---|
376 | const btCollisionObjectArray& getCollisionObjectArray() const |
---|
377 | { |
---|
378 | return m_collisionObjects; |
---|
379 | } |
---|
380 | |
---|
381 | |
---|
382 | void removeCollisionObject(btCollisionObject* collisionObject); |
---|
383 | |
---|
384 | virtual void performDiscreteCollisionDetection(); |
---|
385 | |
---|
386 | btDispatcherInfo& getDispatchInfo() |
---|
387 | { |
---|
388 | return m_dispatchInfo; |
---|
389 | } |
---|
390 | |
---|
391 | const btDispatcherInfo& getDispatchInfo() const |
---|
392 | { |
---|
393 | return m_dispatchInfo; |
---|
394 | } |
---|
395 | |
---|
396 | }; |
---|
397 | |
---|
398 | |
---|
399 | #endif //COLLISION_WORLD_H |
---|