1 | /* |
---|
2 | Bullet Continuous Collision Detection and Physics Library |
---|
3 | Copyright (c) 2003-2006 Erwin Coumans http://bulletphysics.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 general Collision Detection and Physics discussions. |
---|
26 | * Please visit http://www.bulletphysics.com |
---|
27 | * |
---|
28 | * @section install_sec Installation |
---|
29 | * |
---|
30 | * @subsection step1 Step 1: Download |
---|
31 | * You can download the Bullet Physics Library from the Google Code repository: http://code.google.com/p/bullet/downloads/list |
---|
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/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 | |
---|
65 | |
---|
66 | #ifndef COLLISION_WORLD_H |
---|
67 | #define COLLISION_WORLD_H |
---|
68 | |
---|
69 | class btStackAlloc; |
---|
70 | class btCollisionShape; |
---|
71 | class btConvexShape; |
---|
72 | class btBroadphaseInterface; |
---|
73 | #include "LinearMath/btVector3.h" |
---|
74 | #include "LinearMath/btTransform.h" |
---|
75 | #include "btCollisionObject.h" |
---|
76 | #include "btCollisionDispatcher.h" |
---|
77 | #include "BulletCollision/BroadphaseCollision/btOverlappingPairCache.h" |
---|
78 | #include "LinearMath/btAlignedObjectArray.h" |
---|
79 | |
---|
80 | ///CollisionWorld is interface and container for the collision detection |
---|
81 | class btCollisionWorld |
---|
82 | { |
---|
83 | |
---|
84 | |
---|
85 | |
---|
86 | protected: |
---|
87 | |
---|
88 | btAlignedObjectArray<btCollisionObject*> m_collisionObjects; |
---|
89 | |
---|
90 | |
---|
91 | btDispatcher* m_dispatcher1; |
---|
92 | |
---|
93 | btDispatcherInfo m_dispatchInfo; |
---|
94 | |
---|
95 | btStackAlloc* m_stackAlloc; |
---|
96 | |
---|
97 | btBroadphaseInterface* m_broadphasePairCache; |
---|
98 | |
---|
99 | btIDebugDraw* m_debugDrawer; |
---|
100 | |
---|
101 | |
---|
102 | |
---|
103 | public: |
---|
104 | |
---|
105 | //this constructor doesn't own the dispatcher and paircache/broadphase |
---|
106 | btCollisionWorld(btDispatcher* dispatcher,btBroadphaseInterface* broadphasePairCache, btCollisionConfiguration* collisionConfiguration); |
---|
107 | |
---|
108 | virtual ~btCollisionWorld(); |
---|
109 | |
---|
110 | void setBroadphase(btBroadphaseInterface* pairCache) |
---|
111 | { |
---|
112 | m_broadphasePairCache = pairCache; |
---|
113 | } |
---|
114 | |
---|
115 | const btBroadphaseInterface* getBroadphase() const |
---|
116 | { |
---|
117 | return m_broadphasePairCache; |
---|
118 | } |
---|
119 | |
---|
120 | btBroadphaseInterface* getBroadphase() |
---|
121 | { |
---|
122 | return m_broadphasePairCache; |
---|
123 | } |
---|
124 | |
---|
125 | btOverlappingPairCache* getPairCache() |
---|
126 | { |
---|
127 | return m_broadphasePairCache->getOverlappingPairCache(); |
---|
128 | } |
---|
129 | |
---|
130 | |
---|
131 | btDispatcher* getDispatcher() |
---|
132 | { |
---|
133 | return m_dispatcher1; |
---|
134 | } |
---|
135 | |
---|
136 | const btDispatcher* getDispatcher() const |
---|
137 | { |
---|
138 | return m_dispatcher1; |
---|
139 | } |
---|
140 | |
---|
141 | void updateSingleAabb(btCollisionObject* colObj); |
---|
142 | |
---|
143 | virtual void updateAabbs(); |
---|
144 | |
---|
145 | |
---|
146 | virtual void setDebugDrawer(btIDebugDraw* debugDrawer) |
---|
147 | { |
---|
148 | m_debugDrawer = debugDrawer; |
---|
149 | } |
---|
150 | |
---|
151 | virtual btIDebugDraw* getDebugDrawer() |
---|
152 | { |
---|
153 | return m_debugDrawer; |
---|
154 | } |
---|
155 | |
---|
156 | |
---|
157 | ///LocalShapeInfo gives extra information for complex shapes |
---|
158 | ///Currently, only btTriangleMeshShape is available, so it just contains triangleIndex and subpart |
---|
159 | struct LocalShapeInfo |
---|
160 | { |
---|
161 | int m_shapePart; |
---|
162 | int m_triangleIndex; |
---|
163 | |
---|
164 | |
---|
165 | //const btCollisionShape* m_shapeTemp; |
---|
166 | //const btTransform* m_shapeLocalTransform; |
---|
167 | }; |
---|
168 | |
---|
169 | struct LocalRayResult |
---|
170 | { |
---|
171 | LocalRayResult(btCollisionObject* collisionObject, |
---|
172 | LocalShapeInfo* localShapeInfo, |
---|
173 | const btVector3& hitNormalLocal, |
---|
174 | btScalar hitFraction) |
---|
175 | :m_collisionObject(collisionObject), |
---|
176 | m_localShapeInfo(localShapeInfo), |
---|
177 | m_hitNormalLocal(hitNormalLocal), |
---|
178 | m_hitFraction(hitFraction) |
---|
179 | { |
---|
180 | } |
---|
181 | |
---|
182 | btCollisionObject* m_collisionObject; |
---|
183 | LocalShapeInfo* m_localShapeInfo; |
---|
184 | btVector3 m_hitNormalLocal; |
---|
185 | btScalar m_hitFraction; |
---|
186 | |
---|
187 | }; |
---|
188 | |
---|
189 | ///RayResultCallback is used to report new raycast results |
---|
190 | struct RayResultCallback |
---|
191 | { |
---|
192 | btScalar m_closestHitFraction; |
---|
193 | btCollisionObject* m_collisionObject; |
---|
194 | short int m_collisionFilterGroup; |
---|
195 | short int m_collisionFilterMask; |
---|
196 | //@BP Mod - Custom flags, currently used to enable backface culling on tri-meshes, see btRaycastCallback |
---|
197 | unsigned int m_flags; |
---|
198 | |
---|
199 | virtual ~RayResultCallback() |
---|
200 | { |
---|
201 | } |
---|
202 | bool hasHit() const |
---|
203 | { |
---|
204 | return (m_collisionObject != 0); |
---|
205 | } |
---|
206 | |
---|
207 | RayResultCallback() |
---|
208 | :m_closestHitFraction(btScalar(1.)), |
---|
209 | m_collisionObject(0), |
---|
210 | m_collisionFilterGroup(btBroadphaseProxy::DefaultFilter), |
---|
211 | m_collisionFilterMask(btBroadphaseProxy::AllFilter), |
---|
212 | //@BP Mod |
---|
213 | m_flags(0) |
---|
214 | { |
---|
215 | } |
---|
216 | |
---|
217 | virtual bool needsCollision(btBroadphaseProxy* proxy0) const |
---|
218 | { |
---|
219 | bool collides = (proxy0->m_collisionFilterGroup & m_collisionFilterMask) != 0; |
---|
220 | collides = collides && (m_collisionFilterGroup & proxy0->m_collisionFilterMask); |
---|
221 | return collides; |
---|
222 | } |
---|
223 | |
---|
224 | |
---|
225 | virtual btScalar addSingleResult(LocalRayResult& rayResult,bool normalInWorldSpace) = 0; |
---|
226 | }; |
---|
227 | |
---|
228 | struct ClosestRayResultCallback : public RayResultCallback |
---|
229 | { |
---|
230 | ClosestRayResultCallback(const btVector3& rayFromWorld,const btVector3& rayToWorld) |
---|
231 | :m_rayFromWorld(rayFromWorld), |
---|
232 | m_rayToWorld(rayToWorld) |
---|
233 | { |
---|
234 | } |
---|
235 | |
---|
236 | btVector3 m_rayFromWorld;//used to calculate hitPointWorld from hitFraction |
---|
237 | btVector3 m_rayToWorld; |
---|
238 | |
---|
239 | btVector3 m_hitNormalWorld; |
---|
240 | btVector3 m_hitPointWorld; |
---|
241 | |
---|
242 | |
---|
243 | virtual btScalar addSingleResult(LocalRayResult& rayResult,bool normalInWorldSpace) |
---|
244 | { |
---|
245 | //caller already does the filter on the m_closestHitFraction |
---|
246 | btAssert(rayResult.m_hitFraction <= m_closestHitFraction); |
---|
247 | |
---|
248 | |
---|
249 | m_closestHitFraction = rayResult.m_hitFraction; |
---|
250 | m_collisionObject = rayResult.m_collisionObject; |
---|
251 | if (normalInWorldSpace) |
---|
252 | { |
---|
253 | m_hitNormalWorld = rayResult.m_hitNormalLocal; |
---|
254 | } else |
---|
255 | { |
---|
256 | ///need to transform normal into worldspace |
---|
257 | m_hitNormalWorld = m_collisionObject->getWorldTransform().getBasis()*rayResult.m_hitNormalLocal; |
---|
258 | } |
---|
259 | m_hitPointWorld.setInterpolate3(m_rayFromWorld,m_rayToWorld,rayResult.m_hitFraction); |
---|
260 | return rayResult.m_hitFraction; |
---|
261 | } |
---|
262 | }; |
---|
263 | |
---|
264 | |
---|
265 | struct LocalConvexResult |
---|
266 | { |
---|
267 | LocalConvexResult(btCollisionObject* hitCollisionObject, |
---|
268 | LocalShapeInfo* localShapeInfo, |
---|
269 | const btVector3& hitNormalLocal, |
---|
270 | const btVector3& hitPointLocal, |
---|
271 | btScalar hitFraction |
---|
272 | ) |
---|
273 | :m_hitCollisionObject(hitCollisionObject), |
---|
274 | m_localShapeInfo(localShapeInfo), |
---|
275 | m_hitNormalLocal(hitNormalLocal), |
---|
276 | m_hitPointLocal(hitPointLocal), |
---|
277 | m_hitFraction(hitFraction) |
---|
278 | { |
---|
279 | } |
---|
280 | |
---|
281 | btCollisionObject* m_hitCollisionObject; |
---|
282 | LocalShapeInfo* m_localShapeInfo; |
---|
283 | btVector3 m_hitNormalLocal; |
---|
284 | btVector3 m_hitPointLocal; |
---|
285 | btScalar m_hitFraction; |
---|
286 | }; |
---|
287 | |
---|
288 | ///RayResultCallback is used to report new raycast results |
---|
289 | struct ConvexResultCallback |
---|
290 | { |
---|
291 | btScalar m_closestHitFraction; |
---|
292 | short int m_collisionFilterGroup; |
---|
293 | short int m_collisionFilterMask; |
---|
294 | |
---|
295 | |
---|
296 | ConvexResultCallback() |
---|
297 | :m_closestHitFraction(btScalar(1.)), |
---|
298 | m_collisionFilterGroup(btBroadphaseProxy::DefaultFilter), |
---|
299 | m_collisionFilterMask(btBroadphaseProxy::AllFilter) |
---|
300 | { |
---|
301 | } |
---|
302 | |
---|
303 | virtual ~ConvexResultCallback() |
---|
304 | { |
---|
305 | } |
---|
306 | |
---|
307 | |
---|
308 | bool hasHit() const |
---|
309 | { |
---|
310 | return (m_closestHitFraction < btScalar(1.)); |
---|
311 | } |
---|
312 | |
---|
313 | |
---|
314 | |
---|
315 | |
---|
316 | virtual bool needsCollision(btBroadphaseProxy* proxy0) const |
---|
317 | { |
---|
318 | bool collides = (proxy0->m_collisionFilterGroup & m_collisionFilterMask) != 0; |
---|
319 | collides = collides && (m_collisionFilterGroup & proxy0->m_collisionFilterMask); |
---|
320 | return collides; |
---|
321 | } |
---|
322 | |
---|
323 | virtual btScalar addSingleResult(LocalConvexResult& convexResult,bool normalInWorldSpace) = 0; |
---|
324 | }; |
---|
325 | |
---|
326 | struct ClosestConvexResultCallback : public ConvexResultCallback |
---|
327 | { |
---|
328 | ClosestConvexResultCallback(const btVector3& convexFromWorld,const btVector3& convexToWorld) |
---|
329 | :m_convexFromWorld(convexFromWorld), |
---|
330 | m_convexToWorld(convexToWorld), |
---|
331 | m_hitCollisionObject(0) |
---|
332 | { |
---|
333 | } |
---|
334 | |
---|
335 | btVector3 m_convexFromWorld;//used to calculate hitPointWorld from hitFraction |
---|
336 | btVector3 m_convexToWorld; |
---|
337 | |
---|
338 | btVector3 m_hitNormalWorld; |
---|
339 | btVector3 m_hitPointWorld; |
---|
340 | btCollisionObject* m_hitCollisionObject; |
---|
341 | |
---|
342 | |
---|
343 | virtual btScalar addSingleResult(LocalConvexResult& convexResult,bool normalInWorldSpace) |
---|
344 | { |
---|
345 | //caller already does the filter on the m_closestHitFraction |
---|
346 | btAssert(convexResult.m_hitFraction <= m_closestHitFraction); |
---|
347 | |
---|
348 | |
---|
349 | m_closestHitFraction = convexResult.m_hitFraction; |
---|
350 | m_hitCollisionObject = convexResult.m_hitCollisionObject; |
---|
351 | if (normalInWorldSpace) |
---|
352 | { |
---|
353 | m_hitNormalWorld = convexResult.m_hitNormalLocal; |
---|
354 | } else |
---|
355 | { |
---|
356 | ///need to transform normal into worldspace |
---|
357 | m_hitNormalWorld = m_hitCollisionObject->getWorldTransform().getBasis()*convexResult.m_hitNormalLocal; |
---|
358 | } |
---|
359 | m_hitPointWorld = convexResult.m_hitPointLocal; |
---|
360 | return convexResult.m_hitFraction; |
---|
361 | } |
---|
362 | }; |
---|
363 | |
---|
364 | int getNumCollisionObjects() const |
---|
365 | { |
---|
366 | return int(m_collisionObjects.size()); |
---|
367 | } |
---|
368 | |
---|
369 | /// rayTest performs a raycast on all objects in the btCollisionWorld, and calls the resultCallback |
---|
370 | /// This allows for several queries: first hit, all hits, any hit, dependent on the value returned by the callback. |
---|
371 | void rayTest(const btVector3& rayFromWorld, const btVector3& rayToWorld, RayResultCallback& resultCallback) const; |
---|
372 | |
---|
373 | // convexTest performs a swept convex cast on all objects in the btCollisionWorld, and calls the resultCallback |
---|
374 | // This allows for several queries: first hit, all hits, any hit, dependent on the value return by the callback. |
---|
375 | void convexSweepTest (const btConvexShape* castShape, const btTransform& from, const btTransform& to, ConvexResultCallback& resultCallback, btScalar allowedCcdPenetration = btScalar(0.)) const; |
---|
376 | |
---|
377 | |
---|
378 | /// rayTestSingle performs a raycast call and calls the resultCallback. It is used internally by rayTest. |
---|
379 | /// In a future implementation, we consider moving the ray test as a virtual method in btCollisionShape. |
---|
380 | /// This allows more customization. |
---|
381 | static void rayTestSingle(const btTransform& rayFromTrans,const btTransform& rayToTrans, |
---|
382 | btCollisionObject* collisionObject, |
---|
383 | const btCollisionShape* collisionShape, |
---|
384 | const btTransform& colObjWorldTransform, |
---|
385 | RayResultCallback& resultCallback); |
---|
386 | |
---|
387 | /// objectQuerySingle performs a collision detection query and calls the resultCallback. It is used internally by rayTest. |
---|
388 | static void objectQuerySingle(const btConvexShape* castShape, const btTransform& rayFromTrans,const btTransform& rayToTrans, |
---|
389 | btCollisionObject* collisionObject, |
---|
390 | const btCollisionShape* collisionShape, |
---|
391 | const btTransform& colObjWorldTransform, |
---|
392 | ConvexResultCallback& resultCallback, btScalar allowedPenetration); |
---|
393 | |
---|
394 | void addCollisionObject(btCollisionObject* collisionObject,short int collisionFilterGroup=btBroadphaseProxy::DefaultFilter,short int collisionFilterMask=btBroadphaseProxy::AllFilter); |
---|
395 | |
---|
396 | btCollisionObjectArray& getCollisionObjectArray() |
---|
397 | { |
---|
398 | return m_collisionObjects; |
---|
399 | } |
---|
400 | |
---|
401 | const btCollisionObjectArray& getCollisionObjectArray() const |
---|
402 | { |
---|
403 | return m_collisionObjects; |
---|
404 | } |
---|
405 | |
---|
406 | |
---|
407 | void removeCollisionObject(btCollisionObject* collisionObject); |
---|
408 | |
---|
409 | virtual void performDiscreteCollisionDetection(); |
---|
410 | |
---|
411 | btDispatcherInfo& getDispatchInfo() |
---|
412 | { |
---|
413 | return m_dispatchInfo; |
---|
414 | } |
---|
415 | |
---|
416 | const btDispatcherInfo& getDispatchInfo() const |
---|
417 | { |
---|
418 | return m_dispatchInfo; |
---|
419 | } |
---|
420 | |
---|
421 | }; |
---|
422 | |
---|
423 | |
---|
424 | #endif //COLLISION_WORLD_H |
---|