Bullet Collision Detection & Physics Library: btSimpleDynamicsWorld.cpp Source File

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"
17 #include "BulletCollision/CollisionDispatch/btCollisionDispatcher.h"
18 #include "BulletCollision/BroadphaseCollision/btSimpleBroadphase.h"
19 #include "BulletCollision/CollisionShapes/btCollisionShape.h"
20 #include "BulletDynamics/Dynamics/btRigidBody.h"
21 #include "BulletDynamics/ConstraintSolver/btSequentialImpulseConstraintSolver.h"
22 #include "BulletDynamics/ConstraintSolver/btContactSolverInfo.h"
23
24 /*
25 Make sure this dummy function never changes so that it
26 can be used by probes that are checking whether the
27 library is actually installed.
28 */
29 extern "C"
30 {
31 void btBulletDynamicsProbe();
32 void btBulletDynamicsProbe() {}
33 }
34
35 btSimpleDynamicsWorld::btSimpleDynamicsWorld(btDispatcher* dispatcher, btBroadphaseInterface* pairCache, btConstraintSolver* constraintSolver, btCollisionConfiguration* collisionConfiguration)
36 : btDynamicsWorld(dispatcher, pairCache, collisionConfiguration),
37 m_constraintSolver(constraintSolver),
38 m_ownsConstraintSolver(false),
39 m_gravity(0, 0, -10)
40 {
41 }
42
43 btSimpleDynamicsWorld::~btSimpleDynamicsWorld()
44 {
45 if (m_ownsConstraintSolver)
46 btAlignedFree(m_constraintSolver);
47 }
48
49 int btSimpleDynamicsWorld::stepSimulation(btScalar timeStep, int maxSubSteps, btScalar fixedTimeStep)
50 {
51 (void)fixedTimeStep;
52 (void)maxSubSteps;
53
55 predictUnconstraintMotion(timeStep);
56
57 btDispatcherInfo& dispatchInfo = getDispatchInfo();
58 dispatchInfo.m_timeStep = timeStep;
59 dispatchInfo.m_stepCount = 0;
60 dispatchInfo.m_debugDraw = getDebugDrawer();
61
63 performDiscreteCollisionDetection();
64
66 int numManifolds = m_dispatcher1->getNumManifolds();
67 if (numManifolds)
68 {
69 btPersistentManifold** manifoldPtr = ((btCollisionDispatcher*)m_dispatcher1)->getInternalManifoldPointer();
70
71 btContactSolverInfo infoGlobal;
72 infoGlobal.m_timeStep = timeStep;
73 m_constraintSolver->prepareSolve(0, numManifolds);
74 m_constraintSolver->solveGroup(&getCollisionObjectArray()[0], getNumCollisionObjects(), manifoldPtr, numManifolds, 0, 0, infoGlobal, m_debugDrawer, m_dispatcher1);
75 m_constraintSolver->allSolved(infoGlobal, m_debugDrawer);
76 }
77
79 integrateTransforms(timeStep);
80
81 updateAabbs();
82
83 synchronizeMotionStates();
84
85 clearForces();
86
87 return 1;
88 }
89
90 void btSimpleDynamicsWorld::clearForces()
91 {
93 for (int i = 0; i < m_collisionObjects.size(); i++)
94 {
95 btCollisionObject* colObj = m_collisionObjects[i];
96
97 btRigidBody* body = btRigidBody::upcast(colObj);
98 if (body)
99 {
100 body->clearForces();
101 }
102 }
103 }
104
105 void btSimpleDynamicsWorld::setGravity(const btVector3& gravity)
106 {
107 m_gravity = gravity;
108 for (int i = 0; i < m_collisionObjects.size(); i++)
109 {
110 btCollisionObject* colObj = m_collisionObjects[i];
111 btRigidBody* body = btRigidBody::upcast(colObj);
112 if (body)
113 {
114 body->setGravity(gravity);
115 }
116 }
117 }
118
119 btVector3 btSimpleDynamicsWorld::getGravity() const
120 {
121 return m_gravity;
122 }
123
124 void btSimpleDynamicsWorld::removeRigidBody(btRigidBody* body)
125 {
126 btCollisionWorld::removeCollisionObject(body);
127 }
128
129 void btSimpleDynamicsWorld::removeCollisionObject(btCollisionObject* collisionObject)
130 {
131 btRigidBody* body = btRigidBody::upcast(collisionObject);
132 if (body)
133 removeRigidBody(body);
134 else
135 btCollisionWorld::removeCollisionObject(collisionObject);
136 }
137
138 void btSimpleDynamicsWorld::addRigidBody(btRigidBody* body)
139 {
140 body->setGravity(m_gravity);
141
142 if (body->getCollisionShape())
143 {
144 addCollisionObject(body);
145 }
146 }
147
148 void btSimpleDynamicsWorld::addRigidBody(btRigidBody* body, int group, int mask)
149 {
150 body->setGravity(m_gravity);
151
152 if (body->getCollisionShape())
153 {
154 addCollisionObject(body, group, mask);
155 }
156 }
157
158 void btSimpleDynamicsWorld::debugDrawWorld()
159 {
160 }
161
162 void btSimpleDynamicsWorld::addAction(btActionInterface* action)
163 {
164 }
165
166 void btSimpleDynamicsWorld::removeAction(btActionInterface* action)
167 {
168 }
169
170 void btSimpleDynamicsWorld::updateAabbs()
171 {
172 btTransform predictedTrans;
173 for (int i = 0; i < m_collisionObjects.size(); i++)
174 {
175 btCollisionObject* colObj = m_collisionObjects[i];
176 btRigidBody* body = btRigidBody::upcast(colObj);
177 if (body)
178 {
179 if (body->isActive() && (!body->isStaticObject()))
180 {
181 btVector3 minAabb, maxAabb;
182 colObj->getCollisionShape()->getAabb(colObj->getWorldTransform(), minAabb, maxAabb);
183 btBroadphaseInterface* bp = getBroadphase();
184 bp->setAabb(body->getBroadphaseHandle(), minAabb, maxAabb, m_dispatcher1);
185 }
186 }
187 }
188 }
189
190 void btSimpleDynamicsWorld::integrateTransforms(btScalar timeStep)
191 {
192 btTransform predictedTrans;
193 for (int i = 0; i < m_collisionObjects.size(); i++)
194 {
195 btCollisionObject* colObj = m_collisionObjects[i];
196 btRigidBody* body = btRigidBody::upcast(colObj);
197 if (body)
198 {
199 if (body->isActive() && (!body->isStaticObject()))
200 {
201 body->predictIntegratedTransform(timeStep, predictedTrans);
202 body->proceedToTransform(predictedTrans);
203 }
204 }
205 }
206 }
207
208 void btSimpleDynamicsWorld::predictUnconstraintMotion(btScalar timeStep)
209 {
210 for (int i = 0; i < m_collisionObjects.size(); i++)
211 {
212 btCollisionObject* colObj = m_collisionObjects[i];
213 btRigidBody* body = btRigidBody::upcast(colObj);
214 if (body)
215 {
216 if (!body->isStaticObject())
217 {
218 if (body->isActive())
219 {
220 body->applyGravity();
221 body->integrateVelocities(timeStep);
222 body->applyDamping(timeStep);
223 body->predictIntegratedTransform(timeStep, body->getInterpolationWorldTransform());
224 }
225 }
226 }
227 }
228 }
229
230 void btSimpleDynamicsWorld::synchronizeMotionStates()
231 {
233 for (int i = 0; i < m_collisionObjects.size(); i++)
234 {
235 btCollisionObject* colObj = m_collisionObjects[i];
236 btRigidBody* body = btRigidBody::upcast(colObj);
237 if (body && body->getMotionState())
238 {
239 if (body->getActivationState() != ISLAND_SLEEPING)
240 {
241 body->getMotionState()->setWorldTransform(body->getWorldTransform());
242 }
243 }
244 }
245 }
246
247 void btSimpleDynamicsWorld::setConstraintSolver(btConstraintSolver* solver)
248 {
249 if (m_ownsConstraintSolver)
250 {
251 btAlignedFree(m_constraintSolver);
252 }
253 m_ownsConstraintSolver = false;
254 m_constraintSolver = solver;
255 }
256
257 btConstraintSolver* btSimpleDynamicsWorld::getConstraintSolver()
258 {
259 return m_constraintSolver;
260 }
btAlignedFree
#define btAlignedFree(ptr)
Definition: btAlignedAllocator.h:47
btCollisionDispatcher.h
ISLAND_SLEEPING
#define ISLAND_SLEEPING
Definition: btCollisionObject.h:23
btCollisionShape.h
btContactSolverInfo.h
btRigidBody.h
btScalar
float btScalar
The btScalar type abstracts floating point numbers, to easily switch between double and single floati...
Definition: btScalar.h:314
btSequentialImpulseConstraintSolver.h
btSimpleBroadphase.h
btBulletDynamicsProbe
void btBulletDynamicsProbe()
Definition: btSimpleDynamicsWorld.cpp:32
btSimpleDynamicsWorld.h
btActionInterface
Basic interface to allow actions such as vehicles and characters to be updated inside a btDynamicsWor...
Definition: btActionInterface.h:27
btAlignedObjectArray::size
int size() const
return the number of elements in the array
Definition: btAlignedObjectArray.h:142
btBroadphaseInterface
The btBroadphaseInterface class provides an interface to detect aabb-overlapping object pairs.
Definition: btBroadphaseInterface.h:50
btBroadphaseInterface::setAabb
virtual void setAabb(btBroadphaseProxy *proxy, const btVector3 &aabbMin, const btVector3 &aabbMax, btDispatcher *dispatcher)=0
btCollisionConfiguration
btCollisionConfiguration allows to configure Bullet collision detection stack allocator size,...
Definition: btCollisionConfiguration.h:27
btCollisionDispatcher
btCollisionDispatcher supports algorithms that handle ConvexConvex and ConvexConcave collision pairs.
Definition: btCollisionDispatcher.h:43
btCollisionObject
btCollisionObject can be used to manage collision detection objects.
Definition: btCollisionObject.h:51
btCollisionObject::getInterpolationWorldTransform
const btTransform & getInterpolationWorldTransform() const
Definition: btCollisionObject.h:410
btCollisionObject::getWorldTransform
btTransform & getWorldTransform()
Definition: btCollisionObject.h:379
btCollisionObject::getBroadphaseHandle
btBroadphaseProxy * getBroadphaseHandle()
Definition: btCollisionObject.h:395
btCollisionObject::isActive
bool isActive() const
Definition: btCollisionObject.h:306
btCollisionObject::isStaticObject
bool isStaticObject() const
Definition: btCollisionObject.h:197
btCollisionObject::getActivationState
int getActivationState() const
Definition: btCollisionObject.h:289
btCollisionObject::getCollisionShape
const btCollisionShape * getCollisionShape() const
Definition: btCollisionObject.h:228
btCollisionShape::getAabb
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.
btCollisionWorld::getBroadphase
const btBroadphaseInterface * getBroadphase() const
Definition: btCollisionWorld.h:117
btCollisionWorld::getCollisionObjectArray
btCollisionObjectArray & getCollisionObjectArray()
Definition: btCollisionWorld.h:476
btCollisionWorld::removeCollisionObject
virtual void removeCollisionObject(btCollisionObject *collisionObject)
Definition: btCollisionWorld.cpp:239
btCollisionWorld::addCollisionObject
virtual void addCollisionObject(btCollisionObject *collisionObject, int collisionFilterGroup=btBroadphaseProxy::DefaultFilter, int collisionFilterMask=btBroadphaseProxy::AllFilter)
Definition: btCollisionWorld.cpp:124
btCollisionWorld::m_collisionObjects
btAlignedObjectArray< btCollisionObject * > m_collisionObjects
Definition: btCollisionWorld.h:88
btCollisionWorld::getDebugDrawer
virtual btIDebugDraw * getDebugDrawer()
Definition: btCollisionWorld.h:155
btCollisionWorld::getNumCollisionObjects
int getNumCollisionObjects() const
Definition: btCollisionWorld.h:427
btCollisionWorld::performDiscreteCollisionDetection
virtual void performDiscreteCollisionDetection()
Definition: btCollisionWorld.cpp:221
btCollisionWorld::m_debugDrawer
btIDebugDraw * m_debugDrawer
Definition: btCollisionWorld.h:96
btCollisionWorld::getDispatchInfo
btDispatcherInfo & getDispatchInfo()
Definition: btCollisionWorld.h:490
btCollisionWorld::m_dispatcher1
btDispatcher * m_dispatcher1
Definition: btCollisionWorld.h:90
btConstraintSolver
Definition: btConstraintSolver.h:42
btConstraintSolver::allSolved
virtual void allSolved(const btContactSolverInfo &, class btIDebugDraw *)
Definition: btConstraintSolver.h:51
btConstraintSolver::prepareSolve
virtual void prepareSolve(int, int)
Definition: btConstraintSolver.h:46
btConstraintSolver::solveGroup
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
btDispatcher
The btDispatcher interface class can be used in combination with broadphase to dispatch calculations ...
Definition: btDispatcher.h:77
btDispatcher::getNumManifolds
virtual int getNumManifolds() const =0
btDynamicsWorld
The btDynamicsWorld is the interface class for several dynamics implementation, basic,...
Definition: btDynamicsWorld.h:43
btMotionState::setWorldTransform
virtual void setWorldTransform(const btTransform &worldTrans)=0
btPersistentManifold
btPersistentManifold is a contact point cache, it stays persistent as long as objects are overlapping...
Definition: btPersistentManifold.h:65
btRigidBody
The btRigidBody is the main class for rigid body objects.
Definition: btRigidBody.h:60
btRigidBody::applyGravity
void applyGravity()
Definition: btRigidBody.cpp:205
btRigidBody::integrateVelocities
void integrateVelocities(btScalar step)
Definition: btRigidBody.cpp:372
btRigidBody::clearForces
void clearForces()
Definition: btRigidBody.h:415
btRigidBody::getMotionState
btMotionState * getMotionState()
Definition: btRigidBody.h:549
btRigidBody::applyDamping
void applyDamping(btScalar timeStep)
applyDamping damps the velocity, using the given m_linearDamping and m_angularDamping
Definition: btRigidBody.cpp:149
btRigidBody::upcast
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:189
btRigidBody::setGravity
void setGravity(const btVector3 &acceleration)
Definition: btRigidBody.cpp:128
btRigidBody::proceedToTransform
void proceedToTransform(const btTransform &newTrans)
Definition: btRigidBody.cpp:221
btRigidBody::getCollisionShape
const btCollisionShape * getCollisionShape() const
Definition: btRigidBody.h:242
btRigidBody::predictIntegratedTransform
void predictIntegratedTransform(btScalar step, btTransform &predictedTransform)
continuous collision detection needs prediction
Definition: btRigidBody.cpp:100
btSimpleDynamicsWorld::m_ownsConstraintSolver
bool m_ownsConstraintSolver
Definition: btSimpleDynamicsWorld.h:32
btSimpleDynamicsWorld::m_constraintSolver
btConstraintSolver * m_constraintSolver
Definition: btSimpleDynamicsWorld.h:30
btSimpleDynamicsWorld::getConstraintSolver
virtual btConstraintSolver * getConstraintSolver()
Definition: btSimpleDynamicsWorld.cpp:257
btSimpleDynamicsWorld::btSimpleDynamicsWorld
btSimpleDynamicsWorld(btDispatcher *dispatcher, btBroadphaseInterface *pairCache, btConstraintSolver *constraintSolver, btCollisionConfiguration *collisionConfiguration)
this btSimpleDynamicsWorld constructor creates dispatcher, broadphase pairCache and constraintSolver
Definition: btSimpleDynamicsWorld.cpp:35
btSimpleDynamicsWorld::addRigidBody
virtual void addRigidBody(btRigidBody *body)
Definition: btSimpleDynamicsWorld.cpp:138
btSimpleDynamicsWorld::removeCollisionObject
virtual void removeCollisionObject(btCollisionObject *collisionObject)
removeCollisionObject will first check if it is a rigid body, if so call removeRigidBody otherwise ca...
Definition: btSimpleDynamicsWorld.cpp:129
btSimpleDynamicsWorld::updateAabbs
virtual void updateAabbs()
Definition: btSimpleDynamicsWorld.cpp:170
btSimpleDynamicsWorld::setConstraintSolver
virtual void setConstraintSolver(btConstraintSolver *solver)
Definition: btSimpleDynamicsWorld.cpp:247
btSimpleDynamicsWorld::synchronizeMotionStates
virtual void synchronizeMotionStates()
Definition: btSimpleDynamicsWorld.cpp:230
btSimpleDynamicsWorld::removeAction
virtual void removeAction(btActionInterface *action)
Definition: btSimpleDynamicsWorld.cpp:166
btSimpleDynamicsWorld::integrateTransforms
void integrateTransforms(btScalar timeStep)
Definition: btSimpleDynamicsWorld.cpp:190
btSimpleDynamicsWorld::debugDrawWorld
virtual void debugDrawWorld()
Definition: btSimpleDynamicsWorld.cpp:158
btSimpleDynamicsWorld::removeRigidBody
virtual void removeRigidBody(btRigidBody *body)
Definition: btSimpleDynamicsWorld.cpp:124
btSimpleDynamicsWorld::getGravity
virtual btVector3 getGravity() const
Definition: btSimpleDynamicsWorld.cpp:119
btSimpleDynamicsWorld::clearForces
virtual void clearForces()
Definition: btSimpleDynamicsWorld.cpp:90
btSimpleDynamicsWorld::predictUnconstraintMotion
void predictUnconstraintMotion(btScalar timeStep)
Definition: btSimpleDynamicsWorld.cpp:208
btSimpleDynamicsWorld::setGravity
virtual void setGravity(const btVector3 &gravity)
Definition: btSimpleDynamicsWorld.cpp:105
btSimpleDynamicsWorld::addAction
virtual void addAction(btActionInterface *action)
Definition: btSimpleDynamicsWorld.cpp:162
btSimpleDynamicsWorld::~btSimpleDynamicsWorld
virtual ~btSimpleDynamicsWorld()
Definition: btSimpleDynamicsWorld.cpp:43
btSimpleDynamicsWorld::stepSimulation
virtual int stepSimulation(btScalar timeStep, int maxSubSteps=1, btScalar fixedTimeStep=btScalar(1.)/btScalar(60.))
maxSubSteps/fixedTimeStep for interpolation is currently ignored for btSimpleDynamicsWorld,...
Definition: btSimpleDynamicsWorld.cpp:49
btSimpleDynamicsWorld::m_gravity
btVector3 m_gravity
Definition: btSimpleDynamicsWorld.h:38
btTransform
The btTransform class supports rigid transforms with only translation and rotation and no scaling/she...
Definition: btTransform.h:30
btVector3
btVector3 can be used to represent 3D points and vectors.
Definition: btVector3.h:82
btContactSolverInfoData::m_timeStep
btScalar m_timeStep
Definition: btContactSolverInfo.h:42
btContactSolverInfo
Definition: btContactSolverInfo.h:76
btDispatcherInfo
Definition: btDispatcher.h:31
btDispatcherInfo::m_timeStep
btScalar m_timeStep
Definition: btDispatcher.h:53
btDispatcherInfo::m_stepCount
int m_stepCount
Definition: btDispatcher.h:54
btDispatcherInfo::m_debugDraw
class btIDebugDraw * m_debugDraw
Definition: btDispatcher.h:58