Bullet Collision Detection & Physics Library
btCollisionWorld.h
Go to the documentation of this file.
1 /*
2 Bullet Continuous Collision Detection and Physics Library
3 Copyright (c) 2003-2013 Erwin Coumans http://bulletphysics.org
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 
72 #ifndef BT_COLLISION_WORLD_H
73 #define BT_COLLISION_WORLD_H
74 
75 class btCollisionShape;
76 class btConvexShape;
78 class btSerializer;
79 
80 #include "LinearMath/btVector3.h"
81 #include "LinearMath/btTransform.h"
82 #include "btCollisionObject.h"
83 #include "btCollisionDispatcher.h"
86 
89 {
90 
91 
92 protected:
93 
95 
97 
99 
101 
103 
107 
108  void serializeCollisionObjects(btSerializer* serializer);
109 
110 public:
111 
112  //this constructor doesn't own the dispatcher and paircache/broadphase
113  btCollisionWorld(btDispatcher* dispatcher,btBroadphaseInterface* broadphasePairCache, btCollisionConfiguration* collisionConfiguration);
114 
115  virtual ~btCollisionWorld();
116 
118  {
119  m_broadphasePairCache = pairCache;
120  }
121 
123  {
124  return m_broadphasePairCache;
125  }
126 
128  {
129  return m_broadphasePairCache;
130  }
131 
133  {
135  }
136 
137 
139  {
140  return m_dispatcher1;
141  }
142 
144  {
145  return m_dispatcher1;
146  }
147 
148  void updateSingleAabb(btCollisionObject* colObj);
149 
150  virtual void updateAabbs();
151 
154  virtual void computeOverlappingPairs();
155 
156 
157  virtual void setDebugDrawer(btIDebugDraw* debugDrawer)
158  {
159  m_debugDrawer = debugDrawer;
160  }
161 
163  {
164  return m_debugDrawer;
165  }
166 
167  virtual void debugDrawWorld();
168 
169  virtual void debugDrawObject(const btTransform& worldTransform, const btCollisionShape* shape, const btVector3& color);
170 
171 
175  {
178 
179  //const btCollisionShape* m_shapeTemp;
180  //const btTransform* m_shapeLocalTransform;
181  };
182 
184  {
185  LocalRayResult(const btCollisionObject* collisionObject,
186  LocalShapeInfo* localShapeInfo,
187  const btVector3& hitNormalLocal,
188  btScalar hitFraction)
189  :m_collisionObject(collisionObject),
190  m_localShapeInfo(localShapeInfo),
191  m_hitNormalLocal(hitNormalLocal),
192  m_hitFraction(hitFraction)
193  {
194  }
195 
200 
201  };
202 
205  {
210  //@BP Mod - Custom flags, currently used to enable backface culling on tri-meshes, see btRaycastCallback.h. Apply any of the EFlags defined there on m_flags here to invoke.
211  unsigned int m_flags;
212 
214  {
215  }
216  bool hasHit() const
217  {
218  return (m_collisionObject != 0);
219  }
220 
226  //@BP Mod
227  m_flags(0)
228  {
229  }
230 
231  virtual bool needsCollision(btBroadphaseProxy* proxy0) const
232  {
233  bool collides = (proxy0->m_collisionFilterGroup & m_collisionFilterMask) != 0;
234  collides = collides && (m_collisionFilterGroup & proxy0->m_collisionFilterMask);
235  return collides;
236  }
237 
238 
239  virtual btScalar addSingleResult(LocalRayResult& rayResult,bool normalInWorldSpace) = 0;
240  };
241 
243  {
244  ClosestRayResultCallback(const btVector3& rayFromWorld,const btVector3& rayToWorld)
245  :m_rayFromWorld(rayFromWorld),
246  m_rayToWorld(rayToWorld)
247  {
248  }
249 
250  btVector3 m_rayFromWorld;//used to calculate hitPointWorld from hitFraction
252 
255 
256  virtual btScalar addSingleResult(LocalRayResult& rayResult,bool normalInWorldSpace)
257  {
258  //caller already does the filter on the m_closestHitFraction
260 
263  if (normalInWorldSpace)
264  {
266  } else
267  {
270  }
272  return rayResult.m_hitFraction;
273  }
274  };
275 
277  {
278  AllHitsRayResultCallback(const btVector3& rayFromWorld,const btVector3& rayToWorld)
279  :m_rayFromWorld(rayFromWorld),
280  m_rayToWorld(rayToWorld)
281  {
282  }
283 
285 
286  btVector3 m_rayFromWorld;//used to calculate hitPointWorld from hitFraction
288 
292 
293  virtual btScalar addSingleResult(LocalRayResult& rayResult,bool normalInWorldSpace)
294  {
297  btVector3 hitNormalWorld;
298  if (normalInWorldSpace)
299  {
300  hitNormalWorld = rayResult.m_hitNormalLocal;
301  } else
302  {
304  hitNormalWorld = m_collisionObject->getWorldTransform().getBasis()*rayResult.m_hitNormalLocal;
305  }
306  m_hitNormalWorld.push_back(hitNormalWorld);
307  btVector3 hitPointWorld;
308  hitPointWorld.setInterpolate3(m_rayFromWorld,m_rayToWorld,rayResult.m_hitFraction);
309  m_hitPointWorld.push_back(hitPointWorld);
311  return m_closestHitFraction;
312  }
313  };
314 
315 
317  {
318  LocalConvexResult(const btCollisionObject* hitCollisionObject,
319  LocalShapeInfo* localShapeInfo,
320  const btVector3& hitNormalLocal,
321  const btVector3& hitPointLocal,
322  btScalar hitFraction
323  )
324  :m_hitCollisionObject(hitCollisionObject),
325  m_localShapeInfo(localShapeInfo),
326  m_hitNormalLocal(hitNormalLocal),
327  m_hitPointLocal(hitPointLocal),
328  m_hitFraction(hitFraction)
329  {
330  }
331 
337  };
338 
341  {
345 
350  {
351  }
352 
354  {
355  }
356 
357  bool hasHit() const
358  {
359  return (m_closestHitFraction < btScalar(1.));
360  }
361 
362 
363 
364  virtual bool needsCollision(btBroadphaseProxy* proxy0) const
365  {
366  bool collides = (proxy0->m_collisionFilterGroup & m_collisionFilterMask) != 0;
367  collides = collides && (m_collisionFilterGroup & proxy0->m_collisionFilterMask);
368  return collides;
369  }
370 
371  virtual btScalar addSingleResult(LocalConvexResult& convexResult,bool normalInWorldSpace) = 0;
372  };
373 
375  {
376  ClosestConvexResultCallback(const btVector3& convexFromWorld,const btVector3& convexToWorld)
377  :m_convexFromWorld(convexFromWorld),
378  m_convexToWorld(convexToWorld),
380  {
381  }
382 
383  btVector3 m_convexFromWorld;//used to calculate hitPointWorld from hitFraction
385 
389 
390  virtual btScalar addSingleResult(LocalConvexResult& convexResult,bool normalInWorldSpace)
391  {
392 //caller already does the filter on the m_closestHitFraction
394 
395  m_closestHitFraction = convexResult.m_hitFraction;
397  if (normalInWorldSpace)
398  {
399  m_hitNormalWorld = convexResult.m_hitNormalLocal;
400  } else
401  {
404  }
405  m_hitPointWorld = convexResult.m_hitPointLocal;
406  return convexResult.m_hitFraction;
407  }
408  };
409 
412  {
415 
417  :m_collisionFilterGroup(btBroadphaseProxy::DefaultFilter),
419  {
420  }
421 
423  {
424  }
425 
426  virtual bool needsCollision(btBroadphaseProxy* proxy0) const
427  {
428  bool collides = (proxy0->m_collisionFilterGroup & m_collisionFilterMask) != 0;
429  collides = collides && (m_collisionFilterGroup & proxy0->m_collisionFilterMask);
430  return collides;
431  }
432 
433  virtual btScalar addSingleResult(btManifoldPoint& cp, const btCollisionObjectWrapper* colObj0Wrap,int partId0,int index0,const btCollisionObjectWrapper* colObj1Wrap,int partId1,int index1) = 0;
434  };
435 
436 
437 
439  {
440  return int(m_collisionObjects.size());
441  }
442 
445  virtual void rayTest(const btVector3& rayFromWorld, const btVector3& rayToWorld, RayResultCallback& resultCallback) const;
446 
449  void convexSweepTest (const btConvexShape* castShape, const btTransform& from, const btTransform& to, ConvexResultCallback& resultCallback, btScalar allowedCcdPenetration = btScalar(0.)) const;
450 
453  void contactTest(btCollisionObject* colObj, ContactResultCallback& resultCallback);
454 
457  void contactPairTest(btCollisionObject* colObjA, btCollisionObject* colObjB, ContactResultCallback& resultCallback);
458 
459 
463  static void rayTestSingle(const btTransform& rayFromTrans,const btTransform& rayToTrans,
464  btCollisionObject* collisionObject,
465  const btCollisionShape* collisionShape,
466  const btTransform& colObjWorldTransform,
467  RayResultCallback& resultCallback);
468 
469  static void rayTestSingleInternal(const btTransform& rayFromTrans,const btTransform& rayToTrans,
470  const btCollisionObjectWrapper* collisionObjectWrap,
471  RayResultCallback& resultCallback);
472 
474  static void objectQuerySingle(const btConvexShape* castShape, const btTransform& rayFromTrans,const btTransform& rayToTrans,
475  btCollisionObject* collisionObject,
476  const btCollisionShape* collisionShape,
477  const btTransform& colObjWorldTransform,
478  ConvexResultCallback& resultCallback, btScalar allowedPenetration);
479 
480  static void objectQuerySingleInternal(const btConvexShape* castShape,const btTransform& convexFromTrans,const btTransform& convexToTrans,
481  const btCollisionObjectWrapper* colObjWrap,
482  ConvexResultCallback& resultCallback, btScalar allowedPenetration);
483 
484  virtual void addCollisionObject(btCollisionObject* collisionObject,short int collisionFilterGroup=btBroadphaseProxy::DefaultFilter,short int collisionFilterMask=btBroadphaseProxy::AllFilter);
485 
487  {
488  return m_collisionObjects;
489  }
490 
492  {
493  return m_collisionObjects;
494  }
495 
496 
497  virtual void removeCollisionObject(btCollisionObject* collisionObject);
498 
499  virtual void performDiscreteCollisionDetection();
500 
502  {
503  return m_dispatchInfo;
504  }
505 
507  {
508  return m_dispatchInfo;
509  }
510 
512  {
513  return m_forceUpdateAllAabbs;
514  }
515  void setForceUpdateAllAabbs( bool forceUpdateAllAabbs)
516  {
517  m_forceUpdateAllAabbs = forceUpdateAllAabbs;
518  }
519 
521  virtual void serialize(btSerializer* serializer);
522 
523 };
524 
525 
526 #endif //BT_COLLISION_WORLD_H
void serializeCollisionObjects(btSerializer *serializer)
void push_back(const T &_Val)
LocalRayResult(const btCollisionObject *collisionObject, LocalShapeInfo *localShapeInfo, const btVector3 &hitNormalLocal, btScalar hitFraction)
virtual void rayTest(const btVector3 &rayFromWorld, const btVector3 &rayToWorld, RayResultCallback &resultCallback) const
rayTest performs a raycast on all objects in the btCollisionWorld, and calls the resultCallback This ...
virtual void updateAabbs()
btDispatcherInfo m_dispatchInfo
btCollisionWorld(btDispatcher *dispatcher, btBroadphaseInterface *broadphasePairCache, btCollisionConfiguration *collisionConfiguration)
for debug drawing
bool m_forceUpdateAllAabbs
m_forceUpdateAllAabbs can be set to false as an optimization to only update active object AABBs it is...
short int m_collisionFilterGroup
virtual void addCollisionObject(btCollisionObject *collisionObject, short int collisionFilterGroup=btBroadphaseProxy::DefaultFilter, short int collisionFilterMask=btBroadphaseProxy::AllFilter)
#define btAssert(x)
Definition: btScalar.h:101
btOverlappingPairCache * getPairCache()
ContactResultCallback is used to report contact points.
btCollisionConfiguration allows to configure Bullet collision detection stack allocator size...
const btCollisionObject * m_collisionObject
The btCollisionShape class provides an interface for collision shapes that can be shared among btColl...
btCollisionObjectArray & getCollisionObjectArray()
RayResultCallback is used to report new raycast results.
int getNumCollisionObjects() const
ManifoldContactPoint collects and maintains persistent contactpoints.
btDispatcher * m_dispatcher1
virtual btScalar addSingleResult(LocalConvexResult &convexResult, bool normalInWorldSpace)
static void rayTestSingleInternal(const btTransform &rayFromTrans, const btTransform &rayToTrans, const btCollisionObjectWrapper *collisionObjectWrap, RayResultCallback &resultCallback)
virtual void computeOverlappingPairs()
the computeOverlappingPairs is usually already called by performDiscreteCollisionDetection (or stepSi...
The btConvexShape is an abstract shape interface, implemented by all convex shapes such as btBoxShape...
Definition: btConvexShape.h:31
The btOverlappingPairCache provides an interface for overlapping pair management (add, remove, storage), used by the btBroadphaseInterface broadphases.
const btCollisionObject * m_hitCollisionObject
virtual btOverlappingPairCache * getOverlappingPairCache()=0
virtual void setDebugDrawer(btIDebugDraw *debugDrawer)
virtual btScalar addSingleResult(LocalRayResult &rayResult, bool normalInWorldSpace)=0
btTransform & getWorldTransform()
void setBroadphase(btBroadphaseInterface *pairCache)
const btDispatcher * getDispatcher() const
int size() const
return the number of elements in the array
btIDebugDraw * m_debugDrawer
const btCollisionObjectArray & getCollisionObjectArray() const
virtual btIDebugDraw * getDebugDrawer()
void contactPairTest(btCollisionObject *colObjA, btCollisionObject *colObjB, ContactResultCallback &resultCallback)
contactTest performs a discrete collision test between two collision objects and calls the resultCall...
void setForceUpdateAllAabbs(bool forceUpdateAllAabbs)
btAlignedObjectArray< btScalar > m_hitFractions
ClosestConvexResultCallback(const btVector3 &convexFromWorld, const btVector3 &convexToWorld)
virtual void debugDrawObject(const btTransform &worldTransform, const btCollisionShape *shape, const btVector3 &color)
void contactTest(btCollisionObject *colObj, ContactResultCallback &resultCallback)
contactTest performs a discrete collision test between colObj against all objects in the btCollisionW...
btCollisionObject can be used to manage collision detection objects.
btMatrix3x3 & getBasis()
Return the basis matrix for the rotation.
Definition: btTransform.h:112
static void rayTestSingle(const btTransform &rayFromTrans, const btTransform &rayToTrans, btCollisionObject *collisionObject, const btCollisionShape *collisionShape, const btTransform &colObjWorldTransform, RayResultCallback &resultCallback)
rayTestSingle performs a raycast call and calls the resultCallback.
The btIDebugDraw interface class allows hooking up a debug renderer to visually debug simulations...
Definition: btIDebugDraw.h:28
LocalShapeInfo gives extra information for complex shapes Currently, only btTriangleMeshShape is avai...
virtual void removeCollisionObject(btCollisionObject *collisionObject)
const btCollisionObject * m_collisionObject
btAlignedObjectArray< btVector3 > m_hitNormalWorld
virtual ~btCollisionWorld()
The btBroadphaseInterface class provides an interface to detect aabb-overlapping object pairs...
virtual bool needsCollision(btBroadphaseProxy *proxy0) const
btDispatcher * getDispatcher()
The btBroadphaseProxy is the main class that can be used with the Bullet broadphases.
virtual btScalar addSingleResult(btManifoldPoint &cp, const btCollisionObjectWrapper *colObj0Wrap, int partId0, int index0, const btCollisionObjectWrapper *colObj1Wrap, int partId1, int index1)=0
btVector3 can be used to represent 3D points and vectors.
Definition: btVector3.h:83
virtual btScalar addSingleResult(LocalRayResult &rayResult, bool normalInWorldSpace)
virtual bool needsCollision(btBroadphaseProxy *proxy0) const
virtual void serialize(btSerializer *serializer)
Preliminary serialization test for Bullet 2.76. Loading those files requires a separate parser (Bulle...
const btBroadphaseInterface * getBroadphase() const
static void objectQuerySingle(const btConvexShape *castShape, const btTransform &rayFromTrans, const btTransform &rayToTrans, btCollisionObject *collisionObject, const btCollisionShape *collisionShape, const btTransform &colObjWorldTransform, ConvexResultCallback &resultCallback, btScalar allowedPenetration)
objectQuerySingle performs a collision detection query and calls the resultCallback. It is used internally by rayTest.
The btTransform class supports rigid transforms with only translation and rotation and no scaling/she...
Definition: btTransform.h:34
btBroadphaseInterface * getBroadphase()
CollisionWorld is interface and container for the collision detection.
btDispatcherInfo & getDispatchInfo()
virtual bool needsCollision(btBroadphaseProxy *proxy0) const
btAlignedObjectArray< btVector3 > m_hitPointWorld
btAlignedObjectArray< btCollisionObject * > m_collisionObjects
virtual void performDiscreteCollisionDetection()
btAlignedObjectArray< const btCollisionObject * > m_collisionObjects
btBroadphaseInterface * m_broadphasePairCache
bool getForceUpdateAllAabbs() const
short int m_collisionFilterMask
const btDispatcherInfo & getDispatchInfo() const
LocalConvexResult(const btCollisionObject *hitCollisionObject, LocalShapeInfo *localShapeInfo, const btVector3 &hitNormalLocal, const btVector3 &hitPointLocal, btScalar hitFraction)
virtual btScalar addSingleResult(LocalConvexResult &convexResult, bool normalInWorldSpace)=0
void updateSingleAabb(btCollisionObject *colObj)
void convexSweepTest(const btConvexShape *castShape, const btTransform &from, const btTransform &to, ConvexResultCallback &resultCallback, btScalar allowedCcdPenetration=btScalar(0.)) const
convexTest performs a swept convex cast on all objects in the btCollisionWorld, and calls the resultC...
RayResultCallback is used to report new raycast results.
virtual void debugDrawWorld()
The btDispatcher interface class can be used in combination with broadphase to dispatch calculations ...
Definition: btDispatcher.h:69
void setInterpolate3(const btVector3 &v0, const btVector3 &v1, btScalar rt)
Definition: btVector3.h:491
virtual btScalar addSingleResult(LocalRayResult &rayResult, bool normalInWorldSpace)
static void objectQuerySingleInternal(const btConvexShape *castShape, const btTransform &convexFromTrans, const btTransform &convexToTrans, const btCollisionObjectWrapper *colObjWrap, ConvexResultCallback &resultCallback, btScalar allowedPenetration)
ClosestRayResultCallback(const btVector3 &rayFromWorld, const btVector3 &rayToWorld)
float btScalar
The btScalar type abstracts floating point numbers, to easily switch between double and single floati...
Definition: btScalar.h:266
AllHitsRayResultCallback(const btVector3 &rayFromWorld, const btVector3 &rayToWorld)