Bullet Collision Detection & Physics Library: btSequentialImpulseConstraintSolver.h Source File

Bullet Collision Detection & Physics Library

btSequentialImpulseConstraintSolver.h
Go to the documentation of this file.
1/*
2Bullet Continuous Collision Detection and Physics Library
3Copyright (c) 2003-2006 Erwin Coumans https://bulletphysics.org
4
5This software is provided 'as-is', without any express or implied warranty.
6In no event will the authors be held liable for any damages arising from the use of this software.
7Permission is granted to anyone to use this software for any purpose,
8including commercial applications, and to alter it and redistribute it freely,
9subject to the following restrictions:
10
111. 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.
122. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
133. This notice may not be removed or altered from any source distribution.
14*/
15
16#ifndef BT_SEQUENTIAL_IMPULSE_CONSTRAINT_SOLVER_H
17#define BT_SEQUENTIAL_IMPULSE_CONSTRAINT_SOLVER_H
18
19class btIDebugDraw;
20class btPersistentManifold;
21class btDispatcher;
22class btCollisionObject;
23#include "BulletDynamics/ConstraintSolver/btTypedConstraint.h"
24#include "BulletDynamics/ConstraintSolver/btContactSolverInfo.h"
25#include "BulletDynamics/ConstraintSolver/btSolverBody.h"
26#include "BulletDynamics/ConstraintSolver/btSolverConstraint.h"
27#include "BulletCollision/NarrowPhaseCollision/btManifoldPoint.h"
28#include "BulletDynamics/ConstraintSolver/btConstraintSolver.h"
29
30 typedef btScalar (*btSingleConstraintRowSolver)(btSolverBody&, btSolverBody&, const btSolverConstraint&);
31
32 struct btSolverAnalyticsData
33{
34 btSolverAnalyticsData()
35 {
36 m_numSolverCalls = 0;
37 m_numIterationsUsed = -1;
38 m_remainingLeastSquaresResidual = -1;
39 m_islandId = -2;
40 }
41 int m_islandId;
42 int m_numBodies;
43 int m_numContactManifolds;
44 int m_numSolverCalls;
45 int m_numIterationsUsed;
46 double m_remainingLeastSquaresResidual;
47};
48
50 ATTRIBUTE_ALIGNED16(class)
51 btSequentialImpulseConstraintSolver : public btConstraintSolver
52{
53
54
55protected:
56 btAlignedObjectArray<btSolverBody> m_tmpSolverBodyPool;
57 btConstraintArray m_tmpSolverContactConstraintPool;
58 btConstraintArray m_tmpSolverNonContactConstraintPool;
59 btConstraintArray m_tmpSolverContactFrictionConstraintPool;
60 btConstraintArray m_tmpSolverContactRollingFrictionConstraintPool;
61
62 btAlignedObjectArray<int> m_orderTmpConstraintPool;
63 btAlignedObjectArray<int> m_orderNonContactConstraintPool;
64 btAlignedObjectArray<int> m_orderFrictionConstraintPool;
65 btAlignedObjectArray<btTypedConstraint::btConstraintInfo1> m_tmpConstraintSizesPool;
66 int m_maxOverrideNumSolverIterations;
67 int m_fixedBodyId;
68 // When running solvers on multiple threads, a race condition exists for Kinematic objects that
69 // participate in more than one solver.
70 // The getOrInitSolverBody() function writes the companionId of each body (storing the index of the solver body
71 // for the current solver). For normal dynamic bodies it isn't an issue because they can only be in one island
72 // (and therefore one thread) at a time. But kinematic bodies can be in multiple islands at once.
73 // To avoid this race condition, this solver does not write the companionId, instead it stores the solver body
74 // index in this solver-local table, indexed by the uniqueId of the body.
75 btAlignedObjectArray<int> m_kinematicBodyUniqueIdToSolverBodyTable; // only used for multithreading
76
77 btSingleConstraintRowSolver m_resolveSingleConstraintRowGeneric;
78 btSingleConstraintRowSolver m_resolveSingleConstraintRowLowerLimit;
79 btSingleConstraintRowSolver m_resolveSplitPenetrationImpulse;
80 int m_cachedSolverMode; // used to check if SOLVER_SIMD flag has been changed
81 void setupSolverFunctions(bool useSimd);
82
83 btScalar m_leastSquaresResidual;
84
85 void setupFrictionConstraint(btSolverConstraint & solverConstraint, const btVector3& normalAxis, int solverBodyIdA, int solverBodyIdB,
86 btManifoldPoint& cp, const btVector3& rel_pos1, const btVector3& rel_pos2,
87 btCollisionObject* colObj0, btCollisionObject* colObj1, btScalar relaxation,
88 const btContactSolverInfo& infoGlobal,
89 btScalar desiredVelocity = 0., btScalar cfmSlip = 0.);
90
91 void setupTorsionalFrictionConstraint(btSolverConstraint & solverConstraint, const btVector3& normalAxis, int solverBodyIdA, int solverBodyIdB,
92 btManifoldPoint& cp, btScalar combinedTorsionalFriction, const btVector3& rel_pos1, const btVector3& rel_pos2,
93 btCollisionObject* colObj0, btCollisionObject* colObj1, btScalar relaxation,
94 btScalar desiredVelocity = 0., btScalar cfmSlip = 0.);
95
96 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.);
97 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);
98
99 void setupContactConstraint(btSolverConstraint & solverConstraint, int solverBodyIdA, int solverBodyIdB, btManifoldPoint& cp,
100 const btContactSolverInfo& infoGlobal, btScalar& relaxation, const btVector3& rel_pos1, const btVector3& rel_pos2);
101
102 static void applyAnisotropicFriction(btCollisionObject * colObj, btVector3 & frictionDirection, int frictionMode);
103
104 void setFrictionConstraintImpulse(btSolverConstraint & solverConstraint, int solverBodyIdA, int solverBodyIdB,
105 btManifoldPoint& cp, const btContactSolverInfo& infoGlobal);
106
108 unsigned long m_btSeed2;
109
110 btScalar restitutionCurve(btScalar rel_vel, btScalar restitution, btScalar velocityThreshold);
111
112 virtual void convertContacts(btPersistentManifold * *manifoldPtr, int numManifolds, const btContactSolverInfo& infoGlobal);
113
114 void convertContact(btPersistentManifold * manifold, const btContactSolverInfo& infoGlobal);
115
116 virtual void convertJoints(btTypedConstraint * *constraints, int numConstraints, const btContactSolverInfo& infoGlobal);
117 void convertJoint(btSolverConstraint * currentConstraintRow, btTypedConstraint * constraint, const btTypedConstraint::btConstraintInfo1& info1, int solverBodyIdA, int solverBodyIdB, const btContactSolverInfo& infoGlobal);
118
119 virtual void convertBodies(btCollisionObject * *bodies, int numBodies, const btContactSolverInfo& infoGlobal);
120
121 btScalar resolveSplitPenetrationSIMD(btSolverBody & bodyA, btSolverBody & bodyB, const btSolverConstraint& contactConstraint)
122 {
123 return m_resolveSplitPenetrationImpulse(bodyA, bodyB, contactConstraint);
124 }
125
126 btScalar resolveSplitPenetrationImpulseCacheFriendly(btSolverBody & bodyA, btSolverBody & bodyB, const btSolverConstraint& contactConstraint)
127 {
128 return m_resolveSplitPenetrationImpulse(bodyA, bodyB, contactConstraint);
129 }
130
131 //internal method
132 int getOrInitSolverBody(btCollisionObject & body, btScalar timeStep);
133 void initSolverBody(btSolverBody * solverBody, btCollisionObject * collisionObject, btScalar timeStep);
134
135 btScalar resolveSingleConstraintRowGeneric(btSolverBody & bodyA, btSolverBody & bodyB, const btSolverConstraint& contactConstraint);
136 btScalar resolveSingleConstraintRowGenericSIMD(btSolverBody & bodyA, btSolverBody & bodyB, const btSolverConstraint& contactConstraint);
137 btScalar resolveSingleConstraintRowLowerLimit(btSolverBody & bodyA, btSolverBody & bodyB, const btSolverConstraint& contactConstraint);
138 btScalar resolveSingleConstraintRowLowerLimitSIMD(btSolverBody & bodyA, btSolverBody & bodyB, const btSolverConstraint& contactConstraint);
139 btScalar resolveSplitPenetrationImpulse(btSolverBody & bodyA, btSolverBody & bodyB, const btSolverConstraint& contactConstraint)
140 {
141 return m_resolveSplitPenetrationImpulse(bodyA, bodyB, contactConstraint);
142 }
143
144protected:
145 void writeBackContacts(int iBegin, int iEnd, const btContactSolverInfo& infoGlobal);
146 void writeBackJoints(int iBegin, int iEnd, const btContactSolverInfo& infoGlobal);
147 void writeBackBodies(int iBegin, int iEnd, const btContactSolverInfo& infoGlobal);
148 virtual void solveGroupCacheFriendlySplitImpulseIterations(btCollisionObject * *bodies, int numBodies, btPersistentManifold** manifoldPtr, int numManifolds, btTypedConstraint** constraints, int numConstraints, const btContactSolverInfo& infoGlobal, btIDebugDraw* debugDrawer);
149 virtual btScalar solveGroupCacheFriendlyFinish(btCollisionObject * *bodies, int numBodies, const btContactSolverInfo& infoGlobal);
150 virtual btScalar solveSingleIteration(int iteration, btCollisionObject** bodies, int numBodies, btPersistentManifold** manifoldPtr, int numManifolds, btTypedConstraint** constraints, int numConstraints, const btContactSolverInfo& infoGlobal, btIDebugDraw* debugDrawer);
151
152 virtual btScalar solveGroupCacheFriendlySetup(btCollisionObject * *bodies, int numBodies, btPersistentManifold** manifoldPtr, int numManifolds, btTypedConstraint** constraints, int numConstraints, const btContactSolverInfo& infoGlobal, btIDebugDraw* debugDrawer);
153 virtual btScalar solveGroupCacheFriendlyIterations(btCollisionObject * *bodies, int numBodies, btPersistentManifold** manifoldPtr, int numManifolds, btTypedConstraint** constraints, int numConstraints, const btContactSolverInfo& infoGlobal, btIDebugDraw* debugDrawer);
154
155public:
156 BT_DECLARE_ALIGNED_ALLOCATOR();
157
158 btSequentialImpulseConstraintSolver();
159 virtual ~btSequentialImpulseConstraintSolver();
160
161 virtual btScalar solveGroup(btCollisionObject * *bodies, int numBodies, btPersistentManifold** manifold, int numManifolds, btTypedConstraint** constraints, int numConstraints, const btContactSolverInfo& info, btIDebugDraw* debugDrawer, btDispatcher* dispatcher);
162
164 virtual void reset();
165
166 unsigned long btRand2();
167
168 int btRandInt2(int n);
169
170 void setRandSeed(unsigned long seed)
171 {
172 m_btSeed2 = seed;
173 }
174 unsigned long getRandSeed() const
175 {
176 return m_btSeed2;
177 }
178
179 virtual btConstraintSolverType getSolverType() const
180 {
181 return BT_SEQUENTIAL_IMPULSE_SOLVER;
182 }
183
184 btSingleConstraintRowSolver getActiveConstraintRowSolverGeneric()
185 {
186 return m_resolveSingleConstraintRowGeneric;
187 }
188 void setConstraintRowSolverGeneric(btSingleConstraintRowSolver rowSolver)
189 {
190 m_resolveSingleConstraintRowGeneric = rowSolver;
191 }
192 btSingleConstraintRowSolver getActiveConstraintRowSolverLowerLimit()
193 {
194 return m_resolveSingleConstraintRowLowerLimit;
195 }
196 void setConstraintRowSolverLowerLimit(btSingleConstraintRowSolver rowSolver)
197 {
198 m_resolveSingleConstraintRowLowerLimit = rowSolver;
199 }
200
201
202
204 btSingleConstraintRowSolver getScalarConstraintRowSolverGeneric();
205 btSingleConstraintRowSolver getSSE2ConstraintRowSolverGeneric();
206 btSingleConstraintRowSolver getSSE4_1ConstraintRowSolverGeneric();
207
209 btSingleConstraintRowSolver getScalarConstraintRowSolverLowerLimit();
210 btSingleConstraintRowSolver getSSE2ConstraintRowSolverLowerLimit();
211 btSingleConstraintRowSolver getSSE4_1ConstraintRowSolverLowerLimit();
212 btSolverAnalyticsData m_analyticsData;
213};
214
215#endif //BT_SEQUENTIAL_IMPULSE_CONSTRAINT_SOLVER_H
btConstraintSolver.h
btConstraintSolverType
btConstraintSolverType
btConstraintSolver provides solver interface
Definition: btConstraintSolver.h:33
BT_SEQUENTIAL_IMPULSE_SOLVER
@ BT_SEQUENTIAL_IMPULSE_SOLVER
Definition: btConstraintSolver.h:34
btContactSolverInfo.h
btManifoldPoint.h
btScalar
float btScalar
The btScalar type abstracts floating point numbers, to easily switch between double and single floati...
Definition: btScalar.h:314
ATTRIBUTE_ALIGNED16
#define ATTRIBUTE_ALIGNED16(a)
Definition: btScalar.h:99
btSingleConstraintRowSolver
btScalar(* btSingleConstraintRowSolver)(btSolverBody &, btSolverBody &, const btSolverConstraint &)
Definition: btSequentialImpulseConstraintSolver.h:30
seed
static unsigned long seed
Definition: btSoftBody.h:39
btSolverBody.h
btSolverConstraint.h
btTypedConstraint.h
btAlignedObjectArray< btSolverBody >
btCollisionObject
btCollisionObject can be used to manage collision detection objects.
Definition: btCollisionObject.h:51
btConstraintSolver
Definition: btConstraintSolver.h:42
btDispatcher
The btDispatcher interface class can be used in combination with broadphase to dispatch calculations ...
Definition: btDispatcher.h:77
btIDebugDraw
The btIDebugDraw interface class allows hooking up a debug renderer to visually debug simulations.
Definition: btIDebugDraw.h:27
btManifoldPoint
ManifoldContactPoint collects and maintains persistent contactpoints.
Definition: btManifoldPoint.h:52
btPersistentManifold
btPersistentManifold is a contact point cache, it stays persistent as long as objects are overlapping...
Definition: btPersistentManifold.h:65
btSequentialImpulseConstraintSolver
The btSequentialImpulseConstraintSolver is a fast SIMD implementation of the Projected Gauss Seidel (...
Definition: btSequentialImpulseConstraintSolver.h:52
btSequentialImpulseConstraintSolver::getSSE2ConstraintRowSolverGeneric
btSingleConstraintRowSolver getSSE2ConstraintRowSolverGeneric()
btSequentialImpulseConstraintSolver::m_tmpSolverContactConstraintPool
btConstraintArray m_tmpSolverContactConstraintPool
Definition: btSequentialImpulseConstraintSolver.h:57
btSequentialImpulseConstraintSolver::m_maxOverrideNumSolverIterations
int m_maxOverrideNumSolverIterations
Definition: btSequentialImpulseConstraintSolver.h:66
btSequentialImpulseConstraintSolver::m_tmpSolverContactFrictionConstraintPool
btConstraintArray m_tmpSolverContactFrictionConstraintPool
Definition: btSequentialImpulseConstraintSolver.h:59
btSequentialImpulseConstraintSolver::m_orderFrictionConstraintPool
btAlignedObjectArray< int > m_orderFrictionConstraintPool
Definition: btSequentialImpulseConstraintSolver.h:64
btSequentialImpulseConstraintSolver::getActiveConstraintRowSolverLowerLimit
btSingleConstraintRowSolver getActiveConstraintRowSolverLowerLimit()
Definition: btSequentialImpulseConstraintSolver.h:192
btSequentialImpulseConstraintSolver::getActiveConstraintRowSolverGeneric
btSingleConstraintRowSolver getActiveConstraintRowSolverGeneric()
Definition: btSequentialImpulseConstraintSolver.h:184
btSequentialImpulseConstraintSolver::getRandSeed
unsigned long getRandSeed() const
Definition: btSequentialImpulseConstraintSolver.h:174
btSequentialImpulseConstraintSolver::getSolverType
virtual btConstraintSolverType getSolverType() const
Definition: btSequentialImpulseConstraintSolver.h:179
btSequentialImpulseConstraintSolver::m_analyticsData
btSolverAnalyticsData m_analyticsData
Definition: btSequentialImpulseConstraintSolver.h:212
btSequentialImpulseConstraintSolver::resolveSplitPenetrationSIMD
btScalar resolveSplitPenetrationSIMD(btSolverBody &bodyA, btSolverBody &bodyB, const btSolverConstraint &contactConstraint)
Definition: btSequentialImpulseConstraintSolver.h:121
btSequentialImpulseConstraintSolver::m_btSeed2
unsigned long m_btSeed2
m_btSeed2 is used for re-arranging the constraint rows. improves convergence/quality of friction
Definition: btSequentialImpulseConstraintSolver.h:108
btSequentialImpulseConstraintSolver::getSSE2ConstraintRowSolverLowerLimit
btSingleConstraintRowSolver getSSE2ConstraintRowSolverLowerLimit()
btSequentialImpulseConstraintSolver::setRandSeed
void setRandSeed(unsigned long seed)
Definition: btSequentialImpulseConstraintSolver.h:170
btSequentialImpulseConstraintSolver::m_leastSquaresResidual
btScalar m_leastSquaresResidual
Definition: btSequentialImpulseConstraintSolver.h:83
btSequentialImpulseConstraintSolver::m_fixedBodyId
int m_fixedBodyId
Definition: btSequentialImpulseConstraintSolver.h:67
btSequentialImpulseConstraintSolver::m_tmpSolverBodyPool
btAlignedObjectArray< btSolverBody > m_tmpSolverBodyPool
Definition: btSequentialImpulseConstraintSolver.h:56
btSequentialImpulseConstraintSolver::m_resolveSingleConstraintRowLowerLimit
btSingleConstraintRowSolver m_resolveSingleConstraintRowLowerLimit
Definition: btSequentialImpulseConstraintSolver.h:78
btSequentialImpulseConstraintSolver::BT_DECLARE_ALIGNED_ALLOCATOR
BT_DECLARE_ALIGNED_ALLOCATOR()
btSequentialImpulseConstraintSolver::m_resolveSplitPenetrationImpulse
btSingleConstraintRowSolver m_resolveSplitPenetrationImpulse
Definition: btSequentialImpulseConstraintSolver.h:79
btSequentialImpulseConstraintSolver::m_tmpConstraintSizesPool
btAlignedObjectArray< btTypedConstraint::btConstraintInfo1 > m_tmpConstraintSizesPool
Definition: btSequentialImpulseConstraintSolver.h:65
btSequentialImpulseConstraintSolver::m_orderTmpConstraintPool
btAlignedObjectArray< int > m_orderTmpConstraintPool
Definition: btSequentialImpulseConstraintSolver.h:62
btSequentialImpulseConstraintSolver::m_orderNonContactConstraintPool
btAlignedObjectArray< int > m_orderNonContactConstraintPool
Definition: btSequentialImpulseConstraintSolver.h:63
btSequentialImpulseConstraintSolver::getSSE4_1ConstraintRowSolverGeneric
btSingleConstraintRowSolver getSSE4_1ConstraintRowSolverGeneric()
btSequentialImpulseConstraintSolver::setConstraintRowSolverGeneric
void setConstraintRowSolverGeneric(btSingleConstraintRowSolver rowSolver)
Definition: btSequentialImpulseConstraintSolver.h:188
btSequentialImpulseConstraintSolver::m_kinematicBodyUniqueIdToSolverBodyTable
btAlignedObjectArray< int > m_kinematicBodyUniqueIdToSolverBodyTable
Definition: btSequentialImpulseConstraintSolver.h:75
btSequentialImpulseConstraintSolver::m_tmpSolverNonContactConstraintPool
btConstraintArray m_tmpSolverNonContactConstraintPool
Definition: btSequentialImpulseConstraintSolver.h:58
btSequentialImpulseConstraintSolver::m_cachedSolverMode
int m_cachedSolverMode
Definition: btSequentialImpulseConstraintSolver.h:80
btSequentialImpulseConstraintSolver::m_resolveSingleConstraintRowGeneric
btSingleConstraintRowSolver m_resolveSingleConstraintRowGeneric
Definition: btSequentialImpulseConstraintSolver.h:77
btSequentialImpulseConstraintSolver::m_tmpSolverContactRollingFrictionConstraintPool
btConstraintArray m_tmpSolverContactRollingFrictionConstraintPool
Definition: btSequentialImpulseConstraintSolver.h:60
btSequentialImpulseConstraintSolver::resolveSplitPenetrationImpulseCacheFriendly
btScalar resolveSplitPenetrationImpulseCacheFriendly(btSolverBody &bodyA, btSolverBody &bodyB, const btSolverConstraint &contactConstraint)
Definition: btSequentialImpulseConstraintSolver.h:126
btSequentialImpulseConstraintSolver::setConstraintRowSolverLowerLimit
void setConstraintRowSolverLowerLimit(btSingleConstraintRowSolver rowSolver)
Definition: btSequentialImpulseConstraintSolver.h:196
btSequentialImpulseConstraintSolver::getSSE4_1ConstraintRowSolverLowerLimit
btSingleConstraintRowSolver getSSE4_1ConstraintRowSolverLowerLimit()
btSequentialImpulseConstraintSolver::resolveSplitPenetrationImpulse
btScalar resolveSplitPenetrationImpulse(btSolverBody &bodyA, btSolverBody &bodyB, const btSolverConstraint &contactConstraint)
Definition: btSequentialImpulseConstraintSolver.h:139
btTypedConstraint
TypedConstraint is the baseclass for Bullet constraints and vehicles.
Definition: btTypedConstraint.h:76
btVector3
btVector3 can be used to represent 3D points and vectors.
Definition: btVector3.h:82
btContactSolverInfo
Definition: btContactSolverInfo.h:76
btSolverAnalyticsData
Definition: btSequentialImpulseConstraintSolver.h:33
btSolverAnalyticsData::m_numIterationsUsed
int m_numIterationsUsed
Definition: btSequentialImpulseConstraintSolver.h:45
btSolverAnalyticsData::btSolverAnalyticsData
btSolverAnalyticsData()
Definition: btSequentialImpulseConstraintSolver.h:34
btSolverAnalyticsData::m_numContactManifolds
int m_numContactManifolds
Definition: btSequentialImpulseConstraintSolver.h:43
btSolverAnalyticsData::m_numSolverCalls
int m_numSolverCalls
Definition: btSequentialImpulseConstraintSolver.h:44
btSolverAnalyticsData::m_numBodies
int m_numBodies
Definition: btSequentialImpulseConstraintSolver.h:42
btSolverAnalyticsData::m_islandId
int m_islandId
Definition: btSequentialImpulseConstraintSolver.h:41
btSolverAnalyticsData::m_remainingLeastSquaresResidual
double m_remainingLeastSquaresResidual
Definition: btSequentialImpulseConstraintSolver.h:46
btSolverBody
The btSolverBody is an internal datastructure for the constraint solver. Only necessary data is packe...
Definition: btSolverBody.h:105
btSolverConstraint
1D constraint along a normal axis between bodyA and bodyB. It can be combined to solve contact and fr...
Definition: btSolverConstraint.h:31
btTypedConstraint::btConstraintInfo1
Definition: btTypedConstraint.h:114