22#include < vector>
33
44#include < cage-core/collider.h>
5- // #include <cage-core/flatSet.h>
65#include < cage-core/geometry.h>
76#include < cage-core/memoryBuffer.h>
87#include < cage-core/mesh.h>
@@ -430,6 +429,7 @@ namespace cage
430429
431430 CollisionDetector (const ColliderImpl *ao, const ColliderImpl *bo, Transform am, Transform bm, Holder<PointerRange<CollisionPair>> &outputBuffer) : ats(ao->tris.data(), numeric_cast<uint32>(ao->tris.size()), am), bts(bo->tris.data(), numeric_cast<uint32>(bo->tris.size()), bm), abs(ao->boxes.data(), numeric_cast<uint32>(ao->boxes.size()), am), bbs(bo->boxes.data(), numeric_cast<uint32>(bo->boxes.size()), bm), ao(ao), bo(bo), outputBuffer(outputBuffer) {}
432431
432+ private:
433433 void process (uint32 a, uint32 b)
434434 {
435435 CAGE_ASSERT (a < ao->nodes .size ());
@@ -483,6 +483,7 @@ namespace cage
483483 }
484484 }
485485
486+ public:
486487 void process ()
487488 {
488489 process (0 , 0 );
@@ -528,6 +529,7 @@ namespace cage
528529
529530 IntersectionDetector (const ColliderImpl *collider, Transform m) : col(collider), m(m) { CAGE_ASSERT (!collider->dirty ); }
530531
532+ private:
531533 template <class T >
532534 Real distance (const T &l, uint32 nodeIdx)
533535 {
@@ -561,12 +563,16 @@ namespace cage
561563 }
562564 }
563565
566+ public:
564567 template <class T >
565568 Real distance (const T &shape)
566569 {
567- return distance (shape * inverse (m), 0 );
570+ CAGE_THROW_CRITICAL (Exception, " distance to collider is untested and broken" );
571+ // the result is in the original space of the collider and must be converted back to the space of the shape
572+ return distance (shape * inverse (m), 0 ) * m.scale ;
568573 }
569574
575+ private:
570576 template <class T >
571577 bool intersects (const T &l, uint32 nodeIdx)
572578 {
@@ -590,12 +596,14 @@ namespace cage
590596 }
591597 }
592598
599+ public:
593600 template <class T >
594601 bool intersects (const T &shape)
595602 {
596603 return intersects (shape * inverse (m), 0 );
597604 }
598605
606+ private:
599607 bool intersection (Line l, uint32 nodeIdx, Vec3 &point, uint32 &triangleIndex)
600608 {
601609 const Aabb b = col->boxes [nodeIdx];
@@ -664,14 +672,15 @@ namespace cage
664672 }
665673 }
666674
675+ public:
667676 bool intersection (Line l, Vec3 &point, uint32 &triangleIndex)
668677 {
678+ // p is in the original space of the collider and must be converted back to the space of the shape
669679 Vec3 pt;
670680 uint32 ti = 0 ;
671681 if (intersection (l * inverse (m), 0 , pt, ti))
672682 {
673- const Vec4 r4 = Vec4 (pt, 1 ) * Mat4 (m);
674- point = Vec3 (r4) / r4[3 ];
683+ point = pt * m;
675684 triangleIndex = ti;
676685 return true ;
677686 }
@@ -789,6 +798,12 @@ namespace cage
789798 }
790799 }
791800
801+ Real distance (Vec3 shape, const Collider *collider, Transform t)
802+ {
803+ IntersectionDetector d ((const ColliderImpl *)collider, t);
804+ return d.distance (shape);
805+ }
806+
792807 Real distance (Line shape, const Collider *collider, Transform t)
793808 {
794809 IntersectionDetector d ((const ColliderImpl *)collider, t);
@@ -892,7 +907,6 @@ namespace cage
892907
893908 Vec3 intersection (Line shape, const Collider *collider, Transform t, uint32 &triangleIndex)
894909 {
895- // todo
896910 IntersectionDetector d ((const ColliderImpl *)collider, t);
897911 Vec3 p;
898912 if (d.intersection (shape, p, triangleIndex))
0 commit comments