1 | /* |
---|
2 | Bullet Continuous Collision Detection and Physics Library |
---|
3 | Copyright (c) 2003-2006 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 | ///create 125 (5x5x5) dynamic object |
---|
18 | #define ARRAY_SIZE_X 2 |
---|
19 | #define ARRAY_SIZE_Y 2 |
---|
20 | #define ARRAY_SIZE_Z 2 |
---|
21 | |
---|
22 | #define SIZE 2 |
---|
23 | |
---|
24 | //maximum number of objects (and allow user to shoot additional boxes) |
---|
25 | #define MAX_PROXIES (ARRAY_SIZE_X*ARRAY_SIZE_Y*ARRAY_SIZE_Z + 1024) |
---|
26 | |
---|
27 | ///scaling of the objects (0.1 = 20 centimeter boxes ) |
---|
28 | #define SCALING 2. |
---|
29 | #define START_POS_X -5 |
---|
30 | #define START_POS_Y -5 |
---|
31 | #define START_POS_Z -3 |
---|
32 | |
---|
33 | #define M_PI 3.14159265358979323846 |
---|
34 | #define HEIGHT 7 |
---|
35 | |
---|
36 | #include "BasicDemo.h" |
---|
37 | #include "btBulletDynamicsCommon.h" |
---|
38 | #include "GLDebugDrawer.h" |
---|
39 | |
---|
40 | #include <mpi.h> |
---|
41 | #include <stdio.h> |
---|
42 | #include <map> |
---|
43 | #include <string> |
---|
44 | #include <vector> |
---|
45 | |
---|
46 | using namespace std; |
---|
47 | |
---|
48 | btCollisionShape* groundShape; |
---|
49 | btTransform groundTransform; |
---|
50 | btCollisionShape* colShape; |
---|
51 | |
---|
52 | bool enable1, enable2; |
---|
53 | bool simpleSimulation; |
---|
54 | bool debugConstraints; |
---|
55 | |
---|
56 | btScalar BasicDemo::computeVelocity(btScalar currentAngle, btScalar targetAngle) { |
---|
57 | |
---|
58 | btScalar deltaAngle = targetAngle - currentAngle; |
---|
59 | |
---|
60 | if(deltaAngle < 0) |
---|
61 | deltaAngle += 2 * M_PI; |
---|
62 | |
---|
63 | if(deltaAngle > M_PI) |
---|
64 | deltaAngle -= 2 * M_PI; |
---|
65 | |
---|
66 | return deltaAngle; |
---|
67 | } |
---|
68 | |
---|
69 | void BasicDemo::updateSensors() { |
---|
70 | int i; |
---|
71 | btHingeConstraint *joint; |
---|
72 | Sensor *sensor; |
---|
73 | |
---|
74 | for(i = 0; i < m_sensors.size(); i++) { |
---|
75 | joint = (btHingeConstraint*) m_joints[m_sensors[i].first]; |
---|
76 | sensor = m_sensors[i].second; |
---|
77 | sensor->setAngle(joint->getHingeAngle()); |
---|
78 | } |
---|
79 | } |
---|
80 | |
---|
81 | void BasicDemo::updateActuators() { |
---|
82 | int i; |
---|
83 | btHingeConstraint *joint; |
---|
84 | Sensor *sensor; |
---|
85 | |
---|
86 | for(i = 0; i < m_sensors.size(); i++) { |
---|
87 | joint = (btHingeConstraint*) m_joints[m_sensors[i].first]; |
---|
88 | sensor = m_sensors[i].second; |
---|
89 | if (sensor->getForce() > 0.0f) |
---|
90 | joint->enableAngularMotor(true, computeVelocity(joint->getHingeAngle(), btScalar(sensor->getTargetAngle())) * 10, btScalar(sensor->getForce())); |
---|
91 | else |
---|
92 | joint->enableAngularMotor(false, 0, 0); |
---|
93 | } |
---|
94 | } |
---|
95 | |
---|
96 | void BasicDemo::updateAgents() { |
---|
97 | int i; |
---|
98 | |
---|
99 | for(i = 0; i < m_agents.size(); i++) { |
---|
100 | m_agents[i]->step(); |
---|
101 | } |
---|
102 | } |
---|
103 | |
---|
104 | |
---|
105 | void BasicDemo::clientMoveAndDisplay() { |
---|
106 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
---|
107 | //simple dynamics world doesn't handle fixed-time-stepping |
---|
108 | float ms = getDeltaTimeMicroseconds(); |
---|
109 | ///step the simulation |
---|
110 | if (m_dynamicsWorld) { |
---|
111 | if(simpleSimulation) { |
---|
112 | btHingeConstraint *hinge1 = (btHingeConstraint*)m_joints[0]; |
---|
113 | btHingeConstraint *hinge2 = (btHingeConstraint*)m_joints[1]; |
---|
114 | |
---|
115 | if(enable1) |
---|
116 | hinge1->enableAngularMotor(true, computeVelocity(hinge1->getHingeAngle(), btScalar(M_PI * 0.25)) * 10, btScalar(10)); |
---|
117 | else |
---|
118 | hinge1->enableAngularMotor(false, 0, 0); |
---|
119 | if(enable2) |
---|
120 | hinge2->enableAngularMotor(true, computeVelocity(hinge2->getHingeAngle(), btScalar(-M_PI * 0.25)) * 10, btScalar(50)); |
---|
121 | else |
---|
122 | hinge2->enableAngularMotor(false, 0, 0); |
---|
123 | } else { |
---|
124 | updateSensors(); |
---|
125 | updateAgents(); |
---|
126 | updateActuators(); |
---|
127 | } |
---|
128 | |
---|
129 | m_dynamicsWorld->stepSimulation(ms / 1000000.f); |
---|
130 | m_dynamicsWorld->debugDrawWorld(); |
---|
131 | } |
---|
132 | |
---|
133 | renderme(); |
---|
134 | glFlush(); |
---|
135 | glutSwapBuffers(); |
---|
136 | } |
---|
137 | |
---|
138 | void BasicDemo::displayCallback(void) { |
---|
139 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
---|
140 | renderme(); |
---|
141 | //optional but useful: debug drawing to detect problems |
---|
142 | if (m_dynamicsWorld) |
---|
143 | m_dynamicsWorld->debugDrawWorld(); |
---|
144 | glFlush(); |
---|
145 | glutSwapBuffers(); |
---|
146 | } |
---|
147 | |
---|
148 | void BasicDemo::debug() { |
---|
149 | |
---|
150 | } |
---|
151 | |
---|
152 | /** |
---|
153 | * Initialize the objects used for a normal simulation in Bullet. |
---|
154 | * Create the virtual world for the agents. |
---|
155 | * Set the ground of the simulated world |
---|
156 | */ |
---|
157 | void BasicDemo::initPhysics() { |
---|
158 | |
---|
159 | m_ele = 75; |
---|
160 | updateCamera(); |
---|
161 | |
---|
162 | setTexturing(true); |
---|
163 | setShadows(true); |
---|
164 | setCameraDistance(btScalar(SCALING*20.)); |
---|
165 | |
---|
166 | m_collisionConfiguration = new btDefaultCollisionConfiguration(); |
---|
167 | m_dispatcher = new btCollisionDispatcher(m_collisionConfiguration); |
---|
168 | m_broadphase = new btDbvtBroadphase(); |
---|
169 | btSequentialImpulseConstraintSolver* sol = new btSequentialImpulseConstraintSolver; |
---|
170 | m_solver = sol; |
---|
171 | |
---|
172 | m_dynamicsWorld = new btDiscreteDynamicsWorld(m_dispatcher,m_broadphase,m_solver,m_collisionConfiguration); |
---|
173 | m_dynamicsWorld->setGravity(btVector3(0,-10,0)); |
---|
174 | |
---|
175 | groundShape = new btBoxShape(btVector3(btScalar(50.),btScalar(50.),btScalar(50.))); |
---|
176 | m_collisionShapes.push_back(groundShape); |
---|
177 | |
---|
178 | groundTransform.setIdentity(); |
---|
179 | groundTransform.setOrigin(btVector3(0,-50,0)); |
---|
180 | |
---|
181 | btScalar mass(0.); |
---|
182 | //rigidbody is dynamic if and only if mass is non zero, otherwise static |
---|
183 | bool isDynamic = (mass != 0.f); |
---|
184 | btVector3 localInertia(0,0,0); |
---|
185 | if (isDynamic) |
---|
186 | groundShape->calculateLocalInertia(mass,localInertia); |
---|
187 | |
---|
188 | //using motionstate is recommended, it provides interpolation capabilities, and only synchronizes 'active' objects |
---|
189 | btDefaultMotionState* myMotionState = new btDefaultMotionState(groundTransform); |
---|
190 | btRigidBody::btRigidBodyConstructionInfo rbInfo(mass,myMotionState,groundShape,localInertia); |
---|
191 | btRigidBody* body = new btRigidBody(rbInfo); |
---|
192 | |
---|
193 | //add the body to the dynamics world |
---|
194 | m_dynamicsWorld->addRigidBody(body); |
---|
195 | |
---|
196 | m_dynamicsWorld->setDebugDrawer(new GLDebugDrawer()); |
---|
197 | } |
---|
198 | |
---|
199 | btRigidBody* BasicDemo::localCreateRigidBody (btScalar mass, const btTransform& startTransform, btCollisionShape* shape) { |
---|
200 | bool isDynamic = (mass != 0.f); |
---|
201 | |
---|
202 | btVector3 localInertia(0,0,0); |
---|
203 | if (isDynamic) |
---|
204 | shape->calculateLocalInertia(mass,localInertia); |
---|
205 | |
---|
206 | btDefaultMotionState* myMotionState = new btDefaultMotionState(startTransform); |
---|
207 | btRigidBody::btRigidBodyConstructionInfo rbInfo(mass,myMotionState,shape,localInertia); |
---|
208 | rbInfo.m_friction = 100.0; |
---|
209 | btRigidBody* body = new btRigidBody(rbInfo); |
---|
210 | |
---|
211 | m_dynamicsWorld->addRigidBody(body); |
---|
212 | |
---|
213 | return body; |
---|
214 | } |
---|
215 | |
---|
216 | btRigidBody* BasicDemo::createObject(btCollisionShape* colShape, btVector3 position, btScalar mass) { |
---|
217 | m_collisionShapes.push_back(colShape); |
---|
218 | |
---|
219 | btTransform startTransform; |
---|
220 | startTransform.setIdentity(); |
---|
221 | startTransform.setOrigin(SCALING*position); |
---|
222 | |
---|
223 | btRigidBody *body = localCreateRigidBody(mass, startTransform, colShape); |
---|
224 | body->setActivationState(ISLAND_SLEEPING); |
---|
225 | |
---|
226 | return body; |
---|
227 | } |
---|
228 | |
---|
229 | btRigidBody* BasicDemo::createObject(btCollisionShape* colShape, btTransform transform, btScalar mass) { |
---|
230 | m_collisionShapes.push_back(colShape); |
---|
231 | |
---|
232 | btRigidBody *body = localCreateRigidBody(mass, transform, colShape); |
---|
233 | body->setActivationState(ISLAND_SLEEPING); |
---|
234 | |
---|
235 | return body; |
---|
236 | } |
---|
237 | |
---|
238 | void BasicDemo::createHinge(bool enable) { |
---|
239 | |
---|
240 | simpleSimulation = enable; |
---|
241 | |
---|
242 | btCollisionShape* colShape1 = new btBoxShape(SCALING*btVector3(2, 0.1, 1)); |
---|
243 | btCollisionShape* colShape2 = new btBoxShape(SCALING*btVector3(2, 0.1, 1)); |
---|
244 | |
---|
245 | btRigidBody* part1 = createObject(colShape1, btVector3(0,6,0), 1.f); |
---|
246 | btRigidBody* part2 = createObject(colShape2, btVector3(2,6,0), 0.f); |
---|
247 | |
---|
248 | btTransform localA, localB; |
---|
249 | btHingeConstraint* hingeC; |
---|
250 | btHingeConstraint* hingeD; |
---|
251 | |
---|
252 | localA.setIdentity(); |
---|
253 | localB.setIdentity(); |
---|
254 | |
---|
255 | localA.setOrigin(SCALING*btVector3(2, 0, 0)); |
---|
256 | localB.setOrigin(SCALING*btVector3(-2, 0, 0)); |
---|
257 | |
---|
258 | hingeC = new btHingeConstraint(*part1, *part2, localA, localB); |
---|
259 | hingeD = new btHingeConstraint(*part1, *part2, localA, localB); |
---|
260 | //hingeC->setLimit(btScalar(-0.75 * M_PI_4), btScalar(M_PI_8)); |
---|
261 | //hingeC->setLimit(btScalar(-0.1), btScalar(0.1)); |
---|
262 | |
---|
263 | m_joints.push_back(hingeC); |
---|
264 | m_joints.push_back(hingeD); |
---|
265 | |
---|
266 | m_dynamicsWorld->addConstraint(hingeC, true); |
---|
267 | m_dynamicsWorld->addConstraint(hingeD, true); |
---|
268 | |
---|
269 | clientResetScene(); |
---|
270 | } |
---|
271 | |
---|
272 | enum { |
---|
273 | LEFT=0, RIGHT=1, UP=2, DOWN=3 |
---|
274 | }; |
---|
275 | |
---|
276 | void BasicDemo::createMagicCarpet(int n, int m) { |
---|
277 | int i, j, base = m_dynamicsWorld->getNumCollisionObjects(); |
---|
278 | btCollisionShape *shape; |
---|
279 | btRigidBody *body, *body1, *body2; |
---|
280 | btVector3 pivotA, pivotB, axisA, axisB; |
---|
281 | btHingeConstraint* hinge; |
---|
282 | btTransform transform; |
---|
283 | btTransform frameA, frameB; |
---|
284 | |
---|
285 | for(i = 0; i < n; i++) { |
---|
286 | for(j = 0; j < m; j++) { |
---|
287 | transform = btTransform(); |
---|
288 | transform.setIdentity(); |
---|
289 | transform.setOrigin(SCALING*btVector3(i*1.0,HEIGHT,j*1.0)); |
---|
290 | transform.setRotation(btQuaternion(0, 0, 0)); |
---|
291 | |
---|
292 | shape = new btSphereShape(SCALING*btScalar(0.1)); |
---|
293 | body = createObject(shape, transform, (!i && !j) ? 1.f : 1.f); |
---|
294 | |
---|
295 | btAlignedObjectArray<int> tmp; |
---|
296 | tmp.resize(4); |
---|
297 | m_matrix.push_back(tmp); |
---|
298 | } |
---|
299 | } |
---|
300 | |
---|
301 | for(i = 0; i < n; i++) { |
---|
302 | for(j = 0; j < m; j++) { |
---|
303 | if (i > 0) { |
---|
304 | transform = btTransform(); |
---|
305 | transform.setIdentity(); |
---|
306 | transform.setOrigin(SCALING*btVector3(i*1.0-0.5,HEIGHT,j*1.0)); |
---|
307 | transform.setRotation(btQuaternion(0, 0, -M_PI/2)); |
---|
308 | |
---|
309 | shape = new btCapsuleShape(SCALING*btScalar(0.05),SCALING*btScalar(0.6)); |
---|
310 | body = createObject(shape, transform, 1.f); |
---|
311 | |
---|
312 | body1 = btRigidBody::upcast(m_dynamicsWorld->getCollisionObjectArray()[base + (i - 1) * m + j]); |
---|
313 | body2 = btRigidBody::upcast(m_dynamicsWorld->getCollisionObjectArray()[base + i * m + j]); |
---|
314 | |
---|
315 | frameA = btTransform(); |
---|
316 | frameA.setIdentity(); |
---|
317 | frameA.setOrigin(SCALING*btVector3(0, -0.5, 0)); |
---|
318 | frameA.setRotation(btQuaternion(0, 0, M_PI/2)); |
---|
319 | frameB = btTransform(); |
---|
320 | frameB.setIdentity(); |
---|
321 | frameB.setOrigin(SCALING*btVector3(0, 0, 0)); |
---|
322 | frameB.setRotation(btQuaternion(0, 0, 0)); |
---|
323 | hinge = new btHingeConstraint(*body, *body1, frameA, frameB); |
---|
324 | hinge->setLimit(btScalar(- M_PI/2), btScalar(M_PI/2)); |
---|
325 | hinge->setDbgDrawSize(btScalar(1.f)); |
---|
326 | m_matrix[(i - 1) * m + j][RIGHT] = m_joints.size(); |
---|
327 | m_joints.push_back(hinge); |
---|
328 | m_dynamicsWorld->addConstraint(hinge, true); |
---|
329 | |
---|
330 | frameA = btTransform(); |
---|
331 | frameA.setIdentity(); |
---|
332 | frameA.setOrigin(SCALING*btVector3(0, 0.5, 0)); |
---|
333 | frameA.setRotation(btQuaternion(0, 0, M_PI/2)); |
---|
334 | frameB = btTransform(); |
---|
335 | frameB.setIdentity(); |
---|
336 | frameB.setOrigin(SCALING*btVector3(0, 0, 0)); |
---|
337 | frameB.setRotation(btQuaternion(0, 0, 0)); |
---|
338 | hinge = new btHingeConstraint(*body, *body2, frameA, frameB); |
---|
339 | hinge->setLimit(btScalar(- M_PI/2), btScalar(M_PI/2)); |
---|
340 | hinge->setDbgDrawSize(btScalar(1.f)); |
---|
341 | m_matrix[i * m + j][LEFT] = m_joints.size(); |
---|
342 | m_joints.push_back(hinge); |
---|
343 | m_dynamicsWorld->addConstraint(hinge, true); |
---|
344 | } |
---|
345 | if (j > 0) { |
---|
346 | transform = btTransform(); |
---|
347 | transform.setIdentity(); |
---|
348 | transform.setOrigin(SCALING*btVector3(i*1.0,HEIGHT,j*1.0-0.5)); |
---|
349 | transform.setRotation(btQuaternion(-M_PI/2, 0, -M_PI/2)); |
---|
350 | |
---|
351 | shape = new btCapsuleShape(SCALING*btScalar(0.05),SCALING*btScalar(0.6)); |
---|
352 | body = createObject(shape, transform, 1.f); |
---|
353 | |
---|
354 | body1 = btRigidBody::upcast(m_dynamicsWorld->getCollisionObjectArray()[base + i * m + (j - 1)]); |
---|
355 | body2 = btRigidBody::upcast(m_dynamicsWorld->getCollisionObjectArray()[base + i * m + j]); |
---|
356 | |
---|
357 | frameA = btTransform(); |
---|
358 | frameA.setIdentity(); |
---|
359 | frameA.setOrigin(SCALING*btVector3(0, -0.5, 0)); |
---|
360 | frameA.setRotation(btQuaternion(0, 0, 0)); |
---|
361 | frameB = btTransform(); |
---|
362 | frameB.setIdentity(); |
---|
363 | frameB.setOrigin(SCALING*btVector3(0, 0, 0)); |
---|
364 | frameB.setRotation(btQuaternion(-M_PI/2, 0, -M_PI/2)); |
---|
365 | hinge = new btHingeConstraint(*body, *body1, frameA, frameB); |
---|
366 | hinge->setLimit(btScalar(- M_PI/2), btScalar(M_PI/2)); |
---|
367 | hinge->setDbgDrawSize(btScalar(1.f)); |
---|
368 | m_matrix[i * m + (j - 1)][DOWN] = m_joints.size(); |
---|
369 | m_joints.push_back(hinge); |
---|
370 | m_dynamicsWorld->addConstraint(hinge, true); |
---|
371 | |
---|
372 | frameA = btTransform(); |
---|
373 | frameA.setIdentity(); |
---|
374 | frameA.setOrigin(SCALING*btVector3(0, 0.5, 0)); |
---|
375 | frameA.setRotation(btQuaternion(0, 0, 0)); |
---|
376 | frameB = btTransform(); |
---|
377 | frameB.setIdentity(); |
---|
378 | frameB.setOrigin(SCALING*btVector3(0, 0, 0)); |
---|
379 | frameB.setRotation(btQuaternion(-M_PI/2, 0, -M_PI/2)); |
---|
380 | hinge = new btHingeConstraint(*body, *body2, frameA, frameB); |
---|
381 | hinge->setLimit(btScalar(- M_PI/2), btScalar(M_PI/2)); |
---|
382 | hinge->setDbgDrawSize(btScalar(1.f)); |
---|
383 | m_matrix[i * m + j][UP] = m_joints.size(); |
---|
384 | m_joints.push_back(hinge); |
---|
385 | m_dynamicsWorld->addConstraint(hinge, true); |
---|
386 | } |
---|
387 | } |
---|
388 | } |
---|
389 | } |
---|
390 | |
---|
391 | |
---|
392 | void BasicDemo::createSwarm3D(int n, int m, int p) { |
---|
393 | /* |
---|
394 | int i, j, k, base = m_dynamicsWorld->getNumCollisionObjects(); |
---|
395 | btCollisionShape *shape; |
---|
396 | btRigidBody *body, *body1, *body2; |
---|
397 | btVector3 pivotA, pivotB, axisA, axisB; |
---|
398 | btHingeConstraint* hinge; |
---|
399 | btPoint2PointConstraint* hingeP2P; |
---|
400 | btTransform transform; |
---|
401 | for(i = 0; i < n; i++) { |
---|
402 | for(j = 0; j < m; j++) { |
---|
403 | for(k = 0; k < p; k++) { |
---|
404 | shape = new btSphereShape(SCALING*btScalar(0.18)); |
---|
405 | body = createObject(shape, btVector3(i*1.0,k*1.0 + HEIGHT,j*0.0), 0.f); |
---|
406 | } |
---|
407 | } |
---|
408 | } |
---|
409 | |
---|
410 | for(i = 0; i < n; i++) { |
---|
411 | for(j = 0; j < m; j++) { |
---|
412 | for(k = 0; k < p; k++) { |
---|
413 | if (i > 0) { |
---|
414 | transform.setIdentity(); |
---|
415 | transform.setOrigin(SCALING*btVector3(i*1.0-0.5,k*1.0+HEIGHT,j*1.0)); |
---|
416 | transform.setRotation(btQuaternion(btVector3(0,0,1), -M_PI/2)); |
---|
417 | |
---|
418 | shape = new btCapsuleShape(SCALING*btScalar(0.05),SCALING*btScalar(0.6)); |
---|
419 | body = createObject(shape, transform, 1.f); |
---|
420 | |
---|
421 | body1 = btRigidBody::upcast(m_dynamicsWorld->getCollisionObjectArray()[base + (i - 1) * m * p + j * p + k]); |
---|
422 | body2 = btRigidBody::upcast(m_dynamicsWorld->getCollisionObjectArray()[base + i * m * p + j * p + k]); |
---|
423 | |
---|
424 | pivotA = SCALING*btVector3(0, -0.6, 0); |
---|
425 | pivotB = SCALING*btVector3(0, 0, 0); |
---|
426 | axisA = btVector3(0, 0, 1); |
---|
427 | axisB = btVector3(0, 0, 1); |
---|
428 | hinge = new btHingeConstraint(*body, *body1, pivotA, pivotB, axisA, axisB); |
---|
429 | hinge->setLimit(btScalar(- 0.75 * M_PI), btScalar(0.75 * M_PI)); |
---|
430 | m_joints.push_back(hinge); |
---|
431 | m_dynamicsWorld->addConstraint(hinge, true); |
---|
432 | |
---|
433 | pivotA = SCALING*btVector3(0, 0.6, 0); |
---|
434 | pivotB = SCALING*btVector3(0, 0, 0); |
---|
435 | axisA = btVector3(0, 0, 1); |
---|
436 | axisB = btVector3(0, 0, 1); |
---|
437 | hinge = new btHingeConstraint(*body, *body2, pivotA, pivotB, axisA, axisB); |
---|
438 | hinge->setLimit(btScalar(- 0.75 * M_PI), btScalar(0.75 * M_PI)); |
---|
439 | m_joints.push_back(hinge); |
---|
440 | m_dynamicsWorld->addConstraint(hinge, true); |
---|
441 | } |
---|
442 | if (j > 0) { |
---|
443 | btTransform transform; |
---|
444 | transform.setIdentity(); |
---|
445 | transform.setOrigin(SCALING*btVector3(i*1.0,k*1.0+HEIGHT,j*1.0-0.5)); |
---|
446 | transform.setRotation(btQuaternion(btVector3(1,0,0), M_PI/2)); |
---|
447 | |
---|
448 | shape = new btCapsuleShape(SCALING*btScalar(0.05),SCALING*btScalar(0.6)); |
---|
449 | body = createObject(shape, transform, 1.f); |
---|
450 | |
---|
451 | body1 = btRigidBody::upcast(m_dynamicsWorld->getCollisionObjectArray()[base + i * m * p + (j - 1) * p+ k]); |
---|
452 | body2 = btRigidBody::upcast(m_dynamicsWorld->getCollisionObjectArray()[base + i * m * p + j * p + k]); |
---|
453 | |
---|
454 | pivotA = SCALING*btVector3(0, -0.6, 0); |
---|
455 | pivotB = SCALING*btVector3(0, 0, 0); |
---|
456 | axisA = btVector3(1, 0, 0); |
---|
457 | axisB = btVector3(1, 0, 0); |
---|
458 | hinge = new btHingeConstraint(*body, *body1, pivotA, pivotB, axisA, axisB); |
---|
459 | hinge->setLimit(btScalar(- 0.75 * M_PI), btScalar(0.75 * M_PI)); |
---|
460 | m_joints.push_back(hinge); |
---|
461 | m_dynamicsWorld->addConstraint(hinge, true); |
---|
462 | |
---|
463 | pivotA = SCALING*btVector3(0, 0.6, 0); |
---|
464 | pivotB = SCALING*btVector3(0, 0, 0); |
---|
465 | axisA = btVector3(1, 0, 0); |
---|
466 | axisB = btVector3(1, 0, 0); |
---|
467 | hinge = new btHingeConstraint(*body, *body2, pivotA, pivotB, axisA, axisB); |
---|
468 | hinge->setLimit(btScalar(- 0.75 * M_PI), btScalar(0.75 * M_PI)); |
---|
469 | m_joints.push_back(hinge); |
---|
470 | m_dynamicsWorld->addConstraint(hinge, true); |
---|
471 | } |
---|
472 | if(k > 0) { |
---|
473 | btTransform transform; |
---|
474 | transform.setIdentity(); |
---|
475 | transform.setOrigin(SCALING*btVector3(i*1.0,k*1.0+HEIGHT-0.5,j*1.0)); |
---|
476 | |
---|
477 | shape = new btCapsuleShape(SCALING*btScalar(0.05),SCALING*btScalar(0.6)); |
---|
478 | body = createObject(shape, transform, 1.f); |
---|
479 | |
---|
480 | body1 = btRigidBody::upcast(m_dynamicsWorld->getCollisionObjectArray()[base + i * m * p + j * p + (k - 1)]); |
---|
481 | body2 = btRigidBody::upcast(m_dynamicsWorld->getCollisionObjectArray()[base + i * m * p + j * p + k]); |
---|
482 | |
---|
483 | pivotA = SCALING*btVector3(0, -0.6, 0); |
---|
484 | pivotB = SCALING*btVector3(0, 0, 0); |
---|
485 | // axisA = btVector3(1, 0, 0); |
---|
486 | // axisB = btVector3(1, 0, 0); |
---|
487 | // hinge = new btHingeConstraint(*body, *body1, pivotA, pivotB, axisA, axisB); |
---|
488 | hingeP2P = new btPoint2PointConstraint(*body, *body1, pivotA, pivotB); |
---|
489 | // hinge->setLimit(btScalar(- 0.75 * M_PI), btScalar(0.75 * M_PI)); |
---|
490 | m_joints.push_back(hingeP2P); |
---|
491 | m_dynamicsWorld->addConstraint(hingeP2P, true); |
---|
492 | |
---|
493 | pivotA = SCALING*btVector3(0, 0.6, 0); |
---|
494 | pivotB = SCALING*btVector3(0, 0, 0); |
---|
495 | // axisA = btVector3(1, 0, 0); |
---|
496 | // axisB = btVector3(1, 0, 0); |
---|
497 | // hinge = new btHingeConstraint(*body, *body2, pivotA, pivotB, axisA, axisB); |
---|
498 | hingeP2P = new btPoint2PointConstraint(*body, *body2, pivotA, pivotB); |
---|
499 | // hinge->setLimit(btScalar(- 0.75 * M_PI), btScalar(0.75 * M_PI)); |
---|
500 | m_joints.push_back(hingeP2P); |
---|
501 | m_dynamicsWorld->addConstraint(hingeP2P, true); |
---|
502 | } |
---|
503 | } |
---|
504 | } |
---|
505 | } |
---|
506 | */ |
---|
507 | } |
---|
508 | |
---|
509 | |
---|
510 | void BasicDemo::createSimulation2D(int n, int m) { |
---|
511 | //btCollisionShape* colShape = new btSphereShape(SCALING*btScalar(1.5)); |
---|
512 | //btRigidBody *ball = createObject(colShape, btVector3(4,0,3), 8); |
---|
513 | //ball->activate(); |
---|
514 | |
---|
515 | createMagicCarpet(n, m); |
---|
516 | } |
---|
517 | |
---|
518 | void BasicDemo::createSimulation3D(int n, int m, int p) { |
---|
519 | createSwarm3D(n,m,p); |
---|
520 | } |
---|
521 | |
---|
522 | void BasicDemo::keyboardCallback(unsigned char key, int x, int y) { |
---|
523 | if(key == '1') { |
---|
524 | enable1 = !enable1; |
---|
525 | return; |
---|
526 | } |
---|
527 | |
---|
528 | if(key == '2') { |
---|
529 | enable2 = !enable2; |
---|
530 | return; |
---|
531 | } |
---|
532 | |
---|
533 | if(key == 'b') { |
---|
534 | btCollisionShape* colShape0 = new btSphereShape(SCALING*btScalar(0.5)); |
---|
535 | btRigidBody* box = createObject(colShape0, btVector3(-2., 10, 0.5), 4.f); |
---|
536 | |
---|
537 | box->activate(); |
---|
538 | |
---|
539 | return; |
---|
540 | } |
---|
541 | |
---|
542 | if (key == 'c') { |
---|
543 | debugConstraints = !debugConstraints; |
---|
544 | this->setDebugMode(debugConstraints ? btIDebugDraw::DBG_DrawConstraints | btIDebugDraw::DBG_DrawConstraintLimits : 0); |
---|
545 | } |
---|
546 | |
---|
547 | DemoApplication::keyboardCallback(key, x, y); |
---|
548 | } |
---|
549 | |
---|
550 | |
---|
551 | void BasicDemo::exitPhysics() { |
---|
552 | //cleanup in the reverse order of creation/initialization |
---|
553 | //remove the rigidbodies from the dynamics world and delete them |
---|
554 | int i; |
---|
555 | |
---|
556 | for (i = 0; i < m_joints.size(); i++) { |
---|
557 | btTypedConstraint *joint = m_joints[i]; |
---|
558 | delete joint; |
---|
559 | } |
---|
560 | |
---|
561 | for (i=m_dynamicsWorld->getNumCollisionObjects()-1; i>=0 ;i--) { |
---|
562 | btCollisionObject* obj = m_dynamicsWorld->getCollisionObjectArray()[i]; |
---|
563 | btRigidBody* body = btRigidBody::upcast(obj); |
---|
564 | if (body && body->getMotionState()) |
---|
565 | { |
---|
566 | delete body->getMotionState(); |
---|
567 | } |
---|
568 | m_dynamicsWorld->removeCollisionObject( obj ); |
---|
569 | delete obj; |
---|
570 | } |
---|
571 | |
---|
572 | //delete collision shapes |
---|
573 | for (i = 0; i < m_collisionShapes.size(); i++) { |
---|
574 | btCollisionShape* shape = m_collisionShapes[i]; |
---|
575 | delete shape; |
---|
576 | } |
---|
577 | |
---|
578 | delete m_dynamicsWorld; |
---|
579 | |
---|
580 | delete m_solver; |
---|
581 | |
---|
582 | delete m_broadphase; |
---|
583 | |
---|
584 | delete m_dispatcher; |
---|
585 | |
---|
586 | delete m_collisionConfiguration; |
---|
587 | } |
---|