| | | 1 | | //======================================================================= |
| | | 2 | | // FiniteSlabProjectionSweep.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 FixedMathSharp; |
| | | 9 | | using FixedMathSharp.Geometry; |
| | | 10 | | using Gravitas.Colliders; |
| | | 11 | | using Gravitas.CollisionHandling; |
| | | 12 | | using System; |
| | | 13 | | using System.Runtime.CompilerServices; |
| | | 14 | | |
| | | 15 | | namespace Gravitas.Queries; |
| | | 16 | | |
| | | 17 | | /// <summary> |
| | | 18 | | /// Sweeps an X/Z circle against the projection of curved 3D targets clipped to |
| | | 19 | | /// a finite Y slab. |
| | | 20 | | /// </summary> |
| | | 21 | | internal static partial class FiniteSlabProjectionSweep |
| | | 22 | | { |
| | | 23 | | private const int MaxGjkIterations = 32; |
| | | 24 | | private const int MaxConservativeAdvancementIterations = 32; |
| | 1 | 25 | | private static readonly Fixed64 DistanceTolerance = Fixed64.FromFraction(1, 1_048_576); |
| | 1 | 26 | | private static readonly Fixed64 SweepContactTolerance = DistanceTolerance; |
| | | 27 | | |
| | | 28 | | public static bool TrySweepCircleAgainstCapsule( |
| | | 29 | | Vector2d start, |
| | | 30 | | Vector2d end, |
| | | 31 | | Vector2d direction, |
| | | 32 | | Fixed64 length, |
| | | 33 | | Fixed64 radius, |
| | | 34 | | Fixed64 slabMinY, |
| | | 35 | | Fixed64 slabMaxY, |
| | | 36 | | LSCapsuleCollider capsule, |
| | | 37 | | out Fixed64 distance, |
| | | 38 | | int maxConservativeAdvancementIterations = MaxConservativeAdvancementIterations) |
| | | 39 | | { |
| | 92 | 40 | | var target = ProjectionTarget.CreateCapsule(capsule, slabMinY, slabMaxY); |
| | 92 | 41 | | return TrySweepCircle( |
| | 92 | 42 | | start, |
| | 92 | 43 | | end, |
| | 92 | 44 | | direction, |
| | 92 | 45 | | length, |
| | 92 | 46 | | radius, |
| | 92 | 47 | | target, |
| | 92 | 48 | | out distance, |
| | 92 | 49 | | maxConservativeAdvancementIterations); |
| | | 50 | | } |
| | | 51 | | |
| | | 52 | | public static bool TrySweepCircleAgainstCylinder( |
| | | 53 | | Vector2d start, |
| | | 54 | | Vector2d end, |
| | | 55 | | Vector2d direction, |
| | | 56 | | Fixed64 length, |
| | | 57 | | Fixed64 radius, |
| | | 58 | | Fixed64 slabMinY, |
| | | 59 | | Fixed64 slabMaxY, |
| | | 60 | | LSCylinderCollider cylinder, |
| | | 61 | | out Fixed64 distance, |
| | | 62 | | int maxConservativeAdvancementIterations = MaxConservativeAdvancementIterations) |
| | | 63 | | { |
| | 110 | 64 | | var target = ProjectionTarget.CreateCylinder(cylinder, slabMinY, slabMaxY); |
| | 110 | 65 | | return TrySweepCircle( |
| | 110 | 66 | | start, |
| | 110 | 67 | | end, |
| | 110 | 68 | | direction, |
| | 110 | 69 | | length, |
| | 110 | 70 | | radius, |
| | 110 | 71 | | target, |
| | 110 | 72 | | out distance, |
| | 110 | 73 | | maxConservativeAdvancementIterations); |
| | | 74 | | } |
| | | 75 | | |
| | | 76 | | public static bool TrySweepCircleAgainstCone( |
| | | 77 | | Vector2d start, |
| | | 78 | | Vector2d end, |
| | | 79 | | Vector2d direction, |
| | | 80 | | Fixed64 length, |
| | | 81 | | Fixed64 radius, |
| | | 82 | | Fixed64 slabMinY, |
| | | 83 | | Fixed64 slabMaxY, |
| | | 84 | | LSConeCollider cone, |
| | | 85 | | out Fixed64 distance, |
| | | 86 | | int maxConservativeAdvancementIterations = MaxConservativeAdvancementIterations) |
| | | 87 | | { |
| | 90 | 88 | | var target = ProjectionTarget.CreateCone(cone, slabMinY, slabMaxY); |
| | 90 | 89 | | return TrySweepCircle( |
| | 90 | 90 | | start, |
| | 90 | 91 | | end, |
| | 90 | 92 | | direction, |
| | 90 | 93 | | length, |
| | 90 | 94 | | radius, |
| | 90 | 95 | | target, |
| | 90 | 96 | | out distance, |
| | 90 | 97 | | maxConservativeAdvancementIterations); |
| | | 98 | | } |
| | | 99 | | |
| | | 100 | | private static bool TrySweepCircle( |
| | | 101 | | Vector2d start, |
| | | 102 | | Vector2d end, |
| | | 103 | | Vector2d direction, |
| | | 104 | | Fixed64 length, |
| | | 105 | | Fixed64 radius, |
| | | 106 | | ProjectionTarget target, |
| | | 107 | | out Fixed64 distance, |
| | | 108 | | int maxConservativeAdvancementIterations) |
| | | 109 | | { |
| | 292 | 110 | | distance = Fixed64.Zero; |
| | 292 | 111 | | if (!Vector2d.TryGetMagnitude(direction, out Fixed64 directionMagnitude) |
| | 292 | 112 | | || (directionMagnitude != Fixed64.Zero |
| | 292 | 113 | | && FixedMath.Abs(directionMagnitude - Fixed64.One) > Fixed64.Epsilon) |
| | 292 | 114 | | || maxConservativeAdvancementIterations <= 0 |
| | 292 | 115 | | || !target.TrySupport(Vector2d.Right, out Vector2d rightSupport)) |
| | 12 | 116 | | return false; |
| | | 117 | | |
| | 280 | 118 | | if (target.TryGetPlanarCircle(rightSupport, out Vector2d targetCenter, out Fixed64 targetRadius)) |
| | | 119 | | { |
| | 136 | 120 | | return new FixedSegment2d(start, end) |
| | 136 | 121 | | .TryGetCircleIntersectionDistanceInterval( |
| | 136 | 122 | | new FixedBoundCircle(targetCenter, targetRadius), |
| | 136 | 123 | | radius, |
| | 136 | 124 | | length, |
| | 136 | 125 | | out distance, |
| | 136 | 126 | | out _, |
| | 136 | 127 | | out _, |
| | 136 | 128 | | out _); |
| | | 129 | | } |
| | | 130 | | |
| | 144 | 131 | | Fixed64 travelDistance = Fixed64.Zero; |
| | 624 | 132 | | for (int i = 0; i < maxConservativeAdvancementIterations; i++) |
| | | 133 | | { |
| | 311 | 134 | | Vector2d point = GetSweepPoint(start, direction, travelDistance); |
| | 311 | 135 | | if (!TryComputeDistance(point, radius, target, out PlanarGjkResult result)) |
| | | 136 | | { |
| | 1 | 137 | | return false; |
| | | 138 | | } |
| | | 139 | | |
| | 310 | 140 | | if (result.Distance <= SweepContactTolerance) |
| | | 141 | | { |
| | 136 | 142 | | distance = travelDistance; |
| | 136 | 143 | | return true; |
| | | 144 | | } |
| | | 145 | | |
| | 174 | 146 | | Vector2d normal = result.Normal; |
| | 174 | 147 | | Fixed64 closingSpeed = -Vector2d.Dot(direction, normal); |
| | 174 | 148 | | if (closingSpeed <= Fixed64.Epsilon) |
| | 2 | 149 | | return false; |
| | | 150 | | |
| | 172 | 151 | | Fixed64 remainingDistance = length - travelDistance; |
| | 172 | 152 | | bool reachedEndpoint = !Fixed64.TryMultiplyDivide( |
| | 172 | 153 | | result.Distance, |
| | 172 | 154 | | Fixed64.One, |
| | 172 | 155 | | closingSpeed, |
| | 172 | 156 | | out Fixed64 stepDistance) |
| | 172 | 157 | | || stepDistance > remainingDistance; |
| | 172 | 158 | | if (reachedEndpoint) |
| | | 159 | | { |
| | 4 | 160 | | if (!TryComputeDistance(end, radius, target, out PlanarGjkResult endpoint)) |
| | | 161 | | { |
| | 1 | 162 | | return false; |
| | | 163 | | } |
| | | 164 | | |
| | 3 | 165 | | if (endpoint.Distance <= SweepContactTolerance) |
| | | 166 | | { |
| | 1 | 167 | | distance = length; |
| | 1 | 168 | | return true; |
| | | 169 | | } |
| | | 170 | | |
| | 2 | 171 | | return false; |
| | | 172 | | } |
| | | 173 | | |
| | 168 | 174 | | travelDistance += stepDistance; |
| | | 175 | | } |
| | | 176 | | |
| | 1 | 177 | | return false; |
| | | 178 | | } |
| | | 179 | | |
| | | 180 | | private static bool TryComputeDistance( |
| | | 181 | | Vector2d point, |
| | | 182 | | Fixed64 expansionRadius, |
| | | 183 | | ProjectionTarget target, |
| | | 184 | | out PlanarGjkResult result) |
| | | 185 | | { |
| | 315 | 186 | | Span<PlanarSupportPoint> simplex = stackalloc PlanarSupportPoint[3]; |
| | 315 | 187 | | int workingShift = GjkSimplexScale.SelectThreeTermShift( |
| | 315 | 188 | | point, |
| | 315 | 189 | | target.BoundsMin, |
| | 315 | 190 | | target.BoundsMax, |
| | 315 | 191 | | expansionRadius); |
| | 315 | 192 | | Fixed64 workingScale = GjkSimplexScale.GetCoordinateScale(workingShift); |
| | 315 | 193 | | Fixed64 workingScaleSqr = workingScale * workingScale; |
| | 315 | 194 | | Fixed64 workingDistanceTolerance = DistanceTolerance * workingScale; |
| | 315 | 195 | | Fixed64 workingSegmentDegeneracyToleranceSqr = Fixed64.Epsilon * workingScaleSqr; |
| | 315 | 196 | | Fixed64 workingAreaTolerance = DistanceTolerance * workingScaleSqr; |
| | 315 | 197 | | Fixed64 workingCrossSignTolerance = Fixed64.Epsilon * workingScaleSqr; |
| | 315 | 198 | | int simplexCount = 0; |
| | 315 | 199 | | Vector2d direction = GjkSimplexScale.CreateWorkingDifference(target.Center, point, workingShift); |
| | 315 | 200 | | if (direction == Vector2d.Zero) |
| | 1 | 201 | | direction = Vector2d.Right; |
| | | 202 | | |
| | 315 | 203 | | bool hasPreviousDistance = false; |
| | 315 | 204 | | Fixed64 previousDistance = Fixed64.Zero; |
| | 315 | 205 | | ClosestPlanarSimplexResult closest = default; |
| | 315 | 206 | | bool distanceIsRepresentable = false; |
| | 315 | 207 | | Fixed64 workingDistance = Fixed64.MaxValue; |
| | | 208 | | |
| | 1914 | 209 | | for (int i = 0; i < MaxGjkIterations; i++) |
| | | 210 | | { |
| | 957 | 211 | | if (!TryCreateSupportPoint( |
| | 957 | 212 | | point, |
| | 957 | 213 | | expansionRadius, |
| | 957 | 214 | | target, |
| | 957 | 215 | | direction, |
| | 957 | 216 | | workingShift, |
| | 957 | 217 | | out PlanarSupportPoint support)) |
| | | 218 | | { |
| | 2 | 219 | | result = default; |
| | 2 | 220 | | return false; |
| | | 221 | | } |
| | | 222 | | |
| | 955 | 223 | | if (ContainsSupportPoint(simplex, simplexCount, support.Point)) |
| | | 224 | | break; |
| | | 225 | | |
| | 824 | 226 | | simplex[simplexCount++] = support; |
| | 824 | 227 | | closest = SolveClosestSimplex( |
| | 824 | 228 | | simplex, |
| | 824 | 229 | | ref simplexCount, |
| | 824 | 230 | | workingSegmentDegeneracyToleranceSqr, |
| | 824 | 231 | | workingAreaTolerance, |
| | 824 | 232 | | workingCrossSignTolerance); |
| | 824 | 233 | | distanceIsRepresentable = Vector2d.TryGetMagnitude(closest.Point, out workingDistance); |
| | 824 | 234 | | if (closest.Intersects |
| | 824 | 235 | | || (distanceIsRepresentable && workingDistance <= workingDistanceTolerance)) |
| | | 236 | | { |
| | 137 | 237 | | result = PlanarGjkResult.Intersection; |
| | 137 | 238 | | return true; |
| | | 239 | | } |
| | | 240 | | |
| | 687 | 241 | | if (hasPreviousDistance |
| | 687 | 242 | | && distanceIsRepresentable |
| | 687 | 243 | | && previousDistance - workingDistance <= Fixed64.Epsilon) |
| | | 244 | | { |
| | | 245 | | break; |
| | | 246 | | } |
| | | 247 | | |
| | 642 | 248 | | hasPreviousDistance = distanceIsRepresentable; |
| | 642 | 249 | | previousDistance = workingDistance; |
| | 642 | 250 | | direction = -closest.Point; |
| | | 251 | | } |
| | | 252 | | |
| | 176 | 253 | | Fixed64 distance = GjkSimplexScale.RestoreDistance(workingDistance, workingShift); |
| | 176 | 254 | | Vector2d normal = closest.Point.Normalized; |
| | 176 | 255 | | result = new PlanarGjkResult(distance, normal); |
| | 176 | 256 | | return true; |
| | | 257 | | } |
| | | 258 | | |
| | | 259 | | private static bool TryCreateSupportPoint( |
| | | 260 | | Vector2d point, |
| | | 261 | | Fixed64 expansionRadius, |
| | | 262 | | ProjectionTarget target, |
| | | 263 | | Vector2d direction, |
| | | 264 | | int workingShift, |
| | | 265 | | out PlanarSupportPoint support) |
| | | 266 | | { |
| | 957 | 267 | | Vector2d supportDirection = direction.Normalized; |
| | 957 | 268 | | Vector2d targetDirection = -supportDirection; |
| | 957 | 269 | | if (!target.TrySupport(targetDirection, out Vector2d targetSupport)) |
| | | 270 | | { |
| | 2 | 271 | | support = default; |
| | 2 | 272 | | return false; |
| | | 273 | | } |
| | | 274 | | |
| | 955 | 275 | | Vector2d expansion = targetDirection * expansionRadius; |
| | 955 | 276 | | support = new PlanarSupportPoint( |
| | 955 | 277 | | GjkSimplexScale.CreateWorkingDifference(point, targetSupport, expansion, workingShift)); |
| | 955 | 278 | | return true; |
| | | 279 | | } |
| | | 280 | | |
| | | 281 | | // The authored endpoint bounds every monotonic intermediate component. |
| | | 282 | | private static Vector2d GetSweepPoint( |
| | | 283 | | Vector2d start, |
| | | 284 | | Vector2d direction, |
| | | 285 | | Fixed64 distance) => |
| | 311 | 286 | | new( |
| | 311 | 287 | | Fixed64.MultiplyAdd(direction.X, distance, start.X), |
| | 311 | 288 | | Fixed64.MultiplyAdd(direction.Y, distance, start.Y)); |
| | | 289 | | |
| | | 290 | | private static ClosestPlanarSimplexResult SolveClosestSimplex( |
| | | 291 | | Span<PlanarSupportPoint> simplex, |
| | | 292 | | ref int count, |
| | | 293 | | Fixed64 segmentDegeneracyToleranceSqr, |
| | | 294 | | Fixed64 areaTolerance, |
| | | 295 | | Fixed64 crossSignTolerance) |
| | | 296 | | { |
| | 824 | 297 | | if (count == 1) |
| | 313 | 298 | | return ClosestPlanarSimplexResult.FromPoint(simplex[0].Point); |
| | | 299 | | |
| | 511 | 300 | | if (count == 2) |
| | 112 | 301 | | return ReduceSegment(simplex, ref count, segmentDegeneracyToleranceSqr); |
| | | 302 | | |
| | 399 | 303 | | return ReduceTriangle( |
| | 399 | 304 | | simplex, |
| | 399 | 305 | | ref count, |
| | 399 | 306 | | segmentDegeneracyToleranceSqr, |
| | 399 | 307 | | areaTolerance, |
| | 399 | 308 | | crossSignTolerance); |
| | | 309 | | } |
| | | 310 | | |
| | | 311 | | private static ClosestPlanarSimplexResult ReduceSegment( |
| | | 312 | | Span<PlanarSupportPoint> simplex, |
| | | 313 | | ref int count, |
| | | 314 | | Fixed64 segmentDegeneracyToleranceSqr) |
| | | 315 | | { |
| | 1294 | 316 | | Vector2d a = simplex[0].Point; |
| | 1294 | 317 | | Vector2d b = simplex[1].Point; |
| | 1294 | 318 | | Span<Vector2d> scaled = stackalloc Vector2d[2]; |
| | 1294 | 319 | | scaled[0] = a; |
| | 1294 | 320 | | scaled[1] = b; |
| | 1294 | 321 | | Fixed64 productScale = GjkSimplexScale.ScaleForProducts(scaled); |
| | 1294 | 322 | | Vector2d scaledA = scaled[0]; |
| | 1294 | 323 | | Vector2d ab = scaled[1] - scaledA; |
| | 1294 | 324 | | Fixed64 denominator = ab.MagnitudeSquared; |
| | 1294 | 325 | | Fixed64 productScaleSqr = productScale * productScale; |
| | 1294 | 326 | | Fixed64 denominatorTolerance = segmentDegeneracyToleranceSqr * productScaleSqr; |
| | 1294 | 327 | | Fixed64 t = denominator <= denominatorTolerance |
| | 1294 | 328 | | ? Fixed64.Zero |
| | 1294 | 329 | | : FixedMath.Clamp(-Vector2d.Dot(scaledA, ab) / denominator, Fixed64.Zero, Fixed64.One); |
| | | 330 | | |
| | 1294 | 331 | | if (t <= Fixed64.Epsilon) |
| | | 332 | | { |
| | 199 | 333 | | count = 1; |
| | 199 | 334 | | return ClosestPlanarSimplexResult.FromPoint(a); |
| | | 335 | | } |
| | | 336 | | |
| | 1095 | 337 | | if (t >= Fixed64.One - Fixed64.Epsilon) |
| | | 338 | | { |
| | 288 | 339 | | simplex[0] = simplex[1]; |
| | 288 | 340 | | count = 1; |
| | 288 | 341 | | return ClosestPlanarSimplexResult.FromPoint(b); |
| | | 342 | | } |
| | | 343 | | |
| | 807 | 344 | | count = 2; |
| | 807 | 345 | | return ClosestPlanarSimplexResult.FromPoint(a * (Fixed64.One - t) + b * t); |
| | | 346 | | } |
| | | 347 | | |
| | | 348 | | private static ClosestPlanarSimplexResult ReduceTriangle( |
| | | 349 | | Span<PlanarSupportPoint> simplex, |
| | | 350 | | ref int count, |
| | | 351 | | Fixed64 segmentDegeneracyToleranceSqr, |
| | | 352 | | Fixed64 areaTolerance, |
| | | 353 | | Fixed64 crossSignTolerance) |
| | | 354 | | { |
| | 399 | 355 | | Vector2d a = simplex[0].Point; |
| | 399 | 356 | | Vector2d b = simplex[1].Point; |
| | 399 | 357 | | Vector2d c = simplex[2].Point; |
| | 399 | 358 | | if (IsOriginInsideTriangle(a, b, c, areaTolerance, crossSignTolerance)) |
| | | 359 | | { |
| | 5 | 360 | | count = 3; |
| | 5 | 361 | | return ClosestPlanarSimplexResult.Intersection; |
| | | 362 | | } |
| | | 363 | | |
| | 394 | 364 | | bool hasBest = false; |
| | 394 | 365 | | int bestFirst = 0; |
| | 394 | 366 | | int bestSecond = 1; |
| | 394 | 367 | | ClosestPlanarSimplexResult best = default; |
| | 394 | 368 | | EvaluateTriangleEdge(simplex, 0, 1, segmentDegeneracyToleranceSqr, ref best, ref hasBest, ref bestFirst, ref bes |
| | 394 | 369 | | EvaluateTriangleEdge(simplex, 1, 2, segmentDegeneracyToleranceSqr, ref best, ref hasBest, ref bestFirst, ref bes |
| | 394 | 370 | | EvaluateTriangleEdge(simplex, 2, 0, segmentDegeneracyToleranceSqr, ref best, ref hasBest, ref bestFirst, ref bes |
| | | 371 | | |
| | 394 | 372 | | PlanarSupportPoint first = simplex[bestFirst]; |
| | 394 | 373 | | PlanarSupportPoint second = simplex[bestSecond]; |
| | 394 | 374 | | simplex[0] = first; |
| | 394 | 375 | | simplex[1] = second; |
| | 394 | 376 | | count = 2; |
| | 394 | 377 | | return best; |
| | | 378 | | } |
| | | 379 | | |
| | | 380 | | private static void EvaluateTriangleEdge( |
| | | 381 | | Span<PlanarSupportPoint> simplex, |
| | | 382 | | int first, |
| | | 383 | | int second, |
| | | 384 | | Fixed64 segmentDegeneracyToleranceSqr, |
| | | 385 | | ref ClosestPlanarSimplexResult best, |
| | | 386 | | ref bool hasBest, |
| | | 387 | | ref int bestFirst, |
| | | 388 | | ref int bestSecond) |
| | | 389 | | { |
| | 1182 | 390 | | Span<PlanarSupportPoint> edge = stackalloc PlanarSupportPoint[2]; |
| | 1182 | 391 | | edge[0] = simplex[first]; |
| | 1182 | 392 | | edge[1] = simplex[second]; |
| | 1182 | 393 | | int edgeCount = 2; |
| | 1182 | 394 | | ClosestPlanarSimplexResult candidate = ReduceSegment(edge, ref edgeCount, segmentDegeneracyToleranceSqr); |
| | 1182 | 395 | | if (hasBest && !IsCloser( |
| | 1182 | 396 | | candidate.DistanceSqr, |
| | 1182 | 397 | | candidate.Point, |
| | 1182 | 398 | | best.DistanceSqr, |
| | 1182 | 399 | | best.Point)) |
| | 380 | 400 | | return; |
| | | 401 | | |
| | 802 | 402 | | best = candidate; |
| | 802 | 403 | | hasBest = true; |
| | 802 | 404 | | bestFirst = first; |
| | 802 | 405 | | bestSecond = second; |
| | 802 | 406 | | } |
| | | 407 | | |
| | | 408 | | internal static bool IsCloser( |
| | | 409 | | Fixed64 candidateDistanceSqr, |
| | | 410 | | Vector2d candidatePoint, |
| | | 411 | | Fixed64 bestDistanceSqr, |
| | | 412 | | Vector2d bestPoint) |
| | | 413 | | { |
| | 798 | 414 | | if (candidateDistanceSqr != bestDistanceSqr || candidateDistanceSqr != Fixed64.MaxValue) |
| | 791 | 415 | | return candidateDistanceSqr < bestDistanceSqr; |
| | | 416 | | |
| | 7 | 417 | | return Vector2d.CompareMagnitudeSquared(candidatePoint, bestPoint) < 0; |
| | | 418 | | } |
| | | 419 | | |
| | | 420 | | private static bool IsOriginInsideTriangle( |
| | | 421 | | Vector2d a, |
| | | 422 | | Vector2d b, |
| | | 423 | | Vector2d c, |
| | | 424 | | Fixed64 workingAreaTolerance, |
| | | 425 | | Fixed64 workingCrossSignTolerance) |
| | | 426 | | { |
| | 399 | 427 | | Span<Vector2d> scaled = stackalloc Vector2d[3]; |
| | 399 | 428 | | scaled[0] = a; |
| | 399 | 429 | | scaled[1] = b; |
| | 399 | 430 | | scaled[2] = c; |
| | 399 | 431 | | Fixed64 productScale = GjkSimplexScale.ScaleForProducts(scaled); |
| | 399 | 432 | | a = scaled[0]; |
| | 399 | 433 | | b = scaled[1]; |
| | 399 | 434 | | c = scaled[2]; |
| | | 435 | | |
| | 399 | 436 | | Fixed64 productScaleSqr = productScale * productScale; |
| | 399 | 437 | | Fixed64 areaTolerance = workingAreaTolerance * productScaleSqr; |
| | 399 | 438 | | if (Cross(a, b, c).Abs() <= areaTolerance) |
| | 119 | 439 | | return false; |
| | | 440 | | |
| | 280 | 441 | | Fixed64 ab = Cross(a, b, Vector2d.Zero); |
| | 280 | 442 | | Fixed64 bc = Cross(b, c, Vector2d.Zero); |
| | 280 | 443 | | Fixed64 ca = Cross(c, a, Vector2d.Zero); |
| | 280 | 444 | | Fixed64 signTolerance = workingCrossSignTolerance * productScaleSqr; |
| | 280 | 445 | | bool hasPositive = ab > signTolerance || bc > signTolerance || ca > signTolerance; |
| | 280 | 446 | | bool hasNegative = ab < -signTolerance || bc < -signTolerance || ca < -signTolerance; |
| | 280 | 447 | | return !(hasPositive && hasNegative); |
| | | 448 | | } |
| | | 449 | | |
| | | 450 | | private static bool ContainsSupportPoint( |
| | | 451 | | Span<PlanarSupportPoint> simplex, |
| | | 452 | | int count, |
| | | 453 | | Vector2d point) |
| | | 454 | | { |
| | 3730 | 455 | | for (int i = 0; i < count; i++) |
| | | 456 | | { |
| | 1041 | 457 | | if (Vector2d.TryGetDistance(simplex[i].Point, point, out Fixed64 distance) |
| | 1041 | 458 | | && distance <= Fixed64.Epsilon) |
| | 131 | 459 | | return true; |
| | | 460 | | } |
| | | 461 | | |
| | 824 | 462 | | return false; |
| | | 463 | | } |
| | | 464 | | |
| | | 465 | | private readonly struct ProjectionTarget |
| | | 466 | | { |
| | | 467 | | private readonly LSCapsuleCollider? _capsule; |
| | | 468 | | private readonly LSCylinderCollider? _cylinder; |
| | | 469 | | private readonly LSConeCollider? _cone; |
| | | 470 | | private readonly FixedRange _slabY; |
| | | 471 | | private readonly Vector2d _center; |
| | | 472 | | private readonly Vector3d _axis; |
| | | 473 | | |
| | | 474 | | private ProjectionTarget( |
| | | 475 | | LSCapsuleCollider? capsule, |
| | | 476 | | LSCylinderCollider? cylinder, |
| | | 477 | | LSConeCollider? cone, |
| | | 478 | | Fixed64 slabMinY, |
| | | 479 | | Fixed64 slabMaxY, |
| | | 480 | | Vector2d center, |
| | | 481 | | Vector3d axis) |
| | | 482 | | { |
| | 292 | 483 | | _capsule = capsule; |
| | 292 | 484 | | _cylinder = cylinder; |
| | 292 | 485 | | _cone = cone; |
| | 292 | 486 | | _slabY = new FixedRange(slabMinY, slabMaxY); |
| | 292 | 487 | | _center = center; |
| | 292 | 488 | | _axis = axis; |
| | 292 | 489 | | } |
| | | 490 | | |
| | 315 | 491 | | public Vector2d Center => _center; |
| | | 492 | | |
| | 315 | 493 | | public Vector2d BoundsMin => new(TargetBoundsMin.X, TargetBoundsMin.Z); |
| | | 494 | | |
| | 315 | 495 | | public Vector2d BoundsMax => new(TargetBoundsMax.X, TargetBoundsMax.Z); |
| | | 496 | | |
| | 630 | 497 | | private Vector3d TargetBoundsMin => _capsule?.BoundsMin ?? _cylinder?.BoundsMin ?? _cone!.BoundsMin; |
| | | 498 | | |
| | 630 | 499 | | private Vector3d TargetBoundsMax => _capsule?.BoundsMax ?? _cylinder?.BoundsMax ?? _cone!.BoundsMax; |
| | | 500 | | |
| | | 501 | | public static ProjectionTarget CreateCapsule(LSCapsuleCollider capsule, Fixed64 slabMinY, Fixed64 slabMaxY) => |
| | 92 | 502 | | new( |
| | 92 | 503 | | capsule, |
| | 92 | 504 | | null, |
| | 92 | 505 | | null, |
| | 92 | 506 | | slabMinY, |
| | 92 | 507 | | slabMaxY, |
| | 92 | 508 | | new Vector2d(capsule.Center.X, capsule.Center.Z), |
| | 92 | 509 | | GetRigidUpAxis(capsule.Rotation)); |
| | | 510 | | |
| | | 511 | | public static ProjectionTarget CreateCylinder(LSCylinderCollider cylinder, Fixed64 slabMinY, Fixed64 slabMaxY) = |
| | 110 | 512 | | new( |
| | 110 | 513 | | null, |
| | 110 | 514 | | cylinder, |
| | 110 | 515 | | null, |
| | 110 | 516 | | slabMinY, |
| | 110 | 517 | | slabMaxY, |
| | 110 | 518 | | new Vector2d(cylinder.Center.X, cylinder.Center.Z), |
| | 110 | 519 | | GetRigidUpAxis(cylinder.Rotation)); |
| | | 520 | | |
| | | 521 | | public static ProjectionTarget CreateCone(LSConeCollider cone, Fixed64 slabMinY, Fixed64 slabMaxY) => |
| | 90 | 522 | | new( |
| | 90 | 523 | | null, |
| | 90 | 524 | | null, |
| | 90 | 525 | | cone, |
| | 90 | 526 | | slabMinY, |
| | 90 | 527 | | slabMaxY, |
| | 90 | 528 | | new Vector2d(cone.Center.X, cone.Center.Z), |
| | 90 | 529 | | GetRigidUpAxis(cone.Rotation)); |
| | | 530 | | |
| | | 531 | | public bool TrySupport(Vector2d direction, out Vector2d support) |
| | | 532 | | { |
| | 1246 | 533 | | Vector2d normal = direction.Normalized; |
| | 1246 | 534 | | if (_capsule != null) |
| | | 535 | | { |
| | 467 | 536 | | return FixedSlabProjection.TryGetCapsuleSupport( |
| | 467 | 537 | | _capsule.Center, |
| | 467 | 538 | | _axis, |
| | 467 | 539 | | _capsule.AxisLength, |
| | 467 | 540 | | _capsule.ScaledRadius, |
| | 467 | 541 | | _slabY, |
| | 467 | 542 | | normal, |
| | 467 | 543 | | out support); |
| | | 544 | | } |
| | | 545 | | |
| | 779 | 546 | | if (_cylinder != null) |
| | | 547 | | { |
| | 459 | 548 | | return FixedSlabProjection.TryGetCylinderSupport( |
| | 459 | 549 | | _cylinder.Center, |
| | 459 | 550 | | _axis, |
| | 459 | 551 | | _cylinder.Height, |
| | 459 | 552 | | _cylinder.ScaledRadius, |
| | 459 | 553 | | _slabY, |
| | 459 | 554 | | normal, |
| | 459 | 555 | | out support); |
| | | 556 | | } |
| | | 557 | | |
| | 320 | 558 | | return FixedSlabProjection.TryGetConeSupport( |
| | 320 | 559 | | _cone!.Center, |
| | 320 | 560 | | _axis, |
| | 320 | 561 | | _cone.Height, |
| | 320 | 562 | | _cone.ScaledRadius, |
| | 320 | 563 | | _slabY, |
| | 320 | 564 | | normal, |
| | 320 | 565 | | out support); |
| | | 566 | | } |
| | | 567 | | |
| | | 568 | | public bool TryGetPlanarCircle( |
| | | 569 | | Vector2d rightSupport, |
| | | 570 | | out Vector2d center, |
| | | 571 | | out Fixed64 radius) |
| | | 572 | | { |
| | 280 | 573 | | if ((_axis.X != Fixed64.Zero) | (_axis.Z != Fixed64.Zero)) |
| | | 574 | | { |
| | 144 | 575 | | center = default; |
| | 144 | 576 | | radius = default; |
| | 144 | 577 | | return false; |
| | | 578 | | } |
| | | 579 | | |
| | 136 | 580 | | center = _center; |
| | 136 | 581 | | radius = rightSupport.X - _center.X; |
| | 136 | 582 | | return true; |
| | | 583 | | } |
| | | 584 | | |
| | | 585 | | } |
| | | 586 | | |
| | | 587 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 588 | | private static Vector3d GetRigidUpAxis(FixedQuaternion rotation) => |
| | 292 | 589 | | (rotation * Vector3d.Up).Normalized; |
| | | 590 | | |
| | | 591 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 592 | | private static Fixed64 Cross(Vector2d origin, Vector2d first, Vector2d second) => |
| | 1239 | 593 | | (first.X - origin.X) * (second.Y - origin.Y) - (first.Y - origin.Y) * (second.X - origin.X); |
| | | 594 | | |
| | | 595 | | } |