b3FindConcaveSatAxis.h 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832
  1. #ifndef B3_FIND_CONCAVE_SEPARATING_AXIS_H
  2. #define B3_FIND_CONCAVE_SEPARATING_AXIS_H
  3. #define B3_TRIANGLE_NUM_CONVEX_FACES 5
  4. #include "Bullet3Common/shared/b3Int4.h"
  5. #include "Bullet3Collision/NarrowPhaseCollision/shared/b3RigidBodyData.h"
  6. #include "Bullet3Collision/NarrowPhaseCollision/shared/b3Collidable.h"
  7. #include "Bullet3Collision/BroadPhaseCollision/shared/b3Aabb.h"
  8. #include "Bullet3Collision/NarrowPhaseCollision/shared/b3BvhSubtreeInfoData.h"
  9. #include "Bullet3Collision/NarrowPhaseCollision/shared/b3QuantizedBvhNodeData.h"
  10. #include "Bullet3Collision/NarrowPhaseCollision/shared/b3ConvexPolyhedronData.h"
  11. inline void b3Project(__global const b3ConvexPolyhedronData* hull, b3Float4ConstArg pos, b3QuatConstArg orn,
  12. const b3Float4* dir, __global const b3Float4* vertices, float* min, float* max)
  13. {
  14. min[0] = FLT_MAX;
  15. max[0] = -FLT_MAX;
  16. int numVerts = hull->m_numVertices;
  17. const b3Float4 localDir = b3QuatRotate(b3QuatInverse(orn),*dir);
  18. float offset = b3Dot(pos,*dir);
  19. for(int i=0;i<numVerts;i++)
  20. {
  21. float dp = b3Dot(vertices[hull->m_vertexOffset+i],localDir);
  22. if(dp < min[0])
  23. min[0] = dp;
  24. if(dp > max[0])
  25. max[0] = dp;
  26. }
  27. if(min[0]>max[0])
  28. {
  29. float tmp = min[0];
  30. min[0] = max[0];
  31. max[0] = tmp;
  32. }
  33. min[0] += offset;
  34. max[0] += offset;
  35. }
  36. inline bool b3TestSepAxis(const b3ConvexPolyhedronData* hullA, __global const b3ConvexPolyhedronData* hullB,
  37. b3Float4ConstArg posA,b3QuatConstArg ornA,
  38. b3Float4ConstArg posB,b3QuatConstArg ornB,
  39. b3Float4* sep_axis, const b3Float4* verticesA, __global const b3Float4* verticesB,float* depth)
  40. {
  41. float Min0,Max0;
  42. float Min1,Max1;
  43. b3Project(hullA,posA,ornA,sep_axis,verticesA, &Min0, &Max0);
  44. b3Project(hullB,posB,ornB, sep_axis,verticesB, &Min1, &Max1);
  45. if(Max0<Min1 || Max1<Min0)
  46. return false;
  47. float d0 = Max0 - Min1;
  48. float d1 = Max1 - Min0;
  49. *depth = d0<d1 ? d0:d1;
  50. return true;
  51. }
  52. bool b3FindSeparatingAxis( const b3ConvexPolyhedronData* hullA, __global const b3ConvexPolyhedronData* hullB,
  53. b3Float4ConstArg posA1,
  54. b3QuatConstArg ornA,
  55. b3Float4ConstArg posB1,
  56. b3QuatConstArg ornB,
  57. b3Float4ConstArg DeltaC2,
  58. const b3Float4* verticesA,
  59. const b3Float4* uniqueEdgesA,
  60. const b3GpuFace* facesA,
  61. const int* indicesA,
  62. __global const b3Float4* verticesB,
  63. __global const b3Float4* uniqueEdgesB,
  64. __global const b3GpuFace* facesB,
  65. __global const int* indicesB,
  66. b3Float4* sep,
  67. float* dmin)
  68. {
  69. b3Float4 posA = posA1;
  70. posA.w = 0.f;
  71. b3Float4 posB = posB1;
  72. posB.w = 0.f;
  73. /*
  74. static int maxFaceVertex = 0;
  75. int curFaceVertexAB = hullA->m_numFaces*hullB->m_numVertices;
  76. curFaceVertexAB+= hullB->m_numFaces*hullA->m_numVertices;
  77. if (curFaceVertexAB>maxFaceVertex)
  78. {
  79. maxFaceVertex = curFaceVertexAB;
  80. printf("curFaceVertexAB = %d\n",curFaceVertexAB);
  81. printf("hullA->m_numFaces = %d\n",hullA->m_numFaces);
  82. printf("hullA->m_numVertices = %d\n",hullA->m_numVertices);
  83. printf("hullB->m_numVertices = %d\n",hullB->m_numVertices);
  84. }
  85. */
  86. int curPlaneTests=0;
  87. {
  88. int numFacesA = hullA->m_numFaces;
  89. // Test normals from hullA
  90. for(int i=0;i<numFacesA;i++)
  91. {
  92. const b3Float4 normal = facesA[hullA->m_faceOffset+i].m_plane;
  93. b3Float4 faceANormalWS = b3QuatRotate(ornA,normal);
  94. if (b3Dot(DeltaC2,faceANormalWS)<0)
  95. faceANormalWS*=-1.f;
  96. curPlaneTests++;
  97. float d;
  98. if(!b3TestSepAxis( hullA, hullB, posA,ornA,posB,ornB,&faceANormalWS, verticesA, verticesB,&d))
  99. return false;
  100. if(d<*dmin)
  101. {
  102. *dmin = d;
  103. *sep = faceANormalWS;
  104. }
  105. }
  106. }
  107. if((b3Dot(-DeltaC2,*sep))>0.0f)
  108. {
  109. *sep = -(*sep);
  110. }
  111. return true;
  112. }
  113. b3Vector3 unitSphere162[]=
  114. {
  115. b3MakeVector3(0.000000,-1.000000,0.000000),
  116. b3MakeVector3(0.203181,-0.967950,0.147618),
  117. b3MakeVector3(-0.077607,-0.967950,0.238853),
  118. b3MakeVector3(0.723607,-0.447220,0.525725),
  119. b3MakeVector3(0.609547,-0.657519,0.442856),
  120. b3MakeVector3(0.812729,-0.502301,0.295238),
  121. b3MakeVector3(-0.251147,-0.967949,0.000000),
  122. b3MakeVector3(-0.077607,-0.967950,-0.238853),
  123. b3MakeVector3(0.203181,-0.967950,-0.147618),
  124. b3MakeVector3(0.860698,-0.251151,0.442858),
  125. b3MakeVector3(-0.276388,-0.447220,0.850649),
  126. b3MakeVector3(-0.029639,-0.502302,0.864184),
  127. b3MakeVector3(-0.155215,-0.251152,0.955422),
  128. b3MakeVector3(-0.894426,-0.447216,0.000000),
  129. b3MakeVector3(-0.831051,-0.502299,0.238853),
  130. b3MakeVector3(-0.956626,-0.251149,0.147618),
  131. b3MakeVector3(-0.276388,-0.447220,-0.850649),
  132. b3MakeVector3(-0.483971,-0.502302,-0.716565),
  133. b3MakeVector3(-0.436007,-0.251152,-0.864188),
  134. b3MakeVector3(0.723607,-0.447220,-0.525725),
  135. b3MakeVector3(0.531941,-0.502302,-0.681712),
  136. b3MakeVector3(0.687159,-0.251152,-0.681715),
  137. b3MakeVector3(0.687159,-0.251152,0.681715),
  138. b3MakeVector3(-0.436007,-0.251152,0.864188),
  139. b3MakeVector3(-0.956626,-0.251149,-0.147618),
  140. b3MakeVector3(-0.155215,-0.251152,-0.955422),
  141. b3MakeVector3(0.860698,-0.251151,-0.442858),
  142. b3MakeVector3(0.276388,0.447220,0.850649),
  143. b3MakeVector3(0.483971,0.502302,0.716565),
  144. b3MakeVector3(0.232822,0.657519,0.716563),
  145. b3MakeVector3(-0.723607,0.447220,0.525725),
  146. b3MakeVector3(-0.531941,0.502302,0.681712),
  147. b3MakeVector3(-0.609547,0.657519,0.442856),
  148. b3MakeVector3(-0.723607,0.447220,-0.525725),
  149. b3MakeVector3(-0.812729,0.502301,-0.295238),
  150. b3MakeVector3(-0.609547,0.657519,-0.442856),
  151. b3MakeVector3(0.276388,0.447220,-0.850649),
  152. b3MakeVector3(0.029639,0.502302,-0.864184),
  153. b3MakeVector3(0.232822,0.657519,-0.716563),
  154. b3MakeVector3(0.894426,0.447216,0.000000),
  155. b3MakeVector3(0.831051,0.502299,-0.238853),
  156. b3MakeVector3(0.753442,0.657515,0.000000),
  157. b3MakeVector3(-0.232822,-0.657519,0.716563),
  158. b3MakeVector3(-0.162456,-0.850654,0.499995),
  159. b3MakeVector3(0.052790,-0.723612,0.688185),
  160. b3MakeVector3(0.138199,-0.894429,0.425321),
  161. b3MakeVector3(0.262869,-0.525738,0.809012),
  162. b3MakeVector3(0.361805,-0.723611,0.587779),
  163. b3MakeVector3(0.531941,-0.502302,0.681712),
  164. b3MakeVector3(0.425323,-0.850654,0.309011),
  165. b3MakeVector3(0.812729,-0.502301,-0.295238),
  166. b3MakeVector3(0.609547,-0.657519,-0.442856),
  167. b3MakeVector3(0.850648,-0.525736,0.000000),
  168. b3MakeVector3(0.670817,-0.723611,-0.162457),
  169. b3MakeVector3(0.670817,-0.723610,0.162458),
  170. b3MakeVector3(0.425323,-0.850654,-0.309011),
  171. b3MakeVector3(0.447211,-0.894428,0.000001),
  172. b3MakeVector3(-0.753442,-0.657515,0.000000),
  173. b3MakeVector3(-0.525730,-0.850652,0.000000),
  174. b3MakeVector3(-0.638195,-0.723609,0.262864),
  175. b3MakeVector3(-0.361801,-0.894428,0.262864),
  176. b3MakeVector3(-0.688189,-0.525736,0.499997),
  177. b3MakeVector3(-0.447211,-0.723610,0.525729),
  178. b3MakeVector3(-0.483971,-0.502302,0.716565),
  179. b3MakeVector3(-0.232822,-0.657519,-0.716563),
  180. b3MakeVector3(-0.162456,-0.850654,-0.499995),
  181. b3MakeVector3(-0.447211,-0.723611,-0.525727),
  182. b3MakeVector3(-0.361801,-0.894429,-0.262863),
  183. b3MakeVector3(-0.688189,-0.525736,-0.499997),
  184. b3MakeVector3(-0.638195,-0.723609,-0.262863),
  185. b3MakeVector3(-0.831051,-0.502299,-0.238853),
  186. b3MakeVector3(0.361804,-0.723612,-0.587779),
  187. b3MakeVector3(0.138197,-0.894429,-0.425321),
  188. b3MakeVector3(0.262869,-0.525738,-0.809012),
  189. b3MakeVector3(0.052789,-0.723611,-0.688186),
  190. b3MakeVector3(-0.029639,-0.502302,-0.864184),
  191. b3MakeVector3(0.956626,0.251149,0.147618),
  192. b3MakeVector3(0.956626,0.251149,-0.147618),
  193. b3MakeVector3(0.951058,-0.000000,0.309013),
  194. b3MakeVector3(1.000000,0.000000,0.000000),
  195. b3MakeVector3(0.947213,-0.276396,0.162458),
  196. b3MakeVector3(0.951058,0.000000,-0.309013),
  197. b3MakeVector3(0.947213,-0.276396,-0.162458),
  198. b3MakeVector3(0.155215,0.251152,0.955422),
  199. b3MakeVector3(0.436007,0.251152,0.864188),
  200. b3MakeVector3(-0.000000,-0.000000,1.000000),
  201. b3MakeVector3(0.309017,0.000000,0.951056),
  202. b3MakeVector3(0.138199,-0.276398,0.951055),
  203. b3MakeVector3(0.587786,0.000000,0.809017),
  204. b3MakeVector3(0.447216,-0.276398,0.850648),
  205. b3MakeVector3(-0.860698,0.251151,0.442858),
  206. b3MakeVector3(-0.687159,0.251152,0.681715),
  207. b3MakeVector3(-0.951058,-0.000000,0.309013),
  208. b3MakeVector3(-0.809018,0.000000,0.587783),
  209. b3MakeVector3(-0.861803,-0.276396,0.425324),
  210. b3MakeVector3(-0.587786,0.000000,0.809017),
  211. b3MakeVector3(-0.670819,-0.276397,0.688191),
  212. b3MakeVector3(-0.687159,0.251152,-0.681715),
  213. b3MakeVector3(-0.860698,0.251151,-0.442858),
  214. b3MakeVector3(-0.587786,-0.000000,-0.809017),
  215. b3MakeVector3(-0.809018,-0.000000,-0.587783),
  216. b3MakeVector3(-0.670819,-0.276397,-0.688191),
  217. b3MakeVector3(-0.951058,0.000000,-0.309013),
  218. b3MakeVector3(-0.861803,-0.276396,-0.425324),
  219. b3MakeVector3(0.436007,0.251152,-0.864188),
  220. b3MakeVector3(0.155215,0.251152,-0.955422),
  221. b3MakeVector3(0.587786,-0.000000,-0.809017),
  222. b3MakeVector3(0.309017,-0.000000,-0.951056),
  223. b3MakeVector3(0.447216,-0.276398,-0.850648),
  224. b3MakeVector3(0.000000,0.000000,-1.000000),
  225. b3MakeVector3(0.138199,-0.276398,-0.951055),
  226. b3MakeVector3(0.670820,0.276396,0.688190),
  227. b3MakeVector3(0.809019,-0.000002,0.587783),
  228. b3MakeVector3(0.688189,0.525736,0.499997),
  229. b3MakeVector3(0.861804,0.276394,0.425323),
  230. b3MakeVector3(0.831051,0.502299,0.238853),
  231. b3MakeVector3(-0.447216,0.276397,0.850649),
  232. b3MakeVector3(-0.309017,-0.000001,0.951056),
  233. b3MakeVector3(-0.262869,0.525738,0.809012),
  234. b3MakeVector3(-0.138199,0.276397,0.951055),
  235. b3MakeVector3(0.029639,0.502302,0.864184),
  236. b3MakeVector3(-0.947213,0.276396,-0.162458),
  237. b3MakeVector3(-1.000000,0.000001,0.000000),
  238. b3MakeVector3(-0.850648,0.525736,-0.000000),
  239. b3MakeVector3(-0.947213,0.276397,0.162458),
  240. b3MakeVector3(-0.812729,0.502301,0.295238),
  241. b3MakeVector3(-0.138199,0.276397,-0.951055),
  242. b3MakeVector3(-0.309016,-0.000000,-0.951057),
  243. b3MakeVector3(-0.262869,0.525738,-0.809012),
  244. b3MakeVector3(-0.447215,0.276397,-0.850649),
  245. b3MakeVector3(-0.531941,0.502302,-0.681712),
  246. b3MakeVector3(0.861804,0.276396,-0.425322),
  247. b3MakeVector3(0.809019,0.000000,-0.587782),
  248. b3MakeVector3(0.688189,0.525736,-0.499997),
  249. b3MakeVector3(0.670821,0.276397,-0.688189),
  250. b3MakeVector3(0.483971,0.502302,-0.716565),
  251. b3MakeVector3(0.077607,0.967950,0.238853),
  252. b3MakeVector3(0.251147,0.967949,0.000000),
  253. b3MakeVector3(0.000000,1.000000,0.000000),
  254. b3MakeVector3(0.162456,0.850654,0.499995),
  255. b3MakeVector3(0.361800,0.894429,0.262863),
  256. b3MakeVector3(0.447209,0.723612,0.525728),
  257. b3MakeVector3(0.525730,0.850652,0.000000),
  258. b3MakeVector3(0.638194,0.723610,0.262864),
  259. b3MakeVector3(-0.203181,0.967950,0.147618),
  260. b3MakeVector3(-0.425323,0.850654,0.309011),
  261. b3MakeVector3(-0.138197,0.894430,0.425320),
  262. b3MakeVector3(-0.361804,0.723612,0.587778),
  263. b3MakeVector3(-0.052790,0.723612,0.688185),
  264. b3MakeVector3(-0.203181,0.967950,-0.147618),
  265. b3MakeVector3(-0.425323,0.850654,-0.309011),
  266. b3MakeVector3(-0.447210,0.894429,0.000000),
  267. b3MakeVector3(-0.670817,0.723611,-0.162457),
  268. b3MakeVector3(-0.670817,0.723611,0.162457),
  269. b3MakeVector3(0.077607,0.967950,-0.238853),
  270. b3MakeVector3(0.162456,0.850654,-0.499995),
  271. b3MakeVector3(-0.138197,0.894430,-0.425320),
  272. b3MakeVector3(-0.052790,0.723612,-0.688185),
  273. b3MakeVector3(-0.361804,0.723612,-0.587778),
  274. b3MakeVector3(0.361800,0.894429,-0.262863),
  275. b3MakeVector3(0.638194,0.723610,-0.262864),
  276. b3MakeVector3(0.447209,0.723612,-0.525728)
  277. };
  278. bool b3FindSeparatingAxisEdgeEdge( const b3ConvexPolyhedronData* hullA, __global const b3ConvexPolyhedronData* hullB,
  279. b3Float4ConstArg posA1,
  280. b3QuatConstArg ornA,
  281. b3Float4ConstArg posB1,
  282. b3QuatConstArg ornB,
  283. b3Float4ConstArg DeltaC2,
  284. const b3Float4* verticesA,
  285. const b3Float4* uniqueEdgesA,
  286. const b3GpuFace* facesA,
  287. const int* indicesA,
  288. __global const b3Float4* verticesB,
  289. __global const b3Float4* uniqueEdgesB,
  290. __global const b3GpuFace* facesB,
  291. __global const int* indicesB,
  292. b3Float4* sep,
  293. float* dmin,
  294. bool searchAllEdgeEdge)
  295. {
  296. b3Float4 posA = posA1;
  297. posA.w = 0.f;
  298. b3Float4 posB = posB1;
  299. posB.w = 0.f;
  300. int curPlaneTests=0;
  301. int curEdgeEdge = 0;
  302. // Test edges
  303. static int maxEdgeTests = 0;
  304. int curEdgeTests = hullA->m_numUniqueEdges * hullB->m_numUniqueEdges;
  305. if (curEdgeTests >maxEdgeTests )
  306. {
  307. maxEdgeTests = curEdgeTests ;
  308. printf("maxEdgeTests = %d\n",maxEdgeTests );
  309. printf("hullA->m_numUniqueEdges = %d\n",hullA->m_numUniqueEdges);
  310. printf("hullB->m_numUniqueEdges = %d\n",hullB->m_numUniqueEdges);
  311. }
  312. if (searchAllEdgeEdge)
  313. {
  314. for(int e0=0;e0<hullA->m_numUniqueEdges;e0++)
  315. {
  316. const b3Float4 edge0 = uniqueEdgesA[hullA->m_uniqueEdgesOffset+e0];
  317. b3Float4 edge0World = b3QuatRotate(ornA,edge0);
  318. for(int e1=0;e1<hullB->m_numUniqueEdges;e1++)
  319. {
  320. const b3Float4 edge1 = uniqueEdgesB[hullB->m_uniqueEdgesOffset+e1];
  321. b3Float4 edge1World = b3QuatRotate(ornB,edge1);
  322. b3Float4 crossje = b3Cross(edge0World,edge1World);
  323. curEdgeEdge++;
  324. if(!b3IsAlmostZero(crossje))
  325. {
  326. crossje = b3Normalized(crossje);
  327. if (b3Dot(DeltaC2,crossje)<0)
  328. crossje *= -1.f;
  329. float dist;
  330. bool result = true;
  331. {
  332. float Min0,Max0;
  333. float Min1,Max1;
  334. b3Project(hullA,posA,ornA,&crossje,verticesA, &Min0, &Max0);
  335. b3Project(hullB,posB,ornB,&crossje,verticesB, &Min1, &Max1);
  336. if(Max0<Min1 || Max1<Min0)
  337. return false;
  338. float d0 = Max0 - Min1;
  339. float d1 = Max1 - Min0;
  340. dist = d0<d1 ? d0:d1;
  341. result = true;
  342. }
  343. if(dist<*dmin)
  344. {
  345. *dmin = dist;
  346. *sep = crossje;
  347. }
  348. }
  349. }
  350. }
  351. } else
  352. {
  353. int numDirections = sizeof(unitSphere162)/sizeof(b3Vector3);
  354. //printf("numDirections =%d\n",numDirections );
  355. for(int i=0;i<numDirections;i++)
  356. {
  357. b3Float4 crossje = unitSphere162[i];
  358. {
  359. //if (b3Dot(DeltaC2,crossje)>0)
  360. {
  361. float dist;
  362. bool result = true;
  363. {
  364. float Min0,Max0;
  365. float Min1,Max1;
  366. b3Project(hullA,posA,ornA,&crossje,verticesA, &Min0, &Max0);
  367. b3Project(hullB,posB,ornB,&crossje,verticesB, &Min1, &Max1);
  368. if(Max0<Min1 || Max1<Min0)
  369. return false;
  370. float d0 = Max0 - Min1;
  371. float d1 = Max1 - Min0;
  372. dist = d0<d1 ? d0:d1;
  373. result = true;
  374. }
  375. if(dist<*dmin)
  376. {
  377. *dmin = dist;
  378. *sep = crossje;
  379. }
  380. }
  381. }
  382. }
  383. }
  384. if((b3Dot(-DeltaC2,*sep))>0.0f)
  385. {
  386. *sep = -(*sep);
  387. }
  388. return true;
  389. }
  390. inline int b3FindClippingFaces(b3Float4ConstArg separatingNormal,
  391. __global const b3ConvexPolyhedronData_t* hullA, __global const b3ConvexPolyhedronData_t* hullB,
  392. b3Float4ConstArg posA, b3QuatConstArg ornA,b3Float4ConstArg posB, b3QuatConstArg ornB,
  393. __global b3Float4* worldVertsA1,
  394. __global b3Float4* worldNormalsA1,
  395. __global b3Float4* worldVertsB1,
  396. int capacityWorldVerts,
  397. const float minDist, float maxDist,
  398. __global const b3Float4* verticesA,
  399. __global const b3GpuFace_t* facesA,
  400. __global const int* indicesA,
  401. __global const b3Float4* verticesB,
  402. __global const b3GpuFace_t* facesB,
  403. __global const int* indicesB,
  404. __global b3Int4* clippingFaces, int pairIndex)
  405. {
  406. int numContactsOut = 0;
  407. int numWorldVertsB1= 0;
  408. int closestFaceB=-1;
  409. float dmax = -FLT_MAX;
  410. {
  411. for(int face=0;face<hullB->m_numFaces;face++)
  412. {
  413. const b3Float4 Normal = b3MakeFloat4(facesB[hullB->m_faceOffset+face].m_plane.x,
  414. facesB[hullB->m_faceOffset+face].m_plane.y, facesB[hullB->m_faceOffset+face].m_plane.z,0.f);
  415. const b3Float4 WorldNormal = b3QuatRotate(ornB, Normal);
  416. float d = b3Dot(WorldNormal,separatingNormal);
  417. if (d > dmax)
  418. {
  419. dmax = d;
  420. closestFaceB = face;
  421. }
  422. }
  423. }
  424. {
  425. const b3GpuFace_t polyB = facesB[hullB->m_faceOffset+closestFaceB];
  426. const int numVertices = polyB.m_numIndices;
  427. for(int e0=0;e0<numVertices;e0++)
  428. {
  429. const b3Float4 b = verticesB[hullB->m_vertexOffset+indicesB[polyB.m_indexOffset+e0]];
  430. worldVertsB1[pairIndex*capacityWorldVerts+numWorldVertsB1++] = b3TransformPoint(b,posB,ornB);
  431. }
  432. }
  433. int closestFaceA=-1;
  434. {
  435. float dmin = FLT_MAX;
  436. for(int face=0;face<hullA->m_numFaces;face++)
  437. {
  438. const b3Float4 Normal = b3MakeFloat4(
  439. facesA[hullA->m_faceOffset+face].m_plane.x,
  440. facesA[hullA->m_faceOffset+face].m_plane.y,
  441. facesA[hullA->m_faceOffset+face].m_plane.z,
  442. 0.f);
  443. const b3Float4 faceANormalWS = b3QuatRotate(ornA,Normal);
  444. float d = b3Dot(faceANormalWS,separatingNormal);
  445. if (d < dmin)
  446. {
  447. dmin = d;
  448. closestFaceA = face;
  449. worldNormalsA1[pairIndex] = faceANormalWS;
  450. }
  451. }
  452. }
  453. int numVerticesA = facesA[hullA->m_faceOffset+closestFaceA].m_numIndices;
  454. for(int e0=0;e0<numVerticesA;e0++)
  455. {
  456. const b3Float4 a = verticesA[hullA->m_vertexOffset+indicesA[facesA[hullA->m_faceOffset+closestFaceA].m_indexOffset+e0]];
  457. worldVertsA1[pairIndex*capacityWorldVerts+e0] = b3TransformPoint(a, posA,ornA);
  458. }
  459. clippingFaces[pairIndex].x = closestFaceA;
  460. clippingFaces[pairIndex].y = closestFaceB;
  461. clippingFaces[pairIndex].z = numVerticesA;
  462. clippingFaces[pairIndex].w = numWorldVertsB1;
  463. return numContactsOut;
  464. }
  465. __kernel void b3FindConcaveSeparatingAxisKernel( __global b3Int4* concavePairs,
  466. __global const b3RigidBodyData* rigidBodies,
  467. __global const b3Collidable* collidables,
  468. __global const b3ConvexPolyhedronData* convexShapes,
  469. __global const b3Float4* vertices,
  470. __global const b3Float4* uniqueEdges,
  471. __global const b3GpuFace* faces,
  472. __global const int* indices,
  473. __global const b3GpuChildShape* gpuChildShapes,
  474. __global b3Aabb* aabbs,
  475. __global b3Float4* concaveSeparatingNormalsOut,
  476. __global b3Int4* clippingFacesOut,
  477. __global b3Vector3* worldVertsA1Out,
  478. __global b3Vector3* worldNormalsA1Out,
  479. __global b3Vector3* worldVertsB1Out,
  480. __global int* hasSeparatingNormals,
  481. int vertexFaceCapacity,
  482. int numConcavePairs,
  483. int pairIdx
  484. )
  485. {
  486. int i = pairIdx;
  487. /* int i = get_global_id(0);
  488. if (i>=numConcavePairs)
  489. return;
  490. int pairIdx = i;
  491. */
  492. int bodyIndexA = concavePairs[i].x;
  493. int bodyIndexB = concavePairs[i].y;
  494. int collidableIndexA = rigidBodies[bodyIndexA].m_collidableIdx;
  495. int collidableIndexB = rigidBodies[bodyIndexB].m_collidableIdx;
  496. int shapeIndexA = collidables[collidableIndexA].m_shapeIndex;
  497. int shapeIndexB = collidables[collidableIndexB].m_shapeIndex;
  498. if (collidables[collidableIndexB].m_shapeType!=SHAPE_CONVEX_HULL&&
  499. collidables[collidableIndexB].m_shapeType!=SHAPE_COMPOUND_OF_CONVEX_HULLS)
  500. {
  501. concavePairs[pairIdx].w = -1;
  502. return;
  503. }
  504. hasSeparatingNormals[i] = 0;
  505. int numFacesA = convexShapes[shapeIndexA].m_numFaces;
  506. int numActualConcaveConvexTests = 0;
  507. int f = concavePairs[i].z;
  508. bool overlap = false;
  509. b3ConvexPolyhedronData convexPolyhedronA;
  510. //add 3 vertices of the triangle
  511. convexPolyhedronA.m_numVertices = 3;
  512. convexPolyhedronA.m_vertexOffset = 0;
  513. b3Float4 localCenter = b3MakeFloat4(0.f,0.f,0.f,0.f);
  514. b3GpuFace face = faces[convexShapes[shapeIndexA].m_faceOffset+f];
  515. b3Aabb triAabb;
  516. triAabb.m_minVec = b3MakeFloat4(1e30f,1e30f,1e30f,0.f);
  517. triAabb.m_maxVec = b3MakeFloat4(-1e30f,-1e30f,-1e30f,0.f);
  518. b3Float4 verticesA[3];
  519. for (int i=0;i<3;i++)
  520. {
  521. int index = indices[face.m_indexOffset+i];
  522. b3Float4 vert = vertices[convexShapes[shapeIndexA].m_vertexOffset+index];
  523. verticesA[i] = vert;
  524. localCenter += vert;
  525. triAabb.m_minVec = b3MinFloat4(triAabb.m_minVec,vert);
  526. triAabb.m_maxVec = b3MaxFloat4(triAabb.m_maxVec,vert);
  527. }
  528. overlap = true;
  529. overlap = (triAabb.m_minVec.x > aabbs[bodyIndexB].m_maxVec.x || triAabb.m_maxVec.x < aabbs[bodyIndexB].m_minVec.x) ? false : overlap;
  530. overlap = (triAabb.m_minVec.z > aabbs[bodyIndexB].m_maxVec.z || triAabb.m_maxVec.z < aabbs[bodyIndexB].m_minVec.z) ? false : overlap;
  531. overlap = (triAabb.m_minVec.y > aabbs[bodyIndexB].m_maxVec.y || triAabb.m_maxVec.y < aabbs[bodyIndexB].m_minVec.y) ? false : overlap;
  532. if (overlap)
  533. {
  534. float dmin = FLT_MAX;
  535. int hasSeparatingAxis=5;
  536. b3Float4 sepAxis=b3MakeFloat4(1,2,3,4);
  537. int localCC=0;
  538. numActualConcaveConvexTests++;
  539. //a triangle has 3 unique edges
  540. convexPolyhedronA.m_numUniqueEdges = 3;
  541. convexPolyhedronA.m_uniqueEdgesOffset = 0;
  542. b3Float4 uniqueEdgesA[3];
  543. uniqueEdgesA[0] = (verticesA[1]-verticesA[0]);
  544. uniqueEdgesA[1] = (verticesA[2]-verticesA[1]);
  545. uniqueEdgesA[2] = (verticesA[0]-verticesA[2]);
  546. convexPolyhedronA.m_faceOffset = 0;
  547. b3Float4 normal = b3MakeFloat4(face.m_plane.x,face.m_plane.y,face.m_plane.z,0.f);
  548. b3GpuFace facesA[B3_TRIANGLE_NUM_CONVEX_FACES];
  549. int indicesA[3+3+2+2+2];
  550. int curUsedIndices=0;
  551. int fidx=0;
  552. //front size of triangle
  553. {
  554. facesA[fidx].m_indexOffset=curUsedIndices;
  555. indicesA[0] = 0;
  556. indicesA[1] = 1;
  557. indicesA[2] = 2;
  558. curUsedIndices+=3;
  559. float c = face.m_plane.w;
  560. facesA[fidx].m_plane.x = normal.x;
  561. facesA[fidx].m_plane.y = normal.y;
  562. facesA[fidx].m_plane.z = normal.z;
  563. facesA[fidx].m_plane.w = c;
  564. facesA[fidx].m_numIndices=3;
  565. }
  566. fidx++;
  567. //back size of triangle
  568. {
  569. facesA[fidx].m_indexOffset=curUsedIndices;
  570. indicesA[3]=2;
  571. indicesA[4]=1;
  572. indicesA[5]=0;
  573. curUsedIndices+=3;
  574. float c = b3Dot(normal,verticesA[0]);
  575. float c1 = -face.m_plane.w;
  576. facesA[fidx].m_plane.x = -normal.x;
  577. facesA[fidx].m_plane.y = -normal.y;
  578. facesA[fidx].m_plane.z = -normal.z;
  579. facesA[fidx].m_plane.w = c;
  580. facesA[fidx].m_numIndices=3;
  581. }
  582. fidx++;
  583. bool addEdgePlanes = true;
  584. if (addEdgePlanes)
  585. {
  586. int numVertices=3;
  587. int prevVertex = numVertices-1;
  588. for (int i=0;i<numVertices;i++)
  589. {
  590. b3Float4 v0 = verticesA[i];
  591. b3Float4 v1 = verticesA[prevVertex];
  592. b3Float4 edgeNormal = b3Normalized(b3Cross(normal,v1-v0));
  593. float c = -b3Dot(edgeNormal,v0);
  594. facesA[fidx].m_numIndices = 2;
  595. facesA[fidx].m_indexOffset=curUsedIndices;
  596. indicesA[curUsedIndices++]=i;
  597. indicesA[curUsedIndices++]=prevVertex;
  598. facesA[fidx].m_plane.x = edgeNormal.x;
  599. facesA[fidx].m_plane.y = edgeNormal.y;
  600. facesA[fidx].m_plane.z = edgeNormal.z;
  601. facesA[fidx].m_plane.w = c;
  602. fidx++;
  603. prevVertex = i;
  604. }
  605. }
  606. convexPolyhedronA.m_numFaces = B3_TRIANGLE_NUM_CONVEX_FACES;
  607. convexPolyhedronA.m_localCenter = localCenter*(1.f/3.f);
  608. b3Float4 posA = rigidBodies[bodyIndexA].m_pos;
  609. posA.w = 0.f;
  610. b3Float4 posB = rigidBodies[bodyIndexB].m_pos;
  611. posB.w = 0.f;
  612. b3Quaternion ornA = rigidBodies[bodyIndexA].m_quat;
  613. b3Quaternion ornB =rigidBodies[bodyIndexB].m_quat;
  614. ///////////////////
  615. ///compound shape support
  616. if (collidables[collidableIndexB].m_shapeType==SHAPE_COMPOUND_OF_CONVEX_HULLS)
  617. {
  618. int compoundChild = concavePairs[pairIdx].w;
  619. int childShapeIndexB = compoundChild;//collidables[collidableIndexB].m_shapeIndex+compoundChild;
  620. int childColIndexB = gpuChildShapes[childShapeIndexB].m_shapeIndex;
  621. b3Float4 childPosB = gpuChildShapes[childShapeIndexB].m_childPosition;
  622. b3Quaternion childOrnB = gpuChildShapes[childShapeIndexB].m_childOrientation;
  623. b3Float4 newPosB = b3TransformPoint(childPosB,posB,ornB);
  624. b3Quaternion newOrnB = b3QuatMul(ornB,childOrnB);
  625. posB = newPosB;
  626. ornB = newOrnB;
  627. shapeIndexB = collidables[childColIndexB].m_shapeIndex;
  628. }
  629. //////////////////
  630. b3Float4 c0local = convexPolyhedronA.m_localCenter;
  631. b3Float4 c0 = b3TransformPoint(c0local, posA, ornA);
  632. b3Float4 c1local = convexShapes[shapeIndexB].m_localCenter;
  633. b3Float4 c1 = b3TransformPoint(c1local,posB,ornB);
  634. const b3Float4 DeltaC2 = c0 - c1;
  635. bool sepA = b3FindSeparatingAxis( &convexPolyhedronA, &convexShapes[shapeIndexB],
  636. posA,ornA,
  637. posB,ornB,
  638. DeltaC2,
  639. verticesA,uniqueEdgesA,facesA,indicesA,
  640. vertices,uniqueEdges,faces,indices,
  641. &sepAxis,&dmin);
  642. hasSeparatingAxis = 4;
  643. if (!sepA)
  644. {
  645. hasSeparatingAxis = 0;
  646. } else
  647. {
  648. bool sepB = b3FindSeparatingAxis( &convexShapes[shapeIndexB],&convexPolyhedronA,
  649. posB,ornB,
  650. posA,ornA,
  651. DeltaC2,
  652. vertices,uniqueEdges,faces,indices,
  653. verticesA,uniqueEdgesA,facesA,indicesA,
  654. &sepAxis,&dmin);
  655. if (!sepB)
  656. {
  657. hasSeparatingAxis = 0;
  658. } else
  659. {
  660. bool sepEE = b3FindSeparatingAxisEdgeEdge( &convexPolyhedronA, &convexShapes[shapeIndexB],
  661. posA,ornA,
  662. posB,ornB,
  663. DeltaC2,
  664. verticesA,uniqueEdgesA,facesA,indicesA,
  665. vertices,uniqueEdges,faces,indices,
  666. &sepAxis,&dmin,true);
  667. if (!sepEE)
  668. {
  669. hasSeparatingAxis = 0;
  670. } else
  671. {
  672. hasSeparatingAxis = 1;
  673. }
  674. }
  675. }
  676. if (hasSeparatingAxis)
  677. {
  678. hasSeparatingNormals[i]=1;
  679. sepAxis.w = dmin;
  680. concaveSeparatingNormalsOut[pairIdx]=sepAxis;
  681. //now compute clipping faces A and B, and world-space clipping vertices A and B...
  682. float minDist = -1e30f;
  683. float maxDist = 0.02f;
  684. b3FindClippingFaces(sepAxis,
  685. &convexPolyhedronA,
  686. &convexShapes[shapeIndexB],
  687. posA,ornA,
  688. posB,ornB,
  689. worldVertsA1Out,
  690. worldNormalsA1Out,
  691. worldVertsB1Out,
  692. vertexFaceCapacity,
  693. minDist, maxDist,
  694. verticesA,
  695. facesA,
  696. indicesA,
  697. vertices,
  698. faces,
  699. indices,
  700. clippingFacesOut, pairIdx);
  701. } else
  702. {
  703. //mark this pair as in-active
  704. concavePairs[pairIdx].w = -1;
  705. }
  706. }
  707. else
  708. {
  709. //mark this pair as in-active
  710. concavePairs[pairIdx].w = -1;
  711. }
  712. }
  713. #endif //B3_FIND_CONCAVE_SEPARATING_AXIS_H