btMultiBodyJointLimitConstraint.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 "bullet/BulletCollision//CollisionDispatch/btCollisionObject.h"
  18. btMultiBodyJointLimitConstraint::btMultiBodyJointLimitConstraint(btMultiBody* body, int link, btScalar lower, btScalar upper)
  19. :btMultiBodyConstraint(body,body,link,link,2,true),
  20. m_lowerBound(lower),
  21. m_upperBound(upper)
  22. {
  23. // the data.m_jacobians never change, so may as well
  24. // initialize them here
  25. // note: we rely on the fact that data.m_jacobians are
  26. // always initialized to zero by the Constraint ctor
  27. // row 0: the lower bound
  28. jacobianA(0)[6 + link] = 1;
  29. // row 1: the upper bound
  30. jacobianB(1)[6 + link] = -1;
  31. }
  32. btMultiBodyJointLimitConstraint::~btMultiBodyJointLimitConstraint()
  33. {
  34. }
  35. int btMultiBodyJointLimitConstraint::getIslandIdA() const
  36. {
  37. btMultiBodyLinkCollider* col = m_bodyA->getBaseCollider();
  38. if (col)
  39. return col->getIslandTag();
  40. for (int i=0;i<m_bodyA->getNumLinks();i++)
  41. {
  42. if (m_bodyA->getLink(i).m_collider)
  43. return m_bodyA->getLink(i).m_collider->getIslandTag();
  44. }
  45. return -1;
  46. }
  47. int btMultiBodyJointLimitConstraint::getIslandIdB() const
  48. {
  49. btMultiBodyLinkCollider* col = m_bodyB->getBaseCollider();
  50. if (col)
  51. return col->getIslandTag();
  52. for (int i=0;i<m_bodyB->getNumLinks();i++)
  53. {
  54. col = m_bodyB->getLink(i).m_collider;
  55. if (col)
  56. return col->getIslandTag();
  57. }
  58. return -1;
  59. }
  60. void btMultiBodyJointLimitConstraint::createConstraintRows(btMultiBodyConstraintArray& constraintRows,
  61. btMultiBodyJacobianData& data,
  62. const btContactSolverInfo& infoGlobal)
  63. {
  64. // only positions need to be updated -- data.m_jacobians and force
  65. // directions were set in the ctor and never change.
  66. // row 0: the lower bound
  67. setPosition(0, m_bodyA->getJointPos(m_linkA) - m_lowerBound);
  68. // row 1: the upper bound
  69. setPosition(1, m_upperBound - m_bodyA->getJointPos(m_linkA));
  70. for (int row=0;row<getNumRows();row++)
  71. {
  72. btMultiBodySolverConstraint& constraintRow = constraintRows.expandNonInitializing();
  73. constraintRow.m_multiBodyA = m_bodyA;
  74. constraintRow.m_multiBodyB = m_bodyB;
  75. btScalar rel_vel = fillConstraintRowMultiBodyMultiBody(constraintRow,data,jacobianA(row),jacobianB(row),infoGlobal,0,-m_maxAppliedImpulse,m_maxAppliedImpulse);
  76. {
  77. btScalar penetration = getPosition(row);
  78. btScalar positionalError = 0.f;
  79. btScalar velocityError = - rel_vel;// * damping;
  80. btScalar erp = infoGlobal.m_erp2;
  81. if (!infoGlobal.m_splitImpulse || (penetration > infoGlobal.m_splitImpulsePenetrationThreshold))
  82. {
  83. erp = infoGlobal.m_erp;
  84. }
  85. if (penetration>0)
  86. {
  87. positionalError = 0;
  88. velocityError = -penetration / infoGlobal.m_timeStep;
  89. } else
  90. {
  91. positionalError = -penetration * erp/infoGlobal.m_timeStep;
  92. }
  93. btScalar penetrationImpulse = positionalError*constraintRow.m_jacDiagABInv;
  94. btScalar velocityImpulse = velocityError *constraintRow.m_jacDiagABInv;
  95. if (!infoGlobal.m_splitImpulse || (penetration > infoGlobal.m_splitImpulsePenetrationThreshold))
  96. {
  97. //combine position and velocity into rhs
  98. constraintRow.m_rhs = penetrationImpulse+velocityImpulse;
  99. constraintRow.m_rhsPenetration = 0.f;
  100. } else
  101. {
  102. //split position and velocity into rhs and m_rhsPenetration
  103. constraintRow.m_rhs = velocityImpulse;
  104. constraintRow.m_rhsPenetration = penetrationImpulse;
  105. }
  106. }
  107. }
  108. }