btCollisionObject.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  1. /*
  2. Bullet Continuous Collision Detection and Physics Library
  3. Copyright (c) 2003-2006 Erwin Coumans http://continuousphysics.com/Bullet/
  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. #ifndef BT_COLLISION_OBJECT_H
  14. #define BT_COLLISION_OBJECT_H
  15. #include "bullet/LinearMath/btTransform.h"
  16. //island management, m_activationState1
  17. #define ACTIVE_TAG 1
  18. #define ISLAND_SLEEPING 2
  19. #define WANTS_DEACTIVATION 3
  20. #define DISABLE_DEACTIVATION 4
  21. #define DISABLE_SIMULATION 5
  22. struct btBroadphaseProxy;
  23. class btCollisionShape;
  24. struct btCollisionShapeData;
  25. #include "bullet/LinearMath/btMotionState.h"
  26. #include "bullet/LinearMath/btAlignedAllocator.h"
  27. #include "bullet/LinearMath/btAlignedObjectArray.h"
  28. typedef btAlignedObjectArray<class btCollisionObject*> btCollisionObjectArray;
  29. #ifdef BT_USE_DOUBLE_PRECISION
  30. #define btCollisionObjectData btCollisionObjectDoubleData
  31. #define btCollisionObjectDataName "btCollisionObjectDoubleData"
  32. #else
  33. #define btCollisionObjectData btCollisionObjectFloatData
  34. #define btCollisionObjectDataName "btCollisionObjectFloatData"
  35. #endif
  36. /// btCollisionObject can be used to manage collision detection objects.
  37. /// btCollisionObject maintains all information that is needed for a collision detection: Shape, Transform and AABB proxy.
  38. /// They can be added to the btCollisionWorld.
  39. ATTRIBUTE_ALIGNED16(class) btCollisionObject
  40. {
  41. protected:
  42. btTransform m_worldTransform;
  43. ///m_interpolationWorldTransform is used for CCD and interpolation
  44. ///it can be either previous or future (predicted) transform
  45. btTransform m_interpolationWorldTransform;
  46. //those two are experimental: just added for bullet time effect, so you can still apply impulses (directly modifying velocities)
  47. //without destroying the continuous interpolated motion (which uses this interpolation velocities)
  48. btVector3 m_interpolationLinearVelocity;
  49. btVector3 m_interpolationAngularVelocity;
  50. btVector3 m_anisotropicFriction;
  51. int m_hasAnisotropicFriction;
  52. btScalar m_contactProcessingThreshold;
  53. btBroadphaseProxy* m_broadphaseHandle;
  54. btCollisionShape* m_collisionShape;
  55. ///m_extensionPointer is used by some internal low-level Bullet extensions.
  56. void* m_extensionPointer;
  57. ///m_rootCollisionShape is temporarily used to store the original collision shape
  58. ///The m_collisionShape might be temporarily replaced by a child collision shape during collision detection purposes
  59. ///If it is NULL, the m_collisionShape is not temporarily replaced.
  60. btCollisionShape* m_rootCollisionShape;
  61. int m_collisionFlags;
  62. int m_islandTag1;
  63. int m_companionId;
  64. mutable int m_activationState1;
  65. mutable btScalar m_deactivationTime;
  66. btScalar m_friction;
  67. btScalar m_restitution;
  68. btScalar m_rollingFriction;
  69. ///m_internalType is reserved to distinguish Bullet's btCollisionObject, btRigidBody, btSoftBody, btGhostObject etc.
  70. ///do not assign your own m_internalType unless you write a new dynamics object class.
  71. int m_internalType;
  72. ///users can point to their objects, m_userPointer is not used by Bullet, see setUserPointer/getUserPointer
  73. union
  74. {
  75. void* m_userObjectPointer;
  76. int m_userIndex;
  77. };
  78. ///time of impact calculation
  79. btScalar m_hitFraction;
  80. ///Swept sphere radius (0.0 by default), see btConvexConvexAlgorithm::
  81. btScalar m_ccdSweptSphereRadius;
  82. /// Don't do continuous collision detection if the motion (in one step) is less then m_ccdMotionThreshold
  83. btScalar m_ccdMotionThreshold;
  84. /// If some object should have elaborate collision filtering by sub-classes
  85. int m_checkCollideWith;
  86. ///internal update revision number. It will be increased when the object changes. This allows some subsystems to perform lazy evaluation.
  87. int m_updateRevision;
  88. virtual bool checkCollideWithOverride(const btCollisionObject* /* co */) const
  89. {
  90. return true;
  91. }
  92. public:
  93. BT_DECLARE_ALIGNED_ALLOCATOR();
  94. enum CollisionFlags
  95. {
  96. CF_STATIC_OBJECT= 1,
  97. CF_KINEMATIC_OBJECT= 2,
  98. CF_NO_CONTACT_RESPONSE = 4,
  99. CF_CUSTOM_MATERIAL_CALLBACK = 8,//this allows per-triangle material (friction/restitution)
  100. CF_CHARACTER_OBJECT = 16,
  101. CF_DISABLE_VISUALIZE_OBJECT = 32, //disable debug drawing
  102. CF_DISABLE_SPU_COLLISION_PROCESSING = 64//disable parallel/SPU processing
  103. };
  104. enum CollisionObjectTypes
  105. {
  106. CO_COLLISION_OBJECT =1,
  107. CO_RIGID_BODY=2,
  108. ///CO_GHOST_OBJECT keeps track of all objects overlapping its AABB and that pass its collision filter
  109. ///It is useful for collision sensors, explosion objects, character controller etc.
  110. CO_GHOST_OBJECT=4,
  111. CO_SOFT_BODY=8,
  112. CO_HF_FLUID=16,
  113. CO_USER_TYPE=32,
  114. CO_FEATHERSTONE_LINK=64
  115. };
  116. enum AnisotropicFrictionFlags
  117. {
  118. CF_ANISOTROPIC_FRICTION_DISABLED=0,
  119. CF_ANISOTROPIC_FRICTION = 1,
  120. CF_ANISOTROPIC_ROLLING_FRICTION = 2
  121. };
  122. SIMD_FORCE_INLINE bool mergesSimulationIslands() const
  123. {
  124. ///static objects, kinematic and object without contact response don't merge islands
  125. return ((m_collisionFlags & (CF_STATIC_OBJECT | CF_KINEMATIC_OBJECT | CF_NO_CONTACT_RESPONSE) )==0);
  126. }
  127. const btVector3& getAnisotropicFriction() const
  128. {
  129. return m_anisotropicFriction;
  130. }
  131. void setAnisotropicFriction(const btVector3& anisotropicFriction, int frictionMode = CF_ANISOTROPIC_FRICTION)
  132. {
  133. m_anisotropicFriction = anisotropicFriction;
  134. bool isUnity = (anisotropicFriction[0]!=1.f) || (anisotropicFriction[1]!=1.f) || (anisotropicFriction[2]!=1.f);
  135. m_hasAnisotropicFriction = isUnity?frictionMode : 0;
  136. }
  137. bool hasAnisotropicFriction(int frictionMode = CF_ANISOTROPIC_FRICTION) const
  138. {
  139. return (m_hasAnisotropicFriction&frictionMode)!=0;
  140. }
  141. ///the constraint solver can discard solving contacts, if the distance is above this threshold. 0 by default.
  142. ///Note that using contacts with positive distance can improve stability. It increases, however, the chance of colliding with degerate contacts, such as 'interior' triangle edges
  143. void setContactProcessingThreshold( btScalar contactProcessingThreshold)
  144. {
  145. m_contactProcessingThreshold = contactProcessingThreshold;
  146. }
  147. btScalar getContactProcessingThreshold() const
  148. {
  149. return m_contactProcessingThreshold;
  150. }
  151. SIMD_FORCE_INLINE bool isStaticObject() const {
  152. return (m_collisionFlags & CF_STATIC_OBJECT) != 0;
  153. }
  154. SIMD_FORCE_INLINE bool isKinematicObject() const
  155. {
  156. return (m_collisionFlags & CF_KINEMATIC_OBJECT) != 0;
  157. }
  158. SIMD_FORCE_INLINE bool isStaticOrKinematicObject() const
  159. {
  160. return (m_collisionFlags & (CF_KINEMATIC_OBJECT | CF_STATIC_OBJECT)) != 0 ;
  161. }
  162. SIMD_FORCE_INLINE bool hasContactResponse() const {
  163. return (m_collisionFlags & CF_NO_CONTACT_RESPONSE)==0;
  164. }
  165. btCollisionObject();
  166. virtual ~btCollisionObject();
  167. virtual void setCollisionShape(btCollisionShape* collisionShape)
  168. {
  169. m_updateRevision++;
  170. m_collisionShape = collisionShape;
  171. m_rootCollisionShape = collisionShape;
  172. }
  173. SIMD_FORCE_INLINE const btCollisionShape* getCollisionShape() const
  174. {
  175. return m_collisionShape;
  176. }
  177. SIMD_FORCE_INLINE btCollisionShape* getCollisionShape()
  178. {
  179. return m_collisionShape;
  180. }
  181. ///Avoid using this internal API call, the extension pointer is used by some Bullet extensions.
  182. ///If you need to store your own user pointer, use 'setUserPointer/getUserPointer' instead.
  183. void* internalGetExtensionPointer() const
  184. {
  185. return m_extensionPointer;
  186. }
  187. ///Avoid using this internal API call, the extension pointer is used by some Bullet extensions
  188. ///If you need to store your own user pointer, use 'setUserPointer/getUserPointer' instead.
  189. void internalSetExtensionPointer(void* pointer)
  190. {
  191. m_extensionPointer = pointer;
  192. }
  193. SIMD_FORCE_INLINE int getActivationState() const { return m_activationState1;}
  194. void setActivationState(int newState) const;
  195. void setDeactivationTime(btScalar time)
  196. {
  197. m_deactivationTime = time;
  198. }
  199. btScalar getDeactivationTime() const
  200. {
  201. return m_deactivationTime;
  202. }
  203. void forceActivationState(int newState) const;
  204. void activate(bool forceActivation = false) const;
  205. SIMD_FORCE_INLINE bool isActive() const
  206. {
  207. return ((getActivationState() != ISLAND_SLEEPING) && (getActivationState() != DISABLE_SIMULATION));
  208. }
  209. void setRestitution(btScalar rest)
  210. {
  211. m_updateRevision++;
  212. m_restitution = rest;
  213. }
  214. btScalar getRestitution() const
  215. {
  216. return m_restitution;
  217. }
  218. void setFriction(btScalar frict)
  219. {
  220. m_updateRevision++;
  221. m_friction = frict;
  222. }
  223. btScalar getFriction() const
  224. {
  225. return m_friction;
  226. }
  227. void setRollingFriction(btScalar frict)
  228. {
  229. m_updateRevision++;
  230. m_rollingFriction = frict;
  231. }
  232. btScalar getRollingFriction() const
  233. {
  234. return m_rollingFriction;
  235. }
  236. ///reserved for Bullet internal usage
  237. int getInternalType() const
  238. {
  239. return m_internalType;
  240. }
  241. btTransform& getWorldTransform()
  242. {
  243. return m_worldTransform;
  244. }
  245. const btTransform& getWorldTransform() const
  246. {
  247. return m_worldTransform;
  248. }
  249. void setWorldTransform(const btTransform& worldTrans)
  250. {
  251. m_updateRevision++;
  252. m_worldTransform = worldTrans;
  253. }
  254. SIMD_FORCE_INLINE btBroadphaseProxy* getBroadphaseHandle()
  255. {
  256. return m_broadphaseHandle;
  257. }
  258. SIMD_FORCE_INLINE const btBroadphaseProxy* getBroadphaseHandle() const
  259. {
  260. return m_broadphaseHandle;
  261. }
  262. void setBroadphaseHandle(btBroadphaseProxy* handle)
  263. {
  264. m_broadphaseHandle = handle;
  265. }
  266. const btTransform& getInterpolationWorldTransform() const
  267. {
  268. return m_interpolationWorldTransform;
  269. }
  270. btTransform& getInterpolationWorldTransform()
  271. {
  272. return m_interpolationWorldTransform;
  273. }
  274. void setInterpolationWorldTransform(const btTransform& trans)
  275. {
  276. m_updateRevision++;
  277. m_interpolationWorldTransform = trans;
  278. }
  279. void setInterpolationLinearVelocity(const btVector3& linvel)
  280. {
  281. m_updateRevision++;
  282. m_interpolationLinearVelocity = linvel;
  283. }
  284. void setInterpolationAngularVelocity(const btVector3& angvel)
  285. {
  286. m_updateRevision++;
  287. m_interpolationAngularVelocity = angvel;
  288. }
  289. const btVector3& getInterpolationLinearVelocity() const
  290. {
  291. return m_interpolationLinearVelocity;
  292. }
  293. const btVector3& getInterpolationAngularVelocity() const
  294. {
  295. return m_interpolationAngularVelocity;
  296. }
  297. SIMD_FORCE_INLINE int getIslandTag() const
  298. {
  299. return m_islandTag1;
  300. }
  301. void setIslandTag(int tag)
  302. {
  303. m_islandTag1 = tag;
  304. }
  305. SIMD_FORCE_INLINE int getCompanionId() const
  306. {
  307. return m_companionId;
  308. }
  309. void setCompanionId(int id)
  310. {
  311. m_companionId = id;
  312. }
  313. SIMD_FORCE_INLINE btScalar getHitFraction() const
  314. {
  315. return m_hitFraction;
  316. }
  317. void setHitFraction(btScalar hitFraction)
  318. {
  319. m_hitFraction = hitFraction;
  320. }
  321. SIMD_FORCE_INLINE int getCollisionFlags() const
  322. {
  323. return m_collisionFlags;
  324. }
  325. void setCollisionFlags(int flags)
  326. {
  327. m_collisionFlags = flags;
  328. }
  329. ///Swept sphere radius (0.0 by default), see btConvexConvexAlgorithm::
  330. btScalar getCcdSweptSphereRadius() const
  331. {
  332. return m_ccdSweptSphereRadius;
  333. }
  334. ///Swept sphere radius (0.0 by default), see btConvexConvexAlgorithm::
  335. void setCcdSweptSphereRadius(btScalar radius)
  336. {
  337. m_ccdSweptSphereRadius = radius;
  338. }
  339. btScalar getCcdMotionThreshold() const
  340. {
  341. return m_ccdMotionThreshold;
  342. }
  343. btScalar getCcdSquareMotionThreshold() const
  344. {
  345. return m_ccdMotionThreshold*m_ccdMotionThreshold;
  346. }
  347. /// Don't do continuous collision detection if the motion (in one step) is less then m_ccdMotionThreshold
  348. void setCcdMotionThreshold(btScalar ccdMotionThreshold)
  349. {
  350. m_ccdMotionThreshold = ccdMotionThreshold;
  351. }
  352. ///users can point to their objects, userPointer is not used by Bullet
  353. void* getUserPointer() const
  354. {
  355. return m_userObjectPointer;
  356. }
  357. int getUserIndex() const
  358. {
  359. return m_userIndex;
  360. }
  361. ///users can point to their objects, userPointer is not used by Bullet
  362. void setUserPointer(void* userPointer)
  363. {
  364. m_userObjectPointer = userPointer;
  365. }
  366. ///users can point to their objects, userPointer is not used by Bullet
  367. void setUserIndex(int index)
  368. {
  369. m_userIndex = index;
  370. }
  371. int getUpdateRevisionInternal() const
  372. {
  373. return m_updateRevision;
  374. }
  375. inline bool checkCollideWith(const btCollisionObject* co) const
  376. {
  377. if (m_checkCollideWith)
  378. return checkCollideWithOverride(co);
  379. return true;
  380. }
  381. virtual int calculateSerializeBufferSize() const;
  382. ///fills the dataBuffer and returns the struct name (and 0 on failure)
  383. virtual const char* serialize(void* dataBuffer, class btSerializer* serializer) const;
  384. virtual void serializeSingleObject(class btSerializer* serializer) const;
  385. };
  386. ///do not change those serialization structures, it requires an updated sBulletDNAstr/sBulletDNAstr64
  387. struct btCollisionObjectDoubleData
  388. {
  389. void *m_broadphaseHandle;
  390. void *m_collisionShape;
  391. btCollisionShapeData *m_rootCollisionShape;
  392. char *m_name;
  393. btTransformDoubleData m_worldTransform;
  394. btTransformDoubleData m_interpolationWorldTransform;
  395. btVector3DoubleData m_interpolationLinearVelocity;
  396. btVector3DoubleData m_interpolationAngularVelocity;
  397. btVector3DoubleData m_anisotropicFriction;
  398. double m_contactProcessingThreshold;
  399. double m_deactivationTime;
  400. double m_friction;
  401. double m_rollingFriction;
  402. double m_restitution;
  403. double m_hitFraction;
  404. double m_ccdSweptSphereRadius;
  405. double m_ccdMotionThreshold;
  406. int m_hasAnisotropicFriction;
  407. int m_collisionFlags;
  408. int m_islandTag1;
  409. int m_companionId;
  410. int m_activationState1;
  411. int m_internalType;
  412. int m_checkCollideWith;
  413. char m_padding[4];
  414. };
  415. ///do not change those serialization structures, it requires an updated sBulletDNAstr/sBulletDNAstr64
  416. struct btCollisionObjectFloatData
  417. {
  418. void *m_broadphaseHandle;
  419. void *m_collisionShape;
  420. btCollisionShapeData *m_rootCollisionShape;
  421. char *m_name;
  422. btTransformFloatData m_worldTransform;
  423. btTransformFloatData m_interpolationWorldTransform;
  424. btVector3FloatData m_interpolationLinearVelocity;
  425. btVector3FloatData m_interpolationAngularVelocity;
  426. btVector3FloatData m_anisotropicFriction;
  427. float m_contactProcessingThreshold;
  428. float m_deactivationTime;
  429. float m_friction;
  430. float m_rollingFriction;
  431. float m_restitution;
  432. float m_hitFraction;
  433. float m_ccdSweptSphereRadius;
  434. float m_ccdMotionThreshold;
  435. int m_hasAnisotropicFriction;
  436. int m_collisionFlags;
  437. int m_islandTag1;
  438. int m_companionId;
  439. int m_activationState1;
  440. int m_internalType;
  441. int m_checkCollideWith;
  442. char m_padding[4];
  443. };
  444. SIMD_FORCE_INLINE int btCollisionObject::calculateSerializeBufferSize() const
  445. {
  446. return sizeof(btCollisionObjectData);
  447. }
  448. #endif //BT_COLLISION_OBJECT_H