btMultiBodyConstraint.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. #ifndef BT_MULTIBODY_CONSTRAINT_H
  14. #define BT_MULTIBODY_CONSTRAINT_H
  15. #include "bullet/LinearMath/btScalar.h"
  16. #include "bullet/LinearMath/btAlignedObjectArray.h"
  17. #include "btMultiBody.h"
  18. class btMultiBody;
  19. struct btSolverInfo;
  20. #include "btMultiBodySolverConstraint.h"
  21. struct btMultiBodyJacobianData
  22. {
  23. btAlignedObjectArray<btScalar> m_jacobians;
  24. btAlignedObjectArray<btScalar> m_deltaVelocitiesUnitImpulse;
  25. btAlignedObjectArray<btScalar> m_deltaVelocities;
  26. btAlignedObjectArray<btScalar> scratch_r;
  27. btAlignedObjectArray<btVector3> scratch_v;
  28. btAlignedObjectArray<btMatrix3x3> scratch_m;
  29. btAlignedObjectArray<btSolverBody>* m_solverBodyPool;
  30. int m_fixedBodyId;
  31. };
  32. class btMultiBodyConstraint
  33. {
  34. protected:
  35. btMultiBody* m_bodyA;
  36. btMultiBody* m_bodyB;
  37. int m_linkA;
  38. int m_linkB;
  39. int m_num_rows;
  40. int m_jac_size_A;
  41. int m_jac_size_both;
  42. int m_pos_offset;
  43. bool m_isUnilateral;
  44. btScalar m_maxAppliedImpulse;
  45. // data block laid out as follows:
  46. // cached impulses. (one per row.)
  47. // jacobians. (interleaved, row1 body1 then row1 body2 then row2 body 1 etc)
  48. // positions. (one per row.)
  49. btAlignedObjectArray<btScalar> m_data;
  50. void applyDeltaVee(btMultiBodyJacobianData& data, btScalar* delta_vee, btScalar impulse, int velocityIndex, int ndof);
  51. void fillMultiBodyConstraintMixed(btMultiBodySolverConstraint& solverConstraint,
  52. btMultiBodyJacobianData& data,
  53. const btVector3& contactNormalOnB,
  54. const btVector3& posAworld, const btVector3& posBworld,
  55. btScalar position,
  56. const btContactSolverInfo& infoGlobal,
  57. btScalar& relaxation,
  58. bool isFriction, btScalar desiredVelocity=0, btScalar cfmSlip=0);
  59. btScalar fillConstraintRowMultiBodyMultiBody(btMultiBodySolverConstraint& constraintRow,
  60. btMultiBodyJacobianData& data,
  61. btScalar* jacOrgA,btScalar* jacOrgB,
  62. const btContactSolverInfo& infoGlobal,
  63. btScalar desiredVelocity,
  64. btScalar lowerLimit,
  65. btScalar upperLimit);
  66. public:
  67. btMultiBodyConstraint(btMultiBody* bodyA,btMultiBody* bodyB,int linkA, int linkB, int numRows, bool isUnilateral);
  68. virtual ~btMultiBodyConstraint();
  69. virtual int getIslandIdA() const =0;
  70. virtual int getIslandIdB() const =0;
  71. virtual void createConstraintRows(btMultiBodyConstraintArray& constraintRows,
  72. btMultiBodyJacobianData& data,
  73. const btContactSolverInfo& infoGlobal)=0;
  74. int getNumRows() const
  75. {
  76. return m_num_rows;
  77. }
  78. btMultiBody* getMultiBodyA()
  79. {
  80. return m_bodyA;
  81. }
  82. btMultiBody* getMultiBodyB()
  83. {
  84. return m_bodyB;
  85. }
  86. // current constraint position
  87. // constraint is pos >= 0 for unilateral, or pos = 0 for bilateral
  88. // NOTE: ignored position for friction rows.
  89. btScalar getPosition(int row) const
  90. {
  91. return m_data[m_pos_offset + row];
  92. }
  93. void setPosition(int row, btScalar pos)
  94. {
  95. m_data[m_pos_offset + row] = pos;
  96. }
  97. bool isUnilateral() const
  98. {
  99. return m_isUnilateral;
  100. }
  101. // jacobian blocks.
  102. // each of size 6 + num_links. (jacobian2 is null if no body2.)
  103. // format: 3 'omega' coefficients, 3 'v' coefficients, then the 'qdot' coefficients.
  104. btScalar* jacobianA(int row)
  105. {
  106. return &m_data[m_num_rows + row * m_jac_size_both];
  107. }
  108. const btScalar* jacobianA(int row) const
  109. {
  110. return &m_data[m_num_rows + (row * m_jac_size_both)];
  111. }
  112. btScalar* jacobianB(int row)
  113. {
  114. return &m_data[m_num_rows + (row * m_jac_size_both) + m_jac_size_A];
  115. }
  116. const btScalar* jacobianB(int row) const
  117. {
  118. return &m_data[m_num_rows + (row * m_jac_size_both) + m_jac_size_A];
  119. }
  120. btScalar getMaxAppliedImpulse() const
  121. {
  122. return m_maxAppliedImpulse;
  123. }
  124. void setMaxAppliedImpulse(btScalar maxImp)
  125. {
  126. m_maxAppliedImpulse = maxImp;
  127. }
  128. };
  129. #endif //BT_MULTIBODY_CONSTRAINT_H