b3FixedConstraint.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #include "b3FixedConstraint.h"
  2. #include "Bullet3Collision/NarrowPhaseCollision/shared/b3RigidBodyData.h"
  3. #include "Bullet3Common/b3TransformUtil.h"
  4. #include <new>
  5. b3FixedConstraint::b3FixedConstraint(int rbA, int rbB, const b3Transform& frameInA, const b3Transform& frameInB)
  6. : b3TypedConstraint(B3_FIXED_CONSTRAINT_TYPE, rbA, rbB)
  7. {
  8. m_pivotInA = frameInA.getOrigin();
  9. m_pivotInB = frameInB.getOrigin();
  10. m_relTargetAB = frameInA.getRotation() * frameInB.getRotation().inverse();
  11. }
  12. b3FixedConstraint::~b3FixedConstraint()
  13. {
  14. }
  15. void b3FixedConstraint::getInfo1(b3ConstraintInfo1* info, const b3RigidBodyData* bodies)
  16. {
  17. info->m_numConstraintRows = 6;
  18. info->nub = 6;
  19. }
  20. void b3FixedConstraint::getInfo2(b3ConstraintInfo2* info, const b3RigidBodyData* bodies)
  21. {
  22. //fix the 3 linear degrees of freedom
  23. const b3Vector3& worldPosA = bodies[m_rbA].m_pos;
  24. const b3Quaternion& worldOrnA = bodies[m_rbA].m_quat;
  25. const b3Vector3& worldPosB = bodies[m_rbB].m_pos;
  26. const b3Quaternion& worldOrnB = bodies[m_rbB].m_quat;
  27. info->m_J1linearAxis[0] = 1;
  28. info->m_J1linearAxis[info->rowskip + 1] = 1;
  29. info->m_J1linearAxis[2 * info->rowskip + 2] = 1;
  30. b3Vector3 a1 = b3QuatRotate(worldOrnA, m_pivotInA);
  31. {
  32. b3Vector3* angular0 = (b3Vector3*)(info->m_J1angularAxis);
  33. b3Vector3* angular1 = (b3Vector3*)(info->m_J1angularAxis + info->rowskip);
  34. b3Vector3* angular2 = (b3Vector3*)(info->m_J1angularAxis + 2 * info->rowskip);
  35. b3Vector3 a1neg = -a1;
  36. a1neg.getSkewSymmetricMatrix(angular0, angular1, angular2);
  37. }
  38. if (info->m_J2linearAxis)
  39. {
  40. info->m_J2linearAxis[0] = -1;
  41. info->m_J2linearAxis[info->rowskip + 1] = -1;
  42. info->m_J2linearAxis[2 * info->rowskip + 2] = -1;
  43. }
  44. b3Vector3 a2 = b3QuatRotate(worldOrnB, m_pivotInB);
  45. {
  46. // b3Vector3 a2n = -a2;
  47. b3Vector3* angular0 = (b3Vector3*)(info->m_J2angularAxis);
  48. b3Vector3* angular1 = (b3Vector3*)(info->m_J2angularAxis + info->rowskip);
  49. b3Vector3* angular2 = (b3Vector3*)(info->m_J2angularAxis + 2 * info->rowskip);
  50. a2.getSkewSymmetricMatrix(angular0, angular1, angular2);
  51. }
  52. // set right hand side for the linear dofs
  53. b3Scalar k = info->fps * info->erp;
  54. b3Vector3 linearError = k * (a2 + worldPosB - a1 - worldPosA);
  55. int j;
  56. for (j = 0; j < 3; j++)
  57. {
  58. info->m_constraintError[j * info->rowskip] = linearError[j];
  59. //printf("info->m_constraintError[%d]=%f\n",j,info->m_constraintError[j]);
  60. }
  61. //fix the 3 angular degrees of freedom
  62. int start_row = 3;
  63. int s = info->rowskip;
  64. int start_index = start_row * s;
  65. // 3 rows to make body rotations equal
  66. info->m_J1angularAxis[start_index] = 1;
  67. info->m_J1angularAxis[start_index + s + 1] = 1;
  68. info->m_J1angularAxis[start_index + s * 2 + 2] = 1;
  69. if (info->m_J2angularAxis)
  70. {
  71. info->m_J2angularAxis[start_index] = -1;
  72. info->m_J2angularAxis[start_index + s + 1] = -1;
  73. info->m_J2angularAxis[start_index + s * 2 + 2] = -1;
  74. }
  75. // set right hand side for the angular dofs
  76. b3Vector3 diff;
  77. b3Scalar angle;
  78. b3Quaternion qrelCur = worldOrnA * worldOrnB.inverse();
  79. b3TransformUtil::calculateDiffAxisAngleQuaternion(m_relTargetAB, qrelCur, diff, angle);
  80. diff *= -angle;
  81. for (j = 0; j < 3; j++)
  82. {
  83. info->m_constraintError[(3 + j) * info->rowskip] = k * diff[j];
  84. }
  85. }