btGImpactCollisionAlgorithm.cpp 24 KB

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