1 | /* |
---|
2 | This source file is part of GIMPACT Library. |
---|
3 | |
---|
4 | For the latest info, see http://gimpact.sourceforge.net/ |
---|
5 | |
---|
6 | Copyright (c) 2007 Francisco Leon Najera. C.C. 80087371. |
---|
7 | email: projectileman@yahoo.com |
---|
8 | |
---|
9 | |
---|
10 | This software is provided 'as-is', without any express or implied warranty. |
---|
11 | In no event will the authors be held liable for any damages arising from the use of this software. |
---|
12 | Permission is granted to anyone to use this software for any purpose, |
---|
13 | including commercial applications, and to alter it and redistribute it freely, |
---|
14 | subject to the following restrictions: |
---|
15 | |
---|
16 | 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. |
---|
17 | 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. |
---|
18 | 3. This notice may not be removed or altered from any source distribution. |
---|
19 | */ |
---|
20 | /* |
---|
21 | Author: Francisco Len Nßjera |
---|
22 | Concave-Concave Collision |
---|
23 | |
---|
24 | */ |
---|
25 | |
---|
26 | #include "BulletCollision/CollisionDispatch/btManifoldResult.h" |
---|
27 | #include "LinearMath/btIDebugDraw.h" |
---|
28 | #include "BulletCollision/CollisionDispatch/btCollisionObject.h" |
---|
29 | #include "BulletCollision/CollisionShapes/btBoxShape.h" |
---|
30 | #include "btGImpactCollisionAlgorithm.h" |
---|
31 | #include "btContactProcessing.h" |
---|
32 | #include "LinearMath/btQuickprof.h" |
---|
33 | |
---|
34 | |
---|
35 | //! Class for accessing the plane equation |
---|
36 | class btPlaneShape : public btStaticPlaneShape |
---|
37 | { |
---|
38 | public: |
---|
39 | |
---|
40 | btPlaneShape(const btVector3& v, float f) |
---|
41 | :btStaticPlaneShape(v,f) |
---|
42 | { |
---|
43 | } |
---|
44 | |
---|
45 | void get_plane_equation(btVector4 &equation) |
---|
46 | { |
---|
47 | equation[0] = m_planeNormal[0]; |
---|
48 | equation[1] = m_planeNormal[1]; |
---|
49 | equation[2] = m_planeNormal[2]; |
---|
50 | equation[3] = m_planeConstant; |
---|
51 | } |
---|
52 | |
---|
53 | |
---|
54 | void get_plane_equation_transformed(const btTransform & trans,btVector4 &equation) |
---|
55 | { |
---|
56 | equation[0] = trans.getBasis().getRow(0).dot(m_planeNormal); |
---|
57 | equation[1] = trans.getBasis().getRow(1).dot(m_planeNormal); |
---|
58 | equation[2] = trans.getBasis().getRow(2).dot(m_planeNormal); |
---|
59 | equation[3] = trans.getOrigin().dot(m_planeNormal) + m_planeConstant; |
---|
60 | } |
---|
61 | }; |
---|
62 | |
---|
63 | |
---|
64 | |
---|
65 | ////////////////////////////////////////////////////////////////////////////////////////////// |
---|
66 | #ifdef TRI_COLLISION_PROFILING |
---|
67 | |
---|
68 | btClock g_triangle_clock; |
---|
69 | |
---|
70 | float g_accum_triangle_collision_time = 0; |
---|
71 | int g_count_triangle_collision = 0; |
---|
72 | |
---|
73 | void bt_begin_gim02_tri_time() |
---|
74 | { |
---|
75 | g_triangle_clock.reset(); |
---|
76 | } |
---|
77 | |
---|
78 | void bt_end_gim02_tri_time() |
---|
79 | { |
---|
80 | g_accum_triangle_collision_time += g_triangle_clock.getTimeMicroseconds(); |
---|
81 | g_count_triangle_collision++; |
---|
82 | } |
---|
83 | #endif //TRI_COLLISION_PROFILING |
---|
84 | //! Retrieving shapes shapes |
---|
85 | /*! |
---|
86 | Declared here due of insuficent space on Pool allocators |
---|
87 | */ |
---|
88 | //!@{ |
---|
89 | class GIM_ShapeRetriever |
---|
90 | { |
---|
91 | public: |
---|
92 | btGImpactShapeInterface * m_gim_shape; |
---|
93 | btTriangleShapeEx m_trishape; |
---|
94 | btTetrahedronShapeEx m_tetrashape; |
---|
95 | |
---|
96 | public: |
---|
97 | class ChildShapeRetriever |
---|
98 | { |
---|
99 | public: |
---|
100 | GIM_ShapeRetriever * m_parent; |
---|
101 | virtual btCollisionShape * getChildShape(int index) |
---|
102 | { |
---|
103 | return m_parent->m_gim_shape->getChildShape(index); |
---|
104 | } |
---|
105 | }; |
---|
106 | |
---|
107 | class TriangleShapeRetriever:public ChildShapeRetriever |
---|
108 | { |
---|
109 | public: |
---|
110 | |
---|
111 | virtual btCollisionShape * getChildShape(int index) |
---|
112 | { |
---|
113 | m_parent->m_gim_shape->getBulletTriangle(index,m_parent->m_trishape); |
---|
114 | return &m_parent->m_trishape; |
---|
115 | } |
---|
116 | }; |
---|
117 | |
---|
118 | class TetraShapeRetriever:public ChildShapeRetriever |
---|
119 | { |
---|
120 | public: |
---|
121 | |
---|
122 | virtual btCollisionShape * getChildShape(int index) |
---|
123 | { |
---|
124 | m_parent->m_gim_shape->getBulletTetrahedron(index,m_parent->m_tetrashape); |
---|
125 | return &m_parent->m_tetrashape; |
---|
126 | } |
---|
127 | }; |
---|
128 | public: |
---|
129 | ChildShapeRetriever m_child_retriever; |
---|
130 | TriangleShapeRetriever m_tri_retriever; |
---|
131 | TetraShapeRetriever m_tetra_retriever; |
---|
132 | ChildShapeRetriever * m_current_retriever; |
---|
133 | |
---|
134 | GIM_ShapeRetriever(btGImpactShapeInterface * gim_shape) |
---|
135 | { |
---|
136 | m_gim_shape = gim_shape; |
---|
137 | //select retriever |
---|
138 | if(m_gim_shape->needsRetrieveTriangles()) |
---|
139 | { |
---|
140 | m_current_retriever = &m_tri_retriever; |
---|
141 | } |
---|
142 | else if(m_gim_shape->needsRetrieveTetrahedrons()) |
---|
143 | { |
---|
144 | m_current_retriever = &m_tetra_retriever; |
---|
145 | } |
---|
146 | else |
---|
147 | { |
---|
148 | m_current_retriever = &m_child_retriever; |
---|
149 | } |
---|
150 | |
---|
151 | m_current_retriever->m_parent = this; |
---|
152 | } |
---|
153 | |
---|
154 | btCollisionShape * getChildShape(int index) |
---|
155 | { |
---|
156 | return m_current_retriever->getChildShape(index); |
---|
157 | } |
---|
158 | |
---|
159 | |
---|
160 | }; |
---|
161 | |
---|
162 | |
---|
163 | |
---|
164 | //!@} |
---|
165 | |
---|
166 | |
---|
167 | #ifdef TRI_COLLISION_PROFILING |
---|
168 | |
---|
169 | //! Gets the average time in miliseconds of tree collisions |
---|
170 | float btGImpactCollisionAlgorithm::getAverageTreeCollisionTime() |
---|
171 | { |
---|
172 | return btGImpactBoxSet::getAverageTreeCollisionTime(); |
---|
173 | |
---|
174 | } |
---|
175 | |
---|
176 | //! Gets the average time in miliseconds of triangle collisions |
---|
177 | float btGImpactCollisionAlgorithm::getAverageTriangleCollisionTime() |
---|
178 | { |
---|
179 | if(g_count_triangle_collision == 0) return 0; |
---|
180 | |
---|
181 | float avgtime = g_accum_triangle_collision_time; |
---|
182 | avgtime /= (float)g_count_triangle_collision; |
---|
183 | |
---|
184 | g_accum_triangle_collision_time = 0; |
---|
185 | g_count_triangle_collision = 0; |
---|
186 | |
---|
187 | return avgtime; |
---|
188 | } |
---|
189 | |
---|
190 | #endif //TRI_COLLISION_PROFILING |
---|
191 | |
---|
192 | |
---|
193 | |
---|
194 | btGImpactCollisionAlgorithm::btGImpactCollisionAlgorithm( const btCollisionAlgorithmConstructionInfo& ci, btCollisionObject* body0,btCollisionObject* body1) |
---|
195 | : btCollisionAlgorithm(ci) |
---|
196 | { |
---|
197 | m_manifoldPtr = NULL; |
---|
198 | m_convex_algorithm = NULL; |
---|
199 | } |
---|
200 | |
---|
201 | btGImpactCollisionAlgorithm::~btGImpactCollisionAlgorithm() |
---|
202 | { |
---|
203 | clearCache(); |
---|
204 | } |
---|
205 | |
---|
206 | |
---|
207 | |
---|
208 | |
---|
209 | |
---|
210 | void btGImpactCollisionAlgorithm::addContactPoint(btCollisionObject * body0, |
---|
211 | btCollisionObject * body1, |
---|
212 | const btVector3 & point, |
---|
213 | const btVector3 & normal, |
---|
214 | btScalar distance) |
---|
215 | { |
---|
216 | m_resultOut->setShapeIdentifiers(m_part0,m_triface0,m_part1,m_triface1); |
---|
217 | checkManifold(body0,body1); |
---|
218 | m_resultOut->addContactPoint(normal,point,distance); |
---|
219 | } |
---|
220 | |
---|
221 | |
---|
222 | void btGImpactCollisionAlgorithm::shape_vs_shape_collision( |
---|
223 | btCollisionObject * body0, |
---|
224 | btCollisionObject * body1, |
---|
225 | btCollisionShape * shape0, |
---|
226 | btCollisionShape * shape1) |
---|
227 | { |
---|
228 | |
---|
229 | btCollisionShape* tmpShape0 = body0->getCollisionShape(); |
---|
230 | btCollisionShape* tmpShape1 = body1->getCollisionShape(); |
---|
231 | |
---|
232 | body0->internalSetTemporaryCollisionShape(shape0); |
---|
233 | body1->internalSetTemporaryCollisionShape(shape1); |
---|
234 | |
---|
235 | { |
---|
236 | btCollisionAlgorithm* algor = newAlgorithm(body0,body1); |
---|
237 | // post : checkManifold is called |
---|
238 | |
---|
239 | m_resultOut->setShapeIdentifiers(m_part0,m_triface0,m_part1,m_triface1); |
---|
240 | |
---|
241 | algor->processCollision(body0,body1,*m_dispatchInfo,m_resultOut); |
---|
242 | |
---|
243 | algor->~btCollisionAlgorithm(); |
---|
244 | m_dispatcher->freeCollisionAlgorithm(algor); |
---|
245 | } |
---|
246 | |
---|
247 | body0->internalSetTemporaryCollisionShape(tmpShape0); |
---|
248 | body1->internalSetTemporaryCollisionShape(tmpShape1); |
---|
249 | } |
---|
250 | |
---|
251 | void btGImpactCollisionAlgorithm::convex_vs_convex_collision( |
---|
252 | btCollisionObject * body0, |
---|
253 | btCollisionObject * body1, |
---|
254 | btCollisionShape * shape0, |
---|
255 | btCollisionShape * shape1) |
---|
256 | { |
---|
257 | |
---|
258 | btCollisionShape* tmpShape0 = body0->getCollisionShape(); |
---|
259 | btCollisionShape* tmpShape1 = body1->getCollisionShape(); |
---|
260 | |
---|
261 | body0->internalSetTemporaryCollisionShape(shape0); |
---|
262 | body1->internalSetTemporaryCollisionShape(shape1); |
---|
263 | |
---|
264 | |
---|
265 | m_resultOut->setShapeIdentifiers(m_part0,m_triface0,m_part1,m_triface1); |
---|
266 | |
---|
267 | checkConvexAlgorithm(body0,body1); |
---|
268 | m_convex_algorithm->processCollision(body0,body1,*m_dispatchInfo,m_resultOut); |
---|
269 | |
---|
270 | body0->internalSetTemporaryCollisionShape(tmpShape0); |
---|
271 | body1->internalSetTemporaryCollisionShape(tmpShape1); |
---|
272 | |
---|
273 | } |
---|
274 | |
---|
275 | |
---|
276 | |
---|
277 | |
---|
278 | void btGImpactCollisionAlgorithm::gimpact_vs_gimpact_find_pairs( |
---|
279 | const btTransform & trans0, |
---|
280 | const btTransform & trans1, |
---|
281 | btGImpactShapeInterface * shape0, |
---|
282 | btGImpactShapeInterface * shape1,btPairSet & pairset) |
---|
283 | { |
---|
284 | if(shape0->hasBoxSet() && shape1->hasBoxSet()) |
---|
285 | { |
---|
286 | btGImpactBoxSet::find_collision(shape0->getBoxSet(),trans0,shape1->getBoxSet(),trans1,pairset); |
---|
287 | } |
---|
288 | else |
---|
289 | { |
---|
290 | btAABB boxshape0; |
---|
291 | btAABB boxshape1; |
---|
292 | int i = shape0->getNumChildShapes(); |
---|
293 | |
---|
294 | while(i--) |
---|
295 | { |
---|
296 | shape0->getChildAabb(i,trans0,boxshape0.m_min,boxshape0.m_max); |
---|
297 | |
---|
298 | int j = shape1->getNumChildShapes(); |
---|
299 | while(j--) |
---|
300 | { |
---|
301 | shape1->getChildAabb(i,trans1,boxshape1.m_min,boxshape1.m_max); |
---|
302 | |
---|
303 | if(boxshape1.has_collision(boxshape0)) |
---|
304 | { |
---|
305 | pairset.push_pair(i,j); |
---|
306 | } |
---|
307 | } |
---|
308 | } |
---|
309 | } |
---|
310 | |
---|
311 | |
---|
312 | } |
---|
313 | |
---|
314 | |
---|
315 | void btGImpactCollisionAlgorithm::gimpact_vs_shape_find_pairs( |
---|
316 | const btTransform & trans0, |
---|
317 | const btTransform & trans1, |
---|
318 | btGImpactShapeInterface * shape0, |
---|
319 | btCollisionShape * shape1, |
---|
320 | btAlignedObjectArray<int> & collided_primitives) |
---|
321 | { |
---|
322 | |
---|
323 | btAABB boxshape; |
---|
324 | |
---|
325 | |
---|
326 | if(shape0->hasBoxSet()) |
---|
327 | { |
---|
328 | btTransform trans1to0 = trans0.inverse(); |
---|
329 | trans1to0 *= trans1; |
---|
330 | |
---|
331 | shape1->getAabb(trans1to0,boxshape.m_min,boxshape.m_max); |
---|
332 | |
---|
333 | shape0->getBoxSet()->boxQuery(boxshape, collided_primitives); |
---|
334 | } |
---|
335 | else |
---|
336 | { |
---|
337 | shape1->getAabb(trans1,boxshape.m_min,boxshape.m_max); |
---|
338 | |
---|
339 | btAABB boxshape0; |
---|
340 | int i = shape0->getNumChildShapes(); |
---|
341 | |
---|
342 | while(i--) |
---|
343 | { |
---|
344 | shape0->getChildAabb(i,trans0,boxshape0.m_min,boxshape0.m_max); |
---|
345 | |
---|
346 | if(boxshape.has_collision(boxshape0)) |
---|
347 | { |
---|
348 | collided_primitives.push_back(i); |
---|
349 | } |
---|
350 | } |
---|
351 | |
---|
352 | } |
---|
353 | |
---|
354 | } |
---|
355 | |
---|
356 | |
---|
357 | void btGImpactCollisionAlgorithm::collide_gjk_triangles(btCollisionObject * body0, |
---|
358 | btCollisionObject * body1, |
---|
359 | btGImpactMeshShapePart * shape0, |
---|
360 | btGImpactMeshShapePart * shape1, |
---|
361 | const int * pairs, int pair_count) |
---|
362 | { |
---|
363 | btTriangleShapeEx tri0; |
---|
364 | btTriangleShapeEx tri1; |
---|
365 | |
---|
366 | shape0->lockChildShapes(); |
---|
367 | shape1->lockChildShapes(); |
---|
368 | |
---|
369 | const int * pair_pointer = pairs; |
---|
370 | |
---|
371 | while(pair_count--) |
---|
372 | { |
---|
373 | |
---|
374 | m_triface0 = *(pair_pointer); |
---|
375 | m_triface1 = *(pair_pointer+1); |
---|
376 | pair_pointer+=2; |
---|
377 | |
---|
378 | |
---|
379 | |
---|
380 | shape0->getBulletTriangle(m_triface0,tri0); |
---|
381 | shape1->getBulletTriangle(m_triface1,tri1); |
---|
382 | |
---|
383 | |
---|
384 | //collide two convex shapes |
---|
385 | if(tri0.overlap_test_conservative(tri1)) |
---|
386 | { |
---|
387 | convex_vs_convex_collision(body0,body1,&tri0,&tri1); |
---|
388 | } |
---|
389 | |
---|
390 | } |
---|
391 | |
---|
392 | shape0->unlockChildShapes(); |
---|
393 | shape1->unlockChildShapes(); |
---|
394 | } |
---|
395 | |
---|
396 | void btGImpactCollisionAlgorithm::collide_sat_triangles(btCollisionObject * body0, |
---|
397 | btCollisionObject * body1, |
---|
398 | btGImpactMeshShapePart * shape0, |
---|
399 | btGImpactMeshShapePart * shape1, |
---|
400 | const int * pairs, int pair_count) |
---|
401 | { |
---|
402 | btTransform orgtrans0 = body0->getWorldTransform(); |
---|
403 | btTransform orgtrans1 = body1->getWorldTransform(); |
---|
404 | |
---|
405 | btPrimitiveTriangle ptri0; |
---|
406 | btPrimitiveTriangle ptri1; |
---|
407 | BT_TRIANGLE_CONTACT contact_data; |
---|
408 | |
---|
409 | shape0->lockChildShapes(); |
---|
410 | shape1->lockChildShapes(); |
---|
411 | |
---|
412 | const int * pair_pointer = pairs; |
---|
413 | |
---|
414 | while(pair_count--) |
---|
415 | { |
---|
416 | |
---|
417 | m_triface0 = *(pair_pointer); |
---|
418 | m_triface1 = *(pair_pointer+1); |
---|
419 | pair_pointer+=2; |
---|
420 | |
---|
421 | |
---|
422 | shape0->getPrimitiveTriangle(m_triface0,ptri0); |
---|
423 | shape1->getPrimitiveTriangle(m_triface1,ptri1); |
---|
424 | |
---|
425 | #ifdef TRI_COLLISION_PROFILING |
---|
426 | bt_begin_gim02_tri_time(); |
---|
427 | #endif |
---|
428 | |
---|
429 | ptri0.applyTransform(orgtrans0); |
---|
430 | ptri1.applyTransform(orgtrans1); |
---|
431 | |
---|
432 | |
---|
433 | //build planes |
---|
434 | ptri0.buildTriPlane(); |
---|
435 | ptri1.buildTriPlane(); |
---|
436 | // test conservative |
---|
437 | |
---|
438 | |
---|
439 | |
---|
440 | if(ptri0.overlap_test_conservative(ptri1)) |
---|
441 | { |
---|
442 | if(ptri0.find_triangle_collision_clip_method(ptri1,contact_data)) |
---|
443 | { |
---|
444 | |
---|
445 | int j = contact_data.m_point_count; |
---|
446 | while(j--) |
---|
447 | { |
---|
448 | |
---|
449 | addContactPoint(body0, body1, |
---|
450 | contact_data.m_points[j], |
---|
451 | contact_data.m_separating_normal, |
---|
452 | -contact_data.m_penetration_depth); |
---|
453 | } |
---|
454 | } |
---|
455 | } |
---|
456 | |
---|
457 | #ifdef TRI_COLLISION_PROFILING |
---|
458 | bt_end_gim02_tri_time(); |
---|
459 | #endif |
---|
460 | |
---|
461 | } |
---|
462 | |
---|
463 | shape0->unlockChildShapes(); |
---|
464 | shape1->unlockChildShapes(); |
---|
465 | |
---|
466 | } |
---|
467 | |
---|
468 | |
---|
469 | void btGImpactCollisionAlgorithm::gimpact_vs_gimpact( |
---|
470 | btCollisionObject * body0, |
---|
471 | btCollisionObject * body1, |
---|
472 | btGImpactShapeInterface * shape0, |
---|
473 | btGImpactShapeInterface * shape1) |
---|
474 | { |
---|
475 | |
---|
476 | if(shape0->getGImpactShapeType()==CONST_GIMPACT_TRIMESH_SHAPE) |
---|
477 | { |
---|
478 | btGImpactMeshShape * meshshape0 = static_cast<btGImpactMeshShape *>(shape0); |
---|
479 | m_part0 = meshshape0->getMeshPartCount(); |
---|
480 | |
---|
481 | while(m_part0--) |
---|
482 | { |
---|
483 | gimpact_vs_gimpact(body0,body1,meshshape0->getMeshPart(m_part0),shape1); |
---|
484 | } |
---|
485 | |
---|
486 | return; |
---|
487 | } |
---|
488 | |
---|
489 | if(shape1->getGImpactShapeType()==CONST_GIMPACT_TRIMESH_SHAPE) |
---|
490 | { |
---|
491 | btGImpactMeshShape * meshshape1 = static_cast<btGImpactMeshShape *>(shape1); |
---|
492 | m_part1 = meshshape1->getMeshPartCount(); |
---|
493 | |
---|
494 | while(m_part1--) |
---|
495 | { |
---|
496 | |
---|
497 | gimpact_vs_gimpact(body0,body1,shape0,meshshape1->getMeshPart(m_part1)); |
---|
498 | |
---|
499 | } |
---|
500 | |
---|
501 | return; |
---|
502 | } |
---|
503 | |
---|
504 | |
---|
505 | btTransform orgtrans0 = body0->getWorldTransform(); |
---|
506 | btTransform orgtrans1 = body1->getWorldTransform(); |
---|
507 | |
---|
508 | btPairSet pairset; |
---|
509 | |
---|
510 | gimpact_vs_gimpact_find_pairs(orgtrans0,orgtrans1,shape0,shape1,pairset); |
---|
511 | |
---|
512 | if(pairset.size()== 0) return; |
---|
513 | |
---|
514 | if(shape0->getGImpactShapeType() == CONST_GIMPACT_TRIMESH_SHAPE_PART && |
---|
515 | shape1->getGImpactShapeType() == CONST_GIMPACT_TRIMESH_SHAPE_PART) |
---|
516 | { |
---|
517 | btGImpactMeshShapePart * shapepart0 = static_cast<btGImpactMeshShapePart * >(shape0); |
---|
518 | btGImpactMeshShapePart * shapepart1 = static_cast<btGImpactMeshShapePart * >(shape1); |
---|
519 | //specialized function |
---|
520 | #ifdef BULLET_TRIANGLE_COLLISION |
---|
521 | collide_gjk_triangles(body0,body1,shapepart0,shapepart1,&pairset[0].m_index1,pairset.size()); |
---|
522 | #else |
---|
523 | collide_sat_triangles(body0,body1,shapepart0,shapepart1,&pairset[0].m_index1,pairset.size()); |
---|
524 | #endif |
---|
525 | |
---|
526 | return; |
---|
527 | } |
---|
528 | |
---|
529 | //general function |
---|
530 | |
---|
531 | shape0->lockChildShapes(); |
---|
532 | shape1->lockChildShapes(); |
---|
533 | |
---|
534 | GIM_ShapeRetriever retriever0(shape0); |
---|
535 | GIM_ShapeRetriever retriever1(shape1); |
---|
536 | |
---|
537 | bool child_has_transform0 = shape0->childrenHasTransform(); |
---|
538 | bool child_has_transform1 = shape1->childrenHasTransform(); |
---|
539 | |
---|
540 | int i = pairset.size(); |
---|
541 | while(i--) |
---|
542 | { |
---|
543 | BT_PAIR * pair = &pairset[i]; |
---|
544 | m_triface0 = pair->m_index1; |
---|
545 | m_triface1 = pair->m_index2; |
---|
546 | btCollisionShape * colshape0 = retriever0.getChildShape(m_triface0); |
---|
547 | btCollisionShape * colshape1 = retriever1.getChildShape(m_triface1); |
---|
548 | |
---|
549 | if(child_has_transform0) |
---|
550 | { |
---|
551 | body0->setWorldTransform(orgtrans0*shape0->getChildTransform(m_triface0)); |
---|
552 | } |
---|
553 | |
---|
554 | if(child_has_transform1) |
---|
555 | { |
---|
556 | body1->setWorldTransform(orgtrans1*shape1->getChildTransform(m_triface1)); |
---|
557 | } |
---|
558 | |
---|
559 | //collide two convex shapes |
---|
560 | convex_vs_convex_collision(body0,body1,colshape0,colshape1); |
---|
561 | |
---|
562 | |
---|
563 | if(child_has_transform0) |
---|
564 | { |
---|
565 | body0->setWorldTransform(orgtrans0); |
---|
566 | } |
---|
567 | |
---|
568 | if(child_has_transform1) |
---|
569 | { |
---|
570 | body1->setWorldTransform(orgtrans1); |
---|
571 | } |
---|
572 | |
---|
573 | } |
---|
574 | |
---|
575 | shape0->unlockChildShapes(); |
---|
576 | shape1->unlockChildShapes(); |
---|
577 | } |
---|
578 | |
---|
579 | void btGImpactCollisionAlgorithm::gimpact_vs_shape(btCollisionObject * body0, |
---|
580 | btCollisionObject * body1, |
---|
581 | btGImpactShapeInterface * shape0, |
---|
582 | btCollisionShape * shape1,bool swapped) |
---|
583 | { |
---|
584 | if(shape0->getGImpactShapeType()==CONST_GIMPACT_TRIMESH_SHAPE) |
---|
585 | { |
---|
586 | btGImpactMeshShape * meshshape0 = static_cast<btGImpactMeshShape *>(shape0); |
---|
587 | m_part0 = meshshape0->getMeshPartCount(); |
---|
588 | |
---|
589 | while(m_part0--) |
---|
590 | { |
---|
591 | |
---|
592 | gimpact_vs_shape(body0, |
---|
593 | body1, |
---|
594 | meshshape0->getMeshPart(m_part0), |
---|
595 | shape1,swapped); |
---|
596 | |
---|
597 | } |
---|
598 | |
---|
599 | return; |
---|
600 | } |
---|
601 | |
---|
602 | #ifdef GIMPACT_VS_PLANE_COLLISION |
---|
603 | if(shape0->getGImpactShapeType() == CONST_GIMPACT_TRIMESH_SHAPE_PART && |
---|
604 | shape1->getShapeType() == STATIC_PLANE_PROXYTYPE) |
---|
605 | { |
---|
606 | btGImpactMeshShapePart * shapepart = static_cast<btGImpactMeshShapePart *>(shape0); |
---|
607 | btStaticPlaneShape * planeshape = static_cast<btStaticPlaneShape * >(shape1); |
---|
608 | gimpacttrimeshpart_vs_plane_collision(body0,body1,shapepart,planeshape,swapped); |
---|
609 | return; |
---|
610 | } |
---|
611 | |
---|
612 | #endif |
---|
613 | |
---|
614 | |
---|
615 | |
---|
616 | if(shape1->isCompound()) |
---|
617 | { |
---|
618 | btCompoundShape * compoundshape = static_cast<btCompoundShape *>(shape1); |
---|
619 | gimpact_vs_compoundshape(body0,body1,shape0,compoundshape,swapped); |
---|
620 | return; |
---|
621 | } |
---|
622 | else if(shape1->isConcave()) |
---|
623 | { |
---|
624 | btConcaveShape * concaveshape = static_cast<btConcaveShape *>(shape1); |
---|
625 | gimpact_vs_concave(body0,body1,shape0,concaveshape,swapped); |
---|
626 | return; |
---|
627 | } |
---|
628 | |
---|
629 | |
---|
630 | btTransform orgtrans0 = body0->getWorldTransform(); |
---|
631 | |
---|
632 | btTransform orgtrans1 = body1->getWorldTransform(); |
---|
633 | |
---|
634 | btAlignedObjectArray<int> collided_results; |
---|
635 | |
---|
636 | gimpact_vs_shape_find_pairs(orgtrans0,orgtrans1,shape0,shape1,collided_results); |
---|
637 | |
---|
638 | if(collided_results.size() == 0) return; |
---|
639 | |
---|
640 | |
---|
641 | shape0->lockChildShapes(); |
---|
642 | |
---|
643 | GIM_ShapeRetriever retriever0(shape0); |
---|
644 | |
---|
645 | |
---|
646 | bool child_has_transform0 = shape0->childrenHasTransform(); |
---|
647 | |
---|
648 | |
---|
649 | int i = collided_results.size(); |
---|
650 | |
---|
651 | while(i--) |
---|
652 | { |
---|
653 | int child_index = collided_results[i]; |
---|
654 | if(swapped) |
---|
655 | m_triface1 = child_index; |
---|
656 | else |
---|
657 | m_triface0 = child_index; |
---|
658 | |
---|
659 | btCollisionShape * colshape0 = retriever0.getChildShape(child_index); |
---|
660 | |
---|
661 | if(child_has_transform0) |
---|
662 | { |
---|
663 | body0->setWorldTransform(orgtrans0*shape0->getChildTransform(child_index)); |
---|
664 | } |
---|
665 | |
---|
666 | //collide two shapes |
---|
667 | if(swapped) |
---|
668 | { |
---|
669 | shape_vs_shape_collision(body1,body0,shape1,colshape0); |
---|
670 | } |
---|
671 | else |
---|
672 | { |
---|
673 | shape_vs_shape_collision(body0,body1,colshape0,shape1); |
---|
674 | } |
---|
675 | |
---|
676 | //restore transforms |
---|
677 | if(child_has_transform0) |
---|
678 | { |
---|
679 | body0->setWorldTransform(orgtrans0); |
---|
680 | } |
---|
681 | |
---|
682 | } |
---|
683 | |
---|
684 | shape0->unlockChildShapes(); |
---|
685 | |
---|
686 | } |
---|
687 | |
---|
688 | void btGImpactCollisionAlgorithm::gimpact_vs_compoundshape(btCollisionObject * body0, |
---|
689 | btCollisionObject * body1, |
---|
690 | btGImpactShapeInterface * shape0, |
---|
691 | btCompoundShape * shape1,bool swapped) |
---|
692 | { |
---|
693 | btTransform orgtrans1 = body1->getWorldTransform(); |
---|
694 | |
---|
695 | int i = shape1->getNumChildShapes(); |
---|
696 | while(i--) |
---|
697 | { |
---|
698 | |
---|
699 | btCollisionShape * colshape1 = shape1->getChildShape(i); |
---|
700 | btTransform childtrans1 = orgtrans1*shape1->getChildTransform(i); |
---|
701 | |
---|
702 | body1->setWorldTransform(childtrans1); |
---|
703 | |
---|
704 | //collide child shape |
---|
705 | gimpact_vs_shape(body0, body1, |
---|
706 | shape0,colshape1,swapped); |
---|
707 | |
---|
708 | |
---|
709 | //restore transforms |
---|
710 | body1->setWorldTransform(orgtrans1); |
---|
711 | } |
---|
712 | } |
---|
713 | |
---|
714 | void btGImpactCollisionAlgorithm::gimpacttrimeshpart_vs_plane_collision( |
---|
715 | btCollisionObject * body0, |
---|
716 | btCollisionObject * body1, |
---|
717 | btGImpactMeshShapePart * shape0, |
---|
718 | btStaticPlaneShape * shape1,bool swapped) |
---|
719 | { |
---|
720 | |
---|
721 | |
---|
722 | btTransform orgtrans0 = body0->getWorldTransform(); |
---|
723 | btTransform orgtrans1 = body1->getWorldTransform(); |
---|
724 | |
---|
725 | btPlaneShape * planeshape = static_cast<btPlaneShape *>(shape1); |
---|
726 | btVector4 plane; |
---|
727 | planeshape->get_plane_equation_transformed(orgtrans1,plane); |
---|
728 | |
---|
729 | //test box against plane |
---|
730 | |
---|
731 | btAABB tribox; |
---|
732 | shape0->getAabb(orgtrans0,tribox.m_min,tribox.m_max); |
---|
733 | tribox.increment_margin(planeshape->getMargin()); |
---|
734 | |
---|
735 | if( tribox.plane_classify(plane)!= BT_CONST_COLLIDE_PLANE) return; |
---|
736 | |
---|
737 | shape0->lockChildShapes(); |
---|
738 | |
---|
739 | btScalar margin = shape0->getMargin() + planeshape->getMargin(); |
---|
740 | |
---|
741 | btVector3 vertex; |
---|
742 | int vi = shape0->getVertexCount(); |
---|
743 | while(vi--) |
---|
744 | { |
---|
745 | shape0->getVertex(vi,vertex); |
---|
746 | vertex = orgtrans0(vertex); |
---|
747 | |
---|
748 | btScalar distance = vertex.dot(plane) - plane[3] - margin; |
---|
749 | |
---|
750 | if(distance<0.0)//add contact |
---|
751 | { |
---|
752 | if(swapped) |
---|
753 | { |
---|
754 | addContactPoint(body1, body0, |
---|
755 | vertex, |
---|
756 | -plane, |
---|
757 | distance); |
---|
758 | } |
---|
759 | else |
---|
760 | { |
---|
761 | addContactPoint(body0, body1, |
---|
762 | vertex, |
---|
763 | plane, |
---|
764 | distance); |
---|
765 | } |
---|
766 | } |
---|
767 | } |
---|
768 | |
---|
769 | shape0->unlockChildShapes(); |
---|
770 | } |
---|
771 | |
---|
772 | |
---|
773 | |
---|
774 | |
---|
775 | class btGImpactTriangleCallback: public btTriangleCallback |
---|
776 | { |
---|
777 | public: |
---|
778 | btGImpactCollisionAlgorithm * algorithm; |
---|
779 | btCollisionObject * body0; |
---|
780 | btCollisionObject * body1; |
---|
781 | btGImpactShapeInterface * gimpactshape0; |
---|
782 | bool swapped; |
---|
783 | btScalar margin; |
---|
784 | |
---|
785 | virtual void processTriangle(btVector3* triangle, int partId, int triangleIndex) |
---|
786 | { |
---|
787 | btTriangleShapeEx tri1(triangle[0],triangle[1],triangle[2]); |
---|
788 | tri1.setMargin(margin); |
---|
789 | if(swapped) |
---|
790 | { |
---|
791 | algorithm->setPart0(partId); |
---|
792 | algorithm->setFace0(triangleIndex); |
---|
793 | } |
---|
794 | else |
---|
795 | { |
---|
796 | algorithm->setPart1(partId); |
---|
797 | algorithm->setFace1(triangleIndex); |
---|
798 | } |
---|
799 | algorithm->gimpact_vs_shape( |
---|
800 | body0,body1,gimpactshape0,&tri1,swapped); |
---|
801 | } |
---|
802 | }; |
---|
803 | |
---|
804 | |
---|
805 | |
---|
806 | |
---|
807 | void btGImpactCollisionAlgorithm::gimpact_vs_concave( |
---|
808 | btCollisionObject * body0, |
---|
809 | btCollisionObject * body1, |
---|
810 | btGImpactShapeInterface * shape0, |
---|
811 | btConcaveShape * shape1,bool swapped) |
---|
812 | { |
---|
813 | //create the callback |
---|
814 | btGImpactTriangleCallback tricallback; |
---|
815 | tricallback.algorithm = this; |
---|
816 | tricallback.body0 = body0; |
---|
817 | tricallback.body1 = body1; |
---|
818 | tricallback.gimpactshape0 = shape0; |
---|
819 | tricallback.swapped = swapped; |
---|
820 | tricallback.margin = shape1->getMargin(); |
---|
821 | |
---|
822 | //getting the trimesh AABB |
---|
823 | btTransform gimpactInConcaveSpace; |
---|
824 | |
---|
825 | gimpactInConcaveSpace = body1->getWorldTransform().inverse() * body0->getWorldTransform(); |
---|
826 | |
---|
827 | btVector3 minAABB,maxAABB; |
---|
828 | shape0->getAabb(gimpactInConcaveSpace,minAABB,maxAABB); |
---|
829 | |
---|
830 | shape1->processAllTriangles(&tricallback,minAABB,maxAABB); |
---|
831 | |
---|
832 | } |
---|
833 | |
---|
834 | |
---|
835 | |
---|
836 | void btGImpactCollisionAlgorithm::processCollision (btCollisionObject* body0,btCollisionObject* body1,const btDispatcherInfo& dispatchInfo,btManifoldResult* resultOut) |
---|
837 | { |
---|
838 | clearCache(); |
---|
839 | |
---|
840 | m_resultOut = resultOut; |
---|
841 | m_dispatchInfo = &dispatchInfo; |
---|
842 | btGImpactShapeInterface * gimpactshape0; |
---|
843 | btGImpactShapeInterface * gimpactshape1; |
---|
844 | |
---|
845 | if (body0->getCollisionShape()->getShapeType()==GIMPACT_SHAPE_PROXYTYPE) |
---|
846 | { |
---|
847 | gimpactshape0 = static_cast<btGImpactShapeInterface *>(body0->getCollisionShape()); |
---|
848 | |
---|
849 | if( body1->getCollisionShape()->getShapeType()==GIMPACT_SHAPE_PROXYTYPE ) |
---|
850 | { |
---|
851 | gimpactshape1 = static_cast<btGImpactShapeInterface *>(body1->getCollisionShape()); |
---|
852 | |
---|
853 | gimpact_vs_gimpact(body0,body1,gimpactshape0,gimpactshape1); |
---|
854 | } |
---|
855 | else |
---|
856 | { |
---|
857 | gimpact_vs_shape(body0,body1,gimpactshape0,body1->getCollisionShape(),false); |
---|
858 | } |
---|
859 | |
---|
860 | } |
---|
861 | else if (body1->getCollisionShape()->getShapeType()==GIMPACT_SHAPE_PROXYTYPE ) |
---|
862 | { |
---|
863 | gimpactshape1 = static_cast<btGImpactShapeInterface *>(body1->getCollisionShape()); |
---|
864 | |
---|
865 | gimpact_vs_shape(body1,body0,gimpactshape1,body0->getCollisionShape(),true); |
---|
866 | } |
---|
867 | } |
---|
868 | |
---|
869 | |
---|
870 | btScalar btGImpactCollisionAlgorithm::calculateTimeOfImpact(btCollisionObject* body0,btCollisionObject* body1,const btDispatcherInfo& dispatchInfo,btManifoldResult* resultOut) |
---|
871 | { |
---|
872 | return 1.f; |
---|
873 | |
---|
874 | } |
---|
875 | |
---|
876 | ///////////////////////////////////// REGISTERING ALGORITHM ////////////////////////////////////////////// |
---|
877 | |
---|
878 | btGImpactCollisionAlgorithm::CreateFunc g_gimpact_cf; |
---|
879 | |
---|
880 | //! Use this function for register the algorithm externally |
---|
881 | void btGImpactCollisionAlgorithm::registerAlgorithm(btCollisionDispatcher * dispatcher) |
---|
882 | { |
---|
883 | |
---|
884 | int i; |
---|
885 | |
---|
886 | for ( i = 0;i < MAX_BROADPHASE_COLLISION_TYPES ;i++ ) |
---|
887 | { |
---|
888 | dispatcher->registerCollisionCreateFunc(GIMPACT_SHAPE_PROXYTYPE,i ,&g_gimpact_cf); |
---|
889 | } |
---|
890 | |
---|
891 | for ( i = 0;i < MAX_BROADPHASE_COLLISION_TYPES ;i++ ) |
---|
892 | { |
---|
893 | dispatcher->registerCollisionCreateFunc(i,GIMPACT_SHAPE_PROXYTYPE ,&g_gimpact_cf); |
---|
894 | } |
---|
895 | |
---|
896 | } |
---|