Bullet Collision Detection & Physics Library
btDiscreteDynamicsWorldMt.cpp
Go to the documentation of this file.
1 /*
2 Bullet Continuous Collision Detection and Physics Library
3 Copyright (c) 2003-2009 Erwin Coumans http://bulletphysics.org
4 
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:
10 
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.
14 */
15 
16 
18 
19 //collision detection
26 #include "LinearMath/btQuickprof.h"
27 
28 //rigidbody & constraints
40 
41 
44 
45 
47 #include "LinearMath/btQuickprof.h"
49 
51 
52 
54 {
59 
61  btConstraintSolver* solver,
62  btStackAlloc* stackAlloc,
63  btDispatcher* dispatcher)
64  :m_solverInfo(NULL),
65  m_solver(solver),
66  m_debugDrawer(NULL),
67  m_dispatcher(dispatcher)
68  {
69 
70  }
71 
73  {
74  btAssert(0);
75  (void)other;
76  return *this;
77  }
78 
79  SIMD_FORCE_INLINE void setup ( btContactSolverInfo* solverInfo, btIDebugDraw* debugDrawer)
80  {
81  btAssert(solverInfo);
82  m_solverInfo = solverInfo;
83  m_debugDrawer = debugDrawer;
84  }
85 
86 
87  virtual void processIsland( btCollisionObject** bodies,
88  int numBodies,
89  btPersistentManifold** manifolds,
90  int numManifolds,
91  btTypedConstraint** constraints,
92  int numConstraints,
93  int islandId
94  )
95  {
96  m_solver->solveGroup( bodies,
97  numBodies,
98  manifolds,
99  numManifolds,
100  constraints,
101  numConstraints,
102  *m_solverInfo,
105  );
106  }
107 
108 };
109 
110 
114 
116 {
117  int i = 0;
118 #if BT_THREADSAFE
119  i = btGetCurrentThreadIndex() % m_solvers.size();
120 #endif // #if BT_THREADSAFE
121  while ( true )
122  {
123  ThreadSolver& solver = m_solvers[ i ];
124  if ( solver.mutex.tryLock() )
125  {
126  return &solver;
127  }
128  // failed, try the next one
129  i = ( i + 1 ) % m_solvers.size();
130  }
131  return NULL;
132 }
133 
134 void btConstraintSolverPoolMt::init( btConstraintSolver** solvers, int numSolvers )
135 {
137  m_solvers.resize( numSolvers );
138  for ( int i = 0; i < numSolvers; ++i )
139  {
140  m_solvers[ i ].solver = solvers[ i ];
141  }
142  if ( numSolvers > 0 )
143  {
144  m_solverType = solvers[ 0 ]->getSolverType();
145  }
146 }
147 
148 // create the solvers for me
150 {
152  solvers.reserve( numSolvers );
153  for ( int i = 0; i < numSolvers; ++i )
154  {
156  solvers.push_back( solver );
157  }
158  init( &solvers[ 0 ], numSolvers );
159 }
160 
161 // pass in fully constructed solvers (destructor will delete them)
163 {
164  init( solvers, numSolvers );
165 }
166 
168 {
169  // delete all solvers
170  for ( int i = 0; i < m_solvers.size(); ++i )
171  {
172  ThreadSolver& solver = m_solvers[ i ];
173  delete solver.solver;
174  solver.solver = NULL;
175  }
176 }
177 
180  int numBodies,
181  btPersistentManifold** manifolds,
182  int numManifolds,
183  btTypedConstraint** constraints,
184  int numConstraints,
185  const btContactSolverInfo& info,
186  btIDebugDraw* debugDrawer,
187  btDispatcher* dispatcher
188 )
189 {
191  ts->solver->solveGroup( bodies, numBodies, manifolds, numManifolds, constraints, numConstraints, info, debugDrawer, dispatcher );
192  ts->mutex.unlock();
193  return 0.0f;
194 }
195 
197 {
198  for ( int i = 0; i < m_solvers.size(); ++i )
199  {
200  ThreadSolver& solver = m_solvers[ i ];
201  solver.mutex.lock();
202  solver.solver->reset();
203  solver.mutex.unlock();
204  }
205 }
206 
207 
211 
213 : btDiscreteDynamicsWorld(dispatcher,pairCache,constraintSolver,collisionConfiguration)
214 {
216  {
219  }
220  {
221  void* mem = btAlignedAlloc(sizeof(InplaceSolverIslandCallbackMt),16);
223  }
224  {
225  void* mem = btAlignedAlloc(sizeof(btSimulationIslandManagerMt),16);
228  m_islandManager = im;
229  }
230 }
231 
232 
234 {
236  {
237  m_solverIslandCallbackMt->~InplaceSolverIslandCallbackMt();
239  }
241  {
244  }
245 }
246 
247 
249 {
250  BT_PROFILE("solveConstraints");
251 
254 
258 
260 }
261 
262 
264 {
267 
268  void forLoop( int iBegin, int iEnd ) const BT_OVERRIDE
269  {
270  for ( int i = iBegin; i < iEnd; ++i )
271  {
272  btRigidBody* body = rigidBodies[ i ];
273  if ( !body->isStaticOrKinematicObject() )
274  {
275  //don't integrate/update velocities here, it happens in the constraint solver
276  body->applyDamping( timeStep );
278  }
279  }
280  }
281 };
282 
283 
285 {
286  BT_PROFILE( "predictUnconstraintMotion" );
287  if ( m_nonStaticRigidBodies.size() > 0 )
288  {
290  update.timeStep = timeStep;
291  update.rigidBodies = &m_nonStaticRigidBodies[ 0 ];
292  int grainSize = 50; // num of iterations per task for task scheduler
293  btParallelFor( 0, m_nonStaticRigidBodies.size(), grainSize, update );
294  }
295 }
296 
297 
299 {
300  BT_PROFILE( "createPredictiveContacts" );
302  if ( m_nonStaticRigidBodies.size() > 0 )
303  {
305  update.world = this;
306  update.timeStep = timeStep;
307  update.rigidBodies = &m_nonStaticRigidBodies[ 0 ];
308  int grainSize = 50; // num of iterations per task for task scheduler
309  btParallelFor( 0, m_nonStaticRigidBodies.size(), grainSize, update );
310  }
311 }
312 
313 
315 {
316  BT_PROFILE( "integrateTransforms" );
317  if ( m_nonStaticRigidBodies.size() > 0 )
318  {
320  update.world = this;
321  update.timeStep = timeStep;
322  update.rigidBodies = &m_nonStaticRigidBodies[ 0 ];
323  int grainSize = 50; // num of iterations per task for task scheduler
324  btParallelFor( 0, m_nonStaticRigidBodies.size(), grainSize, update );
325  }
326 }
327 
virtual btScalar solveGroup(btCollisionObject **bodies, int numBodies, btPersistentManifold **manifolds, int numManifolds, btTypedConstraint **constraints, int numConstraints, const btContactSolverInfo &info, btIDebugDraw *debugDrawer, btDispatcher *dispatcher) BT_OVERRIDE
solve a group of constraints
btPersistentManifold is a contact point cache, it stays persistent as long as objects are overlapping...
void push_back(const T &_Val)
btConstraintSolverPoolMt - masquerades as a constraint solver, but really it is a threadsafe pool of ...
btDiscreteDynamicsWorldMt(btDispatcher *dispatcher, btBroadphaseInterface *pairCache, btConstraintSolverPoolMt *constraintSolver, btCollisionConfiguration *collisionConfiguration)
btDiscreteDynamicsWorldMt
btSimulationIslandManager * m_islandManager
btAlignedObjectArray< btRigidBody * > m_nonStaticRigidBodies
The btAlignedObjectArray template class uses a subset of the stl::vector interface for its methods It...
virtual void createPredictiveContacts(btScalar timeStep) BT_OVERRIDE
void predictIntegratedTransform(btScalar step, btTransform &predictedTransform)
continuous collision detection needs prediction
virtual void buildAndProcessIslands(btDispatcher *dispatcher, btCollisionWorld *collisionWorld, btAlignedObjectArray< btTypedConstraint *> &constraints, IslandCallback *callback)
virtual void solveConstraints(btContactSolverInfo &solverInfo) BT_OVERRIDE
bool tryLock()
Definition: btThreads.cpp:216
btDiscreteDynamicsWorld provides discrete rigid body simulation those classes replace the obsolete Cc...
#define btAssert(x)
Definition: btScalar.h:131
btCollisionConfiguration allows to configure Bullet collision detection stack allocator size...
virtual btScalar solveGroup(btCollisionObject **bodies, int numBodies, btPersistentManifold **manifold, int numManifolds, btTypedConstraint **constraints, int numConstraints, const btContactSolverInfo &info, class btIDebugDraw *debugDrawer, btDispatcher *dispatcher)=0
solve a group of constraints
#define SIMD_FORCE_INLINE
Definition: btScalar.h:81
void init(btConstraintSolver **solvers, int numSolvers)
InplaceSolverIslandCallbackMt(btConstraintSolver *solver, btStackAlloc *stackAlloc, btDispatcher *dispatcher)
virtual void reset() BT_OVERRIDE
clear internal cached data and reset random seed
virtual void predictUnconstraintMotion(btScalar timeStep) BT_OVERRIDE
bool isStaticOrKinematicObject() const
The StackAlloc class provides some fast stack-based memory allocator (LIFO last-in first-out) ...
Definition: btStackAlloc.h:34
btContactSolverInfo m_solverInfo
int getNumCollisionObjects() const
btCollisionWorld * getCollisionWorld()
#define BT_OVERRIDE
Definition: btThreads.h:28
btIDebugDraw * m_debugDrawer
virtual btIDebugDraw * getDebugDrawer()
void lock()
Definition: btThreads.cpp:206
ThreadSolver * getAndLockThreadSolver()
btConstraintSolverPoolMt
virtual void integrateTransforms(btScalar timeStep) BT_OVERRIDE
void unlock()
Definition: btThreads.cpp:211
#define btAlignedFree(ptr)
btCollisionObject can be used to manage collision detection objects.
The btIDebugDraw interface class allows hooking up a debug renderer to visually debug simulations...
Definition: btIDebugDraw.h:29
const btTransform & getInterpolationWorldTransform() const
The btRigidBody is the main class for rigid body objects.
Definition: btRigidBody.h:62
void setup(btContactSolverInfo *solverInfo, btIDebugDraw *debugDrawer)
The btBroadphaseInterface class provides an interface to detect aabb-overlapping object pairs...
btDispatcher * getDispatcher()
virtual btConstraintSolverType getSolverType() const =0
The btSequentialImpulseConstraintSolver is a fast SIMD implementation of the Projected Gauss Seidel (...
int size() const
return the number of elements in the array
#define BT_PROFILE(name)
Definition: btQuickprof.h:215
btAlignedObjectArray< ThreadSolver > m_solvers
virtual void allSolved(const btContactSolverInfo &, class btIDebugDraw *)
void applyDamping(btScalar timeStep)
applyDamping damps the velocity, using the given m_linearDamping and m_angularDamping ...
virtual void prepareSolve(int, int)
TypedConstraint is the baseclass for Bullet constraints and vehicles.
btConstraintSolverType m_solverType
#define btAlignedAlloc(size, alignment)
btConstraintSolver * m_constraintSolver
virtual void processIsland(btCollisionObject **bodies, int numBodies, btPersistentManifold **manifolds, int numManifolds, btTypedConstraint **constraints, int numConstraints, int islandId)
InplaceSolverIslandCallbackMt & operator=(InplaceSolverIslandCallbackMt &other)
void btParallelFor(int iBegin, int iEnd, int grainSize, const btIParallelForBody &body)
Definition: btThreads.cpp:429
unsigned int btGetCurrentThreadIndex()
Definition: btThreads.cpp:304
void forLoop(int iBegin, int iEnd) const BT_OVERRIDE
virtual void reset()=0
clear internal cached data and reset random seed
The btDispatcher interface class can be used in combination with broadphase to dispatch calculations ...
Definition: btDispatcher.h:75
float btScalar
The btScalar type abstracts floating point numbers, to easily switch between double and single floati...
Definition: btScalar.h:292
InplaceSolverIslandCallbackMt * m_solverIslandCallbackMt
SimulationIslandManagerMt – Multithread capable version of SimulationIslandManager Splits the world...
btAlignedObjectArray< btTypedConstraint * > m_constraints