btMultiBodyMLCPConstraintSolver.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. Bullet Continuous Collision Detection and Physics Library
  3. Copyright (c) 2018 Google Inc. 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_MLCP_CONSTRAINT_SOLVER_H
  14. #define BT_MULTIBODY_MLCP_CONSTRAINT_SOLVER_H
  15. #include "LinearMath/btMatrixX.h"
  16. #include "LinearMath/btThreads.h"
  17. #include "BulletDynamics/Featherstone/btMultiBodyConstraintSolver.h"
  18. class btMLCPSolverInterface;
  19. class btMultiBody;
  20. class btMultiBodyMLCPConstraintSolver : public btMultiBodyConstraintSolver
  21. {
  22. protected:
  23. /// \name MLCP Formulation for Rigid Bodies
  24. /// \{
  25. /// A matrix in the MLCP formulation
  26. btMatrixXu m_A;
  27. /// b vector in the MLCP formulation.
  28. btVectorXu m_b;
  29. /// Constraint impulse, which is an output of MLCP solving.
  30. btVectorXu m_x;
  31. /// Lower bound of constraint impulse, \c m_x.
  32. btVectorXu m_lo;
  33. /// Upper bound of constraint impulse, \c m_x.
  34. btVectorXu m_hi;
  35. /// \}
  36. /// \name Cache Variables for Split Impulse for Rigid Bodies
  37. /// When using 'split impulse' we solve two separate (M)LCPs
  38. /// \{
  39. /// Split impulse Cache vector corresponding to \c m_b.
  40. btVectorXu m_bSplit;
  41. /// Split impulse cache vector corresponding to \c m_x.
  42. btVectorXu m_xSplit;
  43. /// \}
  44. /// \name MLCP Formulation for Multibodies
  45. /// \{
  46. /// A matrix in the MLCP formulation
  47. btMatrixXu m_multiBodyA;
  48. /// b vector in the MLCP formulation.
  49. btVectorXu m_multiBodyB;
  50. /// Constraint impulse, which is an output of MLCP solving.
  51. btVectorXu m_multiBodyX;
  52. /// Lower bound of constraint impulse, \c m_x.
  53. btVectorXu m_multiBodyLo;
  54. /// Upper bound of constraint impulse, \c m_x.
  55. btVectorXu m_multiBodyHi;
  56. /// \}
  57. /// Indices of normal contact constraint associated with frictional contact constraint for rigid bodies.
  58. ///
  59. /// This is used by the MLCP solver to update the upper bounds of frictional contact impulse given intermediate
  60. /// normal contact impulse. For example, i-th element represents the index of a normal constraint that is
  61. /// accosiated with i-th frictional contact constraint if i-th constraint is a frictional contact constraint.
  62. /// Otherwise, -1.
  63. btAlignedObjectArray<int> m_limitDependencies;
  64. /// Indices of normal contact constraint associated with frictional contact constraint for multibodies.
  65. ///
  66. /// This is used by the MLCP solver to update the upper bounds of frictional contact impulse given intermediate
  67. /// normal contact impulse. For example, i-th element represents the index of a normal constraint that is
  68. /// accosiated with i-th frictional contact constraint if i-th constraint is a frictional contact constraint.
  69. /// Otherwise, -1.
  70. btAlignedObjectArray<int> m_multiBodyLimitDependencies;
  71. /// Array of all the rigid body constraints
  72. btAlignedObjectArray<btSolverConstraint*> m_allConstraintPtrArray;
  73. /// Array of all the multibody constraints
  74. btAlignedObjectArray<btMultiBodySolverConstraint*> m_multiBodyAllConstraintPtrArray;
  75. /// MLCP solver
  76. btMLCPSolverInterface* m_solver;
  77. /// Count of fallbacks of using btSequentialImpulseConstraintSolver, which happens when the MLCP solver fails.
  78. int m_fallback;
  79. /// \name MLCP Scratch Variables
  80. /// The following scratch variables are not stateful -- contents are cleared prior to each use.
  81. /// They are only cached here to avoid extra memory allocations and deallocations and to ensure
  82. /// that multiple instances of the solver can be run in parallel.
  83. ///
  84. /// \{
  85. /// Cache variable for constraint Jacobian matrix.
  86. btMatrixXu m_scratchJ3;
  87. /// Cache variable for constraint Jacobian times inverse mass matrix.
  88. btMatrixXu m_scratchJInvM3;
  89. /// Cache variable for offsets.
  90. btAlignedObjectArray<int> m_scratchOfs;
  91. /// \}
  92. /// Constructs MLCP terms, which are \c m_A, \c m_b, \c m_lo, and \c m_hi.
  93. virtual void createMLCPFast(const btContactSolverInfo& infoGlobal);
  94. /// Constructs MLCP terms for constraints of two rigid bodies
  95. void createMLCPFastRigidBody(const btContactSolverInfo& infoGlobal);
  96. /// Constructs MLCP terms for constraints of two multi-bodies or one rigid body and one multibody
  97. void createMLCPFastMultiBody(const btContactSolverInfo& infoGlobal);
  98. /// Solves MLCP and returns the success
  99. virtual bool solveMLCP(const btContactSolverInfo& infoGlobal);
  100. // Documentation inherited
  101. btScalar solveGroupCacheFriendlySetup(
  102. btCollisionObject** bodies,
  103. int numBodies,
  104. btPersistentManifold** manifoldPtr,
  105. int numManifolds,
  106. btTypedConstraint** constraints,
  107. int numConstraints,
  108. const btContactSolverInfo& infoGlobal,
  109. btIDebugDraw* debugDrawer) BT_OVERRIDE;
  110. // Documentation inherited
  111. btScalar solveGroupCacheFriendlyIterations(
  112. btCollisionObject** bodies,
  113. int numBodies,
  114. btPersistentManifold** manifoldPtr,
  115. int numManifolds,
  116. btTypedConstraint** constraints,
  117. int numConstraints,
  118. const btContactSolverInfo& infoGlobal,
  119. btIDebugDraw* debugDrawer) ;
  120. public:
  121. BT_DECLARE_ALIGNED_ALLOCATOR()
  122. /// Constructor
  123. ///
  124. /// \param[in] solver MLCP solver. Assumed it's not null.
  125. /// \param[in] maxLCPSize Maximum size of LCP to solve using MLCP solver. If the MLCP size exceeds this number, sequaltial impulse method will be used.
  126. explicit btMultiBodyMLCPConstraintSolver(btMLCPSolverInterface* solver);
  127. /// Destructor
  128. virtual ~btMultiBodyMLCPConstraintSolver();
  129. /// Sets MLCP solver. Assumed it's not null.
  130. void setMLCPSolver(btMLCPSolverInterface* solver);
  131. /// Returns the number of fallbacks of using btSequentialImpulseConstraintSolver, which happens when the MLCP
  132. /// solver fails.
  133. int getNumFallbacks() const;
  134. /// Sets the number of fallbacks. This function may be used to reset the number to zero.
  135. void setNumFallbacks(int num);
  136. /// Returns the constraint solver type.
  137. virtual btConstraintSolverType getSolverType() const;
  138. };
  139. #endif // BT_MULTIBODY_MLCP_CONSTRAINT_SOLVER_H