Blender V2.61 - r43446

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     if(0 != m_internalPreTickCallback) {
00052         (*m_internalPreTickCallback)(this, timeStep);
00053     }
00054 
00055 
00057     updateAabbs();
00058     //static int frame=0;
00059 //  printf("frame %d\n",frame++);
00060 
00062     predictUnconstraintMotion(timeStep);
00063 
00064     btDispatcherInfo& dispatchInfo = getDispatchInfo();
00065 
00066     dispatchInfo.m_timeStep = timeStep;
00067     dispatchInfo.m_stepCount = 0;
00068     dispatchInfo.m_debugDraw = getDebugDrawer();
00069 
00071     performDiscreteCollisionDetection();
00072 
00073     calculateSimulationIslands();
00074 
00075     
00076     getSolverInfo().m_timeStep = timeStep;
00077     
00078 
00079 
00081     solveConstraints(getSolverInfo());
00082     
00084     calculateTimeOfImpacts(timeStep);
00085 
00086     btScalar toi = dispatchInfo.m_timeOfImpact;
00087 //  if (toi < 1.f)
00088 //      printf("toi = %f\n",toi);
00089     if (toi < 0.f)
00090         printf("toi = %f\n",toi);
00091 
00092 
00094     integrateTransforms(timeStep * toi);
00095 
00097     updateActions(timeStep);
00098 
00099     updateActivationState( timeStep );
00100     
00101     if(0 != m_internalTickCallback) {
00102         (*m_internalTickCallback)(this, timeStep);
00103     }
00104 }
00105 
00106 void    btContinuousDynamicsWorld::calculateTimeOfImpacts(btScalar timeStep)
00107 {
00109         updateTemporalAabbs(timeStep);
00110         
00113         btScalar toi = 1.f;
00114         
00115     
00116         btDispatcherInfo& dispatchInfo = getDispatchInfo();
00117         dispatchInfo.m_timeStep = timeStep;
00118         dispatchInfo.m_timeOfImpact = 1.f;
00119         dispatchInfo.m_stepCount = 0;
00120         dispatchInfo.m_dispatchFunc = btDispatcherInfo::DISPATCH_CONTINUOUS;
00121 
00123 
00124 
00125         btDispatcher* dispatcher = getDispatcher();
00126         if (dispatcher)
00127             dispatcher->dispatchAllCollisionPairs(m_broadphasePairCache->getOverlappingPairCache(),dispatchInfo,m_dispatcher1);
00128 
00129         toi = dispatchInfo.m_timeOfImpact;
00130 
00131         dispatchInfo.m_dispatchFunc = btDispatcherInfo::DISPATCH_DISCRETE;
00132 
00133 }
00134 
00135 void    btContinuousDynamicsWorld::updateTemporalAabbs(btScalar timeStep)
00136 {
00137 
00138     btVector3 temporalAabbMin,temporalAabbMax;
00139 
00140     for ( int i=0;i<m_collisionObjects.size();i++)
00141     {
00142         btCollisionObject* colObj = m_collisionObjects[i];
00143         
00144         btRigidBody* body = btRigidBody::upcast(colObj);
00145         if (body)
00146         {
00147             body->getCollisionShape()->getAabb(m_collisionObjects[i]->getWorldTransform(),temporalAabbMin,temporalAabbMax);
00148             const btVector3& linvel = body->getLinearVelocity();
00149 
00150             //make the AABB temporal
00151             btScalar temporalAabbMaxx = temporalAabbMax.getX();
00152             btScalar temporalAabbMaxy = temporalAabbMax.getY();
00153             btScalar temporalAabbMaxz = temporalAabbMax.getZ();
00154             btScalar temporalAabbMinx = temporalAabbMin.getX();
00155             btScalar temporalAabbMiny = temporalAabbMin.getY();
00156             btScalar temporalAabbMinz = temporalAabbMin.getZ();
00157 
00158             // add linear motion
00159             btVector3 linMotion = linvel*timeStep;
00160         
00161             if (linMotion.x() > 0.f)
00162                 temporalAabbMaxx += linMotion.x(); 
00163             else
00164                 temporalAabbMinx += linMotion.x();
00165             if (linMotion.y() > 0.f)
00166                 temporalAabbMaxy += linMotion.y(); 
00167             else
00168                 temporalAabbMiny += linMotion.y();
00169             if (linMotion.z() > 0.f)
00170                 temporalAabbMaxz += linMotion.z(); 
00171             else
00172                 temporalAabbMinz += linMotion.z();
00173 
00174             //add conservative angular motion
00175             btScalar angularMotion(0);// = angvel.length() * GetAngularMotionDisc() * timeStep;
00176             btVector3 angularMotion3d(angularMotion,angularMotion,angularMotion);
00177             temporalAabbMin = btVector3(temporalAabbMinx,temporalAabbMiny,temporalAabbMinz);
00178             temporalAabbMax = btVector3(temporalAabbMaxx,temporalAabbMaxy,temporalAabbMaxz);
00179 
00180             temporalAabbMin -= angularMotion3d;
00181             temporalAabbMax += angularMotion3d;
00182 
00183             m_broadphasePairCache->setAabb(body->getBroadphaseHandle(),temporalAabbMin,temporalAabbMax,m_dispatcher1);
00184         }
00185     }
00186 
00187     //update aabb (of all moved objects)
00188 
00189     m_broadphasePairCache->calculateOverlappingPairs(m_dispatcher1);
00190     
00191 
00192 
00193 }
00194 
00195 
00196