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 | #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" |
---|
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 | const btBroadphaseInterface* getBroadphase() const |
---|
111 | { |
---|
112 | return m_broadphasePairCache; |
---|
113 | } |
---|
114 | |
---|
115 | btBroadphaseInterface* getBroadphase() |
---|
116 | { |
---|
117 | return m_broadphasePairCache; |
---|
118 | } |
---|
119 | |
---|
120 | btOverlappingPairCache* getPairCache() |
---|
121 | { |
---|
122 | return m_broadphasePairCache->getOverlappingPairCache(); |
---|
123 | } |
---|
124 | |
---|
125 | |
---|
126 | btDispatcher* getDispatcher() |
---|
127 | { |
---|
128 | return m_dispatcher1; |
---|
129 | } |
---|
130 | |
---|
131 | const btDispatcher* getDispatcher() const |
---|
132 | { |
---|
133 | return m_dispatcher1; |
---|
134 | } |
---|
135 | |
---|
136 | virtual void updateAabbs(); |
---|
137 | |
---|
138 | |
---|
139 | virtual void setDebugDrawer(btIDebugDraw* debugDrawer) |
---|
140 | { |
---|
141 | m_debugDrawer = debugDrawer; |
---|
142 | } |
---|
143 | |
---|
144 | virtual btIDebugDraw* getDebugDrawer() |
---|
145 | { |
---|
146 | return m_debugDrawer; |
---|
147 | } |
---|
148 | |
---|
149 | |
---|
150 | ///LocalShapeInfo gives extra information for complex shapes |
---|
151 | ///Currently, only btTriangleMeshShape is available, so it just contains triangleIndex and subpart |
---|
152 | struct LocalShapeInfo |
---|
153 | { |
---|
154 | int m_shapePart; |
---|
155 | int m_triangleIndex; |
---|
156 | |
---|
157 | //const btCollisionShape* m_shapeTemp; |
---|
158 | //const btTransform* m_shapeLocalTransform; |
---|
159 | }; |
---|
160 | |
---|
161 | struct LocalRayResult |
---|
162 | { |
---|
163 | LocalRayResult(btCollisionObject* collisionObject, |
---|
164 | LocalShapeInfo* localShapeInfo, |
---|
165 | const btVector3& hitNormalLocal, |
---|
166 | btScalar hitFraction) |
---|
167 | :m_collisionObject(collisionObject), |
---|
168 | m_localShapeInfo(localShapeInfo), |
---|
169 | m_hitNormalLocal(hitNormalLocal), |
---|
170 | m_hitFraction(hitFraction) |
---|
171 | { |
---|
172 | } |
---|
173 | |
---|
174 | btCollisionObject* m_collisionObject; |
---|
175 | LocalShapeInfo* m_localShapeInfo; |
---|
176 | btVector3 m_hitNormalLocal; |
---|
177 | btScalar m_hitFraction; |
---|
178 | |
---|
179 | }; |
---|
180 | |
---|
181 | ///RayResultCallback is used to report new raycast results |
---|
182 | struct RayResultCallback |
---|
183 | { |
---|
184 | btScalar m_closestHitFraction; |
---|
185 | btCollisionObject* m_collisionObject; |
---|
186 | short int m_collisionFilterGroup; |
---|
187 | short int m_collisionFilterMask; |
---|
188 | |
---|
189 | virtual ~RayResultCallback() |
---|
190 | { |
---|
191 | } |
---|
192 | bool hasHit() const |
---|
193 | { |
---|
194 | return (m_collisionObject != 0); |
---|
195 | } |
---|
196 | |
---|
197 | RayResultCallback() |
---|
198 | :m_closestHitFraction(btScalar(1.)), |
---|
199 | m_collisionObject(0), |
---|
200 | m_collisionFilterGroup(btBroadphaseProxy::DefaultFilter), |
---|
201 | m_collisionFilterMask(btBroadphaseProxy::AllFilter) |
---|
202 | { |
---|
203 | } |
---|
204 | |
---|
205 | virtual bool needsCollision(btBroadphaseProxy* proxy0) const |
---|
206 | { |
---|
207 | bool collides = (proxy0->m_collisionFilterGroup & m_collisionFilterMask) != 0; |
---|
208 | collides = collides && (m_collisionFilterGroup & proxy0->m_collisionFilterMask); |
---|
209 | return collides; |
---|
210 | } |
---|
211 | |
---|
212 | |
---|
213 | virtual btScalar addSingleResult(LocalRayResult& rayResult,bool normalInWorldSpace) = 0; |
---|
214 | }; |
---|
215 | |
---|
216 | struct ClosestRayResultCallback : public RayResultCallback |
---|
217 | { |
---|
218 | ClosestRayResultCallback(const btVector3& rayFromWorld,const btVector3& rayToWorld) |
---|
219 | :m_rayFromWorld(rayFromWorld), |
---|
220 | m_rayToWorld(rayToWorld) |
---|
221 | { |
---|
222 | } |
---|
223 | |
---|
224 | btVector3 m_rayFromWorld;//used to calculate hitPointWorld from hitFraction |
---|
225 | btVector3 m_rayToWorld; |
---|
226 | |
---|
227 | btVector3 m_hitNormalWorld; |
---|
228 | btVector3 m_hitPointWorld; |
---|
229 | |
---|
230 | virtual btScalar addSingleResult(LocalRayResult& rayResult,bool normalInWorldSpace) |
---|
231 | { |
---|
232 | //caller already does the filter on the m_closestHitFraction |
---|
233 | btAssert(rayResult.m_hitFraction <= m_closestHitFraction); |
---|
234 | |
---|
235 | m_closestHitFraction = rayResult.m_hitFraction; |
---|
236 | m_collisionObject = rayResult.m_collisionObject; |
---|
237 | if (normalInWorldSpace) |
---|
238 | { |
---|
239 | m_hitNormalWorld = rayResult.m_hitNormalLocal; |
---|
240 | } else |
---|
241 | { |
---|
242 | ///need to transform normal into worldspace |
---|
243 | m_hitNormalWorld = m_collisionObject->getWorldTransform().getBasis()*rayResult.m_hitNormalLocal; |
---|
244 | } |
---|
245 | m_hitPointWorld.setInterpolate3(m_rayFromWorld,m_rayToWorld,rayResult.m_hitFraction); |
---|
246 | return rayResult.m_hitFraction; |
---|
247 | } |
---|
248 | }; |
---|
249 | |
---|
250 | |
---|
251 | struct LocalConvexResult |
---|
252 | { |
---|
253 | LocalConvexResult(btCollisionObject* hitCollisionObject, |
---|
254 | LocalShapeInfo* localShapeInfo, |
---|
255 | const btVector3& hitNormalLocal, |
---|
256 | const btVector3& hitPointLocal, |
---|
257 | btScalar hitFraction |
---|
258 | ) |
---|
259 | :m_hitCollisionObject(hitCollisionObject), |
---|
260 | m_localShapeInfo(localShapeInfo), |
---|
261 | m_hitNormalLocal(hitNormalLocal), |
---|
262 | m_hitPointLocal(hitPointLocal), |
---|
263 | m_hitFraction(hitFraction) |
---|
264 | { |
---|
265 | } |
---|
266 | |
---|
267 | btCollisionObject* m_hitCollisionObject; |
---|
268 | LocalShapeInfo* m_localShapeInfo; |
---|
269 | btVector3 m_hitNormalLocal; |
---|
270 | btVector3 m_hitPointLocal; |
---|
271 | btScalar m_hitFraction; |
---|
272 | }; |
---|
273 | |
---|
274 | ///RayResultCallback is used to report new raycast results |
---|
275 | struct ConvexResultCallback |
---|
276 | { |
---|
277 | btScalar m_closestHitFraction; |
---|
278 | short int m_collisionFilterGroup; |
---|
279 | short int m_collisionFilterMask; |
---|
280 | |
---|
281 | ConvexResultCallback() |
---|
282 | :m_closestHitFraction(btScalar(1.)), |
---|
283 | m_collisionFilterGroup(btBroadphaseProxy::DefaultFilter), |
---|
284 | m_collisionFilterMask(btBroadphaseProxy::AllFilter) |
---|
285 | { |
---|
286 | } |
---|
287 | |
---|
288 | virtual ~ConvexResultCallback() |
---|
289 | { |
---|
290 | } |
---|
291 | |
---|
292 | bool hasHit() const |
---|
293 | { |
---|
294 | return (m_closestHitFraction < btScalar(1.)); |
---|
295 | } |
---|
296 | |
---|
297 | |
---|
298 | |
---|
299 | virtual bool needsCollision(btBroadphaseProxy* proxy0) const |
---|
300 | { |
---|
301 | bool collides = (proxy0->m_collisionFilterGroup & m_collisionFilterMask) != 0; |
---|
302 | collides = collides && (m_collisionFilterGroup & proxy0->m_collisionFilterMask); |
---|
303 | return collides; |
---|
304 | } |
---|
305 | |
---|
306 | virtual btScalar addSingleResult(LocalConvexResult& convexResult,bool normalInWorldSpace) = 0; |
---|
307 | }; |
---|
308 | |
---|
309 | struct ClosestConvexResultCallback : public ConvexResultCallback |
---|
310 | { |
---|
311 | ClosestConvexResultCallback(const btVector3& convexFromWorld,const btVector3& convexToWorld) |
---|
312 | :m_convexFromWorld(convexFromWorld), |
---|
313 | m_convexToWorld(convexToWorld), |
---|
314 | m_hitCollisionObject(0) |
---|
315 | { |
---|
316 | } |
---|
317 | |
---|
318 | btVector3 m_convexFromWorld;//used to calculate hitPointWorld from hitFraction |
---|
319 | btVector3 m_convexToWorld; |
---|
320 | |
---|
321 | btVector3 m_hitNormalWorld; |
---|
322 | btVector3 m_hitPointWorld; |
---|
323 | btCollisionObject* m_hitCollisionObject; |
---|
324 | |
---|
325 | virtual btScalar addSingleResult(LocalConvexResult& convexResult,bool normalInWorldSpace) |
---|
326 | { |
---|
327 | //caller already does the filter on the m_closestHitFraction |
---|
328 | btAssert(convexResult.m_hitFraction <= m_closestHitFraction); |
---|
329 | |
---|
330 | m_closestHitFraction = convexResult.m_hitFraction; |
---|
331 | m_hitCollisionObject = convexResult.m_hitCollisionObject; |
---|
332 | if (normalInWorldSpace) |
---|
333 | { |
---|
334 | m_hitNormalWorld = convexResult.m_hitNormalLocal; |
---|
335 | } else |
---|
336 | { |
---|
337 | ///need to transform normal into worldspace |
---|
338 | m_hitNormalWorld = m_hitCollisionObject->getWorldTransform().getBasis()*convexResult.m_hitNormalLocal; |
---|
339 | } |
---|
340 | m_hitPointWorld = convexResult.m_hitPointLocal; |
---|
341 | return convexResult.m_hitFraction; |
---|
342 | } |
---|
343 | }; |
---|
344 | |
---|
345 | int getNumCollisionObjects() const |
---|
346 | { |
---|
347 | return int(m_collisionObjects.size()); |
---|
348 | } |
---|
349 | |
---|
350 | /// rayTest performs a raycast on all objects in the btCollisionWorld, and calls the resultCallback |
---|
351 | /// This allows for several queries: first hit, all hits, any hit, dependent on the value returned by the callback. |
---|
352 | void rayTest(const btVector3& rayFromWorld, const btVector3& rayToWorld, RayResultCallback& resultCallback) const; |
---|
353 | |
---|
354 | // convexTest performs a swept convex cast on all objects in the btCollisionWorld, and calls the resultCallback |
---|
355 | // This allows for several queries: first hit, all hits, any hit, dependent on the value return by the callback. |
---|
356 | void convexSweepTest (const btConvexShape* castShape, const btTransform& from, const btTransform& to, ConvexResultCallback& resultCallback, btScalar allowedCcdPenetration = btScalar(0.)) const; |
---|
357 | |
---|
358 | |
---|
359 | /// rayTestSingle performs a raycast call and calls the resultCallback. It is used internally by rayTest. |
---|
360 | /// In a future implementation, we consider moving the ray test as a virtual method in btCollisionShape. |
---|
361 | /// This allows more customization. |
---|
362 | static void rayTestSingle(const btTransform& rayFromTrans,const btTransform& rayToTrans, |
---|
363 | btCollisionObject* collisionObject, |
---|
364 | const btCollisionShape* collisionShape, |
---|
365 | const btTransform& colObjWorldTransform, |
---|
366 | RayResultCallback& resultCallback); |
---|
367 | |
---|
368 | /// objectQuerySingle performs a collision detection query and calls the resultCallback. It is used internally by rayTest. |
---|
369 | static void objectQuerySingle(const btConvexShape* castShape, const btTransform& rayFromTrans,const btTransform& rayToTrans, |
---|
370 | btCollisionObject* collisionObject, |
---|
371 | const btCollisionShape* collisionShape, |
---|
372 | const btTransform& colObjWorldTransform, |
---|
373 | ConvexResultCallback& resultCallback, btScalar allowedPenetration); |
---|
374 | |
---|
375 | void addCollisionObject(btCollisionObject* collisionObject,short int collisionFilterGroup=btBroadphaseProxy::DefaultFilter,short int collisionFilterMask=btBroadphaseProxy::AllFilter); |
---|
376 | |
---|
377 | btCollisionObjectArray& getCollisionObjectArray() |
---|
378 | { |
---|
379 | return m_collisionObjects; |
---|
380 | } |
---|
381 | |
---|
382 | const btCollisionObjectArray& getCollisionObjectArray() const |
---|
383 | { |
---|
384 | return m_collisionObjects; |
---|
385 | } |
---|
386 | |
---|
387 | |
---|
388 | void removeCollisionObject(btCollisionObject* collisionObject); |
---|
389 | |
---|
390 | virtual void performDiscreteCollisionDetection(); |
---|
391 | |
---|
392 | btDispatcherInfo& getDispatchInfo() |
---|
393 | { |
---|
394 | return m_dispatchInfo; |
---|
395 | } |
---|
396 | |
---|
397 | const btDispatcherInfo& getDispatchInfo() const |
---|
398 | { |
---|
399 | return m_dispatchInfo; |
---|
400 | } |
---|
401 | |
---|
402 | }; |
---|
403 | |
---|
404 | |
---|
405 | #endif //COLLISION_WORLD_H |
---|