b3MprPenetration.h 23 KB

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