1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
| float3 closestPointOnLine(float3 p, float3 a, float3 b) { float3 ap = p - a; float3 ab = b - a; float t = dot(ap, ab) / dot(ab, ab); return a + ab * t; }
float3 support(float3 n, float3 p) { float3 d = n > 0? float3(1, 1, 1) : float3(-1, -1, -1); float3 v = p + d * max(0, dot(n, p)); return v; }
bool intersects(float3 p1, float3 q1, float3 p2, float3 q2) { float3 d1 = q1 - p1; float3 d2 = q2 - p2; float3 n = cross(d1, d2); if (dot(n, n) < 0.0001f) { return false; } float3 v1 = support(-n, p1); float3 v2 = support(n, p2); float3 w1 = v1 - p1; float3 w2 = v2 - p2; if (dot(cross(w1, d2), cross(w2, d1)) < 0) { return false; } float3 c1 = closestPointOnLine(v1, p1, q1); float3 c2 = closestPointOnLine(v2, p2, q2); return distance(c1, c2) < 0.0001f; }
bool GJK(float3 p1, float3 d1, float3 p2, float3 d2, out float3 hit) { float3 ab = p2 - p1; float3 ad = d2 - d1; float h = dot(ab, ad); if (h <= 0) { hit = p1; return false; } float3 ac = p1 - p2; float3 bc = d1 - d2; float k = dot(ac, bc); if (k <= 0) { hit = p2; return false; } float3 n = cross(ab, ac); if (dot(n, d1) < 0) { n = -n; } hit = p1 + n * sqrt(h * h - k * k) / dot(n, d1); return true; }
bool EPA(float3 p1, float3 d1, float3 p2, float3 d2, out float3 hit) { const int maxIterations = 100; const float tolerance = 0.001f;
float3 m = normalize(p2 - p1); float3 n = cross(d1, d2); float3 q = p2 + d2 * dot(p2 - p1, d2); float3 a = dot(d1, d1); float3 e = dot(d2, d2); float3 c = dot(d1, n); float3 r = dot(n, n); float3 b = dot(d1, q - p1); float3 d = dot(d2, q - p2); float3 f = dot(n, q - p1);
float3 u = float3(0, 0, 0); float3 v = float3(0, 0, 0); float3 w = float3(0, 0, 0);
float3 closestPoint1 = float3(0, 0, 0); float3 closestPoint2 = float3(0, 0, 0);
for (int i = 0; i < maxIterations; i++) { float t = (-b + sqrt(b * b - a * c)) / (a + e); if (t < 0 || t > 1) { t = (-b - sqrt(b * b - a * c)) / (a + e); if (t < 0 || t > 1) { break; } }
float3 intersection = p1 + d1 * t; float3 distance = intersection - q;
if (dot(distance, distance) < tolerance) { hit = intersection; return true; }
if (dot(distance, n) > 0) { n = -n; r = -r; f = -f; }
float3 tangent = normalize(q - intersection); float3 bitangent = cross(n, tangent);
u = cross(tangent, bitangent); v = cross(bitangent, tangent); w = n;
float3 offset = u * dot(distance, u) + v * dot(distance, v) + w * dot(distance, w); float3 newPoint1 = intersection + offset; float3 newPoint2 = intersection - offset;
if (dot(newPoint1 - closestPoint1, newPoint1 - closestPoint1) < tolerance || dot(newPoint2 - closestPoint2, newPoint2 - closestPoint2) < tolerance) { break; }
closestPoint1 = newPoint1; closestPoint2 = newPoint2; }
hit = float3(0, 0, 0); return false; }
bool CheckCollision(float3 p1, float3 d1, float3 p2, float3 d2, out float3 hit) { float3 gjkHit = float3(0, 0, 0); if (!GJK(p1, d1, p2, d2, gjkHit)
|