[1963] | 1 | /* |
---|
| 2 | Bullet Continuous Collision Detection and Physics Library |
---|
| 3 | Copyright (c) 2003-2007 Erwin Coumans http://continuousphysics.com/Bullet/ |
---|
| 4 | |
---|
| 5 | This software is provided 'as-is', without any express or implied warranty. |
---|
| 6 | In no event will the authors be held liable for any damages arising from the use of this software. |
---|
| 7 | Permission is granted to anyone to use this software for any purpose, |
---|
| 8 | including commercial applications, and to alter it and redistribute it freely, |
---|
| 9 | subject to the following restrictions: |
---|
| 10 | |
---|
| 11 | 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. |
---|
| 12 | 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. |
---|
| 13 | 3. This notice may not be removed or altered from any source distribution. |
---|
| 14 | */ |
---|
| 15 | |
---|
| 16 | |
---|
| 17 | #include "btContinuousDynamicsWorld.h" |
---|
| 18 | #include "LinearMath/btQuickprof.h" |
---|
| 19 | |
---|
| 20 | //collision detection |
---|
| 21 | #include "BulletCollision/CollisionDispatch/btCollisionDispatcher.h" |
---|
| 22 | #include "BulletCollision/BroadphaseCollision/btSimpleBroadphase.h" |
---|
| 23 | #include "BulletCollision/CollisionShapes/btCollisionShape.h" |
---|
| 24 | #include "BulletCollision/CollisionDispatch/btSimulationIslandManager.h" |
---|
| 25 | |
---|
| 26 | //rigidbody & constraints |
---|
| 27 | #include "BulletDynamics/Dynamics/btRigidBody.h" |
---|
| 28 | #include "BulletDynamics/ConstraintSolver/btSequentialImpulseConstraintSolver.h" |
---|
| 29 | #include "BulletDynamics/ConstraintSolver/btContactSolverInfo.h" |
---|
| 30 | #include "BulletDynamics/ConstraintSolver/btTypedConstraint.h" |
---|
| 31 | |
---|
| 32 | |
---|
| 33 | |
---|
| 34 | #include <stdio.h> |
---|
| 35 | |
---|
| 36 | btContinuousDynamicsWorld::btContinuousDynamicsWorld(btDispatcher* dispatcher,btBroadphaseInterface* pairCache,btConstraintSolver* constraintSolver,btCollisionConfiguration* collisionConfiguration) |
---|
| 37 | :btDiscreteDynamicsWorld(dispatcher,pairCache,constraintSolver,collisionConfiguration) |
---|
| 38 | { |
---|
| 39 | } |
---|
| 40 | |
---|
| 41 | btContinuousDynamicsWorld::~btContinuousDynamicsWorld() |
---|
| 42 | { |
---|
| 43 | } |
---|
| 44 | |
---|
| 45 | |
---|
| 46 | void btContinuousDynamicsWorld::internalSingleStepSimulation( btScalar timeStep) |
---|
| 47 | { |
---|
| 48 | |
---|
| 49 | startProfiling(timeStep); |
---|
| 50 | |
---|
| 51 | |
---|
| 52 | ///update aabbs information |
---|
| 53 | updateAabbs(); |
---|
| 54 | //static int frame=0; |
---|
| 55 | // printf("frame %d\n",frame++); |
---|
| 56 | |
---|
| 57 | ///apply gravity, predict motion |
---|
| 58 | predictUnconstraintMotion(timeStep); |
---|
| 59 | |
---|
| 60 | btDispatcherInfo& dispatchInfo = getDispatchInfo(); |
---|
| 61 | |
---|
| 62 | dispatchInfo.m_timeStep = timeStep; |
---|
| 63 | dispatchInfo.m_stepCount = 0; |
---|
| 64 | dispatchInfo.m_debugDraw = getDebugDrawer(); |
---|
| 65 | |
---|
| 66 | ///perform collision detection |
---|
| 67 | performDiscreteCollisionDetection(); |
---|
| 68 | |
---|
| 69 | calculateSimulationIslands(); |
---|
| 70 | |
---|
| 71 | |
---|
| 72 | getSolverInfo().m_timeStep = timeStep; |
---|
| 73 | |
---|
| 74 | |
---|
| 75 | |
---|
| 76 | ///solve contact and other joint constraints |
---|
| 77 | solveConstraints(getSolverInfo()); |
---|
| 78 | |
---|
| 79 | ///CallbackTriggers(); |
---|
| 80 | calculateTimeOfImpacts(timeStep); |
---|
| 81 | |
---|
| 82 | btScalar toi = dispatchInfo.m_timeOfImpact; |
---|
| 83 | // if (toi < 1.f) |
---|
| 84 | // printf("toi = %f\n",toi); |
---|
| 85 | if (toi < 0.f) |
---|
| 86 | printf("toi = %f\n",toi); |
---|
| 87 | |
---|
| 88 | |
---|
| 89 | ///integrate transforms |
---|
| 90 | integrateTransforms(timeStep * toi); |
---|
| 91 | |
---|
| 92 | ///update vehicle simulation |
---|
[2882] | 93 | updateActions(timeStep); |
---|
[1963] | 94 | |
---|
| 95 | updateActivationState( timeStep ); |
---|
| 96 | |
---|
| 97 | if(0 != m_internalTickCallback) { |
---|
| 98 | (*m_internalTickCallback)(this, timeStep); |
---|
| 99 | } |
---|
| 100 | } |
---|
| 101 | |
---|
| 102 | void btContinuousDynamicsWorld::calculateTimeOfImpacts(btScalar timeStep) |
---|
| 103 | { |
---|
| 104 | ///these should be 'temporal' aabbs! |
---|
| 105 | updateTemporalAabbs(timeStep); |
---|
| 106 | |
---|
| 107 | ///'toi' is the global smallest time of impact. However, we just calculate the time of impact for each object individually. |
---|
| 108 | ///so we handle the case moving versus static properly, and we cheat for moving versus moving |
---|
| 109 | btScalar toi = 1.f; |
---|
| 110 | |
---|
| 111 | |
---|
| 112 | btDispatcherInfo& dispatchInfo = getDispatchInfo(); |
---|
| 113 | dispatchInfo.m_timeStep = timeStep; |
---|
| 114 | dispatchInfo.m_timeOfImpact = 1.f; |
---|
| 115 | dispatchInfo.m_stepCount = 0; |
---|
| 116 | dispatchInfo.m_dispatchFunc = btDispatcherInfo::DISPATCH_CONTINUOUS; |
---|
| 117 | |
---|
| 118 | ///calculate time of impact for overlapping pairs |
---|
| 119 | |
---|
| 120 | |
---|
| 121 | btDispatcher* dispatcher = getDispatcher(); |
---|
| 122 | if (dispatcher) |
---|
| 123 | dispatcher->dispatchAllCollisionPairs(m_broadphasePairCache->getOverlappingPairCache(),dispatchInfo,m_dispatcher1); |
---|
| 124 | |
---|
| 125 | toi = dispatchInfo.m_timeOfImpact; |
---|
| 126 | |
---|
| 127 | dispatchInfo.m_dispatchFunc = btDispatcherInfo::DISPATCH_DISCRETE; |
---|
| 128 | |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | void btContinuousDynamicsWorld::updateTemporalAabbs(btScalar timeStep) |
---|
| 132 | { |
---|
| 133 | |
---|
| 134 | btVector3 temporalAabbMin,temporalAabbMax; |
---|
| 135 | |
---|
| 136 | for ( int i=0;i<m_collisionObjects.size();i++) |
---|
| 137 | { |
---|
| 138 | btCollisionObject* colObj = m_collisionObjects[i]; |
---|
| 139 | |
---|
| 140 | btRigidBody* body = btRigidBody::upcast(colObj); |
---|
| 141 | if (body) |
---|
| 142 | { |
---|
| 143 | body->getCollisionShape()->getAabb(m_collisionObjects[i]->getWorldTransform(),temporalAabbMin,temporalAabbMax); |
---|
| 144 | const btVector3& linvel = body->getLinearVelocity(); |
---|
| 145 | |
---|
| 146 | //make the AABB temporal |
---|
| 147 | btScalar temporalAabbMaxx = temporalAabbMax.getX(); |
---|
| 148 | btScalar temporalAabbMaxy = temporalAabbMax.getY(); |
---|
| 149 | btScalar temporalAabbMaxz = temporalAabbMax.getZ(); |
---|
| 150 | btScalar temporalAabbMinx = temporalAabbMin.getX(); |
---|
| 151 | btScalar temporalAabbMiny = temporalAabbMin.getY(); |
---|
| 152 | btScalar temporalAabbMinz = temporalAabbMin.getZ(); |
---|
| 153 | |
---|
| 154 | // add linear motion |
---|
| 155 | btVector3 linMotion = linvel*timeStep; |
---|
| 156 | |
---|
| 157 | if (linMotion.x() > 0.f) |
---|
| 158 | temporalAabbMaxx += linMotion.x(); |
---|
| 159 | else |
---|
| 160 | temporalAabbMinx += linMotion.x(); |
---|
| 161 | if (linMotion.y() > 0.f) |
---|
| 162 | temporalAabbMaxy += linMotion.y(); |
---|
| 163 | else |
---|
| 164 | temporalAabbMiny += linMotion.y(); |
---|
| 165 | if (linMotion.z() > 0.f) |
---|
| 166 | temporalAabbMaxz += linMotion.z(); |
---|
| 167 | else |
---|
| 168 | temporalAabbMinz += linMotion.z(); |
---|
| 169 | |
---|
| 170 | //add conservative angular motion |
---|
| 171 | btScalar angularMotion(0);// = angvel.length() * GetAngularMotionDisc() * timeStep; |
---|
| 172 | btVector3 angularMotion3d(angularMotion,angularMotion,angularMotion); |
---|
| 173 | temporalAabbMin = btVector3(temporalAabbMinx,temporalAabbMiny,temporalAabbMinz); |
---|
| 174 | temporalAabbMax = btVector3(temporalAabbMaxx,temporalAabbMaxy,temporalAabbMaxz); |
---|
| 175 | |
---|
| 176 | temporalAabbMin -= angularMotion3d; |
---|
| 177 | temporalAabbMax += angularMotion3d; |
---|
| 178 | |
---|
| 179 | m_broadphasePairCache->setAabb(body->getBroadphaseHandle(),temporalAabbMin,temporalAabbMax,m_dispatcher1); |
---|
| 180 | } |
---|
| 181 | } |
---|
| 182 | |
---|
| 183 | //update aabb (of all moved objects) |
---|
| 184 | |
---|
| 185 | m_broadphasePairCache->calculateOverlappingPairs(m_dispatcher1); |
---|
| 186 | |
---|
| 187 | |
---|
| 188 | |
---|
| 189 | } |
---|
| 190 | |
---|
| 191 | |
---|
| 192 | |
---|