b3JacobianEntry.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 B3_JACOBIAN_ENTRY_H
  14. #define B3_JACOBIAN_ENTRY_H
  15. #include "Bullet3Common/b3Matrix3x3.h"
  16. //notes:
  17. // Another memory optimization would be to store m_1MinvJt in the remaining 3 w components
  18. // which makes the b3JacobianEntry 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. B3_ATTRIBUTE_ALIGNED16(class) b3JacobianEntry
  24. {
  25. public:
  26. b3JacobianEntry() {};
  27. //constraint between two different rigidbodies
  28. b3JacobianEntry(
  29. const b3Matrix3x3& world2A,
  30. const b3Matrix3x3& world2B,
  31. const b3Vector3& rel_pos1,const b3Vector3& rel_pos2,
  32. const b3Vector3& jointAxis,
  33. const b3Vector3& inertiaInvA,
  34. const b3Scalar massInvA,
  35. const b3Vector3& inertiaInvB,
  36. const b3Scalar 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. b3Assert(m_Adiag > b3Scalar(0.0));
  45. }
  46. //angular constraint between two different rigidbodies
  47. b3JacobianEntry(const b3Vector3& jointAxis,
  48. const b3Matrix3x3& world2A,
  49. const b3Matrix3x3& world2B,
  50. const b3Vector3& inertiaInvA,
  51. const b3Vector3& inertiaInvB)
  52. :m_linearJointAxis(b3MakeVector3(b3Scalar(0.),b3Scalar(0.),b3Scalar(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. b3Assert(m_Adiag > b3Scalar(0.0));
  60. }
  61. //angular constraint between two different rigidbodies
  62. b3JacobianEntry(const b3Vector3& axisInA,
  63. const b3Vector3& axisInB,
  64. const b3Vector3& inertiaInvA,
  65. const b3Vector3& inertiaInvB)
  66. : m_linearJointAxis(b3MakeVector3(b3Scalar(0.),b3Scalar(0.),b3Scalar(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. b3Assert(m_Adiag > b3Scalar(0.0));
  74. }
  75. //constraint on one rigidbody
  76. b3JacobianEntry(
  77. const b3Matrix3x3& world2A,
  78. const b3Vector3& rel_pos1,const b3Vector3& rel_pos2,
  79. const b3Vector3& jointAxis,
  80. const b3Vector3& inertiaInvA,
  81. const b3Scalar 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 = b3MakeVector3(b3Scalar(0.),b3Scalar(0.),b3Scalar(0.));
  88. m_Adiag = massInvA + m_0MinvJt.dot(m_aJ);
  89. b3Assert(m_Adiag > b3Scalar(0.0));
  90. }
  91. b3Scalar getDiagonal() const { return m_Adiag; }
  92. // for two constraints on the same rigidbody (for example vehicle friction)
  93. b3Scalar getNonDiagonal(const b3JacobianEntry& jacB, const b3Scalar massInvA) const
  94. {
  95. const b3JacobianEntry& jacA = *this;
  96. b3Scalar lin = massInvA * jacA.m_linearJointAxis.dot(jacB.m_linearJointAxis);
  97. b3Scalar 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. b3Scalar getNonDiagonal(const b3JacobianEntry& jacB,const b3Scalar massInvA,const b3Scalar massInvB) const
  102. {
  103. const b3JacobianEntry& jacA = *this;
  104. b3Vector3 lin = jacA.m_linearJointAxis * jacB.m_linearJointAxis;
  105. b3Vector3 ang0 = jacA.m_0MinvJt * jacB.m_aJ;
  106. b3Vector3 ang1 = jacA.m_1MinvJt * jacB.m_bJ;
  107. b3Vector3 lin0 = massInvA * lin ;
  108. b3Vector3 lin1 = massInvB * lin;
  109. b3Vector3 sum = ang0+ang1+lin0+lin1;
  110. return sum[0]+sum[1]+sum[2];
  111. }
  112. b3Scalar getRelativeVelocity(const b3Vector3& linvelA,const b3Vector3& angvelA,const b3Vector3& linvelB,const b3Vector3& angvelB)
  113. {
  114. b3Vector3 linrel = linvelA - linvelB;
  115. b3Vector3 angvela = angvelA * m_aJ;
  116. b3Vector3 angvelb = angvelB * m_bJ;
  117. linrel *= m_linearJointAxis;
  118. angvela += angvelb;
  119. angvela += linrel;
  120. b3Scalar rel_vel2 = angvela[0]+angvela[1]+angvela[2];
  121. return rel_vel2 + B3_EPSILON;
  122. }
  123. //private:
  124. b3Vector3 m_linearJointAxis;
  125. b3Vector3 m_aJ;
  126. b3Vector3 m_bJ;
  127. b3Vector3 m_0MinvJt;
  128. b3Vector3 m_1MinvJt;
  129. //Optimization: can be stored in the w/last component of one of the vectors
  130. b3Scalar m_Adiag;
  131. };
  132. #endif //B3_JACOBIAN_ENTRY_H