Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/kicklib2/src/external/bullet/BulletDynamics/Dynamics/btSimpleDynamicsWorld.cpp @ 8474

Last change on this file since 8474 was 8284, checked in by rgrieder, 14 years ago

Merged revisions 7978 - 8096 from kicklib to kicklib2.

  • Property svn:eol-style set to native
File size: 6.6 KB
Line 
1/*
2Bullet Continuous Collision Detection and Physics Library
3Copyright (c) 2003-2006 Erwin Coumans  http://continuousphysics.com/Bullet/
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#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/*
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*/
30extern "C" 
31{
32        void btBulletDynamicsProbe ();
33        void btBulletDynamicsProbe () {}
34}
35
36
37
38
39btSimpleDynamicsWorld::btSimpleDynamicsWorld(btDispatcher* dispatcher,btBroadphaseInterface* pairCache,btConstraintSolver* constraintSolver,btCollisionConfiguration* collisionConfiguration)
40:btDynamicsWorld(dispatcher,pairCache,collisionConfiguration),
41m_constraintSolver(constraintSolver),
42m_ownsConstraintSolver(false),
43m_gravity(0,0,-10)
44{
45
46}
47
48
49btSimpleDynamicsWorld::~btSimpleDynamicsWorld()
50{
51        if (m_ownsConstraintSolver)
52                btAlignedFree( m_constraintSolver);
53}
54
55int             btSimpleDynamicsWorld::stepSimulation( btScalar timeStep,int maxSubSteps, btScalar fixedTimeStep)
56{
57        (void)fixedTimeStep;
58        (void)maxSubSteps;
59
60
61        ///apply gravity, predict motion
62        predictUnconstraintMotion(timeStep);
63
64        btDispatcherInfo&       dispatchInfo = getDispatchInfo();
65        dispatchInfo.m_timeStep = timeStep;
66        dispatchInfo.m_stepCount = 0;
67        dispatchInfo.m_debugDraw = getDebugDrawer();
68
69        ///perform collision detection
70        performDiscreteCollisionDetection();
71
72        ///solve contact constraints
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);
81                m_constraintSolver->solveGroup(0,0,manifoldPtr, numManifolds,0,0,infoGlobal,m_debugDrawer, m_stackAlloc,m_dispatcher1);
82                m_constraintSolver->allSolved(infoGlobal,m_debugDrawer, m_stackAlloc);
83        }
84
85        ///integrate transforms
86        integrateTransforms(timeStep);
87               
88        updateAabbs();
89
90        synchronizeMotionStates();
91
92        clearForces();
93
94        return 1;
95
96}
97
98void    btSimpleDynamicsWorld::clearForces()
99{
100        ///@todo: iterate over awake simulation islands!
101        for ( int i=0;i<m_collisionObjects.size();i++)
102        {
103                btCollisionObject* colObj = m_collisionObjects[i];
104               
105                btRigidBody* body = btRigidBody::upcast(colObj);
106                if (body)
107                {
108                        body->clearForces();
109                }
110        }
111}       
112
113
114void    btSimpleDynamicsWorld::setGravity(const btVector3& gravity)
115{
116        m_gravity = gravity;
117        for ( int i=0;i<m_collisionObjects.size();i++)
118        {
119                btCollisionObject* colObj = m_collisionObjects[i];
120                btRigidBody* body = btRigidBody::upcast(colObj);
121                if (body)
122                {
123                        body->setGravity(gravity);
124                }
125        }
126}
127
128btVector3 btSimpleDynamicsWorld::getGravity () const
129{
130        return m_gravity;
131}
132
133void    btSimpleDynamicsWorld::removeRigidBody(btRigidBody* body)
134{
135        btCollisionWorld::removeCollisionObject(body);
136}
137
138void    btSimpleDynamicsWorld::removeCollisionObject(btCollisionObject* collisionObject)
139{
140        btRigidBody* body = btRigidBody::upcast(collisionObject);
141        if (body)
142                removeRigidBody(body);
143        else
144                btCollisionWorld::removeCollisionObject(collisionObject);
145}
146
147
148void    btSimpleDynamicsWorld::addRigidBody(btRigidBody* body)
149{
150        body->setGravity(m_gravity);
151
152        if (body->getCollisionShape())
153        {
154                addCollisionObject(body);
155        }
156}
157
158void    btSimpleDynamicsWorld::updateAabbs()
159{
160        btTransform predictedTrans;
161        for ( int i=0;i<m_collisionObjects.size();i++)
162        {
163                btCollisionObject* colObj = m_collisionObjects[i];
164                btRigidBody* body = btRigidBody::upcast(colObj);
165                if (body)
166                {
167                        if (body->isActive() && (!body->isStaticObject()))
168                        {
169                                btVector3 minAabb,maxAabb;
170                                colObj->getCollisionShape()->getAabb(colObj->getWorldTransform(), minAabb,maxAabb);
171                                btBroadphaseInterface* bp = getBroadphase();
172                                bp->setAabb(body->getBroadphaseHandle(),minAabb,maxAabb, m_dispatcher1);
173                        }
174                }
175        }
176}
177
178void    btSimpleDynamicsWorld::integrateTransforms(btScalar timeStep)
179{
180        btTransform predictedTrans;
181        for ( int i=0;i<m_collisionObjects.size();i++)
182        {
183                btCollisionObject* colObj = m_collisionObjects[i];
184                btRigidBody* body = btRigidBody::upcast(colObj);
185                if (body)
186                {
187                        if (body->isActive() && (!body->isStaticObject()))
188                        {
189                                body->predictIntegratedTransform(timeStep, predictedTrans);
190                                body->proceedToTransform( predictedTrans);
191                        }
192                }
193        }
194}
195
196
197
198void    btSimpleDynamicsWorld::predictUnconstraintMotion(btScalar timeStep)
199{
200        for ( int i=0;i<m_collisionObjects.size();i++)
201        {
202                btCollisionObject* colObj = m_collisionObjects[i];
203                btRigidBody* body = btRigidBody::upcast(colObj);
204                if (body)
205                {
206                        if (!body->isStaticObject())
207                        {
208                                if (body->isActive())
209                                {
210                                        body->applyGravity();
211                                        body->integrateVelocities( timeStep);
212                                        body->applyDamping(timeStep);
213                                        body->predictIntegratedTransform(timeStep,body->getInterpolationWorldTransform());
214                                }
215                        }
216                }
217        }
218}
219
220
221void    btSimpleDynamicsWorld::synchronizeMotionStates()
222{
223        ///@todo: iterate over awake simulation islands!
224        for ( int i=0;i<m_collisionObjects.size();i++)
225        {
226                btCollisionObject* colObj = m_collisionObjects[i];
227                btRigidBody* body = btRigidBody::upcast(colObj);
228                if (body && body->getMotionState())
229                {
230                        if (body->getActivationState() != ISLAND_SLEEPING)
231                        {
232                                body->getMotionState()->setWorldTransform(body->getWorldTransform());
233                        }
234                }
235        }
236
237}
238
239
240void    btSimpleDynamicsWorld::setConstraintSolver(btConstraintSolver* solver)
241{
242        if (m_ownsConstraintSolver)
243        {
244                btAlignedFree(m_constraintSolver);
245        }
246        m_ownsConstraintSolver = false;
247        m_constraintSolver = solver;
248}
249
250btConstraintSolver* btSimpleDynamicsWorld::getConstraintSolver()
251{
252        return m_constraintSolver;
253}
Note: See TracBrowser for help on using the repository browser.