| | | 1 | | //======================================================================= |
| | | 2 | | // GjkSimplexPolicy.cs |
| | | 3 | | //======================================================================= |
| | | 4 | | // MIT License, Copyright (c) 2026-present David Oravsky (mrdav30) |
| | | 5 | | // See LICENSE file in the project root for full license information. |
| | | 6 | | //======================================================================= |
| | | 7 | | |
| | | 8 | | using System; |
| | | 9 | | using System.Runtime.CompilerServices; |
| | | 10 | | using FixedMathSharp; |
| | | 11 | | using FixedMathSharp.Geometry; |
| | | 12 | | |
| | | 13 | | namespace Gravitas.CollisionHandling; |
| | | 14 | | |
| | | 15 | | /// <summary> |
| | | 16 | | /// Updates the GJK simplex and search direction, using exact predicates |
| | | 17 | | /// when fixed-point products are unsafe. |
| | | 18 | | /// </summary> |
| | | 19 | | internal static class GjkSimplexPolicy |
| | | 20 | | { |
| | | 21 | | // Each vertex component <= 256 in magnitude bounds cubic crosses by 536,870,912; side dots stay exact. |
| | | 22 | | private const long ProductSafeVertexRaw = 256L << FixedMath.SHIFT_AMOUNT_I; |
| | | 23 | | |
| | | 24 | | public static void AddPoint(Span<Vector3d> simplex, ref int count, Vector3d point) |
| | | 25 | | { |
| | 16908 | 26 | | for (int i = Math.Min(count, 3); i > 0; i--) |
| | 5484 | 27 | | simplex[i] = simplex[i - 1]; |
| | | 28 | | |
| | 2970 | 29 | | simplex[0] = point; |
| | 2970 | 30 | | if (count < 4) |
| | 2969 | 31 | | count++; |
| | 2970 | 32 | | } |
| | | 33 | | |
| | | 34 | | public static bool Update(Span<Vector3d> simplex, ref int count, ref Vector3d direction) |
| | | 35 | | { |
| | 2969 | 36 | | return count switch |
| | 2969 | 37 | | { |
| | 1301 | 38 | | 2 => UpdateLine(simplex, ref count, ref direction), |
| | 824 | 39 | | 3 => UpdateTriangle(simplex, ref count, ref direction), |
| | 844 | 40 | | _ => UpdateTetrahedron(simplex, ref count, ref direction) |
| | 2969 | 41 | | }; |
| | | 42 | | } |
| | | 43 | | |
| | | 44 | | public static bool UpdateLine(Span<Vector3d> simplex, ref int count, ref Vector3d direction) |
| | | 45 | | { |
| | 1327 | 46 | | Vector3d a = simplex[0]; |
| | 1327 | 47 | | Vector3d b = simplex[1]; |
| | 1327 | 48 | | Vector3d ab = b - a; |
| | 1327 | 49 | | Vector3d ao = -a; |
| | 1327 | 50 | | bool exact = RequiresExactProducts(simplex, 2); |
| | | 51 | | |
| | 1327 | 52 | | if (SameDirection(ab, ao)) |
| | | 53 | | { |
| | 1324 | 54 | | direction = exact |
| | 1324 | 55 | | ? Vector3d.GetNormalizedProjectionOnPlane(ao, ab) |
| | 1324 | 56 | | : TripleCross(ab, ao, ab); |
| | 1324 | 57 | | if (direction.MagnitudeSquared <= Fixed64.Epsilon) |
| | 226 | 58 | | direction = Perpendicular(ab); |
| | 1324 | 59 | | return false; |
| | | 60 | | } |
| | | 61 | | |
| | 3 | 62 | | simplex[0] = a; |
| | 3 | 63 | | count = 1; |
| | 3 | 64 | | direction = ao; |
| | 3 | 65 | | return false; |
| | | 66 | | } |
| | | 67 | | |
| | | 68 | | public static bool UpdateTriangle(Span<Vector3d> simplex, ref int count, ref Vector3d direction) |
| | | 69 | | { |
| | 837 | 70 | | Vector3d a = simplex[0]; |
| | 837 | 71 | | Vector3d b = simplex[1]; |
| | 837 | 72 | | Vector3d c = simplex[2]; |
| | 837 | 73 | | Vector3d ab = b - a; |
| | 837 | 74 | | Vector3d ac = c - a; |
| | 837 | 75 | | Vector3d ao = -a; |
| | 837 | 76 | | bool exact = RequiresExactProducts(simplex, 3); |
| | 837 | 77 | | Vector3d abc = exact ? default : Vector3d.Cross(ab, ac); |
| | | 78 | | |
| | 837 | 79 | | if (exact ? IsOutsideEdge(ab, ac, ao) : SameDirection(Vector3d.Cross(abc, ac), ao)) |
| | | 80 | | { |
| | 42 | 81 | | if (SameDirection(ac, ao)) |
| | | 82 | | { |
| | 40 | 83 | | simplex[1] = c; |
| | 40 | 84 | | count = 2; |
| | 40 | 85 | | direction = exact |
| | 40 | 86 | | ? Vector3d.GetNormalizedProjectionOnPlane(ao, ac) |
| | 40 | 87 | | : TripleCross(ac, ao, ac); |
| | 40 | 88 | | if (direction.MagnitudeSquared <= Fixed64.Epsilon) |
| | 1 | 89 | | direction = Perpendicular(ac); |
| | 40 | 90 | | return false; |
| | | 91 | | } |
| | | 92 | | |
| | 2 | 93 | | simplex[1] = b; |
| | 2 | 94 | | count = 2; |
| | 2 | 95 | | return UpdateLine(simplex, ref count, ref direction); |
| | | 96 | | } |
| | | 97 | | |
| | 795 | 98 | | if (exact ? IsOutsideEdge(ac, ab, ao) : SameDirection(Vector3d.Cross(ab, abc), ao)) |
| | | 99 | | { |
| | 20 | 100 | | simplex[1] = b; |
| | 20 | 101 | | count = 2; |
| | 20 | 102 | | return UpdateLine(simplex, ref count, ref direction); |
| | | 103 | | } |
| | | 104 | | |
| | 775 | 105 | | if (exact) |
| | 193 | 106 | | abc = GetFaceDirection(ab, ac); |
| | 775 | 107 | | if (exact ? WideGeometry.GetTripleProductSign( |
| | 775 | 108 | | ao.X, ao.Y, ao.Z, ab.X, ab.Y, ab.Z, ac.X, ac.Y, ac.Z) > 0 |
| | 775 | 109 | | : SameDirection(abc, ao)) |
| | | 110 | | { |
| | 103 | 111 | | direction = abc; |
| | 103 | 112 | | return false; |
| | | 113 | | } |
| | | 114 | | |
| | 672 | 115 | | simplex[1] = c; |
| | 672 | 116 | | simplex[2] = b; |
| | 672 | 117 | | direction = -abc; |
| | 672 | 118 | | return false; |
| | | 119 | | } |
| | | 120 | | |
| | | 121 | | public static bool UpdateTetrahedron(Span<Vector3d> simplex, ref int count, ref Vector3d direction) |
| | | 122 | | { |
| | 854 | 123 | | Vector3d a = simplex[0]; |
| | 854 | 124 | | Vector3d b = simplex[1]; |
| | 854 | 125 | | Vector3d c = simplex[2]; |
| | 854 | 126 | | Vector3d d = simplex[3]; |
| | 854 | 127 | | bool exact = RequiresExactProducts(simplex, 4); |
| | | 128 | | |
| | 854 | 129 | | if (IsOutsideFace(a, b, c, d, exact, out Vector3d abc)) |
| | | 130 | | { |
| | 63 | 131 | | simplex[0] = a; |
| | 63 | 132 | | simplex[1] = b; |
| | 63 | 133 | | simplex[2] = c; |
| | 63 | 134 | | count = 3; |
| | 63 | 135 | | direction = abc; |
| | 63 | 136 | | return false; |
| | | 137 | | } |
| | | 138 | | |
| | 791 | 139 | | if (IsOutsideFace(a, c, d, b, exact, out Vector3d acd)) |
| | | 140 | | { |
| | 34 | 141 | | simplex[0] = a; |
| | 34 | 142 | | simplex[1] = c; |
| | 34 | 143 | | simplex[2] = d; |
| | 34 | 144 | | count = 3; |
| | 34 | 145 | | direction = acd; |
| | 34 | 146 | | return false; |
| | | 147 | | } |
| | | 148 | | |
| | 757 | 149 | | if (IsOutsideFace(a, d, b, c, exact, out Vector3d adb)) |
| | | 150 | | { |
| | 36 | 151 | | simplex[0] = a; |
| | 36 | 152 | | simplex[1] = d; |
| | 36 | 153 | | simplex[2] = b; |
| | 36 | 154 | | count = 3; |
| | 36 | 155 | | direction = adb; |
| | 36 | 156 | | return false; |
| | | 157 | | } |
| | | 158 | | |
| | 721 | 159 | | return true; |
| | | 160 | | } |
| | | 161 | | |
| | | 162 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 163 | | private static bool SameDirection(Vector3d first, Vector3d second) => |
| | 6877 | 164 | | WideGeometry.GetDifferenceDotProduct3D( |
| | 6877 | 165 | | first.X, Fixed64.Zero, first.Y, Fixed64.Zero, first.Z, Fixed64.Zero, |
| | 6877 | 166 | | second.X, Fixed64.Zero, second.Y, Fixed64.Zero, second.Z, Fixed64.Zero).Sign > 0; |
| | | 167 | | |
| | | 168 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 169 | | private static Vector3d TripleCross(Vector3d first, Vector3d second, Vector3d third) => |
| | 789 | 170 | | Vector3d.Cross(Vector3d.Cross(first, second), third); |
| | | 171 | | |
| | | 172 | | private static bool IsOutsideFace( |
| | | 173 | | Vector3d a, Vector3d b, Vector3d c, Vector3d opposite, |
| | | 174 | | bool exact, out Vector3d normal) |
| | | 175 | | { |
| | 2402 | 176 | | Vector3d ab = b - a; |
| | 2402 | 177 | | Vector3d ac = c - a; |
| | 2402 | 178 | | if (!exact) |
| | | 179 | | { |
| | 1866 | 180 | | normal = Vector3d.Cross(ab, ac); |
| | 1866 | 181 | | if (SameDirection(normal, opposite - a)) |
| | 8 | 182 | | normal = -normal; |
| | 1866 | 183 | | return SameDirection(normal, -a); |
| | | 184 | | } |
| | | 185 | | |
| | 536 | 186 | | Vector3d oppositeOffset = opposite - a; |
| | 536 | 187 | | Vector3d ao = -a; |
| | 536 | 188 | | bool reversed = WideGeometry.GetTripleProductSign( |
| | 536 | 189 | | oppositeOffset.X, oppositeOffset.Y, oppositeOffset.Z, |
| | 536 | 190 | | ab.X, ab.Y, ab.Z, ac.X, ac.Y, ac.Z) > 0; |
| | 536 | 191 | | int side = WideGeometry.GetTripleProductSign( |
| | 536 | 192 | | ao.X, ao.Y, ao.Z, ab.X, ab.Y, ab.Z, ac.X, ac.Y, ac.Z); |
| | 536 | 193 | | if ((reversed ? -side : side) <= 0) |
| | | 194 | | { |
| | 522 | 195 | | normal = default; |
| | 522 | 196 | | return false; |
| | | 197 | | } |
| | 14 | 198 | | normal = GetFaceDirection(ab, ac); |
| | 14 | 199 | | if (reversed) |
| | 2 | 200 | | normal = -normal; |
| | 14 | 201 | | return true; |
| | | 202 | | } |
| | | 203 | | |
| | | 204 | | public static bool HasSeparatingSupport(Vector3d point, Vector3d direction) |
| | | 205 | | { |
| | | 206 | | // This gate precedes simplex reduction: a new support can exceed its old bound. |
| | | 207 | | // 3 * 64 * 2^23 < 2^31 bounds the complete scalar dot, including partial sums. |
| | 3597 | 208 | | if (HasBoundedComponents(point, 64L << FixedMath.SHIFT_AMOUNT_I) |
| | 3597 | 209 | | && HasBoundedComponents(direction, (1L << 23) << FixedMath.SHIFT_AMOUNT_I)) |
| | | 210 | | { |
| | 2272 | 211 | | return Vector3d.Dot(point, direction) < -Fixed64.Epsilon; |
| | | 212 | | } |
| | 1325 | 213 | | Signed192 dot = WideGeometry.GetDifferenceDotProduct3D( |
| | 1325 | 214 | | point.X, Fixed64.Zero, point.Y, Fixed64.Zero, point.Z, Fixed64.Zero, |
| | 1325 | 215 | | direction.X, Fixed64.Zero, direction.Y, Fixed64.Zero, direction.Z, Fixed64.Zero); |
| | 1325 | 216 | | return WideArithmetic.AddSigned192( |
| | 1325 | 217 | | dot, Fixed64.GetExactRawProduct(Fixed64.Epsilon, Fixed64.One)).Sign < 0; |
| | | 218 | | } |
| | | 219 | | |
| | | 220 | | private static bool RequiresExactProducts(Span<Vector3d> simplex, int count) |
| | | 221 | | { |
| | 18146 | 222 | | for (int i = 0; i < count; i++) |
| | 7021 | 223 | | if (!HasBoundedComponents(simplex[i], ProductSafeVertexRaw)) |
| | 966 | 224 | | return true; |
| | 2052 | 225 | | return false; |
| | | 226 | | } |
| | | 227 | | |
| | | 228 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 229 | | private static bool HasBoundedComponents(Vector3d value, long limit) => |
| | 12903 | 230 | | value.X.m_rawValue >= -limit && value.X.m_rawValue <= limit |
| | 12903 | 231 | | && value.Y.m_rawValue >= -limit && value.Y.m_rawValue <= limit |
| | 12903 | 232 | | && value.Z.m_rawValue >= -limit && value.Z.m_rawValue <= limit; |
| | | 233 | | |
| | | 234 | | private static bool IsOutsideEdge(Vector3d firstEdge, Vector3d secondEdge, Vector3d ao) |
| | | 235 | | { |
| | | 236 | | // Both outward edge signs negate the plane-projection numerator. Test its dot directly: |
| | | 237 | | // |second|^2*(first.ao) - (first.second)*(second.ao); raw quartics fit Signed320. |
| | 438 | 238 | | Signed192 q = WideGeometry.GetDifferenceDotProduct3D( |
| | 438 | 239 | | secondEdge.X, Fixed64.Zero, secondEdge.Y, Fixed64.Zero, secondEdge.Z, Fixed64.Zero, |
| | 438 | 240 | | secondEdge.X, Fixed64.Zero, secondEdge.Y, Fixed64.Zero, secondEdge.Z, Fixed64.Zero); |
| | 438 | 241 | | Signed192 firstTowardOrigin = WideGeometry.GetDifferenceDotProduct3D( |
| | 438 | 242 | | firstEdge.X, Fixed64.Zero, firstEdge.Y, Fixed64.Zero, firstEdge.Z, Fixed64.Zero, |
| | 438 | 243 | | ao.X, Fixed64.Zero, ao.Y, Fixed64.Zero, ao.Z, Fixed64.Zero); |
| | 438 | 244 | | Signed192 projection = WideGeometry.GetDifferenceDotProduct3D( |
| | 438 | 245 | | firstEdge.X, Fixed64.Zero, firstEdge.Y, Fixed64.Zero, firstEdge.Z, Fixed64.Zero, |
| | 438 | 246 | | secondEdge.X, Fixed64.Zero, secondEdge.Y, Fixed64.Zero, secondEdge.Z, Fixed64.Zero); |
| | 438 | 247 | | Signed192 secondTowardOrigin = WideGeometry.GetDifferenceDotProduct3D( |
| | 438 | 248 | | secondEdge.X, Fixed64.Zero, secondEdge.Y, Fixed64.Zero, secondEdge.Z, Fixed64.Zero, |
| | 438 | 249 | | ao.X, Fixed64.Zero, ao.Y, Fixed64.Zero, ao.Z, Fixed64.Zero); |
| | 438 | 250 | | return WideArithmetic.SubtractSigned320( |
| | 438 | 251 | | WideArithmetic.MultiplySigned192(q, firstTowardOrigin), |
| | 438 | 252 | | WideArithmetic.MultiplySigned192(projection, secondTowardOrigin)).Sign < 0; |
| | | 253 | | } |
| | | 254 | | |
| | | 255 | | private static Vector3d GetFaceDirection(Vector3d ab, Vector3d ac) |
| | | 256 | | { |
| | 207 | 257 | | WideGeometry.GetDifferenceCrossProduct3D( |
| | 207 | 258 | | ab.X, Fixed64.Zero, ab.Y, Fixed64.Zero, ab.Z, Fixed64.Zero, |
| | 207 | 259 | | ac.X, Fixed64.Zero, ac.Y, Fixed64.Zero, ac.Z, Fixed64.Zero, |
| | 207 | 260 | | out Signed192 x, out Signed192 y, out Signed192 z); |
| | 207 | 261 | | return WideNormalization.GetNormalized(x, y, z); |
| | | 262 | | } |
| | | 263 | | |
| | | 264 | | private static Vector3d Perpendicular(Vector3d vector) |
| | | 265 | | { |
| | 227 | 266 | | Vector3d candidate = Vector3d.Cross(vector, Vector3d.Up); |
| | 227 | 267 | | if (candidate.MagnitudeSquared > Fixed64.Epsilon) |
| | 224 | 268 | | return candidate; |
| | | 269 | | |
| | 3 | 270 | | candidate = Vector3d.Cross(vector, Vector3d.Right); |
| | 3 | 271 | | return candidate.MagnitudeSquared > Fixed64.Epsilon ? candidate : Vector3d.Forward; |
| | | 272 | | } |
| | | 273 | | } |