Blender V2.61 - r43446
|
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" 00031 { 00032 void btBulletDynamicsProbe (); 00033 void btBulletDynamicsProbe () {} 00034 } 00035 00036 00037 00038 00039 btSimpleDynamicsWorld::btSimpleDynamicsWorld(btDispatcher* dispatcher,btBroadphaseInterface* pairCache,btConstraintSolver* constraintSolver,btCollisionConfiguration* collisionConfiguration) 00040 :btDynamicsWorld(dispatcher,pairCache,collisionConfiguration), 00041 m_constraintSolver(constraintSolver), 00042 m_ownsConstraintSolver(false), 00043 m_gravity(0,0,-10) 00044 { 00045 00046 } 00047 00048 00049 btSimpleDynamicsWorld::~btSimpleDynamicsWorld() 00050 { 00051 if (m_ownsConstraintSolver) 00052 btAlignedFree( m_constraintSolver); 00053 } 00054 00055 int btSimpleDynamicsWorld::stepSimulation( btScalar timeStep,int maxSubSteps, btScalar fixedTimeStep) 00056 { 00057 (void)fixedTimeStep; 00058 (void)maxSubSteps; 00059 00060 00062 predictUnconstraintMotion(timeStep); 00063 00064 btDispatcherInfo& dispatchInfo = getDispatchInfo(); 00065 dispatchInfo.m_timeStep = timeStep; 00066 dispatchInfo.m_stepCount = 0; 00067 dispatchInfo.m_debugDraw = getDebugDrawer(); 00068 00070 performDiscreteCollisionDetection(); 00071 00073 int numManifolds = m_dispatcher1->getNumManifolds(); 00074 if (numManifolds) 00075 { 00076 btPersistentManifold** manifoldPtr = ((btCollisionDispatcher*)m_dispatcher1)->getInternalManifoldPointer(); 00077 00078 btContactSolverInfo infoGlobal; 00079 infoGlobal.m_timeStep = timeStep; 00080 m_constraintSolver->prepareSolve(0,numManifolds); 00081 m_constraintSolver->solveGroup(&getCollisionObjectArray()[0],getNumCollisionObjects(),manifoldPtr, numManifolds,0,0,infoGlobal,m_debugDrawer, m_stackAlloc,m_dispatcher1); 00082 m_constraintSolver->allSolved(infoGlobal,m_debugDrawer, m_stackAlloc); 00083 } 00084 00086 integrateTransforms(timeStep); 00087 00088 updateAabbs(); 00089 00090 synchronizeMotionStates(); 00091 00092 clearForces(); 00093 00094 return 1; 00095 00096 } 00097 00098 void btSimpleDynamicsWorld::clearForces() 00099 { 00101 for ( int i=0;i<m_collisionObjects.size();i++) 00102 { 00103 btCollisionObject* colObj = m_collisionObjects[i]; 00104 00105 btRigidBody* body = btRigidBody::upcast(colObj); 00106 if (body) 00107 { 00108 body->clearForces(); 00109 } 00110 } 00111 } 00112 00113 00114 void btSimpleDynamicsWorld::setGravity(const btVector3& gravity) 00115 { 00116 m_gravity = gravity; 00117 for ( int i=0;i<m_collisionObjects.size();i++) 00118 { 00119 btCollisionObject* colObj = m_collisionObjects[i]; 00120 btRigidBody* body = btRigidBody::upcast(colObj); 00121 if (body) 00122 { 00123 body->setGravity(gravity); 00124 } 00125 } 00126 } 00127 00128 btVector3 btSimpleDynamicsWorld::getGravity () const 00129 { 00130 return m_gravity; 00131 } 00132 00133 void btSimpleDynamicsWorld::removeRigidBody(btRigidBody* body) 00134 { 00135 btCollisionWorld::removeCollisionObject(body); 00136 } 00137 00138 void btSimpleDynamicsWorld::removeCollisionObject(btCollisionObject* collisionObject) 00139 { 00140 btRigidBody* body = btRigidBody::upcast(collisionObject); 00141 if (body) 00142 removeRigidBody(body); 00143 else 00144 btCollisionWorld::removeCollisionObject(collisionObject); 00145 } 00146 00147 00148 void btSimpleDynamicsWorld::addRigidBody(btRigidBody* body) 00149 { 00150 body->setGravity(m_gravity); 00151 00152 if (body->getCollisionShape()) 00153 { 00154 addCollisionObject(body); 00155 } 00156 } 00157 00158 void btSimpleDynamicsWorld::addRigidBody(btRigidBody* body, short group, short mask) 00159 { 00160 body->setGravity(m_gravity); 00161 00162 if (body->getCollisionShape()) 00163 { 00164 addCollisionObject(body,group,mask); 00165 } 00166 } 00167 00168 00169 void btSimpleDynamicsWorld::debugDrawWorld() 00170 { 00171 00172 } 00173 00174 void btSimpleDynamicsWorld::addAction(btActionInterface* action) 00175 { 00176 00177 } 00178 00179 void btSimpleDynamicsWorld::removeAction(btActionInterface* action) 00180 { 00181 00182 } 00183 00184 00185 void btSimpleDynamicsWorld::updateAabbs() 00186 { 00187 btTransform predictedTrans; 00188 for ( int i=0;i<m_collisionObjects.size();i++) 00189 { 00190 btCollisionObject* colObj = m_collisionObjects[i]; 00191 btRigidBody* body = btRigidBody::upcast(colObj); 00192 if (body) 00193 { 00194 if (body->isActive() && (!body->isStaticObject())) 00195 { 00196 btVector3 minAabb,maxAabb; 00197 colObj->getCollisionShape()->getAabb(colObj->getWorldTransform(), minAabb,maxAabb); 00198 btBroadphaseInterface* bp = getBroadphase(); 00199 bp->setAabb(body->getBroadphaseHandle(),minAabb,maxAabb, m_dispatcher1); 00200 } 00201 } 00202 } 00203 } 00204 00205 void btSimpleDynamicsWorld::integrateTransforms(btScalar timeStep) 00206 { 00207 btTransform predictedTrans; 00208 for ( int i=0;i<m_collisionObjects.size();i++) 00209 { 00210 btCollisionObject* colObj = m_collisionObjects[i]; 00211 btRigidBody* body = btRigidBody::upcast(colObj); 00212 if (body) 00213 { 00214 if (body->isActive() && (!body->isStaticObject())) 00215 { 00216 body->predictIntegratedTransform(timeStep, predictedTrans); 00217 body->proceedToTransform( predictedTrans); 00218 } 00219 } 00220 } 00221 } 00222 00223 00224 00225 void btSimpleDynamicsWorld::predictUnconstraintMotion(btScalar timeStep) 00226 { 00227 for ( int i=0;i<m_collisionObjects.size();i++) 00228 { 00229 btCollisionObject* colObj = m_collisionObjects[i]; 00230 btRigidBody* body = btRigidBody::upcast(colObj); 00231 if (body) 00232 { 00233 if (!body->isStaticObject()) 00234 { 00235 if (body->isActive()) 00236 { 00237 body->applyGravity(); 00238 body->integrateVelocities( timeStep); 00239 body->applyDamping(timeStep); 00240 body->predictIntegratedTransform(timeStep,body->getInterpolationWorldTransform()); 00241 } 00242 } 00243 } 00244 } 00245 } 00246 00247 00248 void btSimpleDynamicsWorld::synchronizeMotionStates() 00249 { 00251 for ( int i=0;i<m_collisionObjects.size();i++) 00252 { 00253 btCollisionObject* colObj = m_collisionObjects[i]; 00254 btRigidBody* body = btRigidBody::upcast(colObj); 00255 if (body && body->getMotionState()) 00256 { 00257 if (body->getActivationState() != ISLAND_SLEEPING) 00258 { 00259 body->getMotionState()->setWorldTransform(body->getWorldTransform()); 00260 } 00261 } 00262 } 00263 00264 } 00265 00266 00267 void btSimpleDynamicsWorld::setConstraintSolver(btConstraintSolver* solver) 00268 { 00269 if (m_ownsConstraintSolver) 00270 { 00271 btAlignedFree(m_constraintSolver); 00272 } 00273 m_ownsConstraintSolver = false; 00274 m_constraintSolver = solver; 00275 } 00276 00277 btConstraintSolver* btSimpleDynamicsWorld::getConstraintSolver() 00278 { 00279 return m_constraintSolver; 00280 }