btMultiBodyGearConstraint.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 "btMultiBodyGearConstraint.h"
  15. #include "btMultiBody.h"
  16. #include "btMultiBodyLinkCollider.h"
  17. #include "BulletCollision/CollisionDispatch/btCollisionObject.h"
  18. btMultiBodyGearConstraint::btMultiBodyGearConstraint(btMultiBody* bodyA, int linkA, btMultiBody* bodyB, int linkB, const btVector3& pivotInA, const btVector3& pivotInB, const btMatrix3x3& frameInA, const btMatrix3x3& frameInB)
  19. : btMultiBodyConstraint(bodyA, bodyB, linkA, linkB, 1, false),
  20. m_gearRatio(1),
  21. m_gearAuxLink(-1),
  22. m_erp(0),
  23. m_relativePositionTarget(0)
  24. {
  25. }
  26. void btMultiBodyGearConstraint::finalizeMultiDof()
  27. {
  28. allocateJacobiansMultiDof();
  29. m_numDofsFinalized = m_jacSizeBoth;
  30. }
  31. btMultiBodyGearConstraint::~btMultiBodyGearConstraint()
  32. {
  33. }
  34. int btMultiBodyGearConstraint::getIslandIdA() const
  35. {
  36. if (m_bodyA)
  37. {
  38. if (m_linkA < 0)
  39. {
  40. btMultiBodyLinkCollider* col = m_bodyA->getBaseCollider();
  41. if (col)
  42. return col->getIslandTag();
  43. }
  44. else
  45. {
  46. if (m_bodyA->getLink(m_linkA).m_collider)
  47. return m_bodyA->getLink(m_linkA).m_collider->getIslandTag();
  48. }
  49. }
  50. return -1;
  51. }
  52. int btMultiBodyGearConstraint::getIslandIdB() const
  53. {
  54. if (m_bodyB)
  55. {
  56. if (m_linkB < 0)
  57. {
  58. btMultiBodyLinkCollider* col = m_bodyB->getBaseCollider();
  59. if (col)
  60. return col->getIslandTag();
  61. }
  62. else
  63. {
  64. if (m_bodyB->getLink(m_linkB).m_collider)
  65. return m_bodyB->getLink(m_linkB).m_collider->getIslandTag();
  66. }
  67. }
  68. return -1;
  69. }
  70. void btMultiBodyGearConstraint::createConstraintRows(btMultiBodyConstraintArray& constraintRows,
  71. btMultiBodyJacobianData& data,
  72. const btContactSolverInfo& infoGlobal)
  73. {
  74. // only positions need to be updated -- data.m_jacobians and force
  75. // directions were set in the ctor and never change.
  76. if (m_numDofsFinalized != m_jacSizeBoth)
  77. {
  78. finalizeMultiDof();
  79. }
  80. //don't crash
  81. if (m_numDofsFinalized != m_jacSizeBoth)
  82. return;
  83. if (m_maxAppliedImpulse == 0.f)
  84. return;
  85. // note: we rely on the fact that data.m_jacobians are
  86. // always initialized to zero by the Constraint ctor
  87. int linkDoF = 0;
  88. unsigned int offsetA = 6 + (m_bodyA->getLink(m_linkA).m_dofOffset + linkDoF);
  89. unsigned int offsetB = 6 + (m_bodyB->getLink(m_linkB).m_dofOffset + linkDoF);
  90. // row 0: the lower bound
  91. jacobianA(0)[offsetA] = 1;
  92. jacobianB(0)[offsetB] = m_gearRatio;
  93. btScalar posError = 0;
  94. const btVector3 dummy(0, 0, 0);
  95. btScalar kp = 1;
  96. btScalar kd = 1;
  97. int numRows = getNumRows();
  98. for (int row = 0; row < numRows; row++)
  99. {
  100. btMultiBodySolverConstraint& constraintRow = constraintRows.expandNonInitializing();
  101. int dof = 0;
  102. btScalar currentPosition = m_bodyA->getJointPosMultiDof(m_linkA)[dof];
  103. btScalar currentVelocity = m_bodyA->getJointVelMultiDof(m_linkA)[dof];
  104. btScalar auxVel = 0;
  105. if (m_gearAuxLink >= 0)
  106. {
  107. auxVel = m_bodyA->getJointVelMultiDof(m_gearAuxLink)[dof];
  108. }
  109. currentVelocity += auxVel;
  110. if (m_erp != 0)
  111. {
  112. btScalar currentPositionA = m_bodyA->getJointPosMultiDof(m_linkA)[dof];
  113. if (m_gearAuxLink >= 0)
  114. {
  115. currentPositionA -= m_bodyA->getJointPosMultiDof(m_gearAuxLink)[dof];
  116. }
  117. btScalar currentPositionB = m_gearRatio * m_bodyA->getJointPosMultiDof(m_linkB)[dof];
  118. btScalar diff = currentPositionB + currentPositionA;
  119. btScalar desiredPositionDiff = this->m_relativePositionTarget;
  120. posError = -m_erp * (desiredPositionDiff - diff);
  121. }
  122. btScalar desiredRelativeVelocity = auxVel;
  123. fillMultiBodyConstraint(constraintRow, data, jacobianA(row), jacobianB(row), dummy, dummy, dummy, dummy, posError, infoGlobal, -m_maxAppliedImpulse, m_maxAppliedImpulse, false, 1, false, desiredRelativeVelocity);
  124. constraintRow.m_orgConstraint = this;
  125. constraintRow.m_orgDofIndex = row;
  126. {
  127. //expect either prismatic or revolute joint type for now
  128. btAssert((m_bodyA->getLink(m_linkA).m_jointType == btMultibodyLink::eRevolute) || (m_bodyA->getLink(m_linkA).m_jointType == btMultibodyLink::ePrismatic));
  129. switch (m_bodyA->getLink(m_linkA).m_jointType)
  130. {
  131. case btMultibodyLink::eRevolute:
  132. {
  133. constraintRow.m_contactNormal1.setZero();
  134. constraintRow.m_contactNormal2.setZero();
  135. btVector3 revoluteAxisInWorld = quatRotate(m_bodyA->getLink(m_linkA).m_cachedWorldTransform.getRotation(), m_bodyA->getLink(m_linkA).m_axes[0].m_topVec);
  136. constraintRow.m_relpos1CrossNormal = revoluteAxisInWorld;
  137. constraintRow.m_relpos2CrossNormal = -revoluteAxisInWorld;
  138. break;
  139. }
  140. case btMultibodyLink::ePrismatic:
  141. {
  142. btVector3 prismaticAxisInWorld = quatRotate(m_bodyA->getLink(m_linkA).m_cachedWorldTransform.getRotation(), m_bodyA->getLink(m_linkA).m_axes[0].m_bottomVec);
  143. constraintRow.m_contactNormal1 = prismaticAxisInWorld;
  144. constraintRow.m_contactNormal2 = -prismaticAxisInWorld;
  145. constraintRow.m_relpos1CrossNormal.setZero();
  146. constraintRow.m_relpos2CrossNormal.setZero();
  147. break;
  148. }
  149. default:
  150. {
  151. btAssert(0);
  152. }
  153. };
  154. }
  155. }
  156. }