btMultiBodyConstraint.h 6.0 KB

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