3 * ---------------------------------
4 * Copyright (c)2012 Daniel Fiser <danfis@danfis.cz>
6 * This file was ported from mpr.c file, part of libccd.
7 * The Minkoski Portal Refinement implementation was ported
8 * to OpenCL by Erwin Coumans for the Bullet 3 Physics library.
9 * at http://github.com/erwincoumans/bullet3
11 * Distributed under the OSI-approved BSD License (the "License");
12 * see <http://www.opensource.org/licenses/bsd-license.php>.
13 * This software is distributed WITHOUT ANY WARRANTY; without even the
14 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15 * See the License for more information.
18 #ifndef B3_MPR_PENETRATION_H
19 #define B3_MPR_PENETRATION_H
21 #include "Bullet3Common/shared/b3PlatformDefinitions.h"
22 #include "Bullet3Common/shared/b3Float4.h"
23 #include "Bullet3Collision/NarrowPhaseCollision/shared/b3RigidBodyData.h"
24 #include "Bullet3Collision/NarrowPhaseCollision/shared/b3ConvexPolyhedronData.h"
25 #include "Bullet3Collision/NarrowPhaseCollision/shared/b3Collidable.h"
28 #define B3_MPR_SQRT sqrtf
30 #define B3_MPR_SQRT sqrt
32 #define B3_MPR_FMIN(x, y) ((x) < (y) ? (x) : (y))
33 #define B3_MPR_FABS fabs
35 #define B3_MPR_TOLERANCE 1E-6f
36 #define B3_MPR_MAX_ITERATIONS 1000
38 struct _b3MprSupport_t
40 b3Float4 v; //!< Support point in minkowski sum
41 b3Float4 v1; //!< Support point in obj1
42 b3Float4 v2; //!< Support point in obj2
44 typedef struct _b3MprSupport_t b3MprSupport_t;
46 struct _b3MprSimplex_t
49 int last; //!< index of last added point
51 typedef struct _b3MprSimplex_t b3MprSimplex_t;
53 inline b3MprSupport_t *b3MprSimplexPointW(b3MprSimplex_t *s, int idx)
58 inline void b3MprSimplexSetSize(b3MprSimplex_t *s, int size)
63 inline int b3MprSimplexSize(const b3MprSimplex_t *s)
68 inline const b3MprSupport_t *b3MprSimplexPoint(const b3MprSimplex_t *s, int idx)
70 // here is no check on boundaries
74 inline void b3MprSupportCopy(b3MprSupport_t *d, const b3MprSupport_t *s)
79 inline void b3MprSimplexSet(b3MprSimplex_t *s, size_t pos, const b3MprSupport_t *a)
81 b3MprSupportCopy(s->ps + pos, a);
84 inline void b3MprSimplexSwap(b3MprSimplex_t *s, size_t pos1, size_t pos2)
88 b3MprSupportCopy(&supp, &s->ps[pos1]);
89 b3MprSupportCopy(&s->ps[pos1], &s->ps[pos2]);
90 b3MprSupportCopy(&s->ps[pos2], &supp);
93 inline int b3MprIsZero(float val)
95 return B3_MPR_FABS(val) < FLT_EPSILON;
98 inline int b3MprEq(float _a, float _b)
103 ab = B3_MPR_FABS(_a - _b);
104 if (B3_MPR_FABS(ab) < FLT_EPSILON)
111 return ab < FLT_EPSILON * b;
115 return ab < FLT_EPSILON * a;
119 inline int b3MprVec3Eq(const b3Float4 *a, const b3Float4 *b)
121 return b3MprEq((*a).x, (*b).x) && b3MprEq((*a).y, (*b).y) && b3MprEq((*a).z, (*b).z);
124 inline b3Float4 b3LocalGetSupportVertex(b3Float4ConstArg supportVec, __global const b3ConvexPolyhedronData_t *hull, b3ConstArray(b3Float4) verticesA)
126 b3Float4 supVec = b3MakeFloat4(0, 0, 0, 0);
127 float maxDot = -B3_LARGE_FLOAT;
129 if (0 < hull->m_numVertices)
131 const b3Float4 scaled = supportVec;
132 int index = b3MaxDot(scaled, &verticesA[hull->m_vertexOffset], hull->m_numVertices, &maxDot);
133 return verticesA[hull->m_vertexOffset + index];
139 B3_STATIC void b3MprConvexSupport(int pairIndex, int bodyIndex, b3ConstArray(b3RigidBodyData_t) cpuBodyBuf,
140 b3ConstArray(b3ConvexPolyhedronData_t) cpuConvexData,
141 b3ConstArray(b3Collidable_t) cpuCollidables,
142 b3ConstArray(b3Float4) cpuVertices,
143 __global b3Float4 *sepAxis,
144 const b3Float4 *_dir, b3Float4 *outp, int logme)
146 //dir is in worldspace, move to local space
148 b3Float4 pos = cpuBodyBuf[bodyIndex].m_pos;
149 b3Quat orn = cpuBodyBuf[bodyIndex].m_quat;
151 b3Float4 dir = b3MakeFloat4((*_dir).x, (*_dir).y, (*_dir).z, 0.f);
153 const b3Float4 localDir = b3QuatRotate(b3QuatInverse(orn), dir);
155 //find local support vertex
156 int colIndex = cpuBodyBuf[bodyIndex].m_collidableIdx;
158 b3Assert(cpuCollidables[colIndex].m_shapeType == SHAPE_CONVEX_HULL);
159 __global const b3ConvexPolyhedronData_t *hull = &cpuConvexData[cpuCollidables[colIndex].m_shapeIndex];
164 // b3Float4 supVec = b3MakeFloat4(0,0,0,0);
165 float maxDot = -B3_LARGE_FLOAT;
167 if (0 < hull->m_numVertices)
169 const b3Float4 scaled = localDir;
170 int index = b3MaxDot(scaled, &cpuVertices[hull->m_vertexOffset], hull->m_numVertices, &maxDot);
171 pInA = cpuVertices[hull->m_vertexOffset + index];
176 pInA = b3LocalGetSupportVertex(localDir, hull, cpuVertices);
179 //move vertex to world space
180 *outp = b3TransformPoint(pInA, pos, orn);
183 inline void b3MprSupport(int pairIndex, int bodyIndexA, int bodyIndexB, b3ConstArray(b3RigidBodyData_t) cpuBodyBuf,
184 b3ConstArray(b3ConvexPolyhedronData_t) cpuConvexData,
185 b3ConstArray(b3Collidable_t) cpuCollidables,
186 b3ConstArray(b3Float4) cpuVertices,
187 __global b3Float4 *sepAxis,
188 const b3Float4 *_dir, b3MprSupport_t *supp)
192 b3MprConvexSupport(pairIndex, bodyIndexA, cpuBodyBuf, cpuConvexData, cpuCollidables, cpuVertices, sepAxis, &dir, &supp->v1, 0);
194 b3MprConvexSupport(pairIndex, bodyIndexB, cpuBodyBuf, cpuConvexData, cpuCollidables, cpuVertices, sepAxis, &dir, &supp->v2, 0);
195 supp->v = supp->v1 - supp->v2;
198 inline void b3FindOrigin(int bodyIndexA, int bodyIndexB, b3ConstArray(b3RigidBodyData_t) cpuBodyBuf, b3MprSupport_t *center)
200 center->v1 = cpuBodyBuf[bodyIndexA].m_pos;
201 center->v2 = cpuBodyBuf[bodyIndexB].m_pos;
202 center->v = center->v1 - center->v2;
205 inline void b3MprVec3Set(b3Float4 *v, float x, float y, float z)
213 inline void b3MprVec3Add(b3Float4 *v, const b3Float4 *w)
220 inline void b3MprVec3Copy(b3Float4 *v, const b3Float4 *w)
225 inline void b3MprVec3Scale(b3Float4 *d, float k)
230 inline float b3MprVec3Dot(const b3Float4 *a, const b3Float4 *b)
234 dot = b3Dot3F4(*a, *b);
238 inline float b3MprVec3Len2(const b3Float4 *v)
240 return b3MprVec3Dot(v, v);
243 inline void b3MprVec3Normalize(b3Float4 *d)
245 float k = 1.f / B3_MPR_SQRT(b3MprVec3Len2(d));
246 b3MprVec3Scale(d, k);
249 inline void b3MprVec3Cross(b3Float4 *d, const b3Float4 *a, const b3Float4 *b)
251 *d = b3Cross3(*a, *b);
254 inline void b3MprVec3Sub2(b3Float4 *d, const b3Float4 *v, const b3Float4 *w)
259 inline void b3PortalDir(const b3MprSimplex_t *portal, b3Float4 *dir)
263 b3MprVec3Sub2(&v2v1, &b3MprSimplexPoint(portal, 2)->v,
264 &b3MprSimplexPoint(portal, 1)->v);
265 b3MprVec3Sub2(&v3v1, &b3MprSimplexPoint(portal, 3)->v,
266 &b3MprSimplexPoint(portal, 1)->v);
267 b3MprVec3Cross(dir, &v2v1, &v3v1);
268 b3MprVec3Normalize(dir);
271 inline int portalEncapsulesOrigin(const b3MprSimplex_t *portal,
275 dot = b3MprVec3Dot(dir, &b3MprSimplexPoint(portal, 1)->v);
276 return b3MprIsZero(dot) || dot > 0.f;
279 inline int portalReachTolerance(const b3MprSimplex_t *portal,
280 const b3MprSupport_t *v4,
283 float dv1, dv2, dv3, dv4;
284 float dot1, dot2, dot3;
286 // find the smallest dot product of dir and {v1-v4, v2-v4, v3-v4}
288 dv1 = b3MprVec3Dot(&b3MprSimplexPoint(portal, 1)->v, dir);
289 dv2 = b3MprVec3Dot(&b3MprSimplexPoint(portal, 2)->v, dir);
290 dv3 = b3MprVec3Dot(&b3MprSimplexPoint(portal, 3)->v, dir);
291 dv4 = b3MprVec3Dot(&v4->v, dir);
297 dot1 = B3_MPR_FMIN(dot1, dot2);
298 dot1 = B3_MPR_FMIN(dot1, dot3);
300 return b3MprEq(dot1, B3_MPR_TOLERANCE) || dot1 < B3_MPR_TOLERANCE;
303 inline int portalCanEncapsuleOrigin(const b3MprSimplex_t *portal,
304 const b3MprSupport_t *v4,
308 dot = b3MprVec3Dot(&v4->v, dir);
309 return b3MprIsZero(dot) || dot > 0.f;
312 inline void b3ExpandPortal(b3MprSimplex_t *portal,
313 const b3MprSupport_t *v4)
318 b3MprVec3Cross(&v4v0, &v4->v, &b3MprSimplexPoint(portal, 0)->v);
319 dot = b3MprVec3Dot(&b3MprSimplexPoint(portal, 1)->v, &v4v0);
322 dot = b3MprVec3Dot(&b3MprSimplexPoint(portal, 2)->v, &v4v0);
325 b3MprSimplexSet(portal, 1, v4);
329 b3MprSimplexSet(portal, 3, v4);
334 dot = b3MprVec3Dot(&b3MprSimplexPoint(portal, 3)->v, &v4v0);
337 b3MprSimplexSet(portal, 2, v4);
341 b3MprSimplexSet(portal, 1, v4);
346 B3_STATIC int b3DiscoverPortal(int pairIndex, int bodyIndexA, int bodyIndexB, b3ConstArray(b3RigidBodyData_t) cpuBodyBuf,
347 b3ConstArray(b3ConvexPolyhedronData_t) cpuConvexData,
348 b3ConstArray(b3Collidable_t) cpuCollidables,
349 b3ConstArray(b3Float4) cpuVertices,
350 __global b3Float4 *sepAxis,
351 __global int *hasSepAxis,
352 b3MprSimplex_t *portal)
354 b3Float4 dir, va, vb;
358 // vertex 0 is center of portal
359 b3FindOrigin(bodyIndexA, bodyIndexB, cpuBodyBuf, b3MprSimplexPointW(portal, 0));
360 // vertex 0 is center of portal
361 b3MprSimplexSetSize(portal, 1);
363 b3Float4 zero = b3MakeFloat4(0, 0, 0, 0);
364 b3Float4 *b3mpr_vec3_origin = &zero;
366 if (b3MprVec3Eq(&b3MprSimplexPoint(portal, 0)->v, b3mpr_vec3_origin))
368 // Portal's center lies on origin (0,0,0) => we know that objects
369 // intersect but we would need to know penetration info.
370 // So move center little bit...
371 b3MprVec3Set(&va, FLT_EPSILON * 10.f, 0.f, 0.f);
372 b3MprVec3Add(&b3MprSimplexPointW(portal, 0)->v, &va);
375 // vertex 1 = support in direction of origin
376 b3MprVec3Copy(&dir, &b3MprSimplexPoint(portal, 0)->v);
377 b3MprVec3Scale(&dir, -1.f);
378 b3MprVec3Normalize(&dir);
380 b3MprSupport(pairIndex, bodyIndexA, bodyIndexB, cpuBodyBuf, cpuConvexData, cpuCollidables, cpuVertices, sepAxis, &dir, b3MprSimplexPointW(portal, 1));
382 b3MprSimplexSetSize(portal, 2);
384 // test if origin isn't outside of v1
385 dot = b3MprVec3Dot(&b3MprSimplexPoint(portal, 1)->v, &dir);
387 if (b3MprIsZero(dot) || dot < 0.f)
391 b3MprVec3Cross(&dir, &b3MprSimplexPoint(portal, 0)->v,
392 &b3MprSimplexPoint(portal, 1)->v);
393 if (b3MprIsZero(b3MprVec3Len2(&dir)))
395 if (b3MprVec3Eq(&b3MprSimplexPoint(portal, 1)->v, b3mpr_vec3_origin))
402 // origin lies on v0-v1 segment
407 b3MprVec3Normalize(&dir);
408 b3MprSupport(pairIndex, bodyIndexA, bodyIndexB, cpuBodyBuf, cpuConvexData, cpuCollidables, cpuVertices, sepAxis, &dir, b3MprSimplexPointW(portal, 2));
410 dot = b3MprVec3Dot(&b3MprSimplexPoint(portal, 2)->v, &dir);
411 if (b3MprIsZero(dot) || dot < 0.f)
414 b3MprSimplexSetSize(portal, 3);
416 // vertex 3 direction
417 b3MprVec3Sub2(&va, &b3MprSimplexPoint(portal, 1)->v,
418 &b3MprSimplexPoint(portal, 0)->v);
419 b3MprVec3Sub2(&vb, &b3MprSimplexPoint(portal, 2)->v,
420 &b3MprSimplexPoint(portal, 0)->v);
421 b3MprVec3Cross(&dir, &va, &vb);
422 b3MprVec3Normalize(&dir);
424 // it is better to form portal faces to be oriented "outside" origin
425 dot = b3MprVec3Dot(&dir, &b3MprSimplexPoint(portal, 0)->v);
428 b3MprSimplexSwap(portal, 1, 2);
429 b3MprVec3Scale(&dir, -1.f);
432 while (b3MprSimplexSize(portal) < 4)
434 b3MprSupport(pairIndex, bodyIndexA, bodyIndexB, cpuBodyBuf, cpuConvexData, cpuCollidables, cpuVertices, sepAxis, &dir, b3MprSimplexPointW(portal, 3));
436 dot = b3MprVec3Dot(&b3MprSimplexPoint(portal, 3)->v, &dir);
437 if (b3MprIsZero(dot) || dot < 0.f)
442 // test if origin is outside (v1, v0, v3) - set v2 as v3 and
444 b3MprVec3Cross(&va, &b3MprSimplexPoint(portal, 1)->v,
445 &b3MprSimplexPoint(portal, 3)->v);
446 dot = b3MprVec3Dot(&va, &b3MprSimplexPoint(portal, 0)->v);
447 if (dot < 0.f && !b3MprIsZero(dot))
449 b3MprSimplexSet(portal, 2, b3MprSimplexPoint(portal, 3));
455 // test if origin is outside (v3, v0, v2) - set v1 as v3 and
457 b3MprVec3Cross(&va, &b3MprSimplexPoint(portal, 3)->v,
458 &b3MprSimplexPoint(portal, 2)->v);
459 dot = b3MprVec3Dot(&va, &b3MprSimplexPoint(portal, 0)->v);
460 if (dot < 0.f && !b3MprIsZero(dot))
462 b3MprSimplexSet(portal, 1, b3MprSimplexPoint(portal, 3));
469 b3MprVec3Sub2(&va, &b3MprSimplexPoint(portal, 1)->v,
470 &b3MprSimplexPoint(portal, 0)->v);
471 b3MprVec3Sub2(&vb, &b3MprSimplexPoint(portal, 2)->v,
472 &b3MprSimplexPoint(portal, 0)->v);
473 b3MprVec3Cross(&dir, &va, &vb);
474 b3MprVec3Normalize(&dir);
478 b3MprSimplexSetSize(portal, 4);
485 B3_STATIC int b3RefinePortal(int pairIndex, int bodyIndexA, int bodyIndexB, b3ConstArray(b3RigidBodyData_t) cpuBodyBuf,
486 b3ConstArray(b3ConvexPolyhedronData_t) cpuConvexData,
487 b3ConstArray(b3Collidable_t) cpuCollidables,
488 b3ConstArray(b3Float4) cpuVertices,
489 __global b3Float4 *sepAxis,
490 b3MprSimplex_t *portal)
495 for (int i = 0; i < B3_MPR_MAX_ITERATIONS; i++)
498 // compute direction outside the portal (from v0 throught v1,v2,v3
500 b3PortalDir(portal, &dir);
502 // test if origin is inside the portal
503 if (portalEncapsulesOrigin(portal, &dir))
506 // get next support point
508 b3MprSupport(pairIndex, bodyIndexA, bodyIndexB, cpuBodyBuf, cpuConvexData, cpuCollidables, cpuVertices, sepAxis, &dir, &v4);
510 // test if v4 can expand portal to contain origin and if portal
511 // expanding doesn't reach given tolerance
512 if (!portalCanEncapsuleOrigin(portal, &v4, &dir) || portalReachTolerance(portal, &v4, &dir))
517 // v1-v2-v3 triangle must be rearranged to face outside Minkowski
518 // difference (direction from v0).
519 b3ExpandPortal(portal, &v4);
525 B3_STATIC void b3FindPos(const b3MprSimplex_t *portal, b3Float4 *pos)
527 b3Float4 zero = b3MakeFloat4(0, 0, 0, 0);
528 b3Float4 *b3mpr_vec3_origin = &zero;
532 float b[4], sum, inv;
533 b3Float4 vec, p1, p2;
535 b3PortalDir(portal, &dir);
537 // use barycentric coordinates of tetrahedron to find origin
538 b3MprVec3Cross(&vec, &b3MprSimplexPoint(portal, 1)->v,
539 &b3MprSimplexPoint(portal, 2)->v);
540 b[0] = b3MprVec3Dot(&vec, &b3MprSimplexPoint(portal, 3)->v);
542 b3MprVec3Cross(&vec, &b3MprSimplexPoint(portal, 3)->v,
543 &b3MprSimplexPoint(portal, 2)->v);
544 b[1] = b3MprVec3Dot(&vec, &b3MprSimplexPoint(portal, 0)->v);
546 b3MprVec3Cross(&vec, &b3MprSimplexPoint(portal, 0)->v,
547 &b3MprSimplexPoint(portal, 1)->v);
548 b[2] = b3MprVec3Dot(&vec, &b3MprSimplexPoint(portal, 3)->v);
550 b3MprVec3Cross(&vec, &b3MprSimplexPoint(portal, 2)->v,
551 &b3MprSimplexPoint(portal, 1)->v);
552 b[3] = b3MprVec3Dot(&vec, &b3MprSimplexPoint(portal, 0)->v);
554 sum = b[0] + b[1] + b[2] + b[3];
556 if (b3MprIsZero(sum) || sum < 0.f)
560 b3MprVec3Cross(&vec, &b3MprSimplexPoint(portal, 2)->v,
561 &b3MprSimplexPoint(portal, 3)->v);
562 b[1] = b3MprVec3Dot(&vec, &dir);
563 b3MprVec3Cross(&vec, &b3MprSimplexPoint(portal, 3)->v,
564 &b3MprSimplexPoint(portal, 1)->v);
565 b[2] = b3MprVec3Dot(&vec, &dir);
566 b3MprVec3Cross(&vec, &b3MprSimplexPoint(portal, 1)->v,
567 &b3MprSimplexPoint(portal, 2)->v);
568 b[3] = b3MprVec3Dot(&vec, &dir);
570 sum = b[1] + b[2] + b[3];
575 b3MprVec3Copy(&p1, b3mpr_vec3_origin);
576 b3MprVec3Copy(&p2, b3mpr_vec3_origin);
577 for (i = 0; i < 4; i++)
579 b3MprVec3Copy(&vec, &b3MprSimplexPoint(portal, i)->v1);
580 b3MprVec3Scale(&vec, b[i]);
581 b3MprVec3Add(&p1, &vec);
583 b3MprVec3Copy(&vec, &b3MprSimplexPoint(portal, i)->v2);
584 b3MprVec3Scale(&vec, b[i]);
585 b3MprVec3Add(&p2, &vec);
587 b3MprVec3Scale(&p1, inv);
588 b3MprVec3Scale(&p2, inv);
590 b3MprVec3Copy(pos, &p1);
591 b3MprVec3Add(pos, &p2);
592 b3MprVec3Scale(pos, 0.5);
595 inline float b3MprVec3Dist2(const b3Float4 *a, const b3Float4 *b)
598 b3MprVec3Sub2(&ab, a, b);
599 return b3MprVec3Len2(&ab);
602 inline float _b3MprVec3PointSegmentDist2(const b3Float4 *P,
607 // The computation comes from solving equation of segment:
609 // where - x0 is initial point of segment
610 // - d is direction of segment from x0 (|d| > 0)
611 // - t belongs to <0, 1> interval
613 // Than, distance from a segment to some point P can be expressed:
614 // D(t) = |x0 + t.d - P|^2
615 // which is distance from any point on segment. Minimization
616 // of this function brings distance from P to segment.
617 // Minimization of D(t) leads to simple quadratic equation that's
618 // solving is straightforward.
620 // Bonus of this method is witness point for free.
625 // direction of segment
626 b3MprVec3Sub2(&d, b, x0);
628 // precompute vector from P to x0
629 b3MprVec3Sub2(&a, x0, P);
631 t = -1.f * b3MprVec3Dot(&a, &d);
632 t /= b3MprVec3Len2(&d);
634 if (t < 0.f || b3MprIsZero(t))
636 dist = b3MprVec3Dist2(x0, P);
638 b3MprVec3Copy(witness, x0);
640 else if (t > 1.f || b3MprEq(t, 1.f))
642 dist = b3MprVec3Dist2(b, P);
644 b3MprVec3Copy(witness, b);
650 b3MprVec3Copy(witness, &d);
651 b3MprVec3Scale(witness, t);
652 b3MprVec3Add(witness, x0);
653 dist = b3MprVec3Dist2(witness, P);
657 // recycling variables
658 b3MprVec3Scale(&d, t);
659 b3MprVec3Add(&d, &a);
660 dist = b3MprVec3Len2(&d);
667 inline float b3MprVec3PointTriDist2(const b3Float4 *P,
668 const b3Float4 *x0, const b3Float4 *B,
672 // Computation comes from analytic expression for triangle (x0, B, C)
673 // T(s, t) = x0 + s.d1 + t.d2, where d1 = B - x0 and d2 = C - x0 and
674 // Then equation for distance is:
675 // D(s, t) = | T(s, t) - P |^2
676 // This leads to minimization of quadratic function of two variables.
677 // The solution from is taken only if s is between 0 and 1, t is
678 // between 0 and 1 and t + s < 1, otherwise distance from segment is
682 float u, v, w, p, q, r;
683 float s, t, dist, dist2;
686 b3MprVec3Sub2(&d1, B, x0);
687 b3MprVec3Sub2(&d2, C, x0);
688 b3MprVec3Sub2(&a, x0, P);
690 u = b3MprVec3Dot(&a, &a);
691 v = b3MprVec3Dot(&d1, &d1);
692 w = b3MprVec3Dot(&d2, &d2);
693 p = b3MprVec3Dot(&a, &d1);
694 q = b3MprVec3Dot(&a, &d2);
695 r = b3MprVec3Dot(&d1, &d2);
697 s = (q * r - w * p) / (w * v - r * r);
698 t = (-s * r - q) / w;
700 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))
704 b3MprVec3Scale(&d1, s);
705 b3MprVec3Scale(&d2, t);
706 b3MprVec3Copy(witness, x0);
707 b3MprVec3Add(witness, &d1);
708 b3MprVec3Add(witness, &d2);
710 dist = b3MprVec3Dist2(witness, P);
716 dist += 2.f * s * t * r;
724 dist = _b3MprVec3PointSegmentDist2(P, x0, B, witness);
726 dist2 = _b3MprVec3PointSegmentDist2(P, x0, C, &witness2);
731 b3MprVec3Copy(witness, &witness2);
734 dist2 = _b3MprVec3PointSegmentDist2(P, B, C, &witness2);
739 b3MprVec3Copy(witness, &witness2);
746 B3_STATIC void b3FindPenetr(int pairIndex, int bodyIndexA, int bodyIndexB, b3ConstArray(b3RigidBodyData_t) cpuBodyBuf,
747 b3ConstArray(b3ConvexPolyhedronData_t) cpuConvexData,
748 b3ConstArray(b3Collidable_t) cpuCollidables,
749 b3ConstArray(b3Float4) cpuVertices,
750 __global b3Float4 *sepAxis,
751 b3MprSimplex_t *portal,
752 float *depth, b3Float4 *pdir, b3Float4 *pos)
756 unsigned long iterations;
758 b3Float4 zero = b3MakeFloat4(0, 0, 0, 0);
759 b3Float4 *b3mpr_vec3_origin = &zero;
762 for (int i = 0; i < B3_MPR_MAX_ITERATIONS; i++)
765 // compute portal direction and obtain next support point
766 b3PortalDir(portal, &dir);
768 b3MprSupport(pairIndex, bodyIndexA, bodyIndexB, cpuBodyBuf, cpuConvexData, cpuCollidables, cpuVertices, sepAxis, &dir, &v4);
770 // reached tolerance -> find penetration info
771 if (portalReachTolerance(portal, &v4, &dir) || iterations == B3_MPR_MAX_ITERATIONS)
773 *depth = b3MprVec3PointTriDist2(b3mpr_vec3_origin, &b3MprSimplexPoint(portal, 1)->v, &b3MprSimplexPoint(portal, 2)->v, &b3MprSimplexPoint(portal, 3)->v, pdir);
774 *depth = B3_MPR_SQRT(*depth);
776 if (b3MprIsZero((*pdir).x) && b3MprIsZero((*pdir).y) && b3MprIsZero((*pdir).z))
780 b3MprVec3Normalize(pdir);
782 // barycentric coordinates:
783 b3FindPos(portal, pos);
788 b3ExpandPortal(portal, &v4);
794 B3_STATIC void b3FindPenetrTouch(b3MprSimplex_t *portal, float *depth, b3Float4 *dir, b3Float4 *pos)
796 // Touching contact on portal's v1 - so depth is zero and direction
797 // is unimportant and pos can be guessed
799 b3Float4 zero = b3MakeFloat4(0, 0, 0, 0);
800 b3Float4 *b3mpr_vec3_origin = &zero;
802 b3MprVec3Copy(dir, b3mpr_vec3_origin);
804 b3MprVec3Copy(pos, &b3MprSimplexPoint(portal, 1)->v1);
805 b3MprVec3Add(pos, &b3MprSimplexPoint(portal, 1)->v2);
806 b3MprVec3Scale(pos, 0.5);
809 B3_STATIC void b3FindPenetrSegment(b3MprSimplex_t *portal,
810 float *depth, b3Float4 *dir, b3Float4 *pos)
812 // Origin lies on v0-v1 segment.
813 // Depth is distance to v1, direction also and position must be
816 b3MprVec3Copy(pos, &b3MprSimplexPoint(portal, 1)->v1);
817 b3MprVec3Add(pos, &b3MprSimplexPoint(portal, 1)->v2);
818 b3MprVec3Scale(pos, 0.5f);
820 b3MprVec3Copy(dir, &b3MprSimplexPoint(portal, 1)->v);
821 *depth = B3_MPR_SQRT(b3MprVec3Len2(dir));
822 b3MprVec3Normalize(dir);
825 inline int b3MprPenetration(int pairIndex, int bodyIndexA, int bodyIndexB,
826 b3ConstArray(b3RigidBodyData_t) cpuBodyBuf,
827 b3ConstArray(b3ConvexPolyhedronData_t) cpuConvexData,
828 b3ConstArray(b3Collidable_t) cpuCollidables,
829 b3ConstArray(b3Float4) cpuVertices,
830 __global b3Float4 *sepAxis,
831 __global int *hasSepAxis,
832 float *depthOut, b3Float4 *dirOut, b3Float4 *posOut)
834 b3MprSimplex_t portal;
836 // if (!hasSepAxis[pairIndex])
839 hasSepAxis[pairIndex] = 0;
842 // Phase 1: Portal discovery
843 res = b3DiscoverPortal(pairIndex, bodyIndexA, bodyIndexB, cpuBodyBuf, cpuConvexData, cpuCollidables, cpuVertices, sepAxis, hasSepAxis, &portal);
845 //sepAxis[pairIndex] = *pdir;//or -dir?
851 // Phase 2: Portal refinement
853 res = b3RefinePortal(pairIndex, bodyIndexA, bodyIndexB, cpuBodyBuf, cpuConvexData, cpuCollidables, cpuVertices, sepAxis, &portal);
857 // Phase 3. Penetration info
858 b3FindPenetr(pairIndex, bodyIndexA, bodyIndexB, cpuBodyBuf, cpuConvexData, cpuCollidables, cpuVertices, sepAxis, &portal, depthOut, dirOut, posOut);
859 hasSepAxis[pairIndex] = 1;
860 sepAxis[pairIndex] = -*dirOut;
865 // Touching contact on portal's v1.
866 b3FindPenetrTouch(&portal, depthOut, dirOut, posOut);
871 b3FindPenetrSegment(&portal, depthOut, dirOut, posOut);
876 hasSepAxis[pairIndex] = 0;
879 // Origin isn't inside portal - no collision.
888 #endif //B3_MPR_PENETRATION_H