b3FindConcaveSatAxis.h 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798
  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. bool b3FindSeparatingAxisEdgeEdge(const b3ConvexPolyhedronData* hullA, __global const b3ConvexPolyhedronData* hullB,
  278. b3Float4ConstArg posA1,
  279. b3QuatConstArg ornA,
  280. b3Float4ConstArg posB1,
  281. b3QuatConstArg ornB,
  282. b3Float4ConstArg DeltaC2,
  283. const b3Float4* verticesA,
  284. const b3Float4* uniqueEdgesA,
  285. const b3GpuFace* facesA,
  286. const int* indicesA,
  287. __global const b3Float4* verticesB,
  288. __global const b3Float4* uniqueEdgesB,
  289. __global const b3GpuFace* facesB,
  290. __global const int* indicesB,
  291. b3Float4* sep,
  292. float* dmin,
  293. bool searchAllEdgeEdge)
  294. {
  295. b3Float4 posA = posA1;
  296. posA.w = 0.f;
  297. b3Float4 posB = posB1;
  298. posB.w = 0.f;
  299. // int curPlaneTests=0;
  300. int curEdgeEdge = 0;
  301. // Test edges
  302. static int maxEdgeTests = 0;
  303. int curEdgeTests = hullA->m_numUniqueEdges * hullB->m_numUniqueEdges;
  304. if (curEdgeTests > maxEdgeTests)
  305. {
  306. maxEdgeTests = curEdgeTests;
  307. printf("maxEdgeTests = %d\n", maxEdgeTests);
  308. printf("hullA->m_numUniqueEdges = %d\n", hullA->m_numUniqueEdges);
  309. printf("hullB->m_numUniqueEdges = %d\n", hullB->m_numUniqueEdges);
  310. }
  311. if (searchAllEdgeEdge)
  312. {
  313. for (int e0 = 0; e0 < hullA->m_numUniqueEdges; e0++)
  314. {
  315. const b3Float4 edge0 = uniqueEdgesA[hullA->m_uniqueEdgesOffset + e0];
  316. b3Float4 edge0World = b3QuatRotate(ornA, edge0);
  317. for (int e1 = 0; e1 < hullB->m_numUniqueEdges; e1++)
  318. {
  319. const b3Float4 edge1 = uniqueEdgesB[hullB->m_uniqueEdgesOffset + e1];
  320. b3Float4 edge1World = b3QuatRotate(ornB, edge1);
  321. b3Float4 crossje = b3Cross(edge0World, edge1World);
  322. curEdgeEdge++;
  323. if (!b3IsAlmostZero(crossje))
  324. {
  325. crossje = b3Normalized(crossje);
  326. if (b3Dot(DeltaC2, crossje) < 0)
  327. crossje *= -1.f;
  328. float dist;
  329. bool result = true;
  330. {
  331. float Min0, Max0;
  332. float Min1, Max1;
  333. b3Project(hullA, posA, ornA, &crossje, verticesA, &Min0, &Max0);
  334. b3Project(hullB, posB, ornB, &crossje, verticesB, &Min1, &Max1);
  335. if (Max0 < Min1 || Max1 < Min0)
  336. return false;
  337. float d0 = Max0 - Min1;
  338. float d1 = Max1 - Min0;
  339. dist = d0 < d1 ? d0 : d1;
  340. result = true;
  341. }
  342. if (dist < *dmin)
  343. {
  344. *dmin = dist;
  345. *sep = crossje;
  346. }
  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. int i = pairIdx;
  486. /* int i = get_global_id(0);
  487. if (i>=numConcavePairs)
  488. return;
  489. int pairIdx = i;
  490. */
  491. int bodyIndexA = concavePairs[i].x;
  492. int bodyIndexB = concavePairs[i].y;
  493. int collidableIndexA = rigidBodies[bodyIndexA].m_collidableIdx;
  494. int collidableIndexB = rigidBodies[bodyIndexB].m_collidableIdx;
  495. int shapeIndexA = collidables[collidableIndexA].m_shapeIndex;
  496. int shapeIndexB = collidables[collidableIndexB].m_shapeIndex;
  497. if (collidables[collidableIndexB].m_shapeType != SHAPE_CONVEX_HULL &&
  498. collidables[collidableIndexB].m_shapeType != SHAPE_COMPOUND_OF_CONVEX_HULLS)
  499. {
  500. concavePairs[pairIdx].w = -1;
  501. return;
  502. }
  503. hasSeparatingNormals[i] = 0;
  504. // int numFacesA = convexShapes[shapeIndexA].m_numFaces;
  505. int numActualConcaveConvexTests = 0;
  506. int f = concavePairs[i].z;
  507. bool overlap = false;
  508. b3ConvexPolyhedronData convexPolyhedronA;
  509. //add 3 vertices of the triangle
  510. convexPolyhedronA.m_numVertices = 3;
  511. convexPolyhedronA.m_vertexOffset = 0;
  512. b3Float4 localCenter = b3MakeFloat4(0.f, 0.f, 0.f, 0.f);
  513. b3GpuFace face = faces[convexShapes[shapeIndexA].m_faceOffset + f];
  514. b3Aabb triAabb;
  515. triAabb.m_minVec = b3MakeFloat4(1e30f, 1e30f, 1e30f, 0.f);
  516. triAabb.m_maxVec = b3MakeFloat4(-1e30f, -1e30f, -1e30f, 0.f);
  517. b3Float4 verticesA[3];
  518. for (int i = 0; i < 3; i++)
  519. {
  520. int index = indices[face.m_indexOffset + i];
  521. b3Float4 vert = vertices[convexShapes[shapeIndexA].m_vertexOffset + index];
  522. verticesA[i] = vert;
  523. localCenter += vert;
  524. triAabb.m_minVec = b3MinFloat4(triAabb.m_minVec, vert);
  525. triAabb.m_maxVec = b3MaxFloat4(triAabb.m_maxVec, vert);
  526. }
  527. overlap = true;
  528. overlap = (triAabb.m_minVec.x > aabbs[bodyIndexB].m_maxVec.x || triAabb.m_maxVec.x < aabbs[bodyIndexB].m_minVec.x) ? false : overlap;
  529. overlap = (triAabb.m_minVec.z > aabbs[bodyIndexB].m_maxVec.z || triAabb.m_maxVec.z < aabbs[bodyIndexB].m_minVec.z) ? false : overlap;
  530. overlap = (triAabb.m_minVec.y > aabbs[bodyIndexB].m_maxVec.y || triAabb.m_maxVec.y < aabbs[bodyIndexB].m_minVec.y) ? false : overlap;
  531. if (overlap)
  532. {
  533. float dmin = FLT_MAX;
  534. int hasSeparatingAxis = 5;
  535. b3Float4 sepAxis = b3MakeFloat4(1, 2, 3, 4);
  536. // int localCC=0;
  537. numActualConcaveConvexTests++;
  538. //a triangle has 3 unique edges
  539. convexPolyhedronA.m_numUniqueEdges = 3;
  540. convexPolyhedronA.m_uniqueEdgesOffset = 0;
  541. b3Float4 uniqueEdgesA[3];
  542. uniqueEdgesA[0] = (verticesA[1] - verticesA[0]);
  543. uniqueEdgesA[1] = (verticesA[2] - verticesA[1]);
  544. uniqueEdgesA[2] = (verticesA[0] - verticesA[2]);
  545. convexPolyhedronA.m_faceOffset = 0;
  546. b3Float4 normal = b3MakeFloat4(face.m_plane.x, face.m_plane.y, face.m_plane.z, 0.f);
  547. b3GpuFace facesA[B3_TRIANGLE_NUM_CONVEX_FACES];
  548. int indicesA[3 + 3 + 2 + 2 + 2];
  549. int curUsedIndices = 0;
  550. int fidx = 0;
  551. //front size of triangle
  552. {
  553. facesA[fidx].m_indexOffset = curUsedIndices;
  554. indicesA[0] = 0;
  555. indicesA[1] = 1;
  556. indicesA[2] = 2;
  557. curUsedIndices += 3;
  558. float c = face.m_plane.w;
  559. facesA[fidx].m_plane.x = normal.x;
  560. facesA[fidx].m_plane.y = normal.y;
  561. facesA[fidx].m_plane.z = normal.z;
  562. facesA[fidx].m_plane.w = c;
  563. facesA[fidx].m_numIndices = 3;
  564. }
  565. fidx++;
  566. //back size of triangle
  567. {
  568. facesA[fidx].m_indexOffset = curUsedIndices;
  569. indicesA[3] = 2;
  570. indicesA[4] = 1;
  571. indicesA[5] = 0;
  572. curUsedIndices += 3;
  573. float c = b3Dot(normal, verticesA[0]);
  574. // float c1 = -face.m_plane.w;
  575. facesA[fidx].m_plane.x = -normal.x;
  576. facesA[fidx].m_plane.y = -normal.y;
  577. facesA[fidx].m_plane.z = -normal.z;
  578. facesA[fidx].m_plane.w = c;
  579. facesA[fidx].m_numIndices = 3;
  580. }
  581. fidx++;
  582. bool addEdgePlanes = true;
  583. if (addEdgePlanes)
  584. {
  585. int numVertices = 3;
  586. int prevVertex = numVertices - 1;
  587. for (int i = 0; i < numVertices; i++)
  588. {
  589. b3Float4 v0 = verticesA[i];
  590. b3Float4 v1 = verticesA[prevVertex];
  591. b3Float4 edgeNormal = b3Normalized(b3Cross(normal, v1 - v0));
  592. float c = -b3Dot(edgeNormal, v0);
  593. facesA[fidx].m_numIndices = 2;
  594. facesA[fidx].m_indexOffset = curUsedIndices;
  595. indicesA[curUsedIndices++] = i;
  596. indicesA[curUsedIndices++] = prevVertex;
  597. facesA[fidx].m_plane.x = edgeNormal.x;
  598. facesA[fidx].m_plane.y = edgeNormal.y;
  599. facesA[fidx].m_plane.z = edgeNormal.z;
  600. facesA[fidx].m_plane.w = c;
  601. fidx++;
  602. prevVertex = i;
  603. }
  604. }
  605. convexPolyhedronA.m_numFaces = B3_TRIANGLE_NUM_CONVEX_FACES;
  606. convexPolyhedronA.m_localCenter = localCenter * (1.f / 3.f);
  607. b3Float4 posA = rigidBodies[bodyIndexA].m_pos;
  608. posA.w = 0.f;
  609. b3Float4 posB = rigidBodies[bodyIndexB].m_pos;
  610. posB.w = 0.f;
  611. b3Quaternion ornA = rigidBodies[bodyIndexA].m_quat;
  612. b3Quaternion ornB = rigidBodies[bodyIndexB].m_quat;
  613. ///////////////////
  614. ///compound shape support
  615. if (collidables[collidableIndexB].m_shapeType == SHAPE_COMPOUND_OF_CONVEX_HULLS)
  616. {
  617. int compoundChild = concavePairs[pairIdx].w;
  618. int childShapeIndexB = compoundChild; //collidables[collidableIndexB].m_shapeIndex+compoundChild;
  619. int childColIndexB = gpuChildShapes[childShapeIndexB].m_shapeIndex;
  620. b3Float4 childPosB = gpuChildShapes[childShapeIndexB].m_childPosition;
  621. b3Quaternion childOrnB = gpuChildShapes[childShapeIndexB].m_childOrientation;
  622. b3Float4 newPosB = b3TransformPoint(childPosB, posB, ornB);
  623. b3Quaternion newOrnB = b3QuatMul(ornB, childOrnB);
  624. posB = newPosB;
  625. ornB = newOrnB;
  626. shapeIndexB = collidables[childColIndexB].m_shapeIndex;
  627. }
  628. //////////////////
  629. b3Float4 c0local = convexPolyhedronA.m_localCenter;
  630. b3Float4 c0 = b3TransformPoint(c0local, posA, ornA);
  631. b3Float4 c1local = convexShapes[shapeIndexB].m_localCenter;
  632. b3Float4 c1 = b3TransformPoint(c1local, posB, ornB);
  633. const b3Float4 DeltaC2 = c0 - c1;
  634. bool sepA = b3FindSeparatingAxis(&convexPolyhedronA, &convexShapes[shapeIndexB],
  635. posA, ornA,
  636. posB, ornB,
  637. DeltaC2,
  638. verticesA, uniqueEdgesA, facesA, indicesA,
  639. vertices, uniqueEdges, faces, indices,
  640. &sepAxis, &dmin);
  641. hasSeparatingAxis = 4;
  642. if (!sepA)
  643. {
  644. hasSeparatingAxis = 0;
  645. }
  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. }
  659. else
  660. {
  661. bool sepEE = b3FindSeparatingAxisEdgeEdge(&convexPolyhedronA, &convexShapes[shapeIndexB],
  662. posA, ornA,
  663. posB, ornB,
  664. DeltaC2,
  665. verticesA, uniqueEdgesA, facesA, indicesA,
  666. vertices, uniqueEdges, faces, indices,
  667. &sepAxis, &dmin, true);
  668. if (!sepEE)
  669. {
  670. hasSeparatingAxis = 0;
  671. }
  672. else
  673. {
  674. hasSeparatingAxis = 1;
  675. }
  676. }
  677. }
  678. if (hasSeparatingAxis)
  679. {
  680. hasSeparatingNormals[i] = 1;
  681. sepAxis.w = dmin;
  682. concaveSeparatingNormalsOut[pairIdx] = sepAxis;
  683. //now compute clipping faces A and B, and world-space clipping vertices A and B...
  684. float minDist = -1e30f;
  685. float maxDist = 0.02f;
  686. b3FindClippingFaces(sepAxis,
  687. &convexPolyhedronA,
  688. &convexShapes[shapeIndexB],
  689. posA, ornA,
  690. posB, ornB,
  691. worldVertsA1Out,
  692. worldNormalsA1Out,
  693. worldVertsB1Out,
  694. vertexFaceCapacity,
  695. minDist, maxDist,
  696. verticesA,
  697. facesA,
  698. indicesA,
  699. vertices,
  700. faces,
  701. indices,
  702. clippingFacesOut, pairIdx);
  703. }
  704. else
  705. {
  706. //mark this pair as in-active
  707. concavePairs[pairIdx].w = -1;
  708. }
  709. }
  710. else
  711. {
  712. //mark this pair as in-active
  713. concavePairs[pairIdx].w = -1;
  714. }
  715. }
  716. #endif //B3_FIND_CONCAVE_SEPARATING_AXIS_H