b3MprPenetration.h 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919
  1. /***
  2. * ---------------------------------
  3. * Copyright (c)2012 Daniel Fiser <[email protected]>
  4. *
  5. * This file was ported from mpr.c file, part of libccd.
  6. * The Minkoski Portal Refinement implementation was ported
  7. * to OpenCL by Erwin Coumans for the Bullet 3 Physics library.
  8. * at http://github.com/erwincoumans/bullet3
  9. *
  10. * Distributed under the OSI-approved BSD License (the "License");
  11. * see <http://www.opensource.org/licenses/bsd-license.php>.
  12. * This software is distributed WITHOUT ANY WARRANTY; without even the
  13. * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. * See the License for more information.
  15. */
  16. #ifndef B3_MPR_PENETRATION_H
  17. #define B3_MPR_PENETRATION_H
  18. #include "Bullet3Common/shared/b3Float4.h"
  19. #include "Bullet3Collision/NarrowPhaseCollision/shared/b3RigidBodyData.h"
  20. #include "Bullet3Collision/NarrowPhaseCollision/shared/b3ConvexPolyhedronData.h"
  21. #include "Bullet3Collision/NarrowPhaseCollision/shared/b3Collidable.h"
  22. #ifdef __cplusplus
  23. #define B3_MPR_SQRT sqrtf
  24. #else
  25. #define B3_MPR_SQRT sqrt
  26. #endif
  27. #define B3_MPR_FMIN(x, y) ((x) < (y) ? (x) : (y))
  28. #define B3_MPR_FABS fabs
  29. #define B3_MPR_TOLERANCE 1E-6f
  30. #define B3_MPR_MAX_ITERATIONS 1000
  31. struct _b3MprSupport_t
  32. {
  33. b3Float4 v; //!< Support point in minkowski sum
  34. b3Float4 v1; //!< Support point in obj1
  35. b3Float4 v2; //!< Support point in obj2
  36. };
  37. typedef struct _b3MprSupport_t b3MprSupport_t;
  38. struct _b3MprSimplex_t
  39. {
  40. b3MprSupport_t ps[4];
  41. int last; //!< index of last added point
  42. };
  43. typedef struct _b3MprSimplex_t b3MprSimplex_t;
  44. inline b3MprSupport_t* b3MprSimplexPointW(b3MprSimplex_t *s, int idx)
  45. {
  46. return &s->ps[idx];
  47. }
  48. inline void b3MprSimplexSetSize(b3MprSimplex_t *s, int size)
  49. {
  50. s->last = size - 1;
  51. }
  52. inline int b3MprSimplexSize(const b3MprSimplex_t *s)
  53. {
  54. return s->last + 1;
  55. }
  56. inline const b3MprSupport_t* b3MprSimplexPoint(const b3MprSimplex_t* s, int idx)
  57. {
  58. // here is no check on boundaries
  59. return &s->ps[idx];
  60. }
  61. inline void b3MprSupportCopy(b3MprSupport_t *d, const b3MprSupport_t *s)
  62. {
  63. *d = *s;
  64. }
  65. inline void b3MprSimplexSet(b3MprSimplex_t *s, size_t pos, const b3MprSupport_t *a)
  66. {
  67. b3MprSupportCopy(s->ps + pos, a);
  68. }
  69. inline void b3MprSimplexSwap(b3MprSimplex_t *s, size_t pos1, size_t pos2)
  70. {
  71. b3MprSupport_t supp;
  72. b3MprSupportCopy(&supp, &s->ps[pos1]);
  73. b3MprSupportCopy(&s->ps[pos1], &s->ps[pos2]);
  74. b3MprSupportCopy(&s->ps[pos2], &supp);
  75. }
  76. inline int b3MprIsZero(float val)
  77. {
  78. return B3_MPR_FABS(val) < FLT_EPSILON;
  79. }
  80. inline int b3MprEq(float _a, float _b)
  81. {
  82. float ab;
  83. float a, b;
  84. ab = B3_MPR_FABS(_a - _b);
  85. if (B3_MPR_FABS(ab) < FLT_EPSILON)
  86. return 1;
  87. a = B3_MPR_FABS(_a);
  88. b = B3_MPR_FABS(_b);
  89. if (b > a){
  90. return ab < FLT_EPSILON * b;
  91. }else{
  92. return ab < FLT_EPSILON * a;
  93. }
  94. }
  95. inline int b3MprVec3Eq(const b3Float4* a, const b3Float4 *b)
  96. {
  97. return b3MprEq((*a).x, (*b).x)
  98. && b3MprEq((*a).y, (*b).y)
  99. && b3MprEq((*a).z, (*b).z);
  100. }
  101. inline b3Float4 b3LocalGetSupportVertex(b3Float4ConstArg supportVec,__global const b3ConvexPolyhedronData_t* hull, b3ConstArray(b3Float4) verticesA)
  102. {
  103. b3Float4 supVec = b3MakeFloat4(0,0,0,0);
  104. float maxDot = -B3_LARGE_FLOAT;
  105. if( 0 < hull->m_numVertices )
  106. {
  107. const b3Float4 scaled = supportVec;
  108. int index = b3MaxDot(scaled, &verticesA[hull->m_vertexOffset], hull->m_numVertices, &maxDot);
  109. return verticesA[hull->m_vertexOffset+index];
  110. }
  111. return supVec;
  112. }
  113. static void b3MprConvexSupport(int pairIndex,int bodyIndex, b3ConstArray(b3RigidBodyData_t) cpuBodyBuf,
  114. b3ConstArray(b3ConvexPolyhedronData_t) cpuConvexData,
  115. b3ConstArray(b3Collidable_t) cpuCollidables,
  116. b3ConstArray(b3Float4) cpuVertices,
  117. __global b3Float4* sepAxis,
  118. const b3Float4* _dir, b3Float4* outp, int logme)
  119. {
  120. //dir is in worldspace, move to local space
  121. b3Float4 pos = cpuBodyBuf[bodyIndex].m_pos;
  122. b3Quat orn = cpuBodyBuf[bodyIndex].m_quat;
  123. b3Float4 dir = b3MakeFloat4((*_dir).x,(*_dir).y,(*_dir).z,0.f);
  124. const b3Float4 localDir = b3QuatRotate(b3QuatInverse(orn),dir);
  125. //find local support vertex
  126. int colIndex = cpuBodyBuf[bodyIndex].m_collidableIdx;
  127. b3Assert(cpuCollidables[colIndex].m_shapeType==SHAPE_CONVEX_HULL);
  128. __global const b3ConvexPolyhedronData_t* hull = &cpuConvexData[cpuCollidables[colIndex].m_shapeIndex];
  129. b3Float4 pInA;
  130. if (logme)
  131. {
  132. b3Float4 supVec = b3MakeFloat4(0,0,0,0);
  133. float maxDot = -B3_LARGE_FLOAT;
  134. if( 0 < hull->m_numVertices )
  135. {
  136. const b3Float4 scaled = localDir;
  137. int index = b3MaxDot(scaled, &cpuVertices[hull->m_vertexOffset], hull->m_numVertices, &maxDot);
  138. pInA = cpuVertices[hull->m_vertexOffset+index];
  139. }
  140. } else
  141. {
  142. pInA = b3LocalGetSupportVertex(localDir,hull,cpuVertices);
  143. }
  144. //move vertex to world space
  145. *outp = b3TransformPoint(pInA,pos,orn);
  146. }
  147. inline void b3MprSupport(int pairIndex,int bodyIndexA, int bodyIndexB, b3ConstArray(b3RigidBodyData_t) cpuBodyBuf,
  148. b3ConstArray(b3ConvexPolyhedronData_t) cpuConvexData,
  149. b3ConstArray(b3Collidable_t) cpuCollidables,
  150. b3ConstArray(b3Float4) cpuVertices,
  151. __global b3Float4* sepAxis,
  152. const b3Float4* _dir, b3MprSupport_t *supp)
  153. {
  154. b3Float4 dir;
  155. dir = *_dir;
  156. b3MprConvexSupport(pairIndex,bodyIndexA,cpuBodyBuf,cpuConvexData,cpuCollidables,cpuVertices,sepAxis,&dir, &supp->v1,0);
  157. dir = *_dir*-1.f;
  158. b3MprConvexSupport(pairIndex,bodyIndexB,cpuBodyBuf,cpuConvexData,cpuCollidables,cpuVertices,sepAxis,&dir, &supp->v2,0);
  159. supp->v = supp->v1 - supp->v2;
  160. }
  161. inline void b3FindOrigin(int bodyIndexA, int bodyIndexB, b3ConstArray(b3RigidBodyData_t) cpuBodyBuf, b3MprSupport_t *center)
  162. {
  163. center->v1 = cpuBodyBuf[bodyIndexA].m_pos;
  164. center->v2 = cpuBodyBuf[bodyIndexB].m_pos;
  165. center->v = center->v1 - center->v2;
  166. }
  167. inline void b3MprVec3Set(b3Float4 *v, float x, float y, float z)
  168. {
  169. (*v).x = x;
  170. (*v).y = y;
  171. (*v).z = z;
  172. (*v).w = 0.f;
  173. }
  174. inline void b3MprVec3Add(b3Float4 *v, const b3Float4 *w)
  175. {
  176. (*v).x += (*w).x;
  177. (*v).y += (*w).y;
  178. (*v).z += (*w).z;
  179. }
  180. inline void b3MprVec3Copy(b3Float4 *v, const b3Float4 *w)
  181. {
  182. *v = *w;
  183. }
  184. inline void b3MprVec3Scale(b3Float4 *d, float k)
  185. {
  186. *d *= k;
  187. }
  188. inline float b3MprVec3Dot(const b3Float4 *a, const b3Float4 *b)
  189. {
  190. float dot;
  191. dot = b3Dot3F4(*a,*b);
  192. return dot;
  193. }
  194. inline float b3MprVec3Len2(const b3Float4 *v)
  195. {
  196. return b3MprVec3Dot(v, v);
  197. }
  198. inline void b3MprVec3Normalize(b3Float4 *d)
  199. {
  200. float k = 1.f / B3_MPR_SQRT(b3MprVec3Len2(d));
  201. b3MprVec3Scale(d, k);
  202. }
  203. inline void b3MprVec3Cross(b3Float4 *d, const b3Float4 *a, const b3Float4 *b)
  204. {
  205. *d = b3Cross3(*a,*b);
  206. }
  207. inline void b3MprVec3Sub2(b3Float4 *d, const b3Float4 *v, const b3Float4 *w)
  208. {
  209. *d = *v - *w;
  210. }
  211. inline void b3PortalDir(const b3MprSimplex_t *portal, b3Float4 *dir)
  212. {
  213. b3Float4 v2v1, v3v1;
  214. b3MprVec3Sub2(&v2v1, &b3MprSimplexPoint(portal, 2)->v,
  215. &b3MprSimplexPoint(portal, 1)->v);
  216. b3MprVec3Sub2(&v3v1, &b3MprSimplexPoint(portal, 3)->v,
  217. &b3MprSimplexPoint(portal, 1)->v);
  218. b3MprVec3Cross(dir, &v2v1, &v3v1);
  219. b3MprVec3Normalize(dir);
  220. }
  221. inline int portalEncapsulesOrigin(const b3MprSimplex_t *portal,
  222. const b3Float4 *dir)
  223. {
  224. float dot;
  225. dot = b3MprVec3Dot(dir, &b3MprSimplexPoint(portal, 1)->v);
  226. return b3MprIsZero(dot) || dot > 0.f;
  227. }
  228. inline int portalReachTolerance(const b3MprSimplex_t *portal,
  229. const b3MprSupport_t *v4,
  230. const b3Float4 *dir)
  231. {
  232. float dv1, dv2, dv3, dv4;
  233. float dot1, dot2, dot3;
  234. // find the smallest dot product of dir and {v1-v4, v2-v4, v3-v4}
  235. dv1 = b3MprVec3Dot(&b3MprSimplexPoint(portal, 1)->v, dir);
  236. dv2 = b3MprVec3Dot(&b3MprSimplexPoint(portal, 2)->v, dir);
  237. dv3 = b3MprVec3Dot(&b3MprSimplexPoint(portal, 3)->v, dir);
  238. dv4 = b3MprVec3Dot(&v4->v, dir);
  239. dot1 = dv4 - dv1;
  240. dot2 = dv4 - dv2;
  241. dot3 = dv4 - dv3;
  242. dot1 = B3_MPR_FMIN(dot1, dot2);
  243. dot1 = B3_MPR_FMIN(dot1, dot3);
  244. return b3MprEq(dot1, B3_MPR_TOLERANCE) || dot1 < B3_MPR_TOLERANCE;
  245. }
  246. inline int portalCanEncapsuleOrigin(const b3MprSimplex_t *portal,
  247. const b3MprSupport_t *v4,
  248. const b3Float4 *dir)
  249. {
  250. float dot;
  251. dot = b3MprVec3Dot(&v4->v, dir);
  252. return b3MprIsZero(dot) || dot > 0.f;
  253. }
  254. inline void b3ExpandPortal(b3MprSimplex_t *portal,
  255. const b3MprSupport_t *v4)
  256. {
  257. float dot;
  258. b3Float4 v4v0;
  259. b3MprVec3Cross(&v4v0, &v4->v, &b3MprSimplexPoint(portal, 0)->v);
  260. dot = b3MprVec3Dot(&b3MprSimplexPoint(portal, 1)->v, &v4v0);
  261. if (dot > 0.f){
  262. dot = b3MprVec3Dot(&b3MprSimplexPoint(portal, 2)->v, &v4v0);
  263. if (dot > 0.f){
  264. b3MprSimplexSet(portal, 1, v4);
  265. }else{
  266. b3MprSimplexSet(portal, 3, v4);
  267. }
  268. }else{
  269. dot = b3MprVec3Dot(&b3MprSimplexPoint(portal, 3)->v, &v4v0);
  270. if (dot > 0.f){
  271. b3MprSimplexSet(portal, 2, v4);
  272. }else{
  273. b3MprSimplexSet(portal, 1, v4);
  274. }
  275. }
  276. }
  277. static int b3DiscoverPortal(int pairIndex, int bodyIndexA, int bodyIndexB, b3ConstArray(b3RigidBodyData_t) cpuBodyBuf,
  278. b3ConstArray(b3ConvexPolyhedronData_t) cpuConvexData,
  279. b3ConstArray(b3Collidable_t) cpuCollidables,
  280. b3ConstArray(b3Float4) cpuVertices,
  281. __global b3Float4* sepAxis,
  282. __global int* hasSepAxis,
  283. b3MprSimplex_t *portal)
  284. {
  285. b3Float4 dir, va, vb;
  286. float dot;
  287. int cont;
  288. // vertex 0 is center of portal
  289. b3FindOrigin(bodyIndexA,bodyIndexB,cpuBodyBuf, b3MprSimplexPointW(portal, 0));
  290. // vertex 0 is center of portal
  291. b3MprSimplexSetSize(portal, 1);
  292. b3Float4 zero = b3MakeFloat4(0,0,0,0);
  293. b3Float4* b3mpr_vec3_origin = &zero;
  294. if (b3MprVec3Eq(&b3MprSimplexPoint(portal, 0)->v, b3mpr_vec3_origin)){
  295. // Portal's center lies on origin (0,0,0) => we know that objects
  296. // intersect but we would need to know penetration info.
  297. // So move center little bit...
  298. b3MprVec3Set(&va, FLT_EPSILON * 10.f, 0.f, 0.f);
  299. b3MprVec3Add(&b3MprSimplexPointW(portal, 0)->v, &va);
  300. }
  301. // vertex 1 = support in direction of origin
  302. b3MprVec3Copy(&dir, &b3MprSimplexPoint(portal, 0)->v);
  303. b3MprVec3Scale(&dir, -1.f);
  304. b3MprVec3Normalize(&dir);
  305. b3MprSupport(pairIndex,bodyIndexA,bodyIndexB,cpuBodyBuf,cpuConvexData,cpuCollidables,cpuVertices, sepAxis,&dir, b3MprSimplexPointW(portal, 1));
  306. b3MprSimplexSetSize(portal, 2);
  307. // test if origin isn't outside of v1
  308. dot = b3MprVec3Dot(&b3MprSimplexPoint(portal, 1)->v, &dir);
  309. if (b3MprIsZero(dot) || dot < 0.f)
  310. return -1;
  311. // vertex 2
  312. b3MprVec3Cross(&dir, &b3MprSimplexPoint(portal, 0)->v,
  313. &b3MprSimplexPoint(portal, 1)->v);
  314. if (b3MprIsZero(b3MprVec3Len2(&dir))){
  315. if (b3MprVec3Eq(&b3MprSimplexPoint(portal, 1)->v, b3mpr_vec3_origin)){
  316. // origin lies on v1
  317. return 1;
  318. }else{
  319. // origin lies on v0-v1 segment
  320. return 2;
  321. }
  322. }
  323. b3MprVec3Normalize(&dir);
  324. b3MprSupport(pairIndex,bodyIndexA,bodyIndexB,cpuBodyBuf,cpuConvexData,cpuCollidables,cpuVertices, sepAxis,&dir, b3MprSimplexPointW(portal, 2));
  325. dot = b3MprVec3Dot(&b3MprSimplexPoint(portal, 2)->v, &dir);
  326. if (b3MprIsZero(dot) || dot < 0.f)
  327. return -1;
  328. b3MprSimplexSetSize(portal, 3);
  329. // vertex 3 direction
  330. b3MprVec3Sub2(&va, &b3MprSimplexPoint(portal, 1)->v,
  331. &b3MprSimplexPoint(portal, 0)->v);
  332. b3MprVec3Sub2(&vb, &b3MprSimplexPoint(portal, 2)->v,
  333. &b3MprSimplexPoint(portal, 0)->v);
  334. b3MprVec3Cross(&dir, &va, &vb);
  335. b3MprVec3Normalize(&dir);
  336. // it is better to form portal faces to be oriented "outside" origin
  337. dot = b3MprVec3Dot(&dir, &b3MprSimplexPoint(portal, 0)->v);
  338. if (dot > 0.f){
  339. b3MprSimplexSwap(portal, 1, 2);
  340. b3MprVec3Scale(&dir, -1.f);
  341. }
  342. while (b3MprSimplexSize(portal) < 4){
  343. b3MprSupport(pairIndex,bodyIndexA,bodyIndexB,cpuBodyBuf,cpuConvexData,cpuCollidables,cpuVertices, sepAxis,&dir, b3MprSimplexPointW(portal, 3));
  344. dot = b3MprVec3Dot(&b3MprSimplexPoint(portal, 3)->v, &dir);
  345. if (b3MprIsZero(dot) || dot < 0.f)
  346. return -1;
  347. cont = 0;
  348. // test if origin is outside (v1, v0, v3) - set v2 as v3 and
  349. // continue
  350. b3MprVec3Cross(&va, &b3MprSimplexPoint(portal, 1)->v,
  351. &b3MprSimplexPoint(portal, 3)->v);
  352. dot = b3MprVec3Dot(&va, &b3MprSimplexPoint(portal, 0)->v);
  353. if (dot < 0.f && !b3MprIsZero(dot)){
  354. b3MprSimplexSet(portal, 2, b3MprSimplexPoint(portal, 3));
  355. cont = 1;
  356. }
  357. if (!cont){
  358. // test if origin is outside (v3, v0, v2) - set v1 as v3 and
  359. // continue
  360. b3MprVec3Cross(&va, &b3MprSimplexPoint(portal, 3)->v,
  361. &b3MprSimplexPoint(portal, 2)->v);
  362. dot = b3MprVec3Dot(&va, &b3MprSimplexPoint(portal, 0)->v);
  363. if (dot < 0.f && !b3MprIsZero(dot)){
  364. b3MprSimplexSet(portal, 1, b3MprSimplexPoint(portal, 3));
  365. cont = 1;
  366. }
  367. }
  368. if (cont){
  369. b3MprVec3Sub2(&va, &b3MprSimplexPoint(portal, 1)->v,
  370. &b3MprSimplexPoint(portal, 0)->v);
  371. b3MprVec3Sub2(&vb, &b3MprSimplexPoint(portal, 2)->v,
  372. &b3MprSimplexPoint(portal, 0)->v);
  373. b3MprVec3Cross(&dir, &va, &vb);
  374. b3MprVec3Normalize(&dir);
  375. }else{
  376. b3MprSimplexSetSize(portal, 4);
  377. }
  378. }
  379. return 0;
  380. }
  381. static int b3RefinePortal(int pairIndex,int bodyIndexA, int bodyIndexB, b3ConstArray(b3RigidBodyData_t) cpuBodyBuf,
  382. b3ConstArray(b3ConvexPolyhedronData_t) cpuConvexData,
  383. b3ConstArray(b3Collidable_t) cpuCollidables,
  384. b3ConstArray(b3Float4) cpuVertices,
  385. __global b3Float4* sepAxis,
  386. b3MprSimplex_t *portal)
  387. {
  388. b3Float4 dir;
  389. b3MprSupport_t v4;
  390. for (int i=0;i<B3_MPR_MAX_ITERATIONS;i++)
  391. //while (1)
  392. {
  393. // compute direction outside the portal (from v0 throught v1,v2,v3
  394. // face)
  395. b3PortalDir(portal, &dir);
  396. // test if origin is inside the portal
  397. if (portalEncapsulesOrigin(portal, &dir))
  398. return 0;
  399. // get next support point
  400. b3MprSupport(pairIndex,bodyIndexA,bodyIndexB,cpuBodyBuf,cpuConvexData,cpuCollidables,cpuVertices, sepAxis,&dir, &v4);
  401. // test if v4 can expand portal to contain origin and if portal
  402. // expanding doesn't reach given tolerance
  403. if (!portalCanEncapsuleOrigin(portal, &v4, &dir)
  404. || portalReachTolerance(portal, &v4, &dir))
  405. {
  406. return -1;
  407. }
  408. // v1-v2-v3 triangle must be rearranged to face outside Minkowski
  409. // difference (direction from v0).
  410. b3ExpandPortal(portal, &v4);
  411. }
  412. return -1;
  413. }
  414. static void b3FindPos(const b3MprSimplex_t *portal, b3Float4 *pos)
  415. {
  416. b3Float4 zero = b3MakeFloat4(0,0,0,0);
  417. b3Float4* b3mpr_vec3_origin = &zero;
  418. b3Float4 dir;
  419. size_t i;
  420. float b[4], sum, inv;
  421. b3Float4 vec, p1, p2;
  422. b3PortalDir(portal, &dir);
  423. // use barycentric coordinates of tetrahedron to find origin
  424. b3MprVec3Cross(&vec, &b3MprSimplexPoint(portal, 1)->v,
  425. &b3MprSimplexPoint(portal, 2)->v);
  426. b[0] = b3MprVec3Dot(&vec, &b3MprSimplexPoint(portal, 3)->v);
  427. b3MprVec3Cross(&vec, &b3MprSimplexPoint(portal, 3)->v,
  428. &b3MprSimplexPoint(portal, 2)->v);
  429. b[1] = b3MprVec3Dot(&vec, &b3MprSimplexPoint(portal, 0)->v);
  430. b3MprVec3Cross(&vec, &b3MprSimplexPoint(portal, 0)->v,
  431. &b3MprSimplexPoint(portal, 1)->v);
  432. b[2] = b3MprVec3Dot(&vec, &b3MprSimplexPoint(portal, 3)->v);
  433. b3MprVec3Cross(&vec, &b3MprSimplexPoint(portal, 2)->v,
  434. &b3MprSimplexPoint(portal, 1)->v);
  435. b[3] = b3MprVec3Dot(&vec, &b3MprSimplexPoint(portal, 0)->v);
  436. sum = b[0] + b[1] + b[2] + b[3];
  437. if (b3MprIsZero(sum) || sum < 0.f){
  438. b[0] = 0.f;
  439. b3MprVec3Cross(&vec, &b3MprSimplexPoint(portal, 2)->v,
  440. &b3MprSimplexPoint(portal, 3)->v);
  441. b[1] = b3MprVec3Dot(&vec, &dir);
  442. b3MprVec3Cross(&vec, &b3MprSimplexPoint(portal, 3)->v,
  443. &b3MprSimplexPoint(portal, 1)->v);
  444. b[2] = b3MprVec3Dot(&vec, &dir);
  445. b3MprVec3Cross(&vec, &b3MprSimplexPoint(portal, 1)->v,
  446. &b3MprSimplexPoint(portal, 2)->v);
  447. b[3] = b3MprVec3Dot(&vec, &dir);
  448. sum = b[1] + b[2] + b[3];
  449. }
  450. inv = 1.f / sum;
  451. b3MprVec3Copy(&p1, b3mpr_vec3_origin);
  452. b3MprVec3Copy(&p2, b3mpr_vec3_origin);
  453. for (i = 0; i < 4; i++){
  454. b3MprVec3Copy(&vec, &b3MprSimplexPoint(portal, i)->v1);
  455. b3MprVec3Scale(&vec, b[i]);
  456. b3MprVec3Add(&p1, &vec);
  457. b3MprVec3Copy(&vec, &b3MprSimplexPoint(portal, i)->v2);
  458. b3MprVec3Scale(&vec, b[i]);
  459. b3MprVec3Add(&p2, &vec);
  460. }
  461. b3MprVec3Scale(&p1, inv);
  462. b3MprVec3Scale(&p2, inv);
  463. b3MprVec3Copy(pos, &p1);
  464. b3MprVec3Add(pos, &p2);
  465. b3MprVec3Scale(pos, 0.5);
  466. }
  467. inline float b3MprVec3Dist2(const b3Float4 *a, const b3Float4 *b)
  468. {
  469. b3Float4 ab;
  470. b3MprVec3Sub2(&ab, a, b);
  471. return b3MprVec3Len2(&ab);
  472. }
  473. inline float _b3MprVec3PointSegmentDist2(const b3Float4 *P,
  474. const b3Float4 *x0,
  475. const b3Float4 *b,
  476. b3Float4 *witness)
  477. {
  478. // The computation comes from solving equation of segment:
  479. // S(t) = x0 + t.d
  480. // where - x0 is initial point of segment
  481. // - d is direction of segment from x0 (|d| > 0)
  482. // - t belongs to <0, 1> interval
  483. //
  484. // Than, distance from a segment to some point P can be expressed:
  485. // D(t) = |x0 + t.d - P|^2
  486. // which is distance from any point on segment. Minimization
  487. // of this function brings distance from P to segment.
  488. // Minimization of D(t) leads to simple quadratic equation that's
  489. // solving is straightforward.
  490. //
  491. // Bonus of this method is witness point for free.
  492. float dist, t;
  493. b3Float4 d, a;
  494. // direction of segment
  495. b3MprVec3Sub2(&d, b, x0);
  496. // precompute vector from P to x0
  497. b3MprVec3Sub2(&a, x0, P);
  498. t = -1.f * b3MprVec3Dot(&a, &d);
  499. t /= b3MprVec3Len2(&d);
  500. if (t < 0.f || b3MprIsZero(t)){
  501. dist = b3MprVec3Dist2(x0, P);
  502. if (witness)
  503. b3MprVec3Copy(witness, x0);
  504. }else if (t > 1.f || b3MprEq(t, 1.f)){
  505. dist = b3MprVec3Dist2(b, P);
  506. if (witness)
  507. b3MprVec3Copy(witness, b);
  508. }else{
  509. if (witness){
  510. b3MprVec3Copy(witness, &d);
  511. b3MprVec3Scale(witness, t);
  512. b3MprVec3Add(witness, x0);
  513. dist = b3MprVec3Dist2(witness, P);
  514. }else{
  515. // recycling variables
  516. b3MprVec3Scale(&d, t);
  517. b3MprVec3Add(&d, &a);
  518. dist = b3MprVec3Len2(&d);
  519. }
  520. }
  521. return dist;
  522. }
  523. inline float b3MprVec3PointTriDist2(const b3Float4 *P,
  524. const b3Float4 *x0, const b3Float4 *B,
  525. const b3Float4 *C,
  526. b3Float4 *witness)
  527. {
  528. // Computation comes from analytic expression for triangle (x0, B, C)
  529. // T(s, t) = x0 + s.d1 + t.d2, where d1 = B - x0 and d2 = C - x0 and
  530. // Then equation for distance is:
  531. // D(s, t) = | T(s, t) - P |^2
  532. // This leads to minimization of quadratic function of two variables.
  533. // The solution from is taken only if s is between 0 and 1, t is
  534. // between 0 and 1 and t + s < 1, otherwise distance from segment is
  535. // computed.
  536. b3Float4 d1, d2, a;
  537. float u, v, w, p, q, r;
  538. float s, t, dist, dist2;
  539. b3Float4 witness2;
  540. b3MprVec3Sub2(&d1, B, x0);
  541. b3MprVec3Sub2(&d2, C, x0);
  542. b3MprVec3Sub2(&a, x0, P);
  543. u = b3MprVec3Dot(&a, &a);
  544. v = b3MprVec3Dot(&d1, &d1);
  545. w = b3MprVec3Dot(&d2, &d2);
  546. p = b3MprVec3Dot(&a, &d1);
  547. q = b3MprVec3Dot(&a, &d2);
  548. r = b3MprVec3Dot(&d1, &d2);
  549. s = (q * r - w * p) / (w * v - r * r);
  550. t = (-s * r - q) / w;
  551. if ((b3MprIsZero(s) || s > 0.f)
  552. && (b3MprEq(s, 1.f) || s < 1.f)
  553. && (b3MprIsZero(t) || t > 0.f)
  554. && (b3MprEq(t, 1.f) || t < 1.f)
  555. && (b3MprEq(t + s, 1.f) || t + s < 1.f)){
  556. if (witness){
  557. b3MprVec3Scale(&d1, s);
  558. b3MprVec3Scale(&d2, t);
  559. b3MprVec3Copy(witness, x0);
  560. b3MprVec3Add(witness, &d1);
  561. b3MprVec3Add(witness, &d2);
  562. dist = b3MprVec3Dist2(witness, P);
  563. }else{
  564. dist = s * s * v;
  565. dist += t * t * w;
  566. dist += 2.f * s * t * r;
  567. dist += 2.f * s * p;
  568. dist += 2.f * t * q;
  569. dist += u;
  570. }
  571. }else{
  572. dist = _b3MprVec3PointSegmentDist2(P, x0, B, witness);
  573. dist2 = _b3MprVec3PointSegmentDist2(P, x0, C, &witness2);
  574. if (dist2 < dist){
  575. dist = dist2;
  576. if (witness)
  577. b3MprVec3Copy(witness, &witness2);
  578. }
  579. dist2 = _b3MprVec3PointSegmentDist2(P, B, C, &witness2);
  580. if (dist2 < dist){
  581. dist = dist2;
  582. if (witness)
  583. b3MprVec3Copy(witness, &witness2);
  584. }
  585. }
  586. return dist;
  587. }
  588. static void b3FindPenetr(int pairIndex,int bodyIndexA, int bodyIndexB, b3ConstArray(b3RigidBodyData_t) cpuBodyBuf,
  589. b3ConstArray(b3ConvexPolyhedronData_t) cpuConvexData,
  590. b3ConstArray(b3Collidable_t) cpuCollidables,
  591. b3ConstArray(b3Float4) cpuVertices,
  592. __global b3Float4* sepAxis,
  593. b3MprSimplex_t *portal,
  594. float *depth, b3Float4 *pdir, b3Float4 *pos)
  595. {
  596. b3Float4 dir;
  597. b3MprSupport_t v4;
  598. unsigned long iterations;
  599. b3Float4 zero = b3MakeFloat4(0,0,0,0);
  600. b3Float4* b3mpr_vec3_origin = &zero;
  601. iterations = 1UL;
  602. for (int i=0;i<B3_MPR_MAX_ITERATIONS;i++)
  603. //while (1)
  604. {
  605. // compute portal direction and obtain next support point
  606. b3PortalDir(portal, &dir);
  607. b3MprSupport(pairIndex,bodyIndexA,bodyIndexB,cpuBodyBuf,cpuConvexData,cpuCollidables,cpuVertices, sepAxis,&dir, &v4);
  608. // reached tolerance -> find penetration info
  609. if (portalReachTolerance(portal, &v4, &dir)
  610. || iterations ==B3_MPR_MAX_ITERATIONS)
  611. {
  612. *depth = b3MprVec3PointTriDist2(b3mpr_vec3_origin,&b3MprSimplexPoint(portal, 1)->v,&b3MprSimplexPoint(portal, 2)->v,&b3MprSimplexPoint(portal, 3)->v,pdir);
  613. *depth = B3_MPR_SQRT(*depth);
  614. if (b3MprIsZero((*pdir).x) && b3MprIsZero((*pdir).y) && b3MprIsZero((*pdir).z))
  615. {
  616. *pdir = dir;
  617. }
  618. b3MprVec3Normalize(pdir);
  619. // barycentric coordinates:
  620. b3FindPos(portal, pos);
  621. return;
  622. }
  623. b3ExpandPortal(portal, &v4);
  624. iterations++;
  625. }
  626. }
  627. static void b3FindPenetrTouch(b3MprSimplex_t *portal,float *depth, b3Float4 *dir, b3Float4 *pos)
  628. {
  629. // Touching contact on portal's v1 - so depth is zero and direction
  630. // is unimportant and pos can be guessed
  631. *depth = 0.f;
  632. b3Float4 zero = b3MakeFloat4(0,0,0,0);
  633. b3Float4* b3mpr_vec3_origin = &zero;
  634. b3MprVec3Copy(dir, b3mpr_vec3_origin);
  635. b3MprVec3Copy(pos, &b3MprSimplexPoint(portal, 1)->v1);
  636. b3MprVec3Add(pos, &b3MprSimplexPoint(portal, 1)->v2);
  637. b3MprVec3Scale(pos, 0.5);
  638. }
  639. static void b3FindPenetrSegment(b3MprSimplex_t *portal,
  640. float *depth, b3Float4 *dir, b3Float4 *pos)
  641. {
  642. // Origin lies on v0-v1 segment.
  643. // Depth is distance to v1, direction also and position must be
  644. // computed
  645. b3MprVec3Copy(pos, &b3MprSimplexPoint(portal, 1)->v1);
  646. b3MprVec3Add(pos, &b3MprSimplexPoint(portal, 1)->v2);
  647. b3MprVec3Scale(pos, 0.5f);
  648. b3MprVec3Copy(dir, &b3MprSimplexPoint(portal, 1)->v);
  649. *depth = B3_MPR_SQRT(b3MprVec3Len2(dir));
  650. b3MprVec3Normalize(dir);
  651. }
  652. inline int b3MprPenetration(int pairIndex, int bodyIndexA, int bodyIndexB,
  653. b3ConstArray(b3RigidBodyData_t) cpuBodyBuf,
  654. b3ConstArray(b3ConvexPolyhedronData_t) cpuConvexData,
  655. b3ConstArray(b3Collidable_t) cpuCollidables,
  656. b3ConstArray(b3Float4) cpuVertices,
  657. __global b3Float4* sepAxis,
  658. __global int* hasSepAxis,
  659. float *depthOut, b3Float4* dirOut, b3Float4* posOut)
  660. {
  661. b3MprSimplex_t portal;
  662. // if (!hasSepAxis[pairIndex])
  663. // return -1;
  664. hasSepAxis[pairIndex] = 0;
  665. int res;
  666. // Phase 1: Portal discovery
  667. res = b3DiscoverPortal(pairIndex,bodyIndexA,bodyIndexB,cpuBodyBuf,cpuConvexData,cpuCollidables,cpuVertices,sepAxis,hasSepAxis, &portal);
  668. //sepAxis[pairIndex] = *pdir;//or -dir?
  669. switch (res)
  670. {
  671. case 0:
  672. {
  673. // Phase 2: Portal refinement
  674. res = b3RefinePortal(pairIndex,bodyIndexA,bodyIndexB,cpuBodyBuf,cpuConvexData,cpuCollidables,cpuVertices, sepAxis,&portal);
  675. if (res < 0)
  676. return -1;
  677. // Phase 3. Penetration info
  678. b3FindPenetr(pairIndex,bodyIndexA,bodyIndexB,cpuBodyBuf,cpuConvexData,cpuCollidables,cpuVertices, sepAxis,&portal, depthOut, dirOut, posOut);
  679. hasSepAxis[pairIndex] = 1;
  680. sepAxis[pairIndex] = -*dirOut;
  681. break;
  682. }
  683. case 1:
  684. {
  685. // Touching contact on portal's v1.
  686. b3FindPenetrTouch(&portal, depthOut, dirOut, posOut);
  687. break;
  688. }
  689. case 2:
  690. {
  691. b3FindPenetrSegment( &portal, depthOut, dirOut, posOut);
  692. break;
  693. }
  694. default:
  695. {
  696. hasSepAxis[pairIndex]=0;
  697. //if (res < 0)
  698. //{
  699. // Origin isn't inside portal - no collision.
  700. return -1;
  701. //}
  702. }
  703. };
  704. return 0;
  705. };
  706. #endif //B3_MPR_PENETRATION_H