btSequentialImpulseConstraintSolver.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. Bullet Continuous Collision Detection and Physics Library
  3. Copyright (c) 2003-2006 Erwin Coumans https://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_SEQUENTIAL_IMPULSE_CONSTRAINT_SOLVER_H
  14. #define BT_SEQUENTIAL_IMPULSE_CONSTRAINT_SOLVER_H
  15. class btIDebugDraw;
  16. class btPersistentManifold;
  17. class btDispatcher;
  18. class btCollisionObject;
  19. #include "BulletDynamics/ConstraintSolver/btTypedConstraint.h"
  20. #include "BulletDynamics/ConstraintSolver/btContactSolverInfo.h"
  21. #include "BulletDynamics/ConstraintSolver/btSolverBody.h"
  22. #include "BulletDynamics/ConstraintSolver/btSolverConstraint.h"
  23. #include "BulletCollision/NarrowPhaseCollision/btManifoldPoint.h"
  24. #include "BulletDynamics/ConstraintSolver/btConstraintSolver.h"
  25. typedef btScalar (*btSingleConstraintRowSolver)(btSolverBody&, btSolverBody&, const btSolverConstraint&);
  26. struct btSolverAnalyticsData
  27. {
  28. btSolverAnalyticsData()
  29. {
  30. m_numSolverCalls = 0;
  31. m_numIterationsUsed = -1;
  32. m_remainingLeastSquaresResidual = -1;
  33. m_islandId = -2;
  34. }
  35. int m_islandId;
  36. int m_numBodies;
  37. int m_numContactManifolds;
  38. int m_numSolverCalls;
  39. int m_numIterationsUsed;
  40. double m_remainingLeastSquaresResidual;
  41. };
  42. ///The btSequentialImpulseConstraintSolver is a fast SIMD implementation of the Projected Gauss Seidel (iterative LCP) method.
  43. ATTRIBUTE_ALIGNED16(class)
  44. btSequentialImpulseConstraintSolver : public btConstraintSolver
  45. {
  46. protected:
  47. btAlignedObjectArray<btSolverBody> m_tmpSolverBodyPool;
  48. btConstraintArray m_tmpSolverContactConstraintPool;
  49. btConstraintArray m_tmpSolverNonContactConstraintPool;
  50. btConstraintArray m_tmpSolverContactFrictionConstraintPool;
  51. btConstraintArray m_tmpSolverContactRollingFrictionConstraintPool;
  52. btAlignedObjectArray<int> m_orderTmpConstraintPool;
  53. btAlignedObjectArray<int> m_orderNonContactConstraintPool;
  54. btAlignedObjectArray<int> m_orderFrictionConstraintPool;
  55. btAlignedObjectArray<btTypedConstraint::btConstraintInfo1> m_tmpConstraintSizesPool;
  56. int m_maxOverrideNumSolverIterations;
  57. int m_fixedBodyId;
  58. // When running solvers on multiple threads, a race condition exists for Kinematic objects that
  59. // participate in more than one solver.
  60. // The getOrInitSolverBody() function writes the companionId of each body (storing the index of the solver body
  61. // for the current solver). For normal dynamic bodies it isn't an issue because they can only be in one island
  62. // (and therefore one thread) at a time. But kinematic bodies can be in multiple islands at once.
  63. // To avoid this race condition, this solver does not write the companionId, instead it stores the solver body
  64. // index in this solver-local table, indexed by the uniqueId of the body.
  65. btAlignedObjectArray<int> m_kinematicBodyUniqueIdToSolverBodyTable; // only used for multithreading
  66. btSingleConstraintRowSolver m_resolveSingleConstraintRowGeneric;
  67. btSingleConstraintRowSolver m_resolveSingleConstraintRowLowerLimit;
  68. btSingleConstraintRowSolver m_resolveSplitPenetrationImpulse;
  69. int m_cachedSolverMode; // used to check if SOLVER_SIMD flag has been changed
  70. void setupSolverFunctions(bool useSimd);
  71. btScalar m_leastSquaresResidual;
  72. void setupFrictionConstraint(btSolverConstraint & solverConstraint, const btVector3& normalAxis, int solverBodyIdA, int solverBodyIdB,
  73. btManifoldPoint& cp, const btVector3& rel_pos1, const btVector3& rel_pos2,
  74. btCollisionObject* colObj0, btCollisionObject* colObj1, btScalar relaxation,
  75. const btContactSolverInfo& infoGlobal,
  76. btScalar desiredVelocity = 0., btScalar cfmSlip = 0.);
  77. void setupTorsionalFrictionConstraint(btSolverConstraint & solverConstraint, const btVector3& normalAxis, int solverBodyIdA, int solverBodyIdB,
  78. btManifoldPoint& cp, btScalar combinedTorsionalFriction, const btVector3& rel_pos1, const btVector3& rel_pos2,
  79. btCollisionObject* colObj0, btCollisionObject* colObj1, btScalar relaxation,
  80. btScalar desiredVelocity = 0., btScalar cfmSlip = 0.);
  81. btSolverConstraint& addFrictionConstraint(const btVector3& normalAxis, int solverBodyIdA, int solverBodyIdB, int frictionIndex, btManifoldPoint& cp, const btVector3& rel_pos1, const btVector3& rel_pos2, btCollisionObject* colObj0, btCollisionObject* colObj1, btScalar relaxation, const btContactSolverInfo& infoGlobal, btScalar desiredVelocity = 0., btScalar cfmSlip = 0.);
  82. btSolverConstraint& addTorsionalFrictionConstraint(const btVector3& normalAxis, int solverBodyIdA, int solverBodyIdB, int frictionIndex, btManifoldPoint& cp, btScalar torsionalFriction, const btVector3& rel_pos1, const btVector3& rel_pos2, btCollisionObject* colObj0, btCollisionObject* colObj1, btScalar relaxation, btScalar desiredVelocity = 0, btScalar cfmSlip = 0.f);
  83. void setupContactConstraint(btSolverConstraint & solverConstraint, int solverBodyIdA, int solverBodyIdB, btManifoldPoint& cp,
  84. const btContactSolverInfo& infoGlobal, btScalar& relaxation, const btVector3& rel_pos1, const btVector3& rel_pos2);
  85. static void applyAnisotropicFriction(btCollisionObject * colObj, btVector3 & frictionDirection, int frictionMode);
  86. void setFrictionConstraintImpulse(btSolverConstraint & solverConstraint, int solverBodyIdA, int solverBodyIdB,
  87. btManifoldPoint& cp, const btContactSolverInfo& infoGlobal);
  88. ///m_btSeed2 is used for re-arranging the constraint rows. improves convergence/quality of friction
  89. unsigned long m_btSeed2;
  90. btScalar restitutionCurve(btScalar rel_vel, btScalar restitution, btScalar velocityThreshold);
  91. virtual void convertContacts(btPersistentManifold * *manifoldPtr, int numManifolds, const btContactSolverInfo& infoGlobal);
  92. void convertContact(btPersistentManifold * manifold, const btContactSolverInfo& infoGlobal);
  93. virtual void convertJoints(btTypedConstraint * *constraints, int numConstraints, const btContactSolverInfo& infoGlobal);
  94. void convertJoint(btSolverConstraint * currentConstraintRow, btTypedConstraint * constraint, const btTypedConstraint::btConstraintInfo1& info1, int solverBodyIdA, int solverBodyIdB, const btContactSolverInfo& infoGlobal);
  95. virtual void convertBodies(btCollisionObject * *bodies, int numBodies, const btContactSolverInfo& infoGlobal);
  96. btScalar resolveSplitPenetrationSIMD(btSolverBody & bodyA, btSolverBody & bodyB, const btSolverConstraint& contactConstraint)
  97. {
  98. return m_resolveSplitPenetrationImpulse(bodyA, bodyB, contactConstraint);
  99. }
  100. btScalar resolveSplitPenetrationImpulseCacheFriendly(btSolverBody & bodyA, btSolverBody & bodyB, const btSolverConstraint& contactConstraint)
  101. {
  102. return m_resolveSplitPenetrationImpulse(bodyA, bodyB, contactConstraint);
  103. }
  104. //internal method
  105. int getOrInitSolverBody(btCollisionObject & body, btScalar timeStep);
  106. void initSolverBody(btSolverBody * solverBody, btCollisionObject * collisionObject, btScalar timeStep);
  107. btScalar resolveSingleConstraintRowGeneric(btSolverBody & bodyA, btSolverBody & bodyB, const btSolverConstraint& contactConstraint);
  108. btScalar resolveSingleConstraintRowGenericSIMD(btSolverBody & bodyA, btSolverBody & bodyB, const btSolverConstraint& contactConstraint);
  109. btScalar resolveSingleConstraintRowLowerLimit(btSolverBody & bodyA, btSolverBody & bodyB, const btSolverConstraint& contactConstraint);
  110. btScalar resolveSingleConstraintRowLowerLimitSIMD(btSolverBody & bodyA, btSolverBody & bodyB, const btSolverConstraint& contactConstraint);
  111. btScalar resolveSplitPenetrationImpulse(btSolverBody & bodyA, btSolverBody & bodyB, const btSolverConstraint& contactConstraint)
  112. {
  113. return m_resolveSplitPenetrationImpulse(bodyA, bodyB, contactConstraint);
  114. }
  115. protected:
  116. void writeBackContacts(int iBegin, int iEnd, const btContactSolverInfo& infoGlobal);
  117. void writeBackJoints(int iBegin, int iEnd, const btContactSolverInfo& infoGlobal);
  118. void writeBackBodies(int iBegin, int iEnd, const btContactSolverInfo& infoGlobal);
  119. virtual void solveGroupCacheFriendlySplitImpulseIterations(btCollisionObject * *bodies, int numBodies, btPersistentManifold** manifoldPtr, int numManifolds, btTypedConstraint** constraints, int numConstraints, const btContactSolverInfo& infoGlobal, btIDebugDraw* debugDrawer);
  120. virtual btScalar solveGroupCacheFriendlyFinish(btCollisionObject * *bodies, int numBodies, const btContactSolverInfo& infoGlobal);
  121. virtual btScalar solveSingleIteration(int iteration, btCollisionObject** bodies, int numBodies, btPersistentManifold** manifoldPtr, int numManifolds, btTypedConstraint** constraints, int numConstraints, const btContactSolverInfo& infoGlobal, btIDebugDraw* debugDrawer);
  122. virtual btScalar solveGroupCacheFriendlySetup(btCollisionObject * *bodies, int numBodies, btPersistentManifold** manifoldPtr, int numManifolds, btTypedConstraint** constraints, int numConstraints, const btContactSolverInfo& infoGlobal, btIDebugDraw* debugDrawer);
  123. virtual btScalar solveGroupCacheFriendlyIterations(btCollisionObject * *bodies, int numBodies, btPersistentManifold** manifoldPtr, int numManifolds, btTypedConstraint** constraints, int numConstraints, const btContactSolverInfo& infoGlobal, btIDebugDraw* debugDrawer);
  124. public:
  125. BT_DECLARE_ALIGNED_ALLOCATOR();
  126. btSequentialImpulseConstraintSolver();
  127. virtual ~btSequentialImpulseConstraintSolver();
  128. virtual btScalar solveGroup(btCollisionObject * *bodies, int numBodies, btPersistentManifold** manifold, int numManifolds, btTypedConstraint** constraints, int numConstraints, const btContactSolverInfo& info, btIDebugDraw* debugDrawer, btDispatcher* dispatcher);
  129. ///clear internal cached data and reset random seed
  130. virtual void reset();
  131. unsigned long btRand2();
  132. int btRandInt2(int n);
  133. void setRandSeed(unsigned long seed)
  134. {
  135. m_btSeed2 = seed;
  136. }
  137. unsigned long getRandSeed() const
  138. {
  139. return m_btSeed2;
  140. }
  141. virtual btConstraintSolverType getSolverType() const
  142. {
  143. return BT_SEQUENTIAL_IMPULSE_SOLVER;
  144. }
  145. btSingleConstraintRowSolver getActiveConstraintRowSolverGeneric()
  146. {
  147. return m_resolveSingleConstraintRowGeneric;
  148. }
  149. void setConstraintRowSolverGeneric(btSingleConstraintRowSolver rowSolver)
  150. {
  151. m_resolveSingleConstraintRowGeneric = rowSolver;
  152. }
  153. btSingleConstraintRowSolver getActiveConstraintRowSolverLowerLimit()
  154. {
  155. return m_resolveSingleConstraintRowLowerLimit;
  156. }
  157. void setConstraintRowSolverLowerLimit(btSingleConstraintRowSolver rowSolver)
  158. {
  159. m_resolveSingleConstraintRowLowerLimit = rowSolver;
  160. }
  161. ///Various implementations of solving a single constraint row using a generic equality constraint, using scalar reference, SSE2 or SSE4
  162. btSingleConstraintRowSolver getScalarConstraintRowSolverGeneric();
  163. btSingleConstraintRowSolver getSSE2ConstraintRowSolverGeneric();
  164. btSingleConstraintRowSolver getSSE4_1ConstraintRowSolverGeneric();
  165. ///Various implementations of solving a single constraint row using an inequality (lower limit) constraint, using scalar reference, SSE2 or SSE4
  166. btSingleConstraintRowSolver getScalarConstraintRowSolverLowerLimit();
  167. btSingleConstraintRowSolver getSSE2ConstraintRowSolverLowerLimit();
  168. btSingleConstraintRowSolver getSSE4_1ConstraintRowSolverLowerLimit();
  169. btSolverAnalyticsData m_analyticsData;
  170. };
  171. #endif //BT_SEQUENTIAL_IMPULSE_CONSTRAINT_SOLVER_H