btCompoundShape.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. Bullet Continuous Collision Detection and Physics Library
  3. Copyright (c) 2003-2009 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. #ifndef BT_COMPOUND_SHAPE_H
  14. #define BT_COMPOUND_SHAPE_H
  15. #include "btCollisionShape.h"
  16. #include "bullet/LinearMath/btVector3.h"
  17. #include "bullet/LinearMath/btTransform.h"
  18. #include "bullet/LinearMath/btMatrix3x3.h"
  19. #include "btCollisionMargin.h"
  20. #include "bullet/LinearMath/btAlignedObjectArray.h"
  21. //class btOptimizedBvh;
  22. struct btDbvt;
  23. ATTRIBUTE_ALIGNED16(struct) btCompoundShapeChild
  24. {
  25. BT_DECLARE_ALIGNED_ALLOCATOR();
  26. btTransform m_transform;
  27. btCollisionShape* m_childShape;
  28. int m_childShapeType;
  29. btScalar m_childMargin;
  30. struct btDbvtNode* m_node;
  31. };
  32. SIMD_FORCE_INLINE bool operator==(const btCompoundShapeChild& c1, const btCompoundShapeChild& c2)
  33. {
  34. return ( c1.m_transform == c2.m_transform &&
  35. c1.m_childShape == c2.m_childShape &&
  36. c1.m_childShapeType == c2.m_childShapeType &&
  37. c1.m_childMargin == c2.m_childMargin );
  38. }
  39. /// The btCompoundShape allows to store multiple other btCollisionShapes
  40. /// This allows for moving concave collision objects. This is more general then the static concave btBvhTriangleMeshShape.
  41. /// It has an (optional) dynamic aabb tree to accelerate early rejection tests.
  42. /// @todo: This aabb tree can also be use to speed up ray tests on btCompoundShape, see http://code.google.com/p/bullet/issues/detail?id=25
  43. /// Currently, removal of child shapes is only supported when disabling the aabb tree (pass 'false' in the constructor of btCompoundShape)
  44. ATTRIBUTE_ALIGNED16(class) btCompoundShape : public btCollisionShape
  45. {
  46. btAlignedObjectArray<btCompoundShapeChild> m_children;
  47. btVector3 m_localAabbMin;
  48. btVector3 m_localAabbMax;
  49. btDbvt* m_dynamicAabbTree;
  50. ///increment m_updateRevision when adding/removing/replacing child shapes, so that some caches can be updated
  51. int m_updateRevision;
  52. btScalar m_collisionMargin;
  53. protected:
  54. btVector3 m_localScaling;
  55. public:
  56. BT_DECLARE_ALIGNED_ALLOCATOR();
  57. btCompoundShape(bool enableDynamicAabbTree = true);
  58. virtual ~btCompoundShape();
  59. void addChildShape(const btTransform& localTransform,btCollisionShape* shape);
  60. /// Remove all children shapes that contain the specified shape
  61. virtual void removeChildShape(btCollisionShape* shape);
  62. void removeChildShapeByIndex(int childShapeindex);
  63. int getNumChildShapes() const
  64. {
  65. return int (m_children.size());
  66. }
  67. btCollisionShape* getChildShape(int index)
  68. {
  69. return m_children[index].m_childShape;
  70. }
  71. const btCollisionShape* getChildShape(int index) const
  72. {
  73. return m_children[index].m_childShape;
  74. }
  75. btTransform& getChildTransform(int index)
  76. {
  77. return m_children[index].m_transform;
  78. }
  79. const btTransform& getChildTransform(int index) const
  80. {
  81. return m_children[index].m_transform;
  82. }
  83. ///set a new transform for a child, and update internal data structures (local aabb and dynamic tree)
  84. void updateChildTransform(int childIndex, const btTransform& newChildTransform, bool shouldRecalculateLocalAabb = true);
  85. btCompoundShapeChild* getChildList()
  86. {
  87. return &m_children[0];
  88. }
  89. ///getAabb's default implementation is brute force, expected derived classes to implement a fast dedicated version
  90. virtual void getAabb(const btTransform& t,btVector3& aabbMin,btVector3& aabbMax) const;
  91. /** Re-calculate the local Aabb. Is called at the end of removeChildShapes.
  92. Use this yourself if you modify the children or their transforms. */
  93. virtual void recalculateLocalAabb();
  94. virtual void setLocalScaling(const btVector3& scaling);
  95. virtual const btVector3& getLocalScaling() const
  96. {
  97. return m_localScaling;
  98. }
  99. virtual void calculateLocalInertia(btScalar mass,btVector3& inertia) const;
  100. virtual void setMargin(btScalar margin)
  101. {
  102. m_collisionMargin = margin;
  103. }
  104. virtual btScalar getMargin() const
  105. {
  106. return m_collisionMargin;
  107. }
  108. virtual const char* getName()const
  109. {
  110. return "Compound";
  111. }
  112. const btDbvt* getDynamicAabbTree() const
  113. {
  114. return m_dynamicAabbTree;
  115. }
  116. btDbvt* getDynamicAabbTree()
  117. {
  118. return m_dynamicAabbTree;
  119. }
  120. void createAabbTreeFromChildren();
  121. ///computes the exact moment of inertia and the transform from the coordinate system defined by the principal axes of the moment of inertia
  122. ///and the center of mass to the current coordinate system. "masses" points to an array of masses of the children. The resulting transform
  123. ///"principal" has to be applied inversely to all children transforms in order for the local coordinate system of the compound
  124. ///shape to be centered at the center of mass and to coincide with the principal axes. This also necessitates a correction of the world transform
  125. ///of the collision object by the principal transform.
  126. void calculatePrincipalAxisTransform(btScalar* masses, btTransform& principal, btVector3& inertia) const;
  127. int getUpdateRevision() const
  128. {
  129. return m_updateRevision;
  130. }
  131. virtual int calculateSerializeBufferSize() const;
  132. ///fills the dataBuffer and returns the struct name (and 0 on failure)
  133. virtual const char* serialize(void* dataBuffer, btSerializer* serializer) const;
  134. };
  135. ///do not change those serialization structures, it requires an updated sBulletDNAstr/sBulletDNAstr64
  136. struct btCompoundShapeChildData
  137. {
  138. btTransformFloatData m_transform;
  139. btCollisionShapeData *m_childShape;
  140. int m_childShapeType;
  141. float m_childMargin;
  142. };
  143. ///do not change those serialization structures, it requires an updated sBulletDNAstr/sBulletDNAstr64
  144. struct btCompoundShapeData
  145. {
  146. btCollisionShapeData m_collisionShapeData;
  147. btCompoundShapeChildData *m_childShapePtr;
  148. int m_numChildShapes;
  149. float m_collisionMargin;
  150. };
  151. SIMD_FORCE_INLINE int btCompoundShape::calculateSerializeBufferSize() const
  152. {
  153. return sizeof(btCompoundShapeData);
  154. }
  155. #endif //BT_COMPOUND_SHAPE_H