1 /* 2 Bullet Continuous Collision Detection and Physics Library 3 Copyright (c) 2013 Erwin Coumans http://bulletphysics.org 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 BT_MULTIBODY_CONSTRAINT_H 17 #define BT_MULTIBODY_CONSTRAINT_H 18 19 #include "LinearMath/btScalar.h" 20 #include "LinearMath/btAlignedObjectArray.h" 21 #include "btMultiBody.h" 22 23 class btMultiBody; 24 struct btSolverInfo; 25 26 #include "btMultiBodySolverConstraint.h" 27 28 struct btMultiBodyJacobianData 29 { 30 btAlignedObjectArray<btScalar> m_jacobians; 31 btAlignedObjectArray<btScalar> m_deltaVelocitiesUnitImpulse; //holds the joint-space response of the corresp. tree to the test impulse in each constraint space dimension 32 btAlignedObjectArray<btScalar> m_deltaVelocities; //holds joint-space vectors of all the constrained trees accumulating the effect of corrective impulses applied in SI 33 btAlignedObjectArray<btScalar> scratch_r; 34 btAlignedObjectArray<btVector3> scratch_v; 35 btAlignedObjectArray<btMatrix3x3> scratch_m; 36 btAlignedObjectArray<btSolverBody>* m_solverBodyPool; 37 int m_fixedBodyId; 38 39 }; 40 41 42 class btMultiBodyConstraint 43 { 44 protected: 45 46 btMultiBody* m_bodyA; 47 btMultiBody* m_bodyB; 48 int m_linkA; 49 int m_linkB; 50 51 int m_numRows; 52 int m_jacSizeA; 53 int m_jacSizeBoth; 54 int m_posOffset; 55 56 bool m_isUnilateral; 57 int m_numDofsFinalized; 58 btScalar m_maxAppliedImpulse; 59 60 61 // warning: the data block lay out is not consistent for all constraints 62 // data block laid out as follows: 63 // cached impulses. (one per row.) 64 // jacobians. (interleaved, row1 body1 then row1 body2 then row2 body 1 etc) 65 // positions. (one per row.) 66 btAlignedObjectArray<btScalar> m_data; 67 68 void applyDeltaVee(btMultiBodyJacobianData& data, btScalar* delta_vee, btScalar impulse, int velocityIndex, int ndof); 69 70 btScalar fillMultiBodyConstraint(btMultiBodySolverConstraint& solverConstraint, 71 btMultiBodyJacobianData& data, 72 btScalar* jacOrgA, btScalar* jacOrgB, 73 const btVector3& contactNormalOnB, 74 const btVector3& posAworld, const btVector3& posBworld, 75 btScalar posError, 76 const btContactSolverInfo& infoGlobal, 77 btScalar lowerLimit, btScalar upperLimit, 78 btScalar relaxation = 1.f, 79 bool isFriction = false, btScalar desiredVelocity=0, btScalar cfmSlip=0); 80 81 public: 82 83 btMultiBodyConstraint(btMultiBody* bodyA,btMultiBody* bodyB,int linkA, int linkB, int numRows, bool isUnilateral); 84 virtual ~btMultiBodyConstraint(); 85 86 void updateJacobianSizes(); 87 void allocateJacobiansMultiDof(); 88 89 virtual void finalizeMultiDof()=0; 90 91 virtual int getIslandIdA() const =0; 92 virtual int getIslandIdB() const =0; 93 94 virtual void createConstraintRows(btMultiBodyConstraintArray& constraintRows, 95 btMultiBodyJacobianData& data, 96 const btContactSolverInfo& infoGlobal)=0; 97 getNumRows()98 int getNumRows() const 99 { 100 return m_numRows; 101 } 102 getMultiBodyA()103 btMultiBody* getMultiBodyA() 104 { 105 return m_bodyA; 106 } getMultiBodyB()107 btMultiBody* getMultiBodyB() 108 { 109 return m_bodyB; 110 } 111 internalSetAppliedImpulse(int dof,btScalar appliedImpulse)112 void internalSetAppliedImpulse(int dof, btScalar appliedImpulse) 113 { 114 btAssert(dof>=0); 115 btAssert(dof < getNumRows()); 116 m_data[dof] = appliedImpulse; 117 } 118 getAppliedImpulse(int dof)119 btScalar getAppliedImpulse(int dof) 120 { 121 btAssert(dof>=0); 122 btAssert(dof < getNumRows()); 123 return m_data[dof]; 124 } 125 // current constraint position 126 // constraint is pos >= 0 for unilateral, or pos = 0 for bilateral 127 // NOTE: ignored position for friction rows. getPosition(int row)128 btScalar getPosition(int row) const 129 { 130 return m_data[m_posOffset + row]; 131 } 132 setPosition(int row,btScalar pos)133 void setPosition(int row, btScalar pos) 134 { 135 m_data[m_posOffset + row] = pos; 136 } 137 138 isUnilateral()139 bool isUnilateral() const 140 { 141 return m_isUnilateral; 142 } 143 144 // jacobian blocks. 145 // each of size 6 + num_links. (jacobian2 is null if no body2.) 146 // format: 3 'omega' coefficients, 3 'v' coefficients, then the 'qdot' coefficients. jacobianA(int row)147 btScalar* jacobianA(int row) 148 { 149 return &m_data[m_numRows + row * m_jacSizeBoth]; 150 } jacobianA(int row)151 const btScalar* jacobianA(int row) const 152 { 153 return &m_data[m_numRows + (row * m_jacSizeBoth)]; 154 } jacobianB(int row)155 btScalar* jacobianB(int row) 156 { 157 return &m_data[m_numRows + (row * m_jacSizeBoth) + m_jacSizeA]; 158 } jacobianB(int row)159 const btScalar* jacobianB(int row) const 160 { 161 return &m_data[m_numRows + (row * m_jacSizeBoth) + m_jacSizeA]; 162 } 163 getMaxAppliedImpulse()164 btScalar getMaxAppliedImpulse() const 165 { 166 return m_maxAppliedImpulse; 167 } setMaxAppliedImpulse(btScalar maxImp)168 void setMaxAppliedImpulse(btScalar maxImp) 169 { 170 m_maxAppliedImpulse = maxImp; 171 } 172 173 virtual void debugDraw(class btIDebugDraw* drawer)=0; 174 175 }; 176 177 #endif //BT_MULTIBODY_CONSTRAINT_H 178 179