btMultiBodySphericalJointLimit.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /*
  2. Bullet Continuous Collision Detection and Physics Library
  3. Copyright (c) 2018 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 "btMultiBodySphericalJointLimit.h"
  15. #include "btMultiBody.h"
  16. #include "btMultiBodyLinkCollider.h"
  17. #include "BulletCollision/CollisionDispatch/btCollisionObject.h"
  18. #include "LinearMath/btTransformUtil.h"
  19. #include "BulletDynamics/ConstraintSolver/btGeneric6DofSpring2Constraint.h"
  20. #include "LinearMath/btIDebugDraw.h"
  21. btMultiBodySphericalJointLimit::btMultiBodySphericalJointLimit(btMultiBody* body, int link,
  22. btScalar swingxRange,
  23. btScalar swingyRange,
  24. btScalar twistRange,
  25. btScalar maxAppliedImpulse)
  26. : btMultiBodyConstraint(body, body, link, body->getLink(link).m_parent, 3, true, MULTIBODY_CONSTRAINT_SPHERICAL_LIMIT),
  27. m_desiredVelocity(0, 0, 0),
  28. m_desiredPosition(0,0,0,1),
  29. m_use_multi_dof_params(false),
  30. m_kd(1., 1., 1.),
  31. m_kp(0.2, 0.2, 0.2),
  32. m_erp(1),
  33. m_rhsClamp(SIMD_INFINITY),
  34. m_maxAppliedImpulseMultiDof(maxAppliedImpulse, maxAppliedImpulse, maxAppliedImpulse),
  35. m_pivotA(m_bodyA->getLink(link).m_eVector),
  36. m_pivotB(m_bodyB->getLink(link).m_eVector),
  37. m_swingxRange(swingxRange),
  38. m_swingyRange(swingyRange),
  39. m_twistRange(twistRange)
  40. {
  41. m_maxAppliedImpulse = maxAppliedImpulse;
  42. }
  43. void btMultiBodySphericalJointLimit::finalizeMultiDof()
  44. {
  45. allocateJacobiansMultiDof();
  46. // note: we rely on the fact that data.m_jacobians are
  47. // always initialized to zero by the Constraint ctor
  48. int linkDoF = 0;
  49. unsigned int offset = 6 + (m_bodyA->getLink(m_linkA).m_dofOffset + linkDoF);
  50. // row 0: the lower bound
  51. // row 0: the lower bound
  52. jacobianA(0)[offset] = 1;
  53. jacobianB(1)[offset] = -1;
  54. m_numDofsFinalized = m_jacSizeBoth;
  55. }
  56. btMultiBodySphericalJointLimit::~btMultiBodySphericalJointLimit()
  57. {
  58. }
  59. int btMultiBodySphericalJointLimit::getIslandIdA() const
  60. {
  61. if (this->m_linkA < 0)
  62. {
  63. btMultiBodyLinkCollider* col = m_bodyA->getBaseCollider();
  64. if (col)
  65. return col->getIslandTag();
  66. }
  67. else
  68. {
  69. if (m_bodyA->getLink(m_linkA).m_collider)
  70. {
  71. return m_bodyA->getLink(m_linkA).m_collider->getIslandTag();
  72. }
  73. }
  74. return -1;
  75. }
  76. int btMultiBodySphericalJointLimit::getIslandIdB() const
  77. {
  78. if (m_linkB < 0)
  79. {
  80. btMultiBodyLinkCollider* col = m_bodyB->getBaseCollider();
  81. if (col)
  82. return col->getIslandTag();
  83. }
  84. else
  85. {
  86. if (m_bodyB->getLink(m_linkB).m_collider)
  87. {
  88. return m_bodyB->getLink(m_linkB).m_collider->getIslandTag();
  89. }
  90. }
  91. return -1;
  92. }
  93. void btMultiBodySphericalJointLimit::createConstraintRows(btMultiBodyConstraintArray& constraintRows,
  94. btMultiBodyJacobianData& data,
  95. const btContactSolverInfo& infoGlobal)
  96. {
  97. // only positions need to be updated -- data.m_jacobians and force
  98. // directions were set in the ctor and never change.
  99. if (m_numDofsFinalized != m_jacSizeBoth)
  100. {
  101. finalizeMultiDof();
  102. }
  103. //don't crash
  104. if (m_numDofsFinalized != m_jacSizeBoth)
  105. return;
  106. if (m_maxAppliedImpulse == 0.f)
  107. return;
  108. const btScalar posError = 0;
  109. const btVector3 zero(0, 0, 0);
  110. btVector3 axis[3] = { btVector3(1, 0, 0), btVector3(0, 1, 0), btVector3(0, 0, 1) };
  111. btQuaternion currentQuat(m_bodyA->getJointPosMultiDof(m_linkA)[0],
  112. m_bodyA->getJointPosMultiDof(m_linkA)[1],
  113. m_bodyA->getJointPosMultiDof(m_linkA)[2],
  114. m_bodyA->getJointPosMultiDof(m_linkA)[3]);
  115. btQuaternion refQuat = m_desiredPosition;
  116. btVector3 vTwist(0,0,1);
  117. btVector3 vConeNoTwist = quatRotate(currentQuat, vTwist);
  118. vConeNoTwist.normalize();
  119. btQuaternion qABCone = shortestArcQuat(vTwist, vConeNoTwist);
  120. qABCone.normalize();
  121. btQuaternion qABTwist = qABCone.inverse() * currentQuat;
  122. qABTwist.normalize();
  123. btQuaternion desiredQuat = qABTwist;
  124. btQuaternion relRot = currentQuat.inverse() * desiredQuat;
  125. btVector3 angleDiff;
  126. btGeneric6DofSpring2Constraint::matrixToEulerXYZ(btMatrix3x3(relRot), angleDiff);
  127. btScalar limitRanges[3] = {m_swingxRange, m_swingyRange, m_twistRange};
  128. /// twist axis/angle
  129. btQuaternion qMinTwist = qABTwist;
  130. btScalar twistAngle = qABTwist.getAngle();
  131. if (twistAngle > SIMD_PI) // long way around. flip quat and recalculate.
  132. {
  133. qMinTwist = -(qABTwist);
  134. twistAngle = qMinTwist.getAngle();
  135. }
  136. btVector3 vTwistAxis = btVector3(qMinTwist.x(), qMinTwist.y(), qMinTwist.z());
  137. if (twistAngle > SIMD_EPSILON)
  138. vTwistAxis.normalize();
  139. if (vTwistAxis.dot(vTwist)<0)
  140. twistAngle*=-1.;
  141. angleDiff[2] = twistAngle;
  142. for (int row = 0; row < getNumRows(); row++)
  143. {
  144. btScalar allowed = limitRanges[row];
  145. btScalar damp = 1;
  146. if((angleDiff[row]>-allowed)&&(angleDiff[row]<allowed))
  147. {
  148. angleDiff[row]=0;
  149. damp=0;
  150. } else
  151. {
  152. if (angleDiff[row]>allowed)
  153. {
  154. angleDiff[row]-=allowed;
  155. }
  156. if (angleDiff[row]<-allowed)
  157. {
  158. angleDiff[row]+=allowed;
  159. }
  160. }
  161. int dof = row;
  162. btScalar currentVelocity = m_bodyA->getJointVelMultiDof(m_linkA)[dof];
  163. btScalar desiredVelocity = this->m_desiredVelocity[row];
  164. double kd = m_use_multi_dof_params ? m_kd[row % 3] : m_kd[0];
  165. btScalar velocityError = (desiredVelocity - currentVelocity) * kd;
  166. btMatrix3x3 frameAworld;
  167. frameAworld.setIdentity();
  168. frameAworld = m_bodyA->localFrameToWorld(m_linkA, frameAworld);
  169. btScalar posError = 0;
  170. {
  171. btAssert(m_bodyA->getLink(m_linkA).m_jointType == btMultibodyLink::eSpherical);
  172. switch (m_bodyA->getLink(m_linkA).m_jointType)
  173. {
  174. case btMultibodyLink::eSpherical:
  175. {
  176. btVector3 constraintNormalAng = frameAworld.getColumn(row % 3);
  177. double kp = m_use_multi_dof_params ? m_kp[row % 3] : m_kp[0];
  178. posError = kp*angleDiff[row % 3];
  179. double max_applied_impulse = m_use_multi_dof_params ? m_maxAppliedImpulseMultiDof[row % 3] : m_maxAppliedImpulse;
  180. //should multiply by time step
  181. //max_applied_impulse *= infoGlobal.m_timeStep
  182. double min_applied_impulse = -max_applied_impulse;
  183. if (posError>0)
  184. max_applied_impulse=0;
  185. else
  186. min_applied_impulse=0;
  187. if (btFabs(posError)>SIMD_EPSILON)
  188. {
  189. btMultiBodySolverConstraint& constraintRow = constraintRows.expandNonInitializing();
  190. fillMultiBodyConstraint(constraintRow, data, 0, 0, constraintNormalAng,
  191. zero, zero, zero,//pure angular, so zero out linear parts
  192. posError,
  193. infoGlobal,
  194. min_applied_impulse, max_applied_impulse, true,
  195. 1.0, false, 0, 0,
  196. damp);
  197. constraintRow.m_orgConstraint = this;
  198. constraintRow.m_orgDofIndex = row;
  199. }
  200. break;
  201. }
  202. default:
  203. {
  204. btAssert(0);
  205. }
  206. };
  207. }
  208. }
  209. }
  210. void btMultiBodySphericalJointLimit::debugDraw(class btIDebugDraw* drawer)
  211. {
  212. btTransform tr;
  213. tr.setIdentity();
  214. if (m_bodyB)
  215. {
  216. btVector3 pivotBworld = m_bodyB->localPosToWorld(m_linkB, m_pivotB);
  217. tr.setOrigin(pivotBworld);
  218. drawer->drawTransform(tr, 0.1);
  219. }
  220. }