btJacobianEntry.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. Bullet Continuous Collision Detection and Physics Library
  3. Copyright (c) 2003-2006 Erwin Coumans http://continuousphysics.com/Bullet/
  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_JACOBIAN_ENTRY_H
  14. #define BT_JACOBIAN_ENTRY_H
  15. #include "bullet/LinearMath/btMatrix3x3.h"
  16. //notes:
  17. // Another memory optimization would be to store m_1MinvJt in the remaining 3 w components
  18. // which makes the btJacobianEntry memory layout 16 bytes
  19. // if you only are interested in angular part, just feed massInvA and massInvB zero
  20. /// Jacobian entry is an abstraction that allows to describe constraints
  21. /// it can be used in combination with a constraint solver
  22. /// Can be used to relate the effect of an impulse to the constraint error
  23. ATTRIBUTE_ALIGNED16(class) btJacobianEntry
  24. {
  25. public:
  26. btJacobianEntry() {};
  27. //constraint between two different rigidbodies
  28. btJacobianEntry(
  29. const btMatrix3x3& world2A,
  30. const btMatrix3x3& world2B,
  31. const btVector3& rel_pos1,const btVector3& rel_pos2,
  32. const btVector3& jointAxis,
  33. const btVector3& inertiaInvA,
  34. const btScalar massInvA,
  35. const btVector3& inertiaInvB,
  36. const btScalar massInvB)
  37. :m_linearJointAxis(jointAxis)
  38. {
  39. m_aJ = world2A*(rel_pos1.cross(m_linearJointAxis));
  40. m_bJ = world2B*(rel_pos2.cross(-m_linearJointAxis));
  41. m_0MinvJt = inertiaInvA * m_aJ;
  42. m_1MinvJt = inertiaInvB * m_bJ;
  43. m_Adiag = massInvA + m_0MinvJt.dot(m_aJ) + massInvB + m_1MinvJt.dot(m_bJ);
  44. btAssert(m_Adiag > btScalar(0.0));
  45. }
  46. //angular constraint between two different rigidbodies
  47. btJacobianEntry(const btVector3& jointAxis,
  48. const btMatrix3x3& world2A,
  49. const btMatrix3x3& world2B,
  50. const btVector3& inertiaInvA,
  51. const btVector3& inertiaInvB)
  52. :m_linearJointAxis(btVector3(btScalar(0.),btScalar(0.),btScalar(0.)))
  53. {
  54. m_aJ= world2A*jointAxis;
  55. m_bJ = world2B*-jointAxis;
  56. m_0MinvJt = inertiaInvA * m_aJ;
  57. m_1MinvJt = inertiaInvB * m_bJ;
  58. m_Adiag = m_0MinvJt.dot(m_aJ) + m_1MinvJt.dot(m_bJ);
  59. btAssert(m_Adiag > btScalar(0.0));
  60. }
  61. //angular constraint between two different rigidbodies
  62. btJacobianEntry(const btVector3& axisInA,
  63. const btVector3& axisInB,
  64. const btVector3& inertiaInvA,
  65. const btVector3& inertiaInvB)
  66. : m_linearJointAxis(btVector3(btScalar(0.),btScalar(0.),btScalar(0.)))
  67. , m_aJ(axisInA)
  68. , m_bJ(-axisInB)
  69. {
  70. m_0MinvJt = inertiaInvA * m_aJ;
  71. m_1MinvJt = inertiaInvB * m_bJ;
  72. m_Adiag = m_0MinvJt.dot(m_aJ) + m_1MinvJt.dot(m_bJ);
  73. btAssert(m_Adiag > btScalar(0.0));
  74. }
  75. //constraint on one rigidbody
  76. btJacobianEntry(
  77. const btMatrix3x3& world2A,
  78. const btVector3& rel_pos1,const btVector3& rel_pos2,
  79. const btVector3& jointAxis,
  80. const btVector3& inertiaInvA,
  81. const btScalar massInvA)
  82. :m_linearJointAxis(jointAxis)
  83. {
  84. m_aJ= world2A*(rel_pos1.cross(jointAxis));
  85. m_bJ = world2A*(rel_pos2.cross(-jointAxis));
  86. m_0MinvJt = inertiaInvA * m_aJ;
  87. m_1MinvJt = btVector3(btScalar(0.),btScalar(0.),btScalar(0.));
  88. m_Adiag = massInvA + m_0MinvJt.dot(m_aJ);
  89. btAssert(m_Adiag > btScalar(0.0));
  90. }
  91. btScalar getDiagonal() const { return m_Adiag; }
  92. // for two constraints on the same rigidbody (for example vehicle friction)
  93. btScalar getNonDiagonal(const btJacobianEntry& jacB, const btScalar massInvA) const
  94. {
  95. const btJacobianEntry& jacA = *this;
  96. btScalar lin = massInvA * jacA.m_linearJointAxis.dot(jacB.m_linearJointAxis);
  97. btScalar ang = jacA.m_0MinvJt.dot(jacB.m_aJ);
  98. return lin + ang;
  99. }
  100. // for two constraints on sharing two same rigidbodies (for example two contact points between two rigidbodies)
  101. btScalar getNonDiagonal(const btJacobianEntry& jacB,const btScalar massInvA,const btScalar massInvB) const
  102. {
  103. const btJacobianEntry& jacA = *this;
  104. btVector3 lin = jacA.m_linearJointAxis * jacB.m_linearJointAxis;
  105. btVector3 ang0 = jacA.m_0MinvJt * jacB.m_aJ;
  106. btVector3 ang1 = jacA.m_1MinvJt * jacB.m_bJ;
  107. btVector3 lin0 = massInvA * lin ;
  108. btVector3 lin1 = massInvB * lin;
  109. btVector3 sum = ang0+ang1+lin0+lin1;
  110. return sum[0]+sum[1]+sum[2];
  111. }
  112. btScalar getRelativeVelocity(const btVector3& linvelA,const btVector3& angvelA,const btVector3& linvelB,const btVector3& angvelB)
  113. {
  114. btVector3 linrel = linvelA - linvelB;
  115. btVector3 angvela = angvelA * m_aJ;
  116. btVector3 angvelb = angvelB * m_bJ;
  117. linrel *= m_linearJointAxis;
  118. angvela += angvelb;
  119. angvela += linrel;
  120. btScalar rel_vel2 = angvela[0]+angvela[1]+angvela[2];
  121. return rel_vel2 + SIMD_EPSILON;
  122. }
  123. //private:
  124. btVector3 m_linearJointAxis;
  125. btVector3 m_aJ;
  126. btVector3 m_bJ;
  127. btVector3 m_0MinvJt;
  128. btVector3 m_1MinvJt;
  129. //Optimization: can be stored in the w/last component of one of the vectors
  130. btScalar m_Adiag;
  131. };
  132. #endif //BT_JACOBIAN_ENTRY_H