Back to index

supertuxkart  0.5+dfsg1
btContinuousDynamicsWorld.cpp
Go to the documentation of this file.
00001 /*
00002 Bullet Continuous Collision Detection and Physics Library
00003 Copyright (c) 2003-2007 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 
00017 #include "btContinuousDynamicsWorld.h"
00018 #include "LinearMath/btQuickprof.h"
00019 
00020 //collision detection
00021 #include "BulletCollision/CollisionDispatch/btCollisionDispatcher.h"
00022 #include "BulletCollision/BroadphaseCollision/btSimpleBroadphase.h"
00023 #include "BulletCollision/CollisionShapes/btCollisionShape.h"
00024 #include "BulletCollision/CollisionDispatch/btSimulationIslandManager.h"
00025 
00026 //rigidbody & constraints
00027 #include "BulletDynamics/Dynamics/btRigidBody.h"
00028 #include "BulletDynamics/ConstraintSolver/btSequentialImpulseConstraintSolver.h"
00029 #include "BulletDynamics/ConstraintSolver/btContactSolverInfo.h"
00030 #include "BulletDynamics/ConstraintSolver/btTypedConstraint.h"
00031 
00032 
00033 
00034 #include <stdio.h>
00035 
00036 btContinuousDynamicsWorld::btContinuousDynamicsWorld(btDispatcher* dispatcher,btBroadphaseInterface* pairCache,btConstraintSolver* constraintSolver,btCollisionConfiguration* collisionConfiguration)
00037 :btDiscreteDynamicsWorld(dispatcher,pairCache,constraintSolver,collisionConfiguration)
00038 {
00039 }
00040 
00041 btContinuousDynamicsWorld::~btContinuousDynamicsWorld()
00042 {
00043 }
00044 
00045        
00046 void   btContinuousDynamicsWorld::internalSingleStepSimulation( btScalar timeStep)
00047 {
00048        
00049        startProfiling(timeStep);
00050        
00051 
00053        updateAabbs();
00054        //static int frame=0;
00055 //     printf("frame %d\n",frame++);
00056 
00058        predictUnconstraintMotion(timeStep);
00059 
00060        btDispatcherInfo& dispatchInfo = getDispatchInfo();
00061 
00062        dispatchInfo.m_timeStep = timeStep;
00063        dispatchInfo.m_stepCount = 0;
00064        dispatchInfo.m_debugDraw = getDebugDrawer();
00065 
00067        performDiscreteCollisionDetection();
00068 
00069        calculateSimulationIslands();
00070 
00071        
00072        getSolverInfo().m_timeStep = timeStep;
00073        
00074 
00075 
00077        solveConstraints(getSolverInfo());
00078        
00080        calculateTimeOfImpacts(timeStep);
00081 
00082        btScalar toi = dispatchInfo.m_timeOfImpact;
00083 //     if (toi < 1.f)
00084 //            printf("toi = %f\n",toi);
00085        if (toi < 0.f)
00086               printf("toi = %f\n",toi);
00087 
00088 
00090        integrateTransforms(timeStep * toi);
00091 
00093        updateVehicles(timeStep);
00094 
00095 
00096        updateActivationState( timeStep );
00097        
00098        if(0 != m_internalTickCallback) {
00099               (*m_internalTickCallback)(this, timeStep);
00100        }
00101 }
00102 
00103 void   btContinuousDynamicsWorld::calculateTimeOfImpacts(btScalar timeStep)
00104 {
00106               updateTemporalAabbs(timeStep);
00107               
00110               float toi = 1.f;
00111               
00112        
00113               btDispatcherInfo& dispatchInfo = getDispatchInfo();
00114               dispatchInfo.m_timeStep = timeStep;
00115               dispatchInfo.m_timeOfImpact = 1.f;
00116               dispatchInfo.m_stepCount = 0;
00117               dispatchInfo.m_dispatchFunc = btDispatcherInfo::DISPATCH_CONTINUOUS;
00118 
00120 
00121 
00122               btDispatcher* dispatcher = getDispatcher();
00123               if (dispatcher)
00124                      dispatcher->dispatchAllCollisionPairs(m_broadphasePairCache->getOverlappingPairCache(),dispatchInfo,m_dispatcher1);
00125 
00126               toi = dispatchInfo.m_timeOfImpact;
00127 
00128               dispatchInfo.m_dispatchFunc = btDispatcherInfo::DISPATCH_DISCRETE;
00129 
00130 }
00131 
00132 void   btContinuousDynamicsWorld::updateTemporalAabbs(btScalar timeStep)
00133 {
00134 
00135        btVector3 temporalAabbMin,temporalAabbMax;
00136 
00137        for ( int i=0;i<m_collisionObjects.size();i++)
00138        {
00139               btCollisionObject* colObj = m_collisionObjects[i];
00140               
00141               btRigidBody* body = btRigidBody::upcast(colObj);
00142               if (body)
00143               {
00144                      body->getCollisionShape()->getAabb(m_collisionObjects[i]->getWorldTransform(),temporalAabbMin,temporalAabbMax);
00145                      const btVector3& linvel = body->getLinearVelocity();
00146 
00147                      //make the AABB temporal
00148                      float temporalAabbMaxx = temporalAabbMax.getX();
00149                      float temporalAabbMaxy = temporalAabbMax.getY();
00150                      float temporalAabbMaxz = temporalAabbMax.getZ();
00151                      float temporalAabbMinx = temporalAabbMin.getX();
00152                      float temporalAabbMiny = temporalAabbMin.getY();
00153                      float temporalAabbMinz = temporalAabbMin.getZ();
00154 
00155                      // add linear motion
00156                      btVector3 linMotion = linvel*timeStep;
00157               
00158                      if (linMotion.x() > 0.f)
00159                             temporalAabbMaxx += linMotion.x(); 
00160                      else
00161                             temporalAabbMinx += linMotion.x();
00162                      if (linMotion.y() > 0.f)
00163                             temporalAabbMaxy += linMotion.y(); 
00164                      else
00165                             temporalAabbMiny += linMotion.y();
00166                      if (linMotion.z() > 0.f)
00167                             temporalAabbMaxz += linMotion.z(); 
00168                      else
00169                             temporalAabbMinz += linMotion.z();
00170 
00171                      //add conservative angular motion
00172                      btScalar angularMotion(0);// = angvel.length() * GetAngularMotionDisc() * timeStep;
00173                      btVector3 angularMotion3d(angularMotion,angularMotion,angularMotion);
00174                      temporalAabbMin = btVector3(temporalAabbMinx,temporalAabbMiny,temporalAabbMinz);
00175                      temporalAabbMax = btVector3(temporalAabbMaxx,temporalAabbMaxy,temporalAabbMaxz);
00176 
00177                      temporalAabbMin -= angularMotion3d;
00178                      temporalAabbMax += angularMotion3d;
00179 
00180                      m_broadphasePairCache->setAabb(body->getBroadphaseHandle(),temporalAabbMin,temporalAabbMax,m_dispatcher1);
00181               }
00182        }
00183 
00184        //update aabb (of all moved objects)
00185 
00186        m_broadphasePairCache->calculateOverlappingPairs(m_dispatcher1);
00187        
00188 
00189 
00190 }
00191 
00192 
00193