2 Bullet Continuous Collision Detection and Physics Library
3 Copyright (c) 2018 Google Inc. http://bulletphysics.org
5 This software is provided 'as-is', without any express or implied warranty.
6 In no event will the authors be held liable for any damages arising from the use of this software.
7 Permission is granted to anyone to use this software for any purpose,
8 including commercial applications, and to alter it and redistribute it freely,
9 subject to the following restrictions:
11 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.
12 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
13 3. This notice may not be removed or altered from any source distribution.
16 #ifndef BT_MULTIBODY_MLCP_CONSTRAINT_SOLVER_H
17 #define BT_MULTIBODY_MLCP_CONSTRAINT_SOLVER_H
19 #include "LinearMath/btMatrixX.h"
20 #include "LinearMath/btThreads.h"
21 #include "BulletDynamics/Featherstone/btMultiBodyConstraintSolver.h"
23 class btMLCPSolverInterface;
26 class btMultiBodyMLCPConstraintSolver : public btMultiBodyConstraintSolver
29 /// \name MLCP Formulation for Rigid Bodies
32 /// A matrix in the MLCP formulation
35 /// b vector in the MLCP formulation.
38 /// Constraint impulse, which is an output of MLCP solving.
41 /// Lower bound of constraint impulse, \c m_x.
44 /// Upper bound of constraint impulse, \c m_x.
49 /// \name Cache Variables for Split Impulse for Rigid Bodies
50 /// When using 'split impulse' we solve two separate (M)LCPs
53 /// Split impulse Cache vector corresponding to \c m_b.
56 /// Split impulse cache vector corresponding to \c m_x.
61 /// \name MLCP Formulation for Multibodies
64 /// A matrix in the MLCP formulation
65 btMatrixXu m_multiBodyA;
67 /// b vector in the MLCP formulation.
68 btVectorXu m_multiBodyB;
70 /// Constraint impulse, which is an output of MLCP solving.
71 btVectorXu m_multiBodyX;
73 /// Lower bound of constraint impulse, \c m_x.
74 btVectorXu m_multiBodyLo;
76 /// Upper bound of constraint impulse, \c m_x.
77 btVectorXu m_multiBodyHi;
81 /// Indices of normal contact constraint associated with frictional contact constraint for rigid bodies.
83 /// This is used by the MLCP solver to update the upper bounds of frictional contact impulse given intermediate
84 /// normal contact impulse. For example, i-th element represents the index of a normal constraint that is
85 /// accosiated with i-th frictional contact constraint if i-th constraint is a frictional contact constraint.
87 btAlignedObjectArray<int> m_limitDependencies;
89 /// Indices of normal contact constraint associated with frictional contact constraint for multibodies.
91 /// This is used by the MLCP solver to update the upper bounds of frictional contact impulse given intermediate
92 /// normal contact impulse. For example, i-th element represents the index of a normal constraint that is
93 /// accosiated with i-th frictional contact constraint if i-th constraint is a frictional contact constraint.
95 btAlignedObjectArray<int> m_multiBodyLimitDependencies;
97 /// Array of all the rigid body constraints
98 btAlignedObjectArray<btSolverConstraint*> m_allConstraintPtrArray;
100 /// Array of all the multibody constraints
101 btAlignedObjectArray<btMultiBodySolverConstraint*> m_multiBodyAllConstraintPtrArray;
104 btMLCPSolverInterface* m_solver;
106 /// Count of fallbacks of using btSequentialImpulseConstraintSolver, which happens when the MLCP solver fails.
109 /// \name MLCP Scratch Variables
110 /// The following scratch variables are not stateful -- contents are cleared prior to each use.
111 /// They are only cached here to avoid extra memory allocations and deallocations and to ensure
112 /// that multiple instances of the solver can be run in parallel.
116 /// Cache variable for constraint Jacobian matrix.
117 btMatrixXu m_scratchJ3;
119 /// Cache variable for constraint Jacobian times inverse mass matrix.
120 btMatrixXu m_scratchJInvM3;
122 /// Cache variable for offsets.
123 btAlignedObjectArray<int> m_scratchOfs;
127 /// Constructs MLCP terms, which are \c m_A, \c m_b, \c m_lo, and \c m_hi.
128 virtual void createMLCPFast(const btContactSolverInfo& infoGlobal);
130 /// Constructs MLCP terms for constraints of two rigid bodies
131 void createMLCPFastRigidBody(const btContactSolverInfo& infoGlobal);
133 /// Constructs MLCP terms for constraints of two multi-bodies or one rigid body and one multibody
134 void createMLCPFastMultiBody(const btContactSolverInfo& infoGlobal);
136 /// Solves MLCP and returns the success
137 virtual bool solveMLCP(const btContactSolverInfo& infoGlobal);
139 // Documentation inherited
140 btScalar solveGroupCacheFriendlySetup(
141 btCollisionObject** bodies,
143 btPersistentManifold** manifoldPtr,
145 btTypedConstraint** constraints,
147 const btContactSolverInfo& infoGlobal,
148 btIDebugDraw* debugDrawer) BT_OVERRIDE;
150 // Documentation inherited
151 btScalar solveGroupCacheFriendlyIterations(
152 btCollisionObject** bodies,
154 btPersistentManifold** manifoldPtr,
156 btTypedConstraint** constraints,
158 const btContactSolverInfo& infoGlobal,
159 btIDebugDraw* debugDrawer) ;
162 BT_DECLARE_ALIGNED_ALLOCATOR()
166 /// \param[in] solver MLCP solver. Assumed it's not null.
167 /// \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.
168 explicit btMultiBodyMLCPConstraintSolver(btMLCPSolverInterface* solver);
171 virtual ~btMultiBodyMLCPConstraintSolver();
173 /// Sets MLCP solver. Assumed it's not null.
174 void setMLCPSolver(btMLCPSolverInterface* solver);
176 /// Returns the number of fallbacks of using btSequentialImpulseConstraintSolver, which happens when the MLCP
178 int getNumFallbacks() const;
180 /// Sets the number of fallbacks. This function may be used to reset the number to zero.
181 void setNumFallbacks(int num);
183 /// Returns the constraint solver type.
184 virtual btConstraintSolverType getSolverType() const;
187 #endif // BT_MULTIBODY_MLCP_CONSTRAINT_SOLVER_H