btMultiBodyJointLimitConstraint.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. ///This file was written by Erwin Coumans
  14. #include "btMultiBodyJointLimitConstraint.h"
  15. #include "btMultiBody.h"
  16. #include "btMultiBodyLinkCollider.h"
  17. #include "BulletCollision/CollisionDispatch/btCollisionObject.h"
  18. btMultiBodyJointLimitConstraint::btMultiBodyJointLimitConstraint(btMultiBody* body, int link, btScalar lower, btScalar upper)
  19. //:btMultiBodyConstraint(body,0,link,-1,2,true),
  20. :btMultiBodyConstraint(body,body,link,body->getLink(link).m_parent,2,true),
  21. m_lowerBound(lower),
  22. m_upperBound(upper)
  23. {
  24. }
  25. void btMultiBodyJointLimitConstraint::finalizeMultiDof()
  26. {
  27. // the data.m_jacobians never change, so may as well
  28. // initialize them here
  29. allocateJacobiansMultiDof();
  30. unsigned int offset = 6 + m_bodyA->getLink(m_linkA).m_dofOffset;
  31. // row 0: the lower bound
  32. jacobianA(0)[offset] = 1;
  33. // row 1: the upper bound
  34. //jacobianA(1)[offset] = -1;
  35. jacobianB(1)[offset] = -1;
  36. m_numDofsFinalized = m_jacSizeBoth;
  37. }
  38. btMultiBodyJointLimitConstraint::~btMultiBodyJointLimitConstraint()
  39. {
  40. }
  41. int btMultiBodyJointLimitConstraint::getIslandIdA() const
  42. {
  43. if (m_bodyA)
  44. {
  45. if (m_linkA < 0)
  46. {
  47. btMultiBodyLinkCollider* col = m_bodyA->getBaseCollider();
  48. if (col)
  49. return col->getIslandTag();
  50. }
  51. else
  52. {
  53. if (m_bodyA->getLink(m_linkA).m_collider)
  54. return m_bodyA->getLink(m_linkA).m_collider->getIslandTag();
  55. }
  56. }
  57. return -1;
  58. }
  59. int btMultiBodyJointLimitConstraint::getIslandIdB() const
  60. {
  61. if (m_bodyB)
  62. {
  63. if (m_linkB < 0)
  64. {
  65. btMultiBodyLinkCollider* col = m_bodyB->getBaseCollider();
  66. if (col)
  67. return col->getIslandTag();
  68. }
  69. else
  70. {
  71. if (m_bodyB->getLink(m_linkB).m_collider)
  72. return m_bodyB->getLink(m_linkB).m_collider->getIslandTag();
  73. }
  74. }
  75. return -1;
  76. }
  77. void btMultiBodyJointLimitConstraint::createConstraintRows(btMultiBodyConstraintArray& constraintRows,
  78. btMultiBodyJacobianData& data,
  79. const btContactSolverInfo& infoGlobal)
  80. {
  81. // only positions need to be updated -- data.m_jacobians and force
  82. // directions were set in the ctor and never change.
  83. if (m_numDofsFinalized != m_jacSizeBoth)
  84. {
  85. finalizeMultiDof();
  86. }
  87. // row 0: the lower bound
  88. setPosition(0, m_bodyA->getJointPos(m_linkA) - m_lowerBound); //multidof: this is joint-type dependent
  89. // row 1: the upper bound
  90. setPosition(1, m_upperBound - m_bodyA->getJointPos(m_linkA));
  91. for (int row=0;row<getNumRows();row++)
  92. {
  93. btScalar penetration = getPosition(row);
  94. //todo: consider adding some safety threshold here
  95. if (penetration>0)
  96. {
  97. continue;
  98. }
  99. btScalar direction = row? -1 : 1;
  100. btMultiBodySolverConstraint& constraintRow = constraintRows.expandNonInitializing();
  101. constraintRow.m_orgConstraint = this;
  102. constraintRow.m_orgDofIndex = row;
  103. constraintRow.m_multiBodyA = m_bodyA;
  104. constraintRow.m_multiBodyB = m_bodyB;
  105. const btScalar posError = 0; //why assume it's zero?
  106. const btVector3 dummy(0, 0, 0);
  107. btScalar rel_vel = fillMultiBodyConstraint(constraintRow,data,jacobianA(row),jacobianB(row),dummy,dummy,dummy,dummy,posError,infoGlobal,0,m_maxAppliedImpulse);
  108. {
  109. //expect either prismatic or revolute joint type for now
  110. btAssert((m_bodyA->getLink(m_linkA).m_jointType == btMultibodyLink::eRevolute)||(m_bodyA->getLink(m_linkA).m_jointType == btMultibodyLink::ePrismatic));
  111. switch (m_bodyA->getLink(m_linkA).m_jointType)
  112. {
  113. case btMultibodyLink::eRevolute:
  114. {
  115. constraintRow.m_contactNormal1.setZero();
  116. constraintRow.m_contactNormal2.setZero();
  117. btVector3 revoluteAxisInWorld = direction*quatRotate(m_bodyA->getLink(m_linkA).m_cachedWorldTransform.getRotation(),m_bodyA->getLink(m_linkA).m_axes[0].m_topVec);
  118. constraintRow.m_relpos1CrossNormal=revoluteAxisInWorld;
  119. constraintRow.m_relpos2CrossNormal=-revoluteAxisInWorld;
  120. break;
  121. }
  122. case btMultibodyLink::ePrismatic:
  123. {
  124. btVector3 prismaticAxisInWorld = direction* quatRotate(m_bodyA->getLink(m_linkA).m_cachedWorldTransform.getRotation(),m_bodyA->getLink(m_linkA).m_axes[0].m_bottomVec);
  125. constraintRow.m_contactNormal1=prismaticAxisInWorld;
  126. constraintRow.m_contactNormal2=-prismaticAxisInWorld;
  127. constraintRow.m_relpos1CrossNormal.setZero();
  128. constraintRow.m_relpos2CrossNormal.setZero();
  129. break;
  130. }
  131. default:
  132. {
  133. btAssert(0);
  134. }
  135. };
  136. }
  137. {
  138. btScalar positionalError = 0.f;
  139. btScalar velocityError = - rel_vel;// * damping;
  140. btScalar erp = infoGlobal.m_erp2;
  141. if (!infoGlobal.m_splitImpulse || (penetration > infoGlobal.m_splitImpulsePenetrationThreshold))
  142. {
  143. erp = infoGlobal.m_erp;
  144. }
  145. if (penetration>0)
  146. {
  147. positionalError = 0;
  148. velocityError = -penetration / infoGlobal.m_timeStep;
  149. } else
  150. {
  151. positionalError = -penetration * erp/infoGlobal.m_timeStep;
  152. }
  153. btScalar penetrationImpulse = positionalError*constraintRow.m_jacDiagABInv;
  154. btScalar velocityImpulse = velocityError *constraintRow.m_jacDiagABInv;
  155. if (!infoGlobal.m_splitImpulse || (penetration > infoGlobal.m_splitImpulsePenetrationThreshold))
  156. {
  157. //combine position and velocity into rhs
  158. constraintRow.m_rhs = penetrationImpulse+velocityImpulse;
  159. constraintRow.m_rhsPenetration = 0.f;
  160. } else
  161. {
  162. //split position and velocity into rhs and m_rhsPenetration
  163. constraintRow.m_rhs = velocityImpulse;
  164. constraintRow.m_rhsPenetration = penetrationImpulse;
  165. }
  166. }
  167. }
  168. }