btMultiBodyFixedConstraint.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 "btMultiBodyFixedConstraint.h"
  15. #include "btMultiBodyLinkCollider.h"
  16. #include "BulletDynamics/Dynamics/btRigidBody.h"
  17. #include "BulletDynamics/ConstraintSolver/btGeneric6DofSpring2Constraint.h"
  18. #include "LinearMath/btIDebugDraw.h"
  19. #define BTMBFIXEDCONSTRAINT_DIM 6
  20. btMultiBodyFixedConstraint::btMultiBodyFixedConstraint(btMultiBody* body, int link, btRigidBody* bodyB, const btVector3& pivotInA, const btVector3& pivotInB, const btMatrix3x3& frameInA, const btMatrix3x3& frameInB)
  21. :btMultiBodyConstraint(body,0,link,-1,BTMBFIXEDCONSTRAINT_DIM,false),
  22. m_rigidBodyA(0),
  23. m_rigidBodyB(bodyB),
  24. m_pivotInA(pivotInA),
  25. m_pivotInB(pivotInB),
  26. m_frameInA(frameInA),
  27. m_frameInB(frameInB)
  28. {
  29. m_data.resize(BTMBFIXEDCONSTRAINT_DIM);//at least store the applied impulses
  30. }
  31. btMultiBodyFixedConstraint::btMultiBodyFixedConstraint(btMultiBody* bodyA, int linkA, btMultiBody* bodyB, int linkB, const btVector3& pivotInA, const btVector3& pivotInB, const btMatrix3x3& frameInA, const btMatrix3x3& frameInB)
  32. :btMultiBodyConstraint(bodyA,bodyB,linkA,linkB,BTMBFIXEDCONSTRAINT_DIM,false),
  33. m_rigidBodyA(0),
  34. m_rigidBodyB(0),
  35. m_pivotInA(pivotInA),
  36. m_pivotInB(pivotInB),
  37. m_frameInA(frameInA),
  38. m_frameInB(frameInB)
  39. {
  40. m_data.resize(BTMBFIXEDCONSTRAINT_DIM);//at least store the applied impulses
  41. }
  42. void btMultiBodyFixedConstraint::finalizeMultiDof()
  43. {
  44. //not implemented yet
  45. btAssert(0);
  46. }
  47. btMultiBodyFixedConstraint::~btMultiBodyFixedConstraint()
  48. {
  49. }
  50. int btMultiBodyFixedConstraint::getIslandIdA() const
  51. {
  52. if (m_rigidBodyA)
  53. return m_rigidBodyA->getIslandTag();
  54. if (m_bodyA)
  55. {
  56. if (m_linkA < 0)
  57. {
  58. btMultiBodyLinkCollider* col = m_bodyA->getBaseCollider();
  59. if (col)
  60. return col->getIslandTag();
  61. }
  62. else
  63. {
  64. if (m_bodyA->getLink(m_linkA).m_collider)
  65. return m_bodyA->getLink(m_linkA).m_collider->getIslandTag();
  66. }
  67. }
  68. return -1;
  69. }
  70. int btMultiBodyFixedConstraint::getIslandIdB() const
  71. {
  72. if (m_rigidBodyB)
  73. return m_rigidBodyB->getIslandTag();
  74. if (m_bodyB)
  75. {
  76. if (m_linkB < 0)
  77. {
  78. btMultiBodyLinkCollider* col = m_bodyB->getBaseCollider();
  79. if (col)
  80. return col->getIslandTag();
  81. }
  82. else
  83. {
  84. if (m_bodyB->getLink(m_linkB).m_collider)
  85. return m_bodyB->getLink(m_linkB).m_collider->getIslandTag();
  86. }
  87. }
  88. return -1;
  89. }
  90. void btMultiBodyFixedConstraint::createConstraintRows(btMultiBodyConstraintArray& constraintRows, btMultiBodyJacobianData& data, const btContactSolverInfo& infoGlobal)
  91. {
  92. int numDim = BTMBFIXEDCONSTRAINT_DIM;
  93. for (int i=0;i<numDim;i++)
  94. {
  95. btMultiBodySolverConstraint& constraintRow = constraintRows.expandNonInitializing();
  96. constraintRow.m_orgConstraint = this;
  97. constraintRow.m_orgDofIndex = i;
  98. constraintRow.m_relpos1CrossNormal.setValue(0,0,0);
  99. constraintRow.m_contactNormal1.setValue(0,0,0);
  100. constraintRow.m_relpos2CrossNormal.setValue(0,0,0);
  101. constraintRow.m_contactNormal2.setValue(0,0,0);
  102. constraintRow.m_angularComponentA.setValue(0,0,0);
  103. constraintRow.m_angularComponentB.setValue(0,0,0);
  104. constraintRow.m_solverBodyIdA = data.m_fixedBodyId;
  105. constraintRow.m_solverBodyIdB = data.m_fixedBodyId;
  106. // Convert local points back to world
  107. btVector3 pivotAworld = m_pivotInA;
  108. btMatrix3x3 frameAworld = m_frameInA;
  109. if (m_rigidBodyA)
  110. {
  111. constraintRow.m_solverBodyIdA = m_rigidBodyA->getCompanionId();
  112. pivotAworld = m_rigidBodyA->getCenterOfMassTransform()*m_pivotInA;
  113. frameAworld = frameAworld.transpose()*btMatrix3x3(m_rigidBodyA->getOrientation());
  114. } else
  115. {
  116. if (m_bodyA) {
  117. pivotAworld = m_bodyA->localPosToWorld(m_linkA, m_pivotInA);
  118. frameAworld = m_bodyA->localFrameToWorld(m_linkA, frameAworld);
  119. }
  120. }
  121. btVector3 pivotBworld = m_pivotInB;
  122. btMatrix3x3 frameBworld = m_frameInB;
  123. if (m_rigidBodyB)
  124. {
  125. constraintRow.m_solverBodyIdB = m_rigidBodyB->getCompanionId();
  126. pivotBworld = m_rigidBodyB->getCenterOfMassTransform()*m_pivotInB;
  127. frameBworld = frameBworld.transpose()*btMatrix3x3(m_rigidBodyB->getOrientation());
  128. } else
  129. {
  130. if (m_bodyB) {
  131. pivotBworld = m_bodyB->localPosToWorld(m_linkB, m_pivotInB);
  132. frameBworld = m_bodyB->localFrameToWorld(m_linkB, frameBworld);
  133. }
  134. }
  135. btMatrix3x3 relRot = frameAworld.inverse()*frameBworld;
  136. btVector3 angleDiff;
  137. btGeneric6DofSpring2Constraint::matrixToEulerXYZ(relRot,angleDiff);
  138. btVector3 constraintNormalLin(0,0,0);
  139. btVector3 constraintNormalAng(0,0,0);
  140. btScalar posError = 0.0;
  141. if (i < 3) {
  142. constraintNormalLin[i] = 1;
  143. posError = (pivotAworld-pivotBworld).dot(constraintNormalLin);
  144. fillMultiBodyConstraint(constraintRow, data, 0, 0, constraintNormalAng,
  145. constraintNormalLin, pivotAworld, pivotBworld,
  146. posError,
  147. infoGlobal,
  148. -m_maxAppliedImpulse, m_maxAppliedImpulse
  149. );
  150. }
  151. else { //i>=3
  152. constraintNormalAng = frameAworld.getColumn(i%3);
  153. posError = angleDiff[i%3];
  154. fillMultiBodyConstraint(constraintRow, data, 0, 0, constraintNormalAng,
  155. constraintNormalLin, pivotAworld, pivotBworld,
  156. posError,
  157. infoGlobal,
  158. -m_maxAppliedImpulse, m_maxAppliedImpulse, true
  159. );
  160. }
  161. }
  162. }
  163. void btMultiBodyFixedConstraint::debugDraw(class btIDebugDraw* drawer)
  164. {
  165. btTransform tr;
  166. tr.setIdentity();
  167. if (m_rigidBodyA)
  168. {
  169. btVector3 pivot = m_rigidBodyA->getCenterOfMassTransform() * m_pivotInA;
  170. tr.setOrigin(pivot);
  171. drawer->drawTransform(tr, 0.1);
  172. }
  173. if (m_bodyA)
  174. {
  175. btVector3 pivotAworld = m_bodyA->localPosToWorld(m_linkA, m_pivotInA);
  176. tr.setOrigin(pivotAworld);
  177. drawer->drawTransform(tr, 0.1);
  178. }
  179. if (m_rigidBodyB)
  180. {
  181. // that ideally should draw the same frame
  182. btVector3 pivot = m_rigidBodyB->getCenterOfMassTransform() * m_pivotInB;
  183. tr.setOrigin(pivot);
  184. drawer->drawTransform(tr, 0.1);
  185. }
  186. if (m_bodyB)
  187. {
  188. btVector3 pivotBworld = m_bodyB->localPosToWorld(m_linkB, m_pivotInB);
  189. tr.setOrigin(pivotBworld);
  190. drawer->drawTransform(tr, 0.1);
  191. }
  192. }