primitiveContacts.cl 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374
  1. #include "Bullet3Collision/NarrowPhaseCollision/shared/b3Contact4Data.h"
  2. #define SHAPE_CONVEX_HULL 3
  3. #define SHAPE_PLANE 4
  4. #define SHAPE_CONCAVE_TRIMESH 5
  5. #define SHAPE_COMPOUND_OF_CONVEX_HULLS 6
  6. #define SHAPE_SPHERE 7
  7. #pragma OPENCL EXTENSION cl_amd_printf : enable
  8. #pragma OPENCL EXTENSION cl_khr_local_int32_base_atomics : enable
  9. #pragma OPENCL EXTENSION cl_khr_global_int32_base_atomics : enable
  10. #pragma OPENCL EXTENSION cl_khr_local_int32_extended_atomics : enable
  11. #pragma OPENCL EXTENSION cl_khr_global_int32_extended_atomics : enable
  12. #ifdef cl_ext_atomic_counters_32
  13. #pragma OPENCL EXTENSION cl_ext_atomic_counters_32 : enable
  14. #else
  15. #define counter32_t volatile __global int*
  16. #endif
  17. #define GET_GROUP_IDX get_group_id(0)
  18. #define GET_LOCAL_IDX get_local_id(0)
  19. #define GET_GLOBAL_IDX get_global_id(0)
  20. #define GET_GROUP_SIZE get_local_size(0)
  21. #define GET_NUM_GROUPS get_num_groups(0)
  22. #define GROUP_LDS_BARRIER barrier(CLK_LOCAL_MEM_FENCE)
  23. #define GROUP_MEM_FENCE mem_fence(CLK_LOCAL_MEM_FENCE)
  24. #define AtomInc(x) atom_inc(&(x))
  25. #define AtomInc1(x, out) out = atom_inc(&(x))
  26. #define AppendInc(x, out) out = atomic_inc(x)
  27. #define AtomAdd(x, value) atom_add(&(x), value)
  28. #define AtomCmpxhg(x, cmp, value) atom_cmpxchg( &(x), cmp, value )
  29. #define AtomXhg(x, value) atom_xchg ( &(x), value )
  30. #define max2 max
  31. #define min2 min
  32. typedef unsigned int u32;
  33. typedef struct
  34. {
  35. union
  36. {
  37. float4 m_min;
  38. float m_minElems[4];
  39. int m_minIndices[4];
  40. };
  41. union
  42. {
  43. float4 m_max;
  44. float m_maxElems[4];
  45. int m_maxIndices[4];
  46. };
  47. } btAabbCL;
  48. ///keep this in sync with btCollidable.h
  49. typedef struct
  50. {
  51. int m_numChildShapes;
  52. float m_radius;
  53. int m_shapeType;
  54. int m_shapeIndex;
  55. } btCollidableGpu;
  56. typedef struct
  57. {
  58. float4 m_childPosition;
  59. float4 m_childOrientation;
  60. int m_shapeIndex;
  61. int m_unused0;
  62. int m_unused1;
  63. int m_unused2;
  64. } btGpuChildShape;
  65. #define GET_NPOINTS(x) (x).m_worldNormalOnB.w
  66. typedef struct
  67. {
  68. float4 m_pos;
  69. float4 m_quat;
  70. float4 m_linVel;
  71. float4 m_angVel;
  72. u32 m_collidableIdx;
  73. float m_invMass;
  74. float m_restituitionCoeff;
  75. float m_frictionCoeff;
  76. } BodyData;
  77. typedef struct
  78. {
  79. float4 m_localCenter;
  80. float4 m_extents;
  81. float4 mC;
  82. float4 mE;
  83. float m_radius;
  84. int m_faceOffset;
  85. int m_numFaces;
  86. int m_numVertices;
  87. int m_vertexOffset;
  88. int m_uniqueEdgesOffset;
  89. int m_numUniqueEdges;
  90. int m_unused;
  91. } ConvexPolyhedronCL;
  92. typedef struct
  93. {
  94. float4 m_plane;
  95. int m_indexOffset;
  96. int m_numIndices;
  97. } btGpuFace;
  98. #define SELECT_UINT4( b, a, condition ) select( b,a,condition )
  99. #define make_float4 (float4)
  100. #define make_float2 (float2)
  101. #define make_uint4 (uint4)
  102. #define make_int4 (int4)
  103. #define make_uint2 (uint2)
  104. #define make_int2 (int2)
  105. __inline
  106. float fastDiv(float numerator, float denominator)
  107. {
  108. return native_divide(numerator, denominator);
  109. // return numerator/denominator;
  110. }
  111. __inline
  112. float4 fastDiv4(float4 numerator, float4 denominator)
  113. {
  114. return native_divide(numerator, denominator);
  115. }
  116. __inline
  117. float4 cross3(float4 a, float4 b)
  118. {
  119. return cross(a,b);
  120. }
  121. //#define dot3F4 dot
  122. __inline
  123. float dot3F4(float4 a, float4 b)
  124. {
  125. float4 a1 = make_float4(a.xyz,0.f);
  126. float4 b1 = make_float4(b.xyz,0.f);
  127. return dot(a1, b1);
  128. }
  129. __inline
  130. float4 fastNormalize4(float4 v)
  131. {
  132. return fast_normalize(v);
  133. }
  134. ///////////////////////////////////////
  135. // Quaternion
  136. ///////////////////////////////////////
  137. typedef float4 Quaternion;
  138. __inline
  139. Quaternion qtMul(Quaternion a, Quaternion b);
  140. __inline
  141. Quaternion qtNormalize(Quaternion in);
  142. __inline
  143. float4 qtRotate(Quaternion q, float4 vec);
  144. __inline
  145. Quaternion qtInvert(Quaternion q);
  146. __inline
  147. Quaternion qtMul(Quaternion a, Quaternion b)
  148. {
  149. Quaternion ans;
  150. ans = cross3( a, b );
  151. ans += a.w*b+b.w*a;
  152. // ans.w = a.w*b.w - (a.x*b.x+a.y*b.y+a.z*b.z);
  153. ans.w = a.w*b.w - dot3F4(a, b);
  154. return ans;
  155. }
  156. __inline
  157. Quaternion qtNormalize(Quaternion in)
  158. {
  159. return fastNormalize4(in);
  160. // in /= length( in );
  161. // return in;
  162. }
  163. __inline
  164. float4 qtRotate(Quaternion q, float4 vec)
  165. {
  166. Quaternion qInv = qtInvert( q );
  167. float4 vcpy = vec;
  168. vcpy.w = 0.f;
  169. float4 out = qtMul(qtMul(q,vcpy),qInv);
  170. return out;
  171. }
  172. __inline
  173. Quaternion qtInvert(Quaternion q)
  174. {
  175. return (Quaternion)(-q.xyz, q.w);
  176. }
  177. __inline
  178. float4 qtInvRotate(const Quaternion q, float4 vec)
  179. {
  180. return qtRotate( qtInvert( q ), vec );
  181. }
  182. __inline
  183. float4 transform(const float4* p, const float4* translation, const Quaternion* orientation)
  184. {
  185. return qtRotate( *orientation, *p ) + (*translation);
  186. }
  187. void trInverse(float4 translationIn, Quaternion orientationIn,
  188. float4* translationOut, Quaternion* orientationOut)
  189. {
  190. *orientationOut = qtInvert(orientationIn);
  191. *translationOut = qtRotate(*orientationOut, -translationIn);
  192. }
  193. void trMul(float4 translationA, Quaternion orientationA,
  194. float4 translationB, Quaternion orientationB,
  195. float4* translationOut, Quaternion* orientationOut)
  196. {
  197. *orientationOut = qtMul(orientationA,orientationB);
  198. *translationOut = transform(&translationB,&translationA,&orientationA);
  199. }
  200. __inline
  201. float4 normalize3(const float4 a)
  202. {
  203. float4 n = make_float4(a.x, a.y, a.z, 0.f);
  204. return fastNormalize4( n );
  205. }
  206. __inline float4 lerp3(const float4 a,const float4 b, float t)
  207. {
  208. return make_float4( a.x + (b.x - a.x) * t,
  209. a.y + (b.y - a.y) * t,
  210. a.z + (b.z - a.z) * t,
  211. 0.f);
  212. }
  213. float signedDistanceFromPointToPlane(float4 point, float4 planeEqn, float4* closestPointOnFace)
  214. {
  215. float4 n = (float4)(planeEqn.x, planeEqn.y, planeEqn.z, 0);
  216. float dist = dot3F4(n, point) + planeEqn.w;
  217. *closestPointOnFace = point - dist * n;
  218. return dist;
  219. }
  220. inline bool IsPointInPolygon(float4 p,
  221. const btGpuFace* face,
  222. __global const float4* baseVertex,
  223. __global const int* convexIndices,
  224. float4* out)
  225. {
  226. float4 a;
  227. float4 b;
  228. float4 ab;
  229. float4 ap;
  230. float4 v;
  231. float4 plane = make_float4(face->m_plane.x,face->m_plane.y,face->m_plane.z,0.f);
  232. if (face->m_numIndices<2)
  233. return false;
  234. float4 v0 = baseVertex[convexIndices[face->m_indexOffset + face->m_numIndices-1]];
  235. b = v0;
  236. for(unsigned i=0; i != face->m_numIndices; ++i)
  237. {
  238. a = b;
  239. float4 vi = baseVertex[convexIndices[face->m_indexOffset + i]];
  240. b = vi;
  241. ab = b-a;
  242. ap = p-a;
  243. v = cross3(ab,plane);
  244. if (dot(ap, v) > 0.f)
  245. {
  246. float ab_m2 = dot(ab, ab);
  247. float rt = ab_m2 != 0.f ? dot(ab, ap) / ab_m2 : 0.f;
  248. if (rt <= 0.f)
  249. {
  250. *out = a;
  251. }
  252. else if (rt >= 1.f)
  253. {
  254. *out = b;
  255. }
  256. else
  257. {
  258. float s = 1.f - rt;
  259. out[0].x = s * a.x + rt * b.x;
  260. out[0].y = s * a.y + rt * b.y;
  261. out[0].z = s * a.z + rt * b.z;
  262. }
  263. return false;
  264. }
  265. }
  266. return true;
  267. }
  268. void computeContactSphereConvex(int pairIndex,
  269. int bodyIndexA, int bodyIndexB,
  270. int collidableIndexA, int collidableIndexB,
  271. __global const BodyData* rigidBodies,
  272. __global const btCollidableGpu* collidables,
  273. __global const ConvexPolyhedronCL* convexShapes,
  274. __global const float4* convexVertices,
  275. __global const int* convexIndices,
  276. __global const btGpuFace* faces,
  277. __global struct b3Contact4Data* restrict globalContactsOut,
  278. counter32_t nGlobalContactsOut,
  279. int maxContactCapacity,
  280. float4 spherePos2,
  281. float radius,
  282. float4 pos,
  283. float4 quat
  284. )
  285. {
  286. float4 invPos;
  287. float4 invOrn;
  288. trInverse(pos,quat, &invPos,&invOrn);
  289. float4 spherePos = transform(&spherePos2,&invPos,&invOrn);
  290. int shapeIndex = collidables[collidableIndexB].m_shapeIndex;
  291. int numFaces = convexShapes[shapeIndex].m_numFaces;
  292. float4 closestPnt = (float4)(0, 0, 0, 0);
  293. float4 hitNormalWorld = (float4)(0, 0, 0, 0);
  294. float minDist = -1000000.f;
  295. bool bCollide = true;
  296. for ( int f = 0; f < numFaces; f++ )
  297. {
  298. btGpuFace face = faces[convexShapes[shapeIndex].m_faceOffset+f];
  299. // set up a plane equation
  300. float4 planeEqn;
  301. float4 n1 = face.m_plane;
  302. n1.w = 0.f;
  303. planeEqn = n1;
  304. planeEqn.w = face.m_plane.w;
  305. // compute a signed distance from the vertex in cloth to the face of rigidbody.
  306. float4 pntReturn;
  307. float dist = signedDistanceFromPointToPlane(spherePos, planeEqn, &pntReturn);
  308. // If the distance is positive, the plane is a separating plane.
  309. if ( dist > radius )
  310. {
  311. bCollide = false;
  312. break;
  313. }
  314. if (dist>0)
  315. {
  316. //might hit an edge or vertex
  317. float4 out;
  318. float4 zeroPos = make_float4(0,0,0,0);
  319. bool isInPoly = IsPointInPolygon(spherePos,
  320. &face,
  321. &convexVertices[convexShapes[shapeIndex].m_vertexOffset],
  322. convexIndices,
  323. &out);
  324. if (isInPoly)
  325. {
  326. if (dist>minDist)
  327. {
  328. minDist = dist;
  329. closestPnt = pntReturn;
  330. hitNormalWorld = planeEqn;
  331. }
  332. } else
  333. {
  334. float4 tmp = spherePos-out;
  335. float l2 = dot(tmp,tmp);
  336. if (l2<radius*radius)
  337. {
  338. dist = sqrt(l2);
  339. if (dist>minDist)
  340. {
  341. minDist = dist;
  342. closestPnt = out;
  343. hitNormalWorld = tmp/dist;
  344. }
  345. } else
  346. {
  347. bCollide = false;
  348. break;
  349. }
  350. }
  351. } else
  352. {
  353. if ( dist > minDist )
  354. {
  355. minDist = dist;
  356. closestPnt = pntReturn;
  357. hitNormalWorld.xyz = planeEqn.xyz;
  358. }
  359. }
  360. }
  361. if (bCollide && minDist > -10000)
  362. {
  363. float4 normalOnSurfaceB1 = qtRotate(quat,-hitNormalWorld);
  364. float4 pOnB1 = transform(&closestPnt,&pos,&quat);
  365. float actualDepth = minDist-radius;
  366. if (actualDepth<=0.f)
  367. {
  368. pOnB1.w = actualDepth;
  369. int dstIdx;
  370. AppendInc( nGlobalContactsOut, dstIdx );
  371. if (1)//dstIdx < maxContactCapacity)
  372. {
  373. __global struct b3Contact4Data* c = &globalContactsOut[dstIdx];
  374. c->m_worldNormalOnB = -normalOnSurfaceB1;
  375. c->m_restituitionCoeffCmp = (0.f*0xffff);c->m_frictionCoeffCmp = (0.7f*0xffff);
  376. c->m_batchIdx = pairIndex;
  377. c->m_bodyAPtrAndSignBit = rigidBodies[bodyIndexA].m_invMass==0?-bodyIndexA:bodyIndexA;
  378. c->m_bodyBPtrAndSignBit = rigidBodies[bodyIndexB].m_invMass==0?-bodyIndexB:bodyIndexB;
  379. c->m_worldPosB[0] = pOnB1;
  380. c->m_childIndexA = -1;
  381. c->m_childIndexB = -1;
  382. GET_NPOINTS(*c) = 1;
  383. }
  384. }
  385. }//if (hasCollision)
  386. }
  387. int extractManifoldSequential(const float4* p, int nPoints, float4 nearNormal, int4* contactIdx)
  388. {
  389. if( nPoints == 0 )
  390. return 0;
  391. if (nPoints <=4)
  392. return nPoints;
  393. if (nPoints >64)
  394. nPoints = 64;
  395. float4 center = make_float4(0.f);
  396. {
  397. for (int i=0;i<nPoints;i++)
  398. center += p[i];
  399. center /= (float)nPoints;
  400. }
  401. // sample 4 directions
  402. float4 aVector = p[0] - center;
  403. float4 u = cross3( nearNormal, aVector );
  404. float4 v = cross3( nearNormal, u );
  405. u = normalize3( u );
  406. v = normalize3( v );
  407. //keep point with deepest penetration
  408. float minW= FLT_MAX;
  409. int minIndex=-1;
  410. float4 maxDots;
  411. maxDots.x = FLT_MIN;
  412. maxDots.y = FLT_MIN;
  413. maxDots.z = FLT_MIN;
  414. maxDots.w = FLT_MIN;
  415. // idx, distance
  416. for(int ie = 0; ie<nPoints; ie++ )
  417. {
  418. if (p[ie].w<minW)
  419. {
  420. minW = p[ie].w;
  421. minIndex=ie;
  422. }
  423. float f;
  424. float4 r = p[ie]-center;
  425. f = dot3F4( u, r );
  426. if (f<maxDots.x)
  427. {
  428. maxDots.x = f;
  429. contactIdx[0].x = ie;
  430. }
  431. f = dot3F4( -u, r );
  432. if (f<maxDots.y)
  433. {
  434. maxDots.y = f;
  435. contactIdx[0].y = ie;
  436. }
  437. f = dot3F4( v, r );
  438. if (f<maxDots.z)
  439. {
  440. maxDots.z = f;
  441. contactIdx[0].z = ie;
  442. }
  443. f = dot3F4( -v, r );
  444. if (f<maxDots.w)
  445. {
  446. maxDots.w = f;
  447. contactIdx[0].w = ie;
  448. }
  449. }
  450. if (contactIdx[0].x != minIndex && contactIdx[0].y != minIndex && contactIdx[0].z != minIndex && contactIdx[0].w != minIndex)
  451. {
  452. //replace the first contact with minimum (todo: replace contact with least penetration)
  453. contactIdx[0].x = minIndex;
  454. }
  455. return 4;
  456. }
  457. #define MAX_PLANE_CONVEX_POINTS 64
  458. int computeContactPlaneConvex(int pairIndex,
  459. int bodyIndexA, int bodyIndexB,
  460. int collidableIndexA, int collidableIndexB,
  461. __global const BodyData* rigidBodies,
  462. __global const btCollidableGpu*collidables,
  463. __global const ConvexPolyhedronCL* convexShapes,
  464. __global const float4* convexVertices,
  465. __global const int* convexIndices,
  466. __global const btGpuFace* faces,
  467. __global struct b3Contact4Data* restrict globalContactsOut,
  468. counter32_t nGlobalContactsOut,
  469. int maxContactCapacity,
  470. float4 posB,
  471. Quaternion ornB
  472. )
  473. {
  474. int resultIndex=-1;
  475. int shapeIndex = collidables[collidableIndexB].m_shapeIndex;
  476. __global const ConvexPolyhedronCL* hullB = &convexShapes[shapeIndex];
  477. float4 posA;
  478. posA = rigidBodies[bodyIndexA].m_pos;
  479. Quaternion ornA;
  480. ornA = rigidBodies[bodyIndexA].m_quat;
  481. int numContactsOut = 0;
  482. int numWorldVertsB1= 0;
  483. float4 planeEq;
  484. planeEq = faces[collidables[collidableIndexA].m_shapeIndex].m_plane;
  485. float4 planeNormal = make_float4(planeEq.x,planeEq.y,planeEq.z,0.f);
  486. float4 planeNormalWorld;
  487. planeNormalWorld = qtRotate(ornA,planeNormal);
  488. float planeConstant = planeEq.w;
  489. float4 invPosA;Quaternion invOrnA;
  490. float4 convexInPlaneTransPos1; Quaternion convexInPlaneTransOrn1;
  491. {
  492. trInverse(posA,ornA,&invPosA,&invOrnA);
  493. trMul(invPosA,invOrnA,posB,ornB,&convexInPlaneTransPos1,&convexInPlaneTransOrn1);
  494. }
  495. float4 invPosB;Quaternion invOrnB;
  496. float4 planeInConvexPos1; Quaternion planeInConvexOrn1;
  497. {
  498. trInverse(posB,ornB,&invPosB,&invOrnB);
  499. trMul(invPosB,invOrnB,posA,ornA,&planeInConvexPos1,&planeInConvexOrn1);
  500. }
  501. float4 planeNormalInConvex = qtRotate(planeInConvexOrn1,-planeNormal);
  502. float maxDot = -1e30;
  503. int hitVertex=-1;
  504. float4 hitVtx;
  505. float4 contactPoints[MAX_PLANE_CONVEX_POINTS];
  506. int numPoints = 0;
  507. int4 contactIdx;
  508. contactIdx=make_int4(0,1,2,3);
  509. for (int i=0;i<hullB->m_numVertices;i++)
  510. {
  511. float4 vtx = convexVertices[hullB->m_vertexOffset+i];
  512. float curDot = dot(vtx,planeNormalInConvex);
  513. if (curDot>maxDot)
  514. {
  515. hitVertex=i;
  516. maxDot=curDot;
  517. hitVtx = vtx;
  518. //make sure the deepest points is always included
  519. if (numPoints==MAX_PLANE_CONVEX_POINTS)
  520. numPoints--;
  521. }
  522. if (numPoints<MAX_PLANE_CONVEX_POINTS)
  523. {
  524. float4 vtxWorld = transform(&vtx, &posB, &ornB);
  525. float4 vtxInPlane = transform(&vtxWorld, &invPosA, &invOrnA);//oplaneTransform.inverse()*vtxWorld;
  526. float dist = dot(planeNormal,vtxInPlane)-planeConstant;
  527. if (dist<0.f)
  528. {
  529. vtxWorld.w = dist;
  530. contactPoints[numPoints] = vtxWorld;
  531. numPoints++;
  532. }
  533. }
  534. }
  535. int numReducedPoints = numPoints;
  536. if (numPoints>4)
  537. {
  538. numReducedPoints = extractManifoldSequential( contactPoints, numPoints, planeNormalInConvex, &contactIdx);
  539. }
  540. if (numReducedPoints>0)
  541. {
  542. int dstIdx;
  543. AppendInc( nGlobalContactsOut, dstIdx );
  544. if (dstIdx < maxContactCapacity)
  545. {
  546. resultIndex = dstIdx;
  547. __global struct b3Contact4Data* c = &globalContactsOut[dstIdx];
  548. c->m_worldNormalOnB = -planeNormalWorld;
  549. //c->setFrictionCoeff(0.7);
  550. //c->setRestituitionCoeff(0.f);
  551. c->m_restituitionCoeffCmp = (0.f*0xffff);c->m_frictionCoeffCmp = (0.7f*0xffff);
  552. c->m_batchIdx = pairIndex;
  553. c->m_bodyAPtrAndSignBit = rigidBodies[bodyIndexA].m_invMass==0?-bodyIndexA:bodyIndexA;
  554. c->m_bodyBPtrAndSignBit = rigidBodies[bodyIndexB].m_invMass==0?-bodyIndexB:bodyIndexB;
  555. c->m_childIndexA = -1;
  556. c->m_childIndexB = -1;
  557. switch (numReducedPoints)
  558. {
  559. case 4:
  560. c->m_worldPosB[3] = contactPoints[contactIdx.w];
  561. case 3:
  562. c->m_worldPosB[2] = contactPoints[contactIdx.z];
  563. case 2:
  564. c->m_worldPosB[1] = contactPoints[contactIdx.y];
  565. case 1:
  566. c->m_worldPosB[0] = contactPoints[contactIdx.x];
  567. default:
  568. {
  569. }
  570. };
  571. GET_NPOINTS(*c) = numReducedPoints;
  572. }//if (dstIdx < numPairs)
  573. }
  574. return resultIndex;
  575. }
  576. void computeContactPlaneSphere(int pairIndex,
  577. int bodyIndexA, int bodyIndexB,
  578. int collidableIndexA, int collidableIndexB,
  579. __global const BodyData* rigidBodies,
  580. __global const btCollidableGpu* collidables,
  581. __global const btGpuFace* faces,
  582. __global struct b3Contact4Data* restrict globalContactsOut,
  583. counter32_t nGlobalContactsOut,
  584. int maxContactCapacity)
  585. {
  586. float4 planeEq = faces[collidables[collidableIndexA].m_shapeIndex].m_plane;
  587. float radius = collidables[collidableIndexB].m_radius;
  588. float4 posA1 = rigidBodies[bodyIndexA].m_pos;
  589. float4 ornA1 = rigidBodies[bodyIndexA].m_quat;
  590. float4 posB1 = rigidBodies[bodyIndexB].m_pos;
  591. float4 ornB1 = rigidBodies[bodyIndexB].m_quat;
  592. bool hasCollision = false;
  593. float4 planeNormal1 = make_float4(planeEq.x,planeEq.y,planeEq.z,0.f);
  594. float planeConstant = planeEq.w;
  595. float4 convexInPlaneTransPos1; Quaternion convexInPlaneTransOrn1;
  596. {
  597. float4 invPosA;Quaternion invOrnA;
  598. trInverse(posA1,ornA1,&invPosA,&invOrnA);
  599. trMul(invPosA,invOrnA,posB1,ornB1,&convexInPlaneTransPos1,&convexInPlaneTransOrn1);
  600. }
  601. float4 planeInConvexPos1; Quaternion planeInConvexOrn1;
  602. {
  603. float4 invPosB;Quaternion invOrnB;
  604. trInverse(posB1,ornB1,&invPosB,&invOrnB);
  605. trMul(invPosB,invOrnB,posA1,ornA1,&planeInConvexPos1,&planeInConvexOrn1);
  606. }
  607. float4 vtx1 = qtRotate(planeInConvexOrn1,-planeNormal1)*radius;
  608. float4 vtxInPlane1 = transform(&vtx1,&convexInPlaneTransPos1,&convexInPlaneTransOrn1);
  609. float distance = dot3F4(planeNormal1,vtxInPlane1) - planeConstant;
  610. hasCollision = distance < 0.f;//m_manifoldPtr->getContactBreakingThreshold();
  611. if (hasCollision)
  612. {
  613. float4 vtxInPlaneProjected1 = vtxInPlane1 - distance*planeNormal1;
  614. float4 vtxInPlaneWorld1 = transform(&vtxInPlaneProjected1,&posA1,&ornA1);
  615. float4 normalOnSurfaceB1 = qtRotate(ornA1,planeNormal1);
  616. float4 pOnB1 = vtxInPlaneWorld1+normalOnSurfaceB1*distance;
  617. pOnB1.w = distance;
  618. int dstIdx;
  619. AppendInc( nGlobalContactsOut, dstIdx );
  620. if (dstIdx < maxContactCapacity)
  621. {
  622. __global struct b3Contact4Data* c = &globalContactsOut[dstIdx];
  623. c->m_worldNormalOnB = -normalOnSurfaceB1;
  624. c->m_restituitionCoeffCmp = (0.f*0xffff);c->m_frictionCoeffCmp = (0.7f*0xffff);
  625. c->m_batchIdx = pairIndex;
  626. c->m_bodyAPtrAndSignBit = rigidBodies[bodyIndexA].m_invMass==0?-bodyIndexA:bodyIndexA;
  627. c->m_bodyBPtrAndSignBit = rigidBodies[bodyIndexB].m_invMass==0?-bodyIndexB:bodyIndexB;
  628. c->m_worldPosB[0] = pOnB1;
  629. c->m_childIndexA = -1;
  630. c->m_childIndexB = -1;
  631. GET_NPOINTS(*c) = 1;
  632. }//if (dstIdx < numPairs)
  633. }//if (hasCollision)
  634. }
  635. __kernel void primitiveContactsKernel( __global int4* pairs,
  636. __global const BodyData* rigidBodies,
  637. __global const btCollidableGpu* collidables,
  638. __global const ConvexPolyhedronCL* convexShapes,
  639. __global const float4* vertices,
  640. __global const float4* uniqueEdges,
  641. __global const btGpuFace* faces,
  642. __global const int* indices,
  643. __global struct b3Contact4Data* restrict globalContactsOut,
  644. counter32_t nGlobalContactsOut,
  645. int numPairs, int maxContactCapacity)
  646. {
  647. int i = get_global_id(0);
  648. int pairIndex = i;
  649. float4 worldVertsB1[64];
  650. float4 worldVertsB2[64];
  651. int capacityWorldVerts = 64;
  652. float4 localContactsOut[64];
  653. int localContactCapacity=64;
  654. float minDist = -1e30f;
  655. float maxDist = 0.02f;
  656. if (i<numPairs)
  657. {
  658. int bodyIndexA = pairs[i].x;
  659. int bodyIndexB = pairs[i].y;
  660. int collidableIndexA = rigidBodies[bodyIndexA].m_collidableIdx;
  661. int collidableIndexB = rigidBodies[bodyIndexB].m_collidableIdx;
  662. if (collidables[collidableIndexA].m_shapeType == SHAPE_PLANE &&
  663. collidables[collidableIndexB].m_shapeType == SHAPE_CONVEX_HULL)
  664. {
  665. float4 posB;
  666. posB = rigidBodies[bodyIndexB].m_pos;
  667. Quaternion ornB;
  668. ornB = rigidBodies[bodyIndexB].m_quat;
  669. int contactIndex = computeContactPlaneConvex(pairIndex, bodyIndexA, bodyIndexB, collidableIndexA, collidableIndexB,
  670. rigidBodies,collidables,convexShapes,vertices,indices,
  671. faces, globalContactsOut, nGlobalContactsOut,maxContactCapacity, posB,ornB);
  672. if (contactIndex>=0)
  673. pairs[pairIndex].z = contactIndex;
  674. return;
  675. }
  676. if (collidables[collidableIndexA].m_shapeType == SHAPE_CONVEX_HULL &&
  677. collidables[collidableIndexB].m_shapeType == SHAPE_PLANE)
  678. {
  679. float4 posA;
  680. posA = rigidBodies[bodyIndexA].m_pos;
  681. Quaternion ornA;
  682. ornA = rigidBodies[bodyIndexA].m_quat;
  683. int contactIndex = computeContactPlaneConvex( pairIndex, bodyIndexB,bodyIndexA, collidableIndexB,collidableIndexA,
  684. rigidBodies,collidables,convexShapes,vertices,indices,
  685. faces, globalContactsOut, nGlobalContactsOut,maxContactCapacity,posA,ornA);
  686. if (contactIndex>=0)
  687. pairs[pairIndex].z = contactIndex;
  688. return;
  689. }
  690. if (collidables[collidableIndexA].m_shapeType == SHAPE_PLANE &&
  691. collidables[collidableIndexB].m_shapeType == SHAPE_SPHERE)
  692. {
  693. computeContactPlaneSphere(pairIndex, bodyIndexA, bodyIndexB, collidableIndexA, collidableIndexB,
  694. rigidBodies,collidables,faces, globalContactsOut, nGlobalContactsOut,maxContactCapacity);
  695. return;
  696. }
  697. if (collidables[collidableIndexA].m_shapeType == SHAPE_SPHERE &&
  698. collidables[collidableIndexB].m_shapeType == SHAPE_PLANE)
  699. {
  700. computeContactPlaneSphere( pairIndex, bodyIndexB,bodyIndexA, collidableIndexB,collidableIndexA,
  701. rigidBodies,collidables,
  702. faces, globalContactsOut, nGlobalContactsOut,maxContactCapacity);
  703. return;
  704. }
  705. if (collidables[collidableIndexA].m_shapeType == SHAPE_SPHERE &&
  706. collidables[collidableIndexB].m_shapeType == SHAPE_CONVEX_HULL)
  707. {
  708. float4 spherePos = rigidBodies[bodyIndexA].m_pos;
  709. float sphereRadius = collidables[collidableIndexA].m_radius;
  710. float4 convexPos = rigidBodies[bodyIndexB].m_pos;
  711. float4 convexOrn = rigidBodies[bodyIndexB].m_quat;
  712. computeContactSphereConvex(pairIndex, bodyIndexA, bodyIndexB, collidableIndexA, collidableIndexB,
  713. rigidBodies,collidables,convexShapes,vertices,indices,faces, globalContactsOut, nGlobalContactsOut,maxContactCapacity,
  714. spherePos,sphereRadius,convexPos,convexOrn);
  715. return;
  716. }
  717. if (collidables[collidableIndexA].m_shapeType == SHAPE_CONVEX_HULL &&
  718. collidables[collidableIndexB].m_shapeType == SHAPE_SPHERE)
  719. {
  720. float4 spherePos = rigidBodies[bodyIndexB].m_pos;
  721. float sphereRadius = collidables[collidableIndexB].m_radius;
  722. float4 convexPos = rigidBodies[bodyIndexA].m_pos;
  723. float4 convexOrn = rigidBodies[bodyIndexA].m_quat;
  724. computeContactSphereConvex(pairIndex, bodyIndexB, bodyIndexA, collidableIndexB, collidableIndexA,
  725. rigidBodies,collidables,convexShapes,vertices,indices,faces, globalContactsOut, nGlobalContactsOut,maxContactCapacity,
  726. spherePos,sphereRadius,convexPos,convexOrn);
  727. return;
  728. }
  729. if (collidables[collidableIndexA].m_shapeType == SHAPE_SPHERE &&
  730. collidables[collidableIndexB].m_shapeType == SHAPE_SPHERE)
  731. {
  732. //sphere-sphere
  733. float radiusA = collidables[collidableIndexA].m_radius;
  734. float radiusB = collidables[collidableIndexB].m_radius;
  735. float4 posA = rigidBodies[bodyIndexA].m_pos;
  736. float4 posB = rigidBodies[bodyIndexB].m_pos;
  737. float4 diff = posA-posB;
  738. float len = length(diff);
  739. ///iff distance positive, don't generate a new contact
  740. if ( len <= (radiusA+radiusB))
  741. {
  742. ///distance (negative means penetration)
  743. float dist = len - (radiusA+radiusB);
  744. float4 normalOnSurfaceB = make_float4(1.f,0.f,0.f,0.f);
  745. if (len > 0.00001)
  746. {
  747. normalOnSurfaceB = diff / len;
  748. }
  749. float4 contactPosB = posB + normalOnSurfaceB*radiusB;
  750. contactPosB.w = dist;
  751. int dstIdx;
  752. AppendInc( nGlobalContactsOut, dstIdx );
  753. if (dstIdx < maxContactCapacity)
  754. {
  755. __global struct b3Contact4Data* c = &globalContactsOut[dstIdx];
  756. c->m_worldNormalOnB = normalOnSurfaceB;
  757. c->m_restituitionCoeffCmp = (0.f*0xffff);c->m_frictionCoeffCmp = (0.7f*0xffff);
  758. c->m_batchIdx = pairIndex;
  759. int bodyA = pairs[pairIndex].x;
  760. int bodyB = pairs[pairIndex].y;
  761. c->m_bodyAPtrAndSignBit = rigidBodies[bodyA].m_invMass==0?-bodyA:bodyA;
  762. c->m_bodyBPtrAndSignBit = rigidBodies[bodyB].m_invMass==0?-bodyB:bodyB;
  763. c->m_worldPosB[0] = contactPosB;
  764. c->m_childIndexA = -1;
  765. c->m_childIndexB = -1;
  766. GET_NPOINTS(*c) = 1;
  767. }//if (dstIdx < numPairs)
  768. }//if ( len <= (radiusA+radiusB))
  769. return;
  770. }//SHAPE_SPHERE SHAPE_SPHERE
  771. }// if (i<numPairs)
  772. }
  773. // work-in-progress
  774. __kernel void processCompoundPairsPrimitivesKernel( __global const int4* gpuCompoundPairs,
  775. __global const BodyData* rigidBodies,
  776. __global const btCollidableGpu* collidables,
  777. __global const ConvexPolyhedronCL* convexShapes,
  778. __global const float4* vertices,
  779. __global const float4* uniqueEdges,
  780. __global const btGpuFace* faces,
  781. __global const int* indices,
  782. __global btAabbCL* aabbs,
  783. __global const btGpuChildShape* gpuChildShapes,
  784. __global struct b3Contact4Data* restrict globalContactsOut,
  785. counter32_t nGlobalContactsOut,
  786. int numCompoundPairs, int maxContactCapacity
  787. )
  788. {
  789. int i = get_global_id(0);
  790. if (i<numCompoundPairs)
  791. {
  792. int bodyIndexA = gpuCompoundPairs[i].x;
  793. int bodyIndexB = gpuCompoundPairs[i].y;
  794. int childShapeIndexA = gpuCompoundPairs[i].z;
  795. int childShapeIndexB = gpuCompoundPairs[i].w;
  796. int collidableIndexA = -1;
  797. int collidableIndexB = -1;
  798. float4 ornA = rigidBodies[bodyIndexA].m_quat;
  799. float4 posA = rigidBodies[bodyIndexA].m_pos;
  800. float4 ornB = rigidBodies[bodyIndexB].m_quat;
  801. float4 posB = rigidBodies[bodyIndexB].m_pos;
  802. if (childShapeIndexA >= 0)
  803. {
  804. collidableIndexA = gpuChildShapes[childShapeIndexA].m_shapeIndex;
  805. float4 childPosA = gpuChildShapes[childShapeIndexA].m_childPosition;
  806. float4 childOrnA = gpuChildShapes[childShapeIndexA].m_childOrientation;
  807. float4 newPosA = qtRotate(ornA,childPosA)+posA;
  808. float4 newOrnA = qtMul(ornA,childOrnA);
  809. posA = newPosA;
  810. ornA = newOrnA;
  811. } else
  812. {
  813. collidableIndexA = rigidBodies[bodyIndexA].m_collidableIdx;
  814. }
  815. if (childShapeIndexB>=0)
  816. {
  817. collidableIndexB = gpuChildShapes[childShapeIndexB].m_shapeIndex;
  818. float4 childPosB = gpuChildShapes[childShapeIndexB].m_childPosition;
  819. float4 childOrnB = gpuChildShapes[childShapeIndexB].m_childOrientation;
  820. float4 newPosB = transform(&childPosB,&posB,&ornB);
  821. float4 newOrnB = qtMul(ornB,childOrnB);
  822. posB = newPosB;
  823. ornB = newOrnB;
  824. } else
  825. {
  826. collidableIndexB = rigidBodies[bodyIndexB].m_collidableIdx;
  827. }
  828. int shapeIndexA = collidables[collidableIndexA].m_shapeIndex;
  829. int shapeIndexB = collidables[collidableIndexB].m_shapeIndex;
  830. int shapeTypeA = collidables[collidableIndexA].m_shapeType;
  831. int shapeTypeB = collidables[collidableIndexB].m_shapeType;
  832. int pairIndex = i;
  833. if ((shapeTypeA == SHAPE_PLANE) && (shapeTypeB==SHAPE_CONVEX_HULL))
  834. {
  835. computeContactPlaneConvex( pairIndex, bodyIndexA,bodyIndexB, collidableIndexA,collidableIndexB,
  836. rigidBodies,collidables,convexShapes,vertices,indices,
  837. faces, globalContactsOut, nGlobalContactsOut,maxContactCapacity,posB,ornB);
  838. return;
  839. }
  840. if ((shapeTypeA == SHAPE_CONVEX_HULL) && (shapeTypeB==SHAPE_PLANE))
  841. {
  842. computeContactPlaneConvex( pairIndex, bodyIndexB,bodyIndexA, collidableIndexB,collidableIndexA,
  843. rigidBodies,collidables,convexShapes,vertices,indices,
  844. faces, globalContactsOut, nGlobalContactsOut,maxContactCapacity,posA,ornA);
  845. return;
  846. }
  847. if ((shapeTypeA == SHAPE_CONVEX_HULL) && (shapeTypeB == SHAPE_SPHERE))
  848. {
  849. float4 spherePos = rigidBodies[bodyIndexB].m_pos;
  850. float sphereRadius = collidables[collidableIndexB].m_radius;
  851. float4 convexPos = posA;
  852. float4 convexOrn = ornA;
  853. computeContactSphereConvex(pairIndex, bodyIndexB, bodyIndexA , collidableIndexB,collidableIndexA,
  854. rigidBodies,collidables,convexShapes,vertices,indices,faces, globalContactsOut, nGlobalContactsOut,maxContactCapacity,
  855. spherePos,sphereRadius,convexPos,convexOrn);
  856. return;
  857. }
  858. if ((shapeTypeA == SHAPE_SPHERE) && (shapeTypeB == SHAPE_CONVEX_HULL))
  859. {
  860. float4 spherePos = rigidBodies[bodyIndexA].m_pos;
  861. float sphereRadius = collidables[collidableIndexA].m_radius;
  862. float4 convexPos = posB;
  863. float4 convexOrn = ornB;
  864. computeContactSphereConvex(pairIndex, bodyIndexA, bodyIndexB, collidableIndexA, collidableIndexB,
  865. rigidBodies,collidables,convexShapes,vertices,indices,faces, globalContactsOut, nGlobalContactsOut,maxContactCapacity,
  866. spherePos,sphereRadius,convexPos,convexOrn);
  867. return;
  868. }
  869. }// if (i<numCompoundPairs)
  870. }
  871. bool pointInTriangle(const float4* vertices, const float4* normal, float4 *p )
  872. {
  873. const float4* p1 = &vertices[0];
  874. const float4* p2 = &vertices[1];
  875. const float4* p3 = &vertices[2];
  876. float4 edge1; edge1 = (*p2 - *p1);
  877. float4 edge2; edge2 = ( *p3 - *p2 );
  878. float4 edge3; edge3 = ( *p1 - *p3 );
  879. float4 p1_to_p; p1_to_p = ( *p - *p1 );
  880. float4 p2_to_p; p2_to_p = ( *p - *p2 );
  881. float4 p3_to_p; p3_to_p = ( *p - *p3 );
  882. float4 edge1_normal; edge1_normal = ( cross(edge1,*normal));
  883. float4 edge2_normal; edge2_normal = ( cross(edge2,*normal));
  884. float4 edge3_normal; edge3_normal = ( cross(edge3,*normal));
  885. float r1, r2, r3;
  886. r1 = dot(edge1_normal,p1_to_p );
  887. r2 = dot(edge2_normal,p2_to_p );
  888. r3 = dot(edge3_normal,p3_to_p );
  889. if ( r1 > 0 && r2 > 0 && r3 > 0 )
  890. return true;
  891. if ( r1 <= 0 && r2 <= 0 && r3 <= 0 )
  892. return true;
  893. return false;
  894. }
  895. float segmentSqrDistance(float4 from, float4 to,float4 p, float4* nearest)
  896. {
  897. float4 diff = p - from;
  898. float4 v = to - from;
  899. float t = dot(v,diff);
  900. if (t > 0)
  901. {
  902. float dotVV = dot(v,v);
  903. if (t < dotVV)
  904. {
  905. t /= dotVV;
  906. diff -= t*v;
  907. } else
  908. {
  909. t = 1;
  910. diff -= v;
  911. }
  912. } else
  913. {
  914. t = 0;
  915. }
  916. *nearest = from + t*v;
  917. return dot(diff,diff);
  918. }
  919. void computeContactSphereTriangle(int pairIndex,
  920. int bodyIndexA, int bodyIndexB,
  921. int collidableIndexA, int collidableIndexB,
  922. __global const BodyData* rigidBodies,
  923. __global const btCollidableGpu* collidables,
  924. const float4* triangleVertices,
  925. __global struct b3Contact4Data* restrict globalContactsOut,
  926. counter32_t nGlobalContactsOut,
  927. int maxContactCapacity,
  928. float4 spherePos2,
  929. float radius,
  930. float4 pos,
  931. float4 quat,
  932. int faceIndex
  933. )
  934. {
  935. float4 invPos;
  936. float4 invOrn;
  937. trInverse(pos,quat, &invPos,&invOrn);
  938. float4 spherePos = transform(&spherePos2,&invPos,&invOrn);
  939. int numFaces = 3;
  940. float4 closestPnt = (float4)(0, 0, 0, 0);
  941. float4 hitNormalWorld = (float4)(0, 0, 0, 0);
  942. float minDist = -1000000.f;
  943. bool bCollide = false;
  944. //////////////////////////////////////
  945. float4 sphereCenter;
  946. sphereCenter = spherePos;
  947. const float4* vertices = triangleVertices;
  948. float contactBreakingThreshold = 0.f;//todo?
  949. float radiusWithThreshold = radius + contactBreakingThreshold;
  950. float4 edge10;
  951. edge10 = vertices[1]-vertices[0];
  952. edge10.w = 0.f;//is this needed?
  953. float4 edge20;
  954. edge20 = vertices[2]-vertices[0];
  955. edge20.w = 0.f;//is this needed?
  956. float4 normal = cross3(edge10,edge20);
  957. normal = normalize(normal);
  958. float4 p1ToCenter;
  959. p1ToCenter = sphereCenter - vertices[0];
  960. float distanceFromPlane = dot(p1ToCenter,normal);
  961. if (distanceFromPlane < 0.f)
  962. {
  963. //triangle facing the other way
  964. distanceFromPlane *= -1.f;
  965. normal *= -1.f;
  966. }
  967. hitNormalWorld = normal;
  968. bool isInsideContactPlane = distanceFromPlane < radiusWithThreshold;
  969. // Check for contact / intersection
  970. bool hasContact = false;
  971. float4 contactPoint;
  972. if (isInsideContactPlane)
  973. {
  974. if (pointInTriangle(vertices,&normal, &sphereCenter))
  975. {
  976. // Inside the contact wedge - touches a point on the shell plane
  977. hasContact = true;
  978. contactPoint = sphereCenter - normal*distanceFromPlane;
  979. } else {
  980. // Could be inside one of the contact capsules
  981. float contactCapsuleRadiusSqr = radiusWithThreshold*radiusWithThreshold;
  982. float4 nearestOnEdge;
  983. int numEdges = 3;
  984. for (int i = 0; i < numEdges; i++)
  985. {
  986. float4 pa =vertices[i];
  987. float4 pb = vertices[(i+1)%3];
  988. float distanceSqr = segmentSqrDistance(pa,pb,sphereCenter, &nearestOnEdge);
  989. if (distanceSqr < contactCapsuleRadiusSqr)
  990. {
  991. // Yep, we're inside a capsule
  992. hasContact = true;
  993. contactPoint = nearestOnEdge;
  994. }
  995. }
  996. }
  997. }
  998. if (hasContact)
  999. {
  1000. closestPnt = contactPoint;
  1001. float4 contactToCenter = sphereCenter - contactPoint;
  1002. minDist = length(contactToCenter);
  1003. if (minDist>FLT_EPSILON)
  1004. {
  1005. hitNormalWorld = normalize(contactToCenter);//*(1./minDist);
  1006. bCollide = true;
  1007. }
  1008. }
  1009. /////////////////////////////////////
  1010. if (bCollide && minDist > -10000)
  1011. {
  1012. float4 normalOnSurfaceB1 = qtRotate(quat,-hitNormalWorld);
  1013. float4 pOnB1 = transform(&closestPnt,&pos,&quat);
  1014. float actualDepth = minDist-radius;
  1015. if (actualDepth<=0.f)
  1016. {
  1017. pOnB1.w = actualDepth;
  1018. int dstIdx;
  1019. float lenSqr = dot3F4(normalOnSurfaceB1,normalOnSurfaceB1);
  1020. if (lenSqr>FLT_EPSILON)
  1021. {
  1022. AppendInc( nGlobalContactsOut, dstIdx );
  1023. if (dstIdx < maxContactCapacity)
  1024. {
  1025. __global struct b3Contact4Data* c = &globalContactsOut[dstIdx];
  1026. c->m_worldNormalOnB = -normalOnSurfaceB1;
  1027. c->m_restituitionCoeffCmp = (0.f*0xffff);c->m_frictionCoeffCmp = (0.7f*0xffff);
  1028. c->m_batchIdx = pairIndex;
  1029. c->m_bodyAPtrAndSignBit = rigidBodies[bodyIndexA].m_invMass==0?-bodyIndexA:bodyIndexA;
  1030. c->m_bodyBPtrAndSignBit = rigidBodies[bodyIndexB].m_invMass==0?-bodyIndexB:bodyIndexB;
  1031. c->m_worldPosB[0] = pOnB1;
  1032. c->m_childIndexA = -1;
  1033. c->m_childIndexB = faceIndex;
  1034. GET_NPOINTS(*c) = 1;
  1035. }
  1036. }
  1037. }
  1038. }//if (hasCollision)
  1039. }
  1040. // work-in-progress
  1041. __kernel void findConcaveSphereContactsKernel( __global int4* concavePairs,
  1042. __global const BodyData* rigidBodies,
  1043. __global const btCollidableGpu* collidables,
  1044. __global const ConvexPolyhedronCL* convexShapes,
  1045. __global const float4* vertices,
  1046. __global const float4* uniqueEdges,
  1047. __global const btGpuFace* faces,
  1048. __global const int* indices,
  1049. __global btAabbCL* aabbs,
  1050. __global struct b3Contact4Data* restrict globalContactsOut,
  1051. counter32_t nGlobalContactsOut,
  1052. int numConcavePairs, int maxContactCapacity
  1053. )
  1054. {
  1055. int i = get_global_id(0);
  1056. if (i>=numConcavePairs)
  1057. return;
  1058. int pairIdx = i;
  1059. int bodyIndexA = concavePairs[i].x;
  1060. int bodyIndexB = concavePairs[i].y;
  1061. int collidableIndexA = rigidBodies[bodyIndexA].m_collidableIdx;
  1062. int collidableIndexB = rigidBodies[bodyIndexB].m_collidableIdx;
  1063. int shapeIndexA = collidables[collidableIndexA].m_shapeIndex;
  1064. int shapeIndexB = collidables[collidableIndexB].m_shapeIndex;
  1065. if (collidables[collidableIndexB].m_shapeType==SHAPE_SPHERE)
  1066. {
  1067. int f = concavePairs[i].z;
  1068. btGpuFace face = faces[convexShapes[shapeIndexA].m_faceOffset+f];
  1069. float4 verticesA[3];
  1070. for (int i=0;i<3;i++)
  1071. {
  1072. int index = indices[face.m_indexOffset+i];
  1073. float4 vert = vertices[convexShapes[shapeIndexA].m_vertexOffset+index];
  1074. verticesA[i] = vert;
  1075. }
  1076. float4 spherePos = rigidBodies[bodyIndexB].m_pos;
  1077. float sphereRadius = collidables[collidableIndexB].m_radius;
  1078. float4 convexPos = rigidBodies[bodyIndexA].m_pos;
  1079. float4 convexOrn = rigidBodies[bodyIndexA].m_quat;
  1080. computeContactSphereTriangle(i, bodyIndexB, bodyIndexA, collidableIndexB, collidableIndexA,
  1081. rigidBodies,collidables,
  1082. verticesA,
  1083. globalContactsOut, nGlobalContactsOut,maxContactCapacity,
  1084. spherePos,sphereRadius,convexPos,convexOrn, f);
  1085. return;
  1086. }
  1087. }