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 |
---|
93 | updateVehicles(timeStep); |
---|
94 | |
---|
95 | |
---|
96 | updateActivationState( timeStep ); |
---|
97 | |
---|
98 | if(0 != m_internalTickCallback) { |
---|
99 | (*m_internalTickCallback)(this, timeStep); |
---|
100 | } |
---|
101 | } |
---|
102 | |
---|
103 | void btContinuousDynamicsWorld::calculateTimeOfImpacts(btScalar timeStep) |
---|
104 | { |
---|
105 | ///these should be 'temporal' aabbs! |
---|
106 | updateTemporalAabbs(timeStep); |
---|
107 | |
---|
108 | ///'toi' is the global smallest time of impact. However, we just calculate the time of impact for each object individually. |
---|
109 | ///so we handle the case moving versus static properly, and we cheat for moving versus moving |
---|
110 | btScalar toi = 1.f; |
---|
111 | |
---|
112 | |
---|
113 | btDispatcherInfo& dispatchInfo = getDispatchInfo(); |
---|
114 | dispatchInfo.m_timeStep = timeStep; |
---|
115 | dispatchInfo.m_timeOfImpact = 1.f; |
---|
116 | dispatchInfo.m_stepCount = 0; |
---|
117 | dispatchInfo.m_dispatchFunc = btDispatcherInfo::DISPATCH_CONTINUOUS; |
---|
118 | |
---|
119 | ///calculate time of impact for overlapping pairs |
---|
120 | |
---|
121 | |
---|
122 | btDispatcher* dispatcher = getDispatcher(); |
---|
123 | if (dispatcher) |
---|
124 | dispatcher->dispatchAllCollisionPairs(m_broadphasePairCache->getOverlappingPairCache(),dispatchInfo,m_dispatcher1); |
---|
125 | |
---|
126 | toi = dispatchInfo.m_timeOfImpact; |
---|
127 | |
---|
128 | dispatchInfo.m_dispatchFunc = btDispatcherInfo::DISPATCH_DISCRETE; |
---|
129 | |
---|
130 | } |
---|
131 | |
---|
132 | void btContinuousDynamicsWorld::updateTemporalAabbs(btScalar timeStep) |
---|
133 | { |
---|
134 | |
---|
135 | btVector3 temporalAabbMin,temporalAabbMax; |
---|
136 | |
---|
137 | for ( int i=0;i<m_collisionObjects.size();i++) |
---|
138 | { |
---|
139 | btCollisionObject* colObj = m_collisionObjects[i]; |
---|
140 | |
---|
141 | btRigidBody* body = btRigidBody::upcast(colObj); |
---|
142 | if (body) |
---|
143 | { |
---|
144 | body->getCollisionShape()->getAabb(m_collisionObjects[i]->getWorldTransform(),temporalAabbMin,temporalAabbMax); |
---|
145 | const btVector3& linvel = body->getLinearVelocity(); |
---|
146 | |
---|
147 | //make the AABB temporal |
---|
148 | btScalar temporalAabbMaxx = temporalAabbMax.getX(); |
---|
149 | btScalar temporalAabbMaxy = temporalAabbMax.getY(); |
---|
150 | btScalar temporalAabbMaxz = temporalAabbMax.getZ(); |
---|
151 | btScalar temporalAabbMinx = temporalAabbMin.getX(); |
---|
152 | btScalar temporalAabbMiny = temporalAabbMin.getY(); |
---|
153 | btScalar temporalAabbMinz = temporalAabbMin.getZ(); |
---|
154 | |
---|
155 | // add linear motion |
---|
156 | btVector3 linMotion = linvel*timeStep; |
---|
157 | |
---|
158 | if (linMotion.x() > 0.f) |
---|
159 | temporalAabbMaxx += linMotion.x(); |
---|
160 | else |
---|
161 | temporalAabbMinx += linMotion.x(); |
---|
162 | if (linMotion.y() > 0.f) |
---|
163 | temporalAabbMaxy += linMotion.y(); |
---|
164 | else |
---|
165 | temporalAabbMiny += linMotion.y(); |
---|
166 | if (linMotion.z() > 0.f) |
---|
167 | temporalAabbMaxz += linMotion.z(); |
---|
168 | else |
---|
169 | temporalAabbMinz += linMotion.z(); |
---|
170 | |
---|
171 | //add conservative angular motion |
---|
172 | btScalar angularMotion(0);// = angvel.length() * GetAngularMotionDisc() * timeStep; |
---|
173 | btVector3 angularMotion3d(angularMotion,angularMotion,angularMotion); |
---|
174 | temporalAabbMin = btVector3(temporalAabbMinx,temporalAabbMiny,temporalAabbMinz); |
---|
175 | temporalAabbMax = btVector3(temporalAabbMaxx,temporalAabbMaxy,temporalAabbMaxz); |
---|
176 | |
---|
177 | temporalAabbMin -= angularMotion3d; |
---|
178 | temporalAabbMax += angularMotion3d; |
---|
179 | |
---|
180 | m_broadphasePairCache->setAabb(body->getBroadphaseHandle(),temporalAabbMin,temporalAabbMax,m_dispatcher1); |
---|
181 | } |
---|
182 | } |
---|
183 | |
---|
184 | //update aabb (of all moved objects) |
---|
185 | |
---|
186 | m_broadphasePairCache->calculateOverlappingPairs(m_dispatcher1); |
---|
187 | |
---|
188 | |
---|
189 | |
---|
190 | } |
---|
191 | |
---|
192 | |
---|
193 | |
---|