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