Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/bullet/BulletCollision/CollisionDispatch/btCollisionWorld.h @ 2877

Last change on this file since 2877 was 2877, checked in by bknecht, 15 years ago

reverted back everything, sorry KDevelop screws things up sometimes

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