Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/physics/src/bullet/BulletCollision/CollisionDispatch/btCollisionWorld.h @ 1963

Last change on this file since 1963 was 1963, checked in by rgrieder, 16 years ago

Added Bullet physics engine.

  • Property svn:eol-style set to native
File size: 13.7 KB
Line 
1/*
2Bullet Continuous Collision Detection and Physics Library
3Copyright (c) 2003-2006 Erwin Coumans  http://continuousphysics.com/Bullet/
4
5This software is provided 'as-is', without any express or implied warranty.
6In no event will the authors be held liable for any damages arising from the use of this software.
7Permission is granted to anyone to use this software for any purpose,
8including commercial applications, and to alter it and redistribute it freely,
9subject to the following restrictions:
10
111. 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.
122. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
133. 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
67class btStackAlloc;
68class btCollisionShape;
69class btConvexShape;
70class 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
79class btCollisionWorld
80{
81
82       
83protected:
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       
98public:
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        virtual void    setDebugDrawer(btIDebugDraw*    debugDrawer)
139        {
140                        m_debugDrawer = debugDrawer;
141        }
142
143        virtual btIDebugDraw*   getDebugDrawer()
144        {
145                return m_debugDrawer;
146        }
147
148
149        ///LocalShapeInfo gives extra information for complex shapes
150        ///Currently, only btTriangleMeshShape is available, so it just contains triangleIndex and subpart
151        struct  LocalShapeInfo
152        {
153                int     m_shapePart;
154                int     m_triangleIndex;
155               
156                //const btCollisionShape*       m_shapeTemp;
157                //const btTransform*    m_shapeLocalTransform;
158        };
159
160        struct  LocalRayResult
161        {
162                LocalRayResult(btCollisionObject*       collisionObject, 
163                        LocalShapeInfo* localShapeInfo,
164                        const btVector3&                hitNormalLocal,
165                        btScalar hitFraction)
166                :m_collisionObject(collisionObject),
167                m_localShapeInfo(localShapeInfo),
168                m_hitNormalLocal(hitNormalLocal),
169                m_hitFraction(hitFraction)
170                {
171                }
172
173                btCollisionObject*              m_collisionObject;
174                LocalShapeInfo*                 m_localShapeInfo;
175                btVector3                               m_hitNormalLocal;
176                btScalar                                m_hitFraction;
177
178        };
179
180        ///RayResultCallback is used to report new raycast results
181        struct  RayResultCallback
182        {
183                btScalar        m_closestHitFraction;
184                btCollisionObject*              m_collisionObject;
185                short int       m_collisionFilterGroup;
186                short int       m_collisionFilterMask;
187
188                virtual ~RayResultCallback()
189                {
190                }
191                bool    hasHit() const
192                {
193                        return (m_collisionObject != 0);
194                }
195
196                RayResultCallback()
197                        :m_closestHitFraction(btScalar(1.)),
198                        m_collisionObject(0),
199                        m_collisionFilterGroup(btBroadphaseProxy::DefaultFilter),
200                        m_collisionFilterMask(btBroadphaseProxy::AllFilter)
201                {
202                }
203
204                virtual bool needsCollision(btBroadphaseProxy* proxy0) const
205                {
206                        bool collides = (proxy0->m_collisionFilterGroup & m_collisionFilterMask) != 0;
207                        collides = collides && (m_collisionFilterGroup & proxy0->m_collisionFilterMask);
208                        return collides;
209                }
210
211
212                virtual btScalar        addSingleResult(LocalRayResult& rayResult,bool normalInWorldSpace) = 0;
213        };
214
215        struct  ClosestRayResultCallback : public RayResultCallback
216        {
217                ClosestRayResultCallback(const btVector3&       rayFromWorld,const btVector3&   rayToWorld)
218                :m_rayFromWorld(rayFromWorld),
219                m_rayToWorld(rayToWorld)
220                {
221                }
222
223                btVector3       m_rayFromWorld;//used to calculate hitPointWorld from hitFraction
224                btVector3       m_rayToWorld;
225
226                btVector3       m_hitNormalWorld;
227                btVector3       m_hitPointWorld;
228                       
229                virtual btScalar        addSingleResult(LocalRayResult& rayResult,bool normalInWorldSpace)
230                {
231                        //caller already does the filter on the m_closestHitFraction
232                        btAssert(rayResult.m_hitFraction <= m_closestHitFraction);
233                       
234                        m_closestHitFraction = rayResult.m_hitFraction;
235                        m_collisionObject = rayResult.m_collisionObject;
236                        if (normalInWorldSpace)
237                        {
238                                m_hitNormalWorld = rayResult.m_hitNormalLocal;
239                        } else
240                        {
241                                ///need to transform normal into worldspace
242                                m_hitNormalWorld = m_collisionObject->getWorldTransform().getBasis()*rayResult.m_hitNormalLocal;
243                        }
244                        m_hitPointWorld.setInterpolate3(m_rayFromWorld,m_rayToWorld,rayResult.m_hitFraction);
245                        return rayResult.m_hitFraction;
246                }
247        };
248
249
250        struct LocalConvexResult
251        {
252                LocalConvexResult(btCollisionObject*    hitCollisionObject, 
253                        LocalShapeInfo* localShapeInfo,
254                        const btVector3&                hitNormalLocal,
255                        const btVector3&                hitPointLocal,
256                        btScalar hitFraction
257                        )
258                :m_hitCollisionObject(hitCollisionObject),
259                m_localShapeInfo(localShapeInfo),
260                m_hitNormalLocal(hitNormalLocal),
261                m_hitPointLocal(hitPointLocal),
262                m_hitFraction(hitFraction)
263                {
264                }
265
266                btCollisionObject*              m_hitCollisionObject;
267                LocalShapeInfo*                 m_localShapeInfo;
268                btVector3                               m_hitNormalLocal;
269                btVector3                               m_hitPointLocal;
270                btScalar                                m_hitFraction;
271        };
272
273        ///RayResultCallback is used to report new raycast results
274        struct  ConvexResultCallback
275        {
276                btScalar        m_closestHitFraction;
277                short int       m_collisionFilterGroup;
278                short int       m_collisionFilterMask;
279               
280                ConvexResultCallback()
281                        :m_closestHitFraction(btScalar(1.)),
282                        m_collisionFilterGroup(btBroadphaseProxy::DefaultFilter),
283                        m_collisionFilterMask(btBroadphaseProxy::AllFilter)
284                {
285                }
286
287                virtual ~ConvexResultCallback()
288                {
289                }
290               
291                bool    hasHit() const
292                {
293                        return (m_closestHitFraction < btScalar(1.));
294                }
295
296               
297
298                virtual bool needsCollision(btBroadphaseProxy* proxy0) const
299                {
300                        bool collides = (proxy0->m_collisionFilterGroup & m_collisionFilterMask) != 0;
301                        collides = collides && (m_collisionFilterGroup & proxy0->m_collisionFilterMask);
302                        return collides;
303                }
304
305                virtual btScalar        addSingleResult(LocalConvexResult& convexResult,bool normalInWorldSpace) = 0;
306        };
307
308        struct  ClosestConvexResultCallback : public ConvexResultCallback
309        {
310                ClosestConvexResultCallback(const btVector3&    convexFromWorld,const btVector3&        convexToWorld)
311                :m_convexFromWorld(convexFromWorld),
312                m_convexToWorld(convexToWorld),
313                m_hitCollisionObject(0)
314                {
315                }
316
317                btVector3       m_convexFromWorld;//used to calculate hitPointWorld from hitFraction
318                btVector3       m_convexToWorld;
319
320                btVector3       m_hitNormalWorld;
321                btVector3       m_hitPointWorld;
322                btCollisionObject*      m_hitCollisionObject;
323               
324                virtual btScalar        addSingleResult(LocalConvexResult& convexResult,bool normalInWorldSpace)
325                {
326//caller already does the filter on the m_closestHitFraction
327                        btAssert(convexResult.m_hitFraction <= m_closestHitFraction);
328                                               
329                        m_closestHitFraction = convexResult.m_hitFraction;
330                        m_hitCollisionObject = convexResult.m_hitCollisionObject;
331                        if (normalInWorldSpace)
332                        {
333                                m_hitNormalWorld = convexResult.m_hitNormalLocal;
334                        } else
335                        {
336                                ///need to transform normal into worldspace
337                                m_hitNormalWorld = m_hitCollisionObject->getWorldTransform().getBasis()*convexResult.m_hitNormalLocal;
338                        }
339                        m_hitPointWorld = convexResult.m_hitPointLocal;
340                        return convexResult.m_hitFraction;
341                }
342        };
343
344        int     getNumCollisionObjects() const
345        {
346                return int(m_collisionObjects.size());
347        }
348
349        /// rayTest performs a raycast on all objects in the btCollisionWorld, and calls the resultCallback
350        /// This allows for several queries: first hit, all hits, any hit, dependent on the value returned by the callback.
351        void    rayTest(const btVector3& rayFromWorld, const btVector3& rayToWorld, RayResultCallback& resultCallback) const; 
352
353        // convexTest performs a swept convex cast on all objects in the btCollisionWorld, and calls the resultCallback
354        // This allows for several queries: first hit, all hits, any hit, dependent on the value return by the callback.
355        void    convexSweepTest (const btConvexShape* castShape, const btTransform& from, const btTransform& to, ConvexResultCallback& resultCallback) const;
356
357
358        /// rayTestSingle performs a raycast call and calls the resultCallback. It is used internally by rayTest.
359        /// In a future implementation, we consider moving the ray test as a virtual method in btCollisionShape.
360        /// This allows more customization.
361        static void     rayTestSingle(const btTransform& rayFromTrans,const btTransform& rayToTrans,
362                                          btCollisionObject* collisionObject,
363                                          const btCollisionShape* collisionShape,
364                                          const btTransform& colObjWorldTransform,
365                                          RayResultCallback& resultCallback);
366
367        /// objectQuerySingle performs a collision detection query and calls the resultCallback. It is used internally by rayTest.
368        static void     objectQuerySingle(const btConvexShape* castShape, const btTransform& rayFromTrans,const btTransform& rayToTrans,
369                                          btCollisionObject* collisionObject,
370                                          const btCollisionShape* collisionShape,
371                                          const btTransform& colObjWorldTransform,
372                                          ConvexResultCallback& resultCallback, btScalar        allowedPenetration);
373
374        void    addCollisionObject(btCollisionObject* collisionObject,short int collisionFilterGroup=btBroadphaseProxy::DefaultFilter,short int collisionFilterMask=btBroadphaseProxy::AllFilter);
375
376        btCollisionObjectArray& getCollisionObjectArray()
377        {
378                return m_collisionObjects;
379        }
380
381        const btCollisionObjectArray& getCollisionObjectArray() const
382        {
383                return m_collisionObjects;
384        }
385
386
387        void    removeCollisionObject(btCollisionObject* collisionObject);
388
389        virtual void    performDiscreteCollisionDetection();
390
391        btDispatcherInfo& getDispatchInfo()
392        {
393                return m_dispatchInfo;
394        }
395
396        const btDispatcherInfo& getDispatchInfo() const
397        {
398                return m_dispatchInfo;
399        }
400
401};
402
403
404#endif //COLLISION_WORLD_H
Note: See TracBrowser for help on using the repository browser.