Bullet Collision Detection & Physics Library
btSimpleDynamicsWorld.cpp
Go to the documentation of this file.
1 /*
2 Bullet Continuous Collision Detection and Physics Library
3 Copyright (c) 2003-2006 Erwin Coumans http://continuousphysics.com/Bullet/
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 #include "btSimpleDynamicsWorld.h"
23 
24 
25 /*
26  Make sure this dummy function never changes so that it
27  can be used by probes that are checking whether the
28  library is actually installed.
29 */
30 extern "C"
31 {
32  void btBulletDynamicsProbe ();
34 }
35 
36 
37 
38 
40 :btDynamicsWorld(dispatcher,pairCache,collisionConfiguration),
41 m_constraintSolver(constraintSolver),
42 m_ownsConstraintSolver(false),
43 m_gravity(0,0,-10)
44 {
45 
46 }
47 
48 
50 {
53 }
54 
55 int btSimpleDynamicsWorld::stepSimulation( btScalar timeStep,int maxSubSteps, btScalar fixedTimeStep)
56 {
57  (void)fixedTimeStep;
58  (void)maxSubSteps;
59 
60 
62  predictUnconstraintMotion(timeStep);
63 
64  btDispatcherInfo& dispatchInfo = getDispatchInfo();
65  dispatchInfo.m_timeStep = timeStep;
66  dispatchInfo.m_stepCount = 0;
67  dispatchInfo.m_debugDraw = getDebugDrawer();
68 
71 
73  int numManifolds = m_dispatcher1->getNumManifolds();
74  if (numManifolds)
75  {
76  btPersistentManifold** manifoldPtr = ((btCollisionDispatcher*)m_dispatcher1)->getInternalManifoldPointer();
77 
78  btContactSolverInfo infoGlobal;
79  infoGlobal.m_timeStep = timeStep;
80  m_constraintSolver->prepareSolve(0,numManifolds);
83  }
84 
86  integrateTransforms(timeStep);
87 
88  updateAabbs();
89 
91 
92  clearForces();
93 
94  return 1;
95 
96 }
97 
99 {
101  for ( int i=0;i<m_collisionObjects.size();i++)
102  {
104 
105  btRigidBody* body = btRigidBody::upcast(colObj);
106  if (body)
107  {
108  body->clearForces();
109  }
110  }
111 }
112 
113 
115 {
116  m_gravity = gravity;
117  for ( int i=0;i<m_collisionObjects.size();i++)
118  {
120  btRigidBody* body = btRigidBody::upcast(colObj);
121  if (body)
122  {
123  body->setGravity(gravity);
124  }
125  }
126 }
127 
129 {
130  return m_gravity;
131 }
132 
134 {
136 }
137 
139 {
140  btRigidBody* body = btRigidBody::upcast(collisionObject);
141  if (body)
142  removeRigidBody(body);
143  else
145 }
146 
147 
149 {
150  body->setGravity(m_gravity);
151 
152  if (body->getCollisionShape())
153  {
154  addCollisionObject(body);
155  }
156 }
157 
158 void btSimpleDynamicsWorld::addRigidBody(btRigidBody* body, short group, short mask)
159 {
160  body->setGravity(m_gravity);
161 
162  if (body->getCollisionShape())
163  {
164  addCollisionObject(body,group,mask);
165  }
166 }
167 
168 
170 {
171 
172 }
173 
175 {
176 
177 }
178 
180 {
181 
182 }
183 
184 
186 {
187  btTransform predictedTrans;
188  for ( int i=0;i<m_collisionObjects.size();i++)
189  {
191  btRigidBody* body = btRigidBody::upcast(colObj);
192  if (body)
193  {
194  if (body->isActive() && (!body->isStaticObject()))
195  {
196  btVector3 minAabb,maxAabb;
197  colObj->getCollisionShape()->getAabb(colObj->getWorldTransform(), minAabb,maxAabb);
199  bp->setAabb(body->getBroadphaseHandle(),minAabb,maxAabb, m_dispatcher1);
200  }
201  }
202  }
203 }
204 
206 {
207  btTransform predictedTrans;
208  for ( int i=0;i<m_collisionObjects.size();i++)
209  {
211  btRigidBody* body = btRigidBody::upcast(colObj);
212  if (body)
213  {
214  if (body->isActive() && (!body->isStaticObject()))
215  {
216  body->predictIntegratedTransform(timeStep, predictedTrans);
217  body->proceedToTransform( predictedTrans);
218  }
219  }
220  }
221 }
222 
223 
224 
226 {
227  for ( int i=0;i<m_collisionObjects.size();i++)
228  {
230  btRigidBody* body = btRigidBody::upcast(colObj);
231  if (body)
232  {
233  if (!body->isStaticObject())
234  {
235  if (body->isActive())
236  {
237  body->applyGravity();
238  body->integrateVelocities( timeStep);
239  body->applyDamping(timeStep);
241  }
242  }
243  }
244  }
245 }
246 
247 
249 {
251  for ( int i=0;i<m_collisionObjects.size();i++)
252  {
254  btRigidBody* body = btRigidBody::upcast(colObj);
255  if (body && body->getMotionState())
256  {
257  if (body->getActivationState() != ISLAND_SLEEPING)
258  {
260  }
261  }
262  }
263 
264 }
265 
266 
268 {
270  {
272  }
273  m_ownsConstraintSolver = false;
274  m_constraintSolver = solver;
275 }
276 
278 {
279  return m_constraintSolver;
280 }
const btCollisionShape * getCollisionShape() const
Definition: btRigidBody.h:248
static const btRigidBody * upcast(const btCollisionObject *colObj)
to keep collision detection and dynamics separate we don't store a rigidbody pointer but a rigidbody ...
Definition: btRigidBody.h:197
virtual int stepSimulation(btScalar timeStep, int maxSubSteps=1, btScalar fixedTimeStep=btScalar(1.)/btScalar(60.))
maxSubSteps/fixedTimeStep for interpolation is currently ignored for btSimpleDynamicsWorld, use btDiscreteDynamicsWorld instead
btPersistentManifold is a contact point cache, it stays persistent as long as objects are overlapping...
void applyGravity()
void integrateTransforms(btScalar timeStep)
void btBulletDynamicsProbe()
void predictIntegratedTransform(btScalar step, btTransform &predictedTransform)
continuous collision detection needs prediction
virtual void addCollisionObject(btCollisionObject *collisionObject, short int collisionFilterGroup=btBroadphaseProxy::DefaultFilter, short int collisionFilterMask=btBroadphaseProxy::AllFilter)
The btDynamicsWorld is the interface class for several dynamics implementation, basic, discrete, parallel, and continuous etc.
btCollisionConfiguration allows to configure Bullet collision detection stack allocator size...
virtual void removeRigidBody(btRigidBody *body)
btCollisionObjectArray & getCollisionObjectArray()
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
int getNumCollisionObjects() const
btCollisionDispatcher supports algorithms that handle ConvexConvex and ConvexConcave collision pairs...
Basic interface to allow actions such as vehicles and characters to be updated inside a btDynamicsWor...
virtual btVector3 getGravity() const
btDispatcher * m_dispatcher1
class btIDebugDraw * m_debugDraw
Definition: btDispatcher.h:58
#define ISLAND_SLEEPING
virtual void getAabb(const btTransform &t, btVector3 &aabbMin, btVector3 &aabbMax) const =0
getAabb returns the axis aligned bounding box in the coordinate frame of the given transform t...
virtual void removeCollisionObject(btCollisionObject *collisionObject)
removeCollisionObject will first check if it is a rigid body, if so call removeRigidBody otherwise ca...
virtual void synchronizeMotionStates()
const btTransform & getInterpolationWorldTransform() const
void integrateVelocities(btScalar step)
btTransform & getWorldTransform()
int size() const
return the number of elements in the array
btBroadphaseProxy * getBroadphaseHandle()
btIDebugDraw * m_debugDrawer
virtual void removeAction(btActionInterface *action)
virtual btIDebugDraw * getDebugDrawer()
bool isStaticObject() const
#define btAlignedFree(ptr)
btCollisionObject can be used to manage collision detection objects.
void predictUnconstraintMotion(btScalar timeStep)
The btRigidBody is the main class for rigid body objects.
Definition: btRigidBody.h:59
virtual void removeCollisionObject(btCollisionObject *collisionObject)
void clearForces()
Definition: btRigidBody.h:340
The btBroadphaseInterface class provides an interface to detect aabb-overlapping object pairs...
void proceedToTransform(const btTransform &newTrans)
btVector3 can be used to represent 3D points and vectors.
Definition: btVector3.h:83
virtual int getNumManifolds() const =0
const btBroadphaseInterface * getBroadphase() const
The btTransform class supports rigid transforms with only translation and rotation and no scaling/she...
Definition: btTransform.h:34
virtual void allSolved(const btContactSolverInfo &, class btIDebugDraw *)
void applyDamping(btScalar timeStep)
applyDamping damps the velocity, using the given m_linearDamping and m_angularDamping ...
virtual btConstraintSolver * getConstraintSolver()
btDispatcherInfo & getDispatchInfo()
virtual void prepareSolve(int, int)
virtual void setConstraintSolver(btConstraintSolver *solver)
btAlignedObjectArray< btCollisionObject * > m_collisionObjects
virtual void performDiscreteCollisionDetection()
virtual void setAabb(btBroadphaseProxy *proxy, const btVector3 &aabbMin, const btVector3 &aabbMax, btDispatcher *dispatcher)=0
virtual void setGravity(const btVector3 &gravity)
btMotionState * getMotionState()
Definition: btRigidBody.h:468
btSimpleDynamicsWorld(btDispatcher *dispatcher, btBroadphaseInterface *pairCache, btConstraintSolver *constraintSolver, btCollisionConfiguration *collisionConfiguration)
this btSimpleDynamicsWorld constructor creates dispatcher, broadphase pairCache and constraintSolver ...
btScalar m_timeStep
Definition: btDispatcher.h:53
virtual void addRigidBody(btRigidBody *body)
The btDispatcher interface class can be used in combination with broadphase to dispatch calculations ...
Definition: btDispatcher.h:69
btConstraintSolver * m_constraintSolver
const btCollisionShape * getCollisionShape() const
virtual void addAction(btActionInterface *action)
int getActivationState() const
virtual void setWorldTransform(const btTransform &worldTrans)=0
float btScalar
The btScalar type abstracts floating point numbers, to easily switch between double and single floati...
Definition: btScalar.h:266
bool isActive() const
void setGravity(const btVector3 &acceleration)