2
0

btCompoundShape.h 6.5 KB

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