| [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 | |
|---|
| [8351] | 51 | if(0 != m_internalPreTickCallback) { |
|---|
| 52 | (*m_internalPreTickCallback)(this, timeStep); |
|---|
| 53 | } |
|---|
| [1963] | 54 | |
|---|
| [8351] | 55 | |
|---|
| [1963] | 56 | ///update aabbs information |
|---|
| 57 | updateAabbs(); |
|---|
| 58 | //static int frame=0; |
|---|
| 59 | // printf("frame %d\n",frame++); |
|---|
| 60 | |
|---|
| 61 | ///apply gravity, predict motion |
|---|
| 62 | predictUnconstraintMotion(timeStep); |
|---|
| 63 | |
|---|
| 64 | btDispatcherInfo& dispatchInfo = getDispatchInfo(); |
|---|
| 65 | |
|---|
| 66 | dispatchInfo.m_timeStep = timeStep; |
|---|
| 67 | dispatchInfo.m_stepCount = 0; |
|---|
| 68 | dispatchInfo.m_debugDraw = getDebugDrawer(); |
|---|
| 69 | |
|---|
| 70 | ///perform collision detection |
|---|
| 71 | performDiscreteCollisionDetection(); |
|---|
| 72 | |
|---|
| 73 | calculateSimulationIslands(); |
|---|
| 74 | |
|---|
| 75 | |
|---|
| 76 | getSolverInfo().m_timeStep = timeStep; |
|---|
| 77 | |
|---|
| 78 | |
|---|
| 79 | |
|---|
| 80 | ///solve contact and other joint constraints |
|---|
| 81 | solveConstraints(getSolverInfo()); |
|---|
| 82 | |
|---|
| 83 | ///CallbackTriggers(); |
|---|
| 84 | calculateTimeOfImpacts(timeStep); |
|---|
| 85 | |
|---|
| 86 | btScalar toi = dispatchInfo.m_timeOfImpact; |
|---|
| 87 | // if (toi < 1.f) |
|---|
| 88 | // printf("toi = %f\n",toi); |
|---|
| 89 | if (toi < 0.f) |
|---|
| 90 | printf("toi = %f\n",toi); |
|---|
| 91 | |
|---|
| 92 | |
|---|
| 93 | ///integrate transforms |
|---|
| 94 | integrateTransforms(timeStep * toi); |
|---|
| 95 | |
|---|
| 96 | ///update vehicle simulation |
|---|
| [2882] | 97 | updateActions(timeStep); |
|---|
| [1963] | 98 | |
|---|
| 99 | updateActivationState( timeStep ); |
|---|
| 100 | |
|---|
| 101 | if(0 != m_internalTickCallback) { |
|---|
| 102 | (*m_internalTickCallback)(this, timeStep); |
|---|
| 103 | } |
|---|
| 104 | } |
|---|
| 105 | |
|---|
| 106 | void btContinuousDynamicsWorld::calculateTimeOfImpacts(btScalar timeStep) |
|---|
| 107 | { |
|---|
| 108 | ///these should be 'temporal' aabbs! |
|---|
| 109 | updateTemporalAabbs(timeStep); |
|---|
| 110 | |
|---|
| 111 | ///'toi' is the global smallest time of impact. However, we just calculate the time of impact for each object individually. |
|---|
| 112 | ///so we handle the case moving versus static properly, and we cheat for moving versus moving |
|---|
| 113 | btScalar toi = 1.f; |
|---|
| 114 | |
|---|
| 115 | |
|---|
| 116 | btDispatcherInfo& dispatchInfo = getDispatchInfo(); |
|---|
| 117 | dispatchInfo.m_timeStep = timeStep; |
|---|
| 118 | dispatchInfo.m_timeOfImpact = 1.f; |
|---|
| 119 | dispatchInfo.m_stepCount = 0; |
|---|
| 120 | dispatchInfo.m_dispatchFunc = btDispatcherInfo::DISPATCH_CONTINUOUS; |
|---|
| 121 | |
|---|
| 122 | ///calculate time of impact for overlapping pairs |
|---|
| 123 | |
|---|
| 124 | |
|---|
| 125 | btDispatcher* dispatcher = getDispatcher(); |
|---|
| 126 | if (dispatcher) |
|---|
| 127 | dispatcher->dispatchAllCollisionPairs(m_broadphasePairCache->getOverlappingPairCache(),dispatchInfo,m_dispatcher1); |
|---|
| 128 | |
|---|
| 129 | toi = dispatchInfo.m_timeOfImpact; |
|---|
| 130 | |
|---|
| 131 | dispatchInfo.m_dispatchFunc = btDispatcherInfo::DISPATCH_DISCRETE; |
|---|
| 132 | |
|---|
| 133 | } |
|---|
| 134 | |
|---|
| 135 | void btContinuousDynamicsWorld::updateTemporalAabbs(btScalar timeStep) |
|---|
| 136 | { |
|---|
| 137 | |
|---|
| 138 | btVector3 temporalAabbMin,temporalAabbMax; |
|---|
| 139 | |
|---|
| 140 | for ( int i=0;i<m_collisionObjects.size();i++) |
|---|
| 141 | { |
|---|
| 142 | btCollisionObject* colObj = m_collisionObjects[i]; |
|---|
| 143 | |
|---|
| 144 | btRigidBody* body = btRigidBody::upcast(colObj); |
|---|
| 145 | if (body) |
|---|
| 146 | { |
|---|
| 147 | body->getCollisionShape()->getAabb(m_collisionObjects[i]->getWorldTransform(),temporalAabbMin,temporalAabbMax); |
|---|
| 148 | const btVector3& linvel = body->getLinearVelocity(); |
|---|
| 149 | |
|---|
| 150 | //make the AABB temporal |
|---|
| 151 | btScalar temporalAabbMaxx = temporalAabbMax.getX(); |
|---|
| 152 | btScalar temporalAabbMaxy = temporalAabbMax.getY(); |
|---|
| 153 | btScalar temporalAabbMaxz = temporalAabbMax.getZ(); |
|---|
| 154 | btScalar temporalAabbMinx = temporalAabbMin.getX(); |
|---|
| 155 | btScalar temporalAabbMiny = temporalAabbMin.getY(); |
|---|
| 156 | btScalar temporalAabbMinz = temporalAabbMin.getZ(); |
|---|
| 157 | |
|---|
| 158 | // add linear motion |
|---|
| 159 | btVector3 linMotion = linvel*timeStep; |
|---|
| 160 | |
|---|
| 161 | if (linMotion.x() > 0.f) |
|---|
| 162 | temporalAabbMaxx += linMotion.x(); |
|---|
| 163 | else |
|---|
| 164 | temporalAabbMinx += linMotion.x(); |
|---|
| 165 | if (linMotion.y() > 0.f) |
|---|
| 166 | temporalAabbMaxy += linMotion.y(); |
|---|
| 167 | else |
|---|
| 168 | temporalAabbMiny += linMotion.y(); |
|---|
| 169 | if (linMotion.z() > 0.f) |
|---|
| 170 | temporalAabbMaxz += linMotion.z(); |
|---|
| 171 | else |
|---|
| 172 | temporalAabbMinz += linMotion.z(); |
|---|
| 173 | |
|---|
| 174 | //add conservative angular motion |
|---|
| 175 | btScalar angularMotion(0);// = angvel.length() * GetAngularMotionDisc() * timeStep; |
|---|
| 176 | btVector3 angularMotion3d(angularMotion,angularMotion,angularMotion); |
|---|
| 177 | temporalAabbMin = btVector3(temporalAabbMinx,temporalAabbMiny,temporalAabbMinz); |
|---|
| 178 | temporalAabbMax = btVector3(temporalAabbMaxx,temporalAabbMaxy,temporalAabbMaxz); |
|---|
| 179 | |
|---|
| 180 | temporalAabbMin -= angularMotion3d; |
|---|
| 181 | temporalAabbMax += angularMotion3d; |
|---|
| 182 | |
|---|
| 183 | m_broadphasePairCache->setAabb(body->getBroadphaseHandle(),temporalAabbMin,temporalAabbMax,m_dispatcher1); |
|---|
| 184 | } |
|---|
| 185 | } |
|---|
| 186 | |
|---|
| 187 | //update aabb (of all moved objects) |
|---|
| 188 | |
|---|
| 189 | m_broadphasePairCache->calculateOverlappingPairs(m_dispatcher1); |
|---|
| 190 | |
|---|
| 191 | |
|---|
| 192 | |
|---|
| 193 | } |
|---|
| 194 | |
|---|
| 195 | |
|---|
| 196 | |
|---|