Back to index

supertuxkart  0.5+dfsg1
btSimpleDynamicsWorld.cpp
Go to the documentation of this file.
00001 /*
00002 Bullet Continuous Collision Detection and Physics Library
00003 Copyright (c) 2003-2006 Erwin Coumans  http://continuousphysics.com/Bullet/
00004 
00005 This software is provided 'as-is', without any express or implied warranty.
00006 In no event will the authors be held liable for any damages arising from the use of this software.
00007 Permission is granted to anyone to use this software for any purpose, 
00008 including commercial applications, and to alter it and redistribute it freely, 
00009 subject to the following restrictions:
00010 
00011 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.
00012 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
00013 3. This notice may not be removed or altered from any source distribution.
00014 */
00015 
00016 #include "btSimpleDynamicsWorld.h"
00017 #include "BulletCollision/CollisionDispatch/btCollisionDispatcher.h"
00018 #include "BulletCollision/BroadphaseCollision/btSimpleBroadphase.h"
00019 #include "BulletCollision/CollisionShapes/btCollisionShape.h"
00020 #include "BulletDynamics/Dynamics/btRigidBody.h"
00021 #include "BulletDynamics/ConstraintSolver/btSequentialImpulseConstraintSolver.h"
00022 #include "BulletDynamics/ConstraintSolver/btContactSolverInfo.h"
00023 
00024 
00025 /*
00026   Make sure this dummy function never changes so that it
00027   can be used by probes that are checking whether the
00028   library is actually installed.
00029 */
00030 extern "C" void btBulletDynamicsProbe () {}
00031 
00032 
00033 
00034 
00035 btSimpleDynamicsWorld::btSimpleDynamicsWorld(btDispatcher* dispatcher,btBroadphaseInterface* pairCache,btConstraintSolver* constraintSolver,btCollisionConfiguration* collisionConfiguration)
00036 :btDynamicsWorld(dispatcher,pairCache,collisionConfiguration),
00037 m_constraintSolver(constraintSolver),
00038 m_ownsConstraintSolver(false),
00039 m_gravity(0,0,-10)
00040 {
00041 
00042 }
00043 
00044 
00045 btSimpleDynamicsWorld::~btSimpleDynamicsWorld()
00046 {
00047        if (m_ownsConstraintSolver)
00048               btAlignedFree( m_constraintSolver);
00049 }
00050 
00051 int           btSimpleDynamicsWorld::stepSimulation( btScalar timeStep,int maxSubSteps, btScalar fixedTimeStep)
00052 {
00053        (void)fixedTimeStep;
00054        (void)maxSubSteps;
00055 
00056 
00058        predictUnconstraintMotion(timeStep);
00059 
00060        btDispatcherInfo&    dispatchInfo = getDispatchInfo();
00061        dispatchInfo.m_timeStep = timeStep;
00062        dispatchInfo.m_stepCount = 0;
00063        dispatchInfo.m_debugDraw = getDebugDrawer();
00064 
00066        performDiscreteCollisionDetection();
00067 
00069        int numManifolds = m_dispatcher1->getNumManifolds();
00070        if (numManifolds)
00071        {
00072               btPersistentManifold** manifoldPtr = ((btCollisionDispatcher*)m_dispatcher1)->getInternalManifoldPointer();
00073               
00074               btContactSolverInfo infoGlobal;
00075               infoGlobal.m_timeStep = timeStep;
00076               m_constraintSolver->prepareSolve(0,numManifolds);
00077               m_constraintSolver->solveGroup(0,0,manifoldPtr, numManifolds,0,0,infoGlobal,m_debugDrawer, m_stackAlloc,m_dispatcher1);
00078               m_constraintSolver->allSolved(infoGlobal,m_debugDrawer, m_stackAlloc);
00079        }
00080 
00082        integrateTransforms(timeStep);
00083               
00084        updateAabbs();
00085 
00086        synchronizeMotionStates();
00087 
00088        clearForces();
00089 
00090        return 1;
00091 
00092 }
00093 
00094 void   btSimpleDynamicsWorld::clearForces()
00095 {
00096        //todo: iterate over awake simulation islands!
00097        for ( int i=0;i<m_collisionObjects.size();i++)
00098        {
00099               btCollisionObject* colObj = m_collisionObjects[i];
00100               
00101               btRigidBody* body = btRigidBody::upcast(colObj);
00102               if (body)
00103               {
00104                      body->clearForces();
00105               }
00106        }
00107 }      
00108 
00109 
00110 void   btSimpleDynamicsWorld::setGravity(const btVector3& gravity)
00111 {
00112        m_gravity = gravity;
00113        for ( int i=0;i<m_collisionObjects.size();i++)
00114        {
00115               btCollisionObject* colObj = m_collisionObjects[i];
00116               btRigidBody* body = btRigidBody::upcast(colObj);
00117               if (body)
00118               {
00119                      body->setGravity(gravity);
00120               }
00121        }
00122 }
00123 
00124 btVector3 btSimpleDynamicsWorld::getGravity () const
00125 {
00126        return m_gravity;
00127 }
00128 
00129 void   btSimpleDynamicsWorld::removeRigidBody(btRigidBody* body)
00130 {
00131        removeCollisionObject(body);
00132 }
00133 
00134 void   btSimpleDynamicsWorld::addRigidBody(btRigidBody* body)
00135 {
00136        body->setGravity(m_gravity);
00137 
00138        if (body->getCollisionShape())
00139        {
00140               addCollisionObject(body);
00141        }
00142 }
00143 
00144 void   btSimpleDynamicsWorld::updateAabbs()
00145 {
00146        btTransform predictedTrans;
00147        for ( int i=0;i<m_collisionObjects.size();i++)
00148        {
00149               btCollisionObject* colObj = m_collisionObjects[i];
00150               btRigidBody* body = btRigidBody::upcast(colObj);
00151               if (body)
00152               {
00153                      if (body->isActive() && (!body->isStaticObject()))
00154                      {
00155                             btPoint3 minAabb,maxAabb;
00156                             colObj->getCollisionShape()->getAabb(colObj->getWorldTransform(), minAabb,maxAabb);
00157                             btBroadphaseInterface* bp = getBroadphase();
00158                             bp->setAabb(body->getBroadphaseHandle(),minAabb,maxAabb, m_dispatcher1);
00159                      }
00160               }
00161        }
00162 }
00163 
00164 void   btSimpleDynamicsWorld::integrateTransforms(btScalar timeStep)
00165 {
00166        btTransform predictedTrans;
00167        for ( int i=0;i<m_collisionObjects.size();i++)
00168        {
00169               btCollisionObject* colObj = m_collisionObjects[i];
00170               btRigidBody* body = btRigidBody::upcast(colObj);
00171               if (body)
00172               {
00173                      if (body->isActive() && (!body->isStaticObject()))
00174                      {
00175                             body->predictIntegratedTransform(timeStep, predictedTrans);
00176                             body->proceedToTransform( predictedTrans);
00177                      }
00178               }
00179        }
00180 }
00181 
00182 
00183 
00184 void   btSimpleDynamicsWorld::predictUnconstraintMotion(btScalar timeStep)
00185 {
00186        for ( int i=0;i<m_collisionObjects.size();i++)
00187        {
00188               btCollisionObject* colObj = m_collisionObjects[i];
00189               btRigidBody* body = btRigidBody::upcast(colObj);
00190               if (body)
00191               {
00192                      if (!body->isStaticObject())
00193                      {
00194                             if (body->isActive())
00195                             {
00196                                    body->applyGravity();
00197                                    body->integrateVelocities( timeStep);
00198                                    body->applyDamping(timeStep);
00199                                    body->predictIntegratedTransform(timeStep,body->getInterpolationWorldTransform());
00200                             }
00201                      }
00202               }
00203        }
00204 }
00205 
00206 
00207 void   btSimpleDynamicsWorld::synchronizeMotionStates()
00208 {
00209        //todo: iterate over awake simulation islands!
00210        for ( int i=0;i<m_collisionObjects.size();i++)
00211        {
00212               btCollisionObject* colObj = m_collisionObjects[i];
00213               btRigidBody* body = btRigidBody::upcast(colObj);
00214               if (body && body->getMotionState())
00215               {
00216                      if (body->getActivationState() != ISLAND_SLEEPING)
00217                      {
00218                             body->getMotionState()->setWorldTransform(body->getWorldTransform());
00219                      }
00220               }
00221        }
00222 
00223 }
00224 
00225 
00226 void   btSimpleDynamicsWorld::setConstraintSolver(btConstraintSolver* solver)
00227 {
00228        if (m_ownsConstraintSolver)
00229        {
00230               btAlignedFree(m_constraintSolver);
00231        }
00232        m_ownsConstraintSolver = false;
00233        m_constraintSolver = solver;
00234 }
00235 
00236 btConstraintSolver* btSimpleDynamicsWorld::getConstraintSolver()
00237 {
00238        return m_constraintSolver;
00239 }