Bullet Collision Detection & Physics Library
btSimpleDynamicsWorld.cpp
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
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*/
29extern "C"
30{
33}
34
36 : btDynamicsWorld(dispatcher, pairCache, collisionConfiguration),
37 m_constraintSolver(constraintSolver),
38 m_ownsConstraintSolver(false),
39 m_gravity(0, 0, -10)
40{
41}
42
44{
47}
48
49int btSimpleDynamicsWorld::stepSimulation(btScalar timeStep, int maxSubSteps, btScalar fixedTimeStep)
50{
51 (void)fixedTimeStep;
52 (void)maxSubSteps;
53
56
57 btDispatcherInfo& dispatchInfo = getDispatchInfo();
58 dispatchInfo.m_timeStep = timeStep;
59 dispatchInfo.m_stepCount = 0;
60 dispatchInfo.m_debugDraw = getDebugDrawer();
61
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);
76 }
77
79 integrateTransforms(timeStep);
80
82
84
86
87 return 1;
88}
89
91{
93 for (int i = 0; i < m_collisionObjects.size(); i++)
94 {
96
97 btRigidBody* body = btRigidBody::upcast(colObj);
98 if (body)
99 {
100 body->clearForces();
101 }
102 }
103}
104
106{
107 m_gravity = gravity;
108 for (int i = 0; i < m_collisionObjects.size(); i++)
109 {
111 btRigidBody* body = btRigidBody::upcast(colObj);
112 if (body)
113 {
114 body->setGravity(gravity);
115 }
116 }
117}
118
120{
121 return m_gravity;
122}
123
125{
127}
128
130{
131 btRigidBody* body = btRigidBody::upcast(collisionObject);
132 if (body)
133 removeRigidBody(body);
134 else
136}
137
139{
140 body->setGravity(m_gravity);
141
142 if (body->getCollisionShape())
143 {
144 addCollisionObject(body);
145 }
146}
147
148void 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
159{
160}
161
163{
164}
165
167{
168}
169
171{
172 btTransform predictedTrans;
173 for (int i = 0; i < m_collisionObjects.size(); i++)
174 {
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);
184 bp->setAabb(body->getBroadphaseHandle(), minAabb, maxAabb, m_dispatcher1);
185 }
186 }
187 }
188}
189
191{
192 btTransform predictedTrans;
193 for (int i = 0; i < m_collisionObjects.size(); i++)
194 {
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
209{
210 for (int i = 0; i < m_collisionObjects.size(); i++)
211 {
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);
224 }
225 }
226 }
227 }
228}
229
231{
233 for (int i = 0; i < m_collisionObjects.size(); i++)
234 {
236 btRigidBody* body = btRigidBody::upcast(colObj);
237 if (body && body->getMotionState())
238 {
239 if (body->getActivationState() != ISLAND_SLEEPING)
240 {
242 }
243 }
244 }
245}
246
248{
250 {
252 }
254 m_constraintSolver = solver;
255}
256
258{
259 return m_constraintSolver;
260}
#define btAlignedFree(ptr)
#define ISLAND_SLEEPING
float btScalar
The btScalar type abstracts floating point numbers, to easily switch between double and single floati...
Definition: btScalar.h:314
void btBulletDynamicsProbe()
Basic interface to allow actions such as vehicles and characters to be updated inside a btDynamicsWor...
int size() const
return the number of elements in the array
The btBroadphaseInterface class provides an interface to detect aabb-overlapping object pairs.
virtual void setAabb(btBroadphaseProxy *proxy, const btVector3 &aabbMin, const btVector3 &aabbMax, btDispatcher *dispatcher)=0
btCollisionConfiguration allows to configure Bullet collision detection stack allocator size,...
btCollisionDispatcher supports algorithms that handle ConvexConvex and ConvexConcave collision pairs.
btCollisionObject can be used to manage collision detection objects.
btTransform & getWorldTransform()
btBroadphaseProxy * getBroadphaseHandle()
bool isStaticObject() const
const btTransform & getInterpolationWorldTransform() const
const btCollisionShape * getCollisionShape() const
int getActivationState() const
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.
btDispatcherInfo & getDispatchInfo()
virtual btIDebugDraw * getDebugDrawer()
virtual void removeCollisionObject(btCollisionObject *collisionObject)
virtual void addCollisionObject(btCollisionObject *collisionObject, int collisionFilterGroup=btBroadphaseProxy::DefaultFilter, int collisionFilterMask=btBroadphaseProxy::AllFilter)
btAlignedObjectArray< btCollisionObject * > m_collisionObjects
int getNumCollisionObjects() const
virtual void performDiscreteCollisionDetection()
btCollisionObjectArray & getCollisionObjectArray()
btIDebugDraw * m_debugDrawer
btDispatcher * m_dispatcher1
const btBroadphaseInterface * getBroadphase() const
virtual void allSolved(const btContactSolverInfo &, class btIDebugDraw *)
virtual void prepareSolve(int, int)
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
The btDispatcher interface class can be used in combination with broadphase to dispatch calculations ...
Definition: btDispatcher.h:77
virtual int getNumManifolds() const =0
The btDynamicsWorld is the interface class for several dynamics implementation, basic,...
virtual void setWorldTransform(const btTransform &worldTrans)=0
btPersistentManifold is a contact point cache, it stays persistent as long as objects are overlapping...
The btRigidBody is the main class for rigid body objects.
Definition: btRigidBody.h:60
void applyGravity()
void integrateVelocities(btScalar step)
void clearForces()
Definition: btRigidBody.h:415
void applyDamping(btScalar timeStep)
applyDamping damps the velocity, using the given m_linearDamping and m_angularDamping
void setGravity(const btVector3 &acceleration)
void proceedToTransform(const btTransform &newTrans)
const btCollisionShape * getCollisionShape() const
Definition: btRigidBody.h:242
btMotionState * getMotionState()
Definition: btRigidBody.h:549
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
void predictIntegratedTransform(btScalar step, btTransform &predictedTransform)
continuous collision detection needs prediction
btConstraintSolver * m_constraintSolver
virtual btConstraintSolver * getConstraintSolver()
btSimpleDynamicsWorld(btDispatcher *dispatcher, btBroadphaseInterface *pairCache, btConstraintSolver *constraintSolver, btCollisionConfiguration *collisionConfiguration)
this btSimpleDynamicsWorld constructor creates dispatcher, broadphase pairCache and constraintSolver
virtual void addRigidBody(btRigidBody *body)
virtual void removeCollisionObject(btCollisionObject *collisionObject)
removeCollisionObject will first check if it is a rigid body, if so call removeRigidBody otherwise ca...
virtual void setConstraintSolver(btConstraintSolver *solver)
virtual void synchronizeMotionStates()
virtual void removeAction(btActionInterface *action)
void integrateTransforms(btScalar timeStep)
virtual void removeRigidBody(btRigidBody *body)
virtual btVector3 getGravity() const
void predictUnconstraintMotion(btScalar timeStep)
virtual void setGravity(const btVector3 &gravity)
virtual void addAction(btActionInterface *action)
virtual int stepSimulation(btScalar timeStep, int maxSubSteps=1, btScalar fixedTimeStep=btScalar(1.)/btScalar(60.))
maxSubSteps/fixedTimeStep for interpolation is currently ignored for btSimpleDynamicsWorld,...
The btTransform class supports rigid transforms with only translation and rotation and no scaling/she...
Definition: btTransform.h:30
btVector3 can be used to represent 3D points and vectors.
Definition: btVector3.h:82
btScalar m_timeStep
Definition: btDispatcher.h:53
class btIDebugDraw * m_debugDraw
Definition: btDispatcher.h:58