btCollisionWorld.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. /*
  2. Bullet Continuous Collision Detection and Physics Library
  3. Copyright (c) 2003-2013 Erwin Coumans http://bulletphysics.org
  4. This software is provided 'as-is', without any express or implied warranty.
  5. In no event will the authors be held liable for any damages arising from the use of this software.
  6. Permission is granted to anyone to use this software for any purpose,
  7. including commercial applications, and to alter it and redistribute it freely,
  8. subject to the following restrictions:
  9. 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.
  10. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  11. 3. This notice may not be removed or altered from any source distribution.
  12. */
  13. /**
  14. * @mainpage Bullet Documentation
  15. *
  16. * @section intro_sec Introduction
  17. * 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 ).
  18. *
  19. * The main documentation is Bullet_User_Manual.pdf, included in the source code distribution.
  20. * There is the Physics Forum for feedback and general Collision Detection and Physics discussions.
  21. * Please visit http://www.bulletphysics.org
  22. *
  23. * @section install_sec Installation
  24. *
  25. * @subsection step1 Step 1: Download
  26. * You can download the Bullet Physics Library from the github repository: https://github.com/bulletphysics/bullet3/releases
  27. *
  28. * @subsection step2 Step 2: Building
  29. * Bullet has multiple build systems, including premake, cmake and autotools. Premake and cmake support all platforms.
  30. * Premake is included in the Bullet/build folder for Windows, Mac OSX and Linux.
  31. * Under Windows you can click on Bullet/build/vs2010.bat to create Microsoft Visual Studio projects.
  32. * On Mac OSX and Linux you can open a terminal and generate Makefile, codeblocks or Xcode4 projects:
  33. * cd Bullet/build
  34. * ./premake4_osx gmake or ./premake4_linux gmake or ./premake4_linux64 gmake or (for Mac) ./premake4_osx xcode4
  35. * cd Bullet/build/gmake
  36. * make
  37. *
  38. * An alternative to premake is cmake. You can download cmake from http://www.cmake.org
  39. * cmake can autogenerate projectfiles for Microsoft Visual Studio, Apple Xcode, KDevelop and Unix Makefiles.
  40. * The easiest is to run the CMake cmake-gui graphical user interface and choose the options and generate projectfiles.
  41. * You can also use cmake in the command-line. Here are some examples for various platforms:
  42. * cmake . -G "Visual Studio 9 2008"
  43. * cmake . -G Xcode
  44. * cmake . -G "Unix Makefiles"
  45. * Although cmake is recommended, you can also use autotools for UNIX: ./autogen.sh ./configure to create a Makefile and then run make.
  46. *
  47. * @subsection step3 Step 3: Testing demos
  48. * Try to run and experiment with BasicDemo executable as a starting point.
  49. * 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.
  50. * The Dependencies can be seen in this documentation under Directories
  51. *
  52. * @subsection step4 Step 4: Integrating in your application, full Rigid Body and Soft Body simulation
  53. * Check out BasicDemo how to create a btDynamicsWorld, btRigidBody and btCollisionShape, Stepping the simulation and synchronizing your graphics object transform.
  54. * Check out SoftDemo how to use soft body dynamics, using btSoftRigidDynamicsWorld.
  55. * @subsection step5 Step 5 : Integrate the Collision Detection Library (without Dynamics and other Extras)
  56. * Bullet Collision Detection can also be used without the Dynamics/Extras.
  57. * Check out btCollisionWorld and btCollisionObject, and the CollisionInterfaceDemo.
  58. * @subsection step6 Step 6 : Use Snippets like the GJK Closest Point calculation.
  59. * Bullet has been designed in a modular way keeping dependencies to a minimum. The ConvexHullDistance demo demonstrates direct use of btGjkPairDetector.
  60. *
  61. * @section copyright Copyright
  62. * For up-to-data information and copyright and contributors list check out the Bullet_User_Manual.pdf
  63. *
  64. */
  65. #ifndef BT_COLLISION_WORLD_H
  66. #define BT_COLLISION_WORLD_H
  67. class btCollisionShape;
  68. class btConvexShape;
  69. class btBroadphaseInterface;
  70. class btSerializer;
  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. ///CollisionWorld is interface and container for the collision detection
  78. class btCollisionWorld
  79. {
  80. protected:
  81. btAlignedObjectArray<btCollisionObject*> m_collisionObjects;
  82. btDispatcher* m_dispatcher1;
  83. btDispatcherInfo m_dispatchInfo;
  84. btBroadphaseInterface* m_broadphasePairCache;
  85. btIDebugDraw* m_debugDrawer;
  86. ///m_forceUpdateAllAabbs can be set to false as an optimization to only update active object AABBs
  87. ///it is true by default, because it is error-prone (setting the position of static objects wouldn't update their AABB)
  88. bool m_forceUpdateAllAabbs;
  89. void serializeCollisionObjects(btSerializer* serializer);
  90. void serializeContactManifolds(btSerializer* serializer);
  91. public:
  92. //this constructor doesn't own the dispatcher and paircache/broadphase
  93. btCollisionWorld(btDispatcher* dispatcher, btBroadphaseInterface* broadphasePairCache, btCollisionConfiguration* collisionConfiguration);
  94. virtual ~btCollisionWorld();
  95. void setBroadphase(btBroadphaseInterface* pairCache)
  96. {
  97. m_broadphasePairCache = pairCache;
  98. }
  99. const btBroadphaseInterface* getBroadphase() const
  100. {
  101. return m_broadphasePairCache;
  102. }
  103. btBroadphaseInterface* getBroadphase()
  104. {
  105. return m_broadphasePairCache;
  106. }
  107. btOverlappingPairCache* getPairCache()
  108. {
  109. return m_broadphasePairCache->getOverlappingPairCache();
  110. }
  111. btDispatcher* getDispatcher()
  112. {
  113. return m_dispatcher1;
  114. }
  115. const btDispatcher* getDispatcher() const
  116. {
  117. return m_dispatcher1;
  118. }
  119. void updateSingleAabb(btCollisionObject* colObj);
  120. virtual void updateAabbs();
  121. ///the computeOverlappingPairs is usually already called by performDiscreteCollisionDetection (or stepSimulation)
  122. ///it can be useful to use if you perform ray tests without collision detection/simulation
  123. virtual void computeOverlappingPairs();
  124. virtual void setDebugDrawer(btIDebugDraw* debugDrawer)
  125. {
  126. m_debugDrawer = debugDrawer;
  127. }
  128. virtual btIDebugDraw* getDebugDrawer()
  129. {
  130. return m_debugDrawer;
  131. }
  132. virtual void debugDrawWorld();
  133. virtual void debugDrawObject(const btTransform& worldTransform, const btCollisionShape* shape, const btVector3& color);
  134. ///LocalShapeInfo gives extra information for complex shapes
  135. ///Currently, only btTriangleMeshShape is available, so it just contains triangleIndex and subpart
  136. struct LocalShapeInfo
  137. {
  138. int m_shapePart;
  139. int m_triangleIndex;
  140. //const btCollisionShape* m_shapeTemp;
  141. //const btTransform* m_shapeLocalTransform;
  142. };
  143. struct LocalRayResult
  144. {
  145. LocalRayResult(const btCollisionObject* collisionObject,
  146. LocalShapeInfo* localShapeInfo,
  147. const btVector3& hitNormalLocal,
  148. btScalar hitFraction)
  149. : m_collisionObject(collisionObject),
  150. m_localShapeInfo(localShapeInfo),
  151. m_hitNormalLocal(hitNormalLocal),
  152. m_hitFraction(hitFraction)
  153. {
  154. }
  155. const btCollisionObject* m_collisionObject;
  156. LocalShapeInfo* m_localShapeInfo;
  157. btVector3 m_hitNormalLocal;
  158. btScalar m_hitFraction;
  159. };
  160. ///RayResultCallback is used to report new raycast results
  161. struct RayResultCallback
  162. {
  163. btScalar m_closestHitFraction;
  164. const btCollisionObject* m_collisionObject;
  165. int m_collisionFilterGroup;
  166. int m_collisionFilterMask;
  167. //@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.
  168. unsigned int m_flags;
  169. virtual ~RayResultCallback()
  170. {
  171. }
  172. bool hasHit() const
  173. {
  174. return (m_collisionObject != 0);
  175. }
  176. RayResultCallback()
  177. : m_closestHitFraction(btScalar(1.)),
  178. m_collisionObject(0),
  179. m_collisionFilterGroup(btBroadphaseProxy::DefaultFilter),
  180. m_collisionFilterMask(btBroadphaseProxy::AllFilter),
  181. //@BP Mod
  182. m_flags(0)
  183. {
  184. }
  185. virtual bool needsCollision(btBroadphaseProxy* proxy0) const
  186. {
  187. bool collides = (proxy0->m_collisionFilterGroup & m_collisionFilterMask) != 0;
  188. collides = collides && (m_collisionFilterGroup & proxy0->m_collisionFilterMask);
  189. return collides;
  190. }
  191. virtual btScalar addSingleResult(LocalRayResult& rayResult, bool normalInWorldSpace) = 0;
  192. };
  193. struct ClosestRayResultCallback : public RayResultCallback
  194. {
  195. ClosestRayResultCallback(const btVector3& rayFromWorld, const btVector3& rayToWorld)
  196. : m_rayFromWorld(rayFromWorld),
  197. m_rayToWorld(rayToWorld)
  198. {
  199. }
  200. btVector3 m_rayFromWorld; //used to calculate hitPointWorld from hitFraction
  201. btVector3 m_rayToWorld;
  202. btVector3 m_hitNormalWorld;
  203. btVector3 m_hitPointWorld;
  204. virtual btScalar addSingleResult(LocalRayResult& rayResult, bool normalInWorldSpace)
  205. {
  206. //caller already does the filter on the m_closestHitFraction
  207. btAssert(rayResult.m_hitFraction <= m_closestHitFraction);
  208. m_closestHitFraction = rayResult.m_hitFraction;
  209. m_collisionObject = rayResult.m_collisionObject;
  210. if (normalInWorldSpace)
  211. {
  212. m_hitNormalWorld = rayResult.m_hitNormalLocal;
  213. }
  214. else
  215. {
  216. ///need to transform normal into worldspace
  217. m_hitNormalWorld = m_collisionObject->getWorldTransform().getBasis() * rayResult.m_hitNormalLocal;
  218. }
  219. m_hitPointWorld.setInterpolate3(m_rayFromWorld, m_rayToWorld, rayResult.m_hitFraction);
  220. return rayResult.m_hitFraction;
  221. }
  222. };
  223. struct AllHitsRayResultCallback : public RayResultCallback
  224. {
  225. AllHitsRayResultCallback(const btVector3& rayFromWorld, const btVector3& rayToWorld)
  226. : m_rayFromWorld(rayFromWorld),
  227. m_rayToWorld(rayToWorld)
  228. {
  229. }
  230. btAlignedObjectArray<const btCollisionObject*> m_collisionObjects;
  231. btVector3 m_rayFromWorld; //used to calculate hitPointWorld from hitFraction
  232. btVector3 m_rayToWorld;
  233. btAlignedObjectArray<btVector3> m_hitNormalWorld;
  234. btAlignedObjectArray<btVector3> m_hitPointWorld;
  235. btAlignedObjectArray<btScalar> m_hitFractions;
  236. virtual btScalar addSingleResult(LocalRayResult& rayResult, bool normalInWorldSpace)
  237. {
  238. m_collisionObject = rayResult.m_collisionObject;
  239. m_collisionObjects.push_back(rayResult.m_collisionObject);
  240. btVector3 hitNormalWorld;
  241. if (normalInWorldSpace)
  242. {
  243. hitNormalWorld = rayResult.m_hitNormalLocal;
  244. }
  245. else
  246. {
  247. ///need to transform normal into worldspace
  248. hitNormalWorld = m_collisionObject->getWorldTransform().getBasis() * rayResult.m_hitNormalLocal;
  249. }
  250. m_hitNormalWorld.push_back(hitNormalWorld);
  251. btVector3 hitPointWorld;
  252. hitPointWorld.setInterpolate3(m_rayFromWorld, m_rayToWorld, rayResult.m_hitFraction);
  253. m_hitPointWorld.push_back(hitPointWorld);
  254. m_hitFractions.push_back(rayResult.m_hitFraction);
  255. return m_closestHitFraction;
  256. }
  257. };
  258. struct LocalConvexResult
  259. {
  260. LocalConvexResult(const btCollisionObject* hitCollisionObject,
  261. LocalShapeInfo* localShapeInfo,
  262. const btVector3& hitNormalLocal,
  263. const btVector3& hitPointLocal,
  264. btScalar hitFraction)
  265. : m_hitCollisionObject(hitCollisionObject),
  266. m_localShapeInfo(localShapeInfo),
  267. m_hitNormalLocal(hitNormalLocal),
  268. m_hitPointLocal(hitPointLocal),
  269. m_hitFraction(hitFraction)
  270. {
  271. }
  272. const btCollisionObject* m_hitCollisionObject;
  273. LocalShapeInfo* m_localShapeInfo;
  274. btVector3 m_hitNormalLocal;
  275. btVector3 m_hitPointLocal;
  276. btScalar m_hitFraction;
  277. };
  278. ///RayResultCallback is used to report new raycast results
  279. struct ConvexResultCallback
  280. {
  281. btScalar m_closestHitFraction;
  282. int m_collisionFilterGroup;
  283. int m_collisionFilterMask;
  284. ConvexResultCallback()
  285. : m_closestHitFraction(btScalar(1.)),
  286. m_collisionFilterGroup(btBroadphaseProxy::DefaultFilter),
  287. m_collisionFilterMask(btBroadphaseProxy::AllFilter)
  288. {
  289. }
  290. virtual ~ConvexResultCallback()
  291. {
  292. }
  293. bool hasHit() const
  294. {
  295. return (m_closestHitFraction < btScalar(1.));
  296. }
  297. virtual bool needsCollision(btBroadphaseProxy* proxy0) const
  298. {
  299. bool collides = (proxy0->m_collisionFilterGroup & m_collisionFilterMask) != 0;
  300. collides = collides && (m_collisionFilterGroup & proxy0->m_collisionFilterMask);
  301. return collides;
  302. }
  303. virtual btScalar addSingleResult(LocalConvexResult& convexResult, bool normalInWorldSpace) = 0;
  304. };
  305. struct ClosestConvexResultCallback : public ConvexResultCallback
  306. {
  307. ClosestConvexResultCallback(const btVector3& convexFromWorld, const btVector3& convexToWorld)
  308. : m_convexFromWorld(convexFromWorld),
  309. m_convexToWorld(convexToWorld),
  310. m_hitCollisionObject(0)
  311. {
  312. }
  313. btVector3 m_convexFromWorld; //used to calculate hitPointWorld from hitFraction
  314. btVector3 m_convexToWorld;
  315. btVector3 m_hitNormalWorld;
  316. btVector3 m_hitPointWorld;
  317. const btCollisionObject* m_hitCollisionObject;
  318. virtual btScalar addSingleResult(LocalConvexResult& convexResult, bool normalInWorldSpace)
  319. {
  320. //caller already does the filter on the m_closestHitFraction
  321. btAssert(convexResult.m_hitFraction <= m_closestHitFraction);
  322. m_closestHitFraction = convexResult.m_hitFraction;
  323. m_hitCollisionObject = convexResult.m_hitCollisionObject;
  324. if (normalInWorldSpace)
  325. {
  326. m_hitNormalWorld = convexResult.m_hitNormalLocal;
  327. }
  328. else
  329. {
  330. ///need to transform normal into worldspace
  331. m_hitNormalWorld = m_hitCollisionObject->getWorldTransform().getBasis() * convexResult.m_hitNormalLocal;
  332. }
  333. m_hitPointWorld = convexResult.m_hitPointLocal;
  334. return convexResult.m_hitFraction;
  335. }
  336. };
  337. ///ContactResultCallback is used to report contact points
  338. struct ContactResultCallback
  339. {
  340. int m_collisionFilterGroup;
  341. int m_collisionFilterMask;
  342. btScalar m_closestDistanceThreshold;
  343. ContactResultCallback()
  344. : m_collisionFilterGroup(btBroadphaseProxy::DefaultFilter),
  345. m_collisionFilterMask(btBroadphaseProxy::AllFilter),
  346. m_closestDistanceThreshold(0)
  347. {
  348. }
  349. virtual ~ContactResultCallback()
  350. {
  351. }
  352. virtual bool needsCollision(btBroadphaseProxy* proxy0) const
  353. {
  354. bool collides = (proxy0->m_collisionFilterGroup & m_collisionFilterMask) != 0;
  355. collides = collides && (m_collisionFilterGroup & proxy0->m_collisionFilterMask);
  356. return collides;
  357. }
  358. virtual btScalar addSingleResult(btManifoldPoint& cp, const btCollisionObjectWrapper* colObj0Wrap, int partId0, int index0, const btCollisionObjectWrapper* colObj1Wrap, int partId1, int index1) = 0;
  359. };
  360. int getNumCollisionObjects() const
  361. {
  362. return int(m_collisionObjects.size());
  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. virtual void rayTest(const btVector3& rayFromWorld, const btVector3& rayToWorld, RayResultCallback& resultCallback) const;
  367. /// convexTest performs a swept convex cast on all objects in the btCollisionWorld, and calls the resultCallback
  368. /// This allows for several queries: first hit, all hits, any hit, dependent on the value return by the callback.
  369. void convexSweepTest(const btConvexShape* castShape, const btTransform& from, const btTransform& to, ConvexResultCallback& resultCallback, btScalar allowedCcdPenetration = btScalar(0.)) const;
  370. ///contactTest performs a discrete collision test between colObj against all objects in the btCollisionWorld, and calls the resultCallback.
  371. ///it reports one or more contact points for every overlapping object (including the one with deepest penetration)
  372. void contactTest(btCollisionObject* colObj, ContactResultCallback& resultCallback);
  373. ///contactTest performs a discrete collision test between two collision objects and calls the resultCallback if overlap if detected.
  374. ///it reports one or more contact points (including the one with deepest penetration)
  375. void contactPairTest(btCollisionObject* colObjA, btCollisionObject* colObjB, ContactResultCallback& resultCallback);
  376. /// rayTestSingle performs a raycast call and calls the resultCallback. It is used internally by rayTest.
  377. /// In a future implementation, we consider moving the ray test as a virtual method in btCollisionShape.
  378. /// This allows more customization.
  379. static void rayTestSingle(const btTransform& rayFromTrans, const btTransform& rayToTrans,
  380. btCollisionObject* collisionObject,
  381. const btCollisionShape* collisionShape,
  382. const btTransform& colObjWorldTransform,
  383. RayResultCallback& resultCallback);
  384. static void rayTestSingleInternal(const btTransform& rayFromTrans, const btTransform& rayToTrans,
  385. const btCollisionObjectWrapper* collisionObjectWrap,
  386. RayResultCallback& resultCallback);
  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. static void objectQuerySingleInternal(const btConvexShape* castShape, const btTransform& convexFromTrans, const btTransform& convexToTrans,
  394. const btCollisionObjectWrapper* colObjWrap,
  395. ConvexResultCallback& resultCallback, btScalar allowedPenetration);
  396. virtual void addCollisionObject(btCollisionObject* collisionObject, int collisionFilterGroup = btBroadphaseProxy::DefaultFilter, int collisionFilterMask = btBroadphaseProxy::AllFilter);
  397. virtual void refreshBroadphaseProxy(btCollisionObject* collisionObject);
  398. btCollisionObjectArray& getCollisionObjectArray()
  399. {
  400. return m_collisionObjects;
  401. }
  402. const btCollisionObjectArray& getCollisionObjectArray() const
  403. {
  404. return m_collisionObjects;
  405. }
  406. virtual void removeCollisionObject(btCollisionObject* collisionObject);
  407. virtual void performDiscreteCollisionDetection();
  408. btDispatcherInfo& getDispatchInfo()
  409. {
  410. return m_dispatchInfo;
  411. }
  412. const btDispatcherInfo& getDispatchInfo() const
  413. {
  414. return m_dispatchInfo;
  415. }
  416. bool getForceUpdateAllAabbs() const
  417. {
  418. return m_forceUpdateAllAabbs;
  419. }
  420. void setForceUpdateAllAabbs(bool forceUpdateAllAabbs)
  421. {
  422. m_forceUpdateAllAabbs = forceUpdateAllAabbs;
  423. }
  424. ///Preliminary serialization test for Bullet 2.76. Loading those files requires a separate parser (Bullet/Demos/SerializeDemo)
  425. virtual void serialize(btSerializer* serializer);
  426. };
  427. #endif //BT_COLLISION_WORLD_H