btMultiBodyPoint2Point.cpp 6.6 KB

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