| | | 1 | | //======================================================================= |
| | | 2 | | // ConvexSweepQueryWorker.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 SwiftCollections; |
| | | 13 | | using SwiftCollections.Query; |
| | | 14 | | using System; |
| | | 15 | | using System.Runtime.CompilerServices; |
| | | 16 | | |
| | | 17 | | namespace Gravitas.Queries; |
| | | 18 | | |
| | | 19 | | /// <summary> |
| | | 20 | | /// Performs deterministic translational convex-source sweeps against 3D query |
| | | 21 | | /// targets using support-mapped conservative advancement. |
| | | 22 | | /// </summary> |
| | | 23 | | internal sealed partial class ConvexSweepQueryWorker |
| | | 24 | | { |
| | | 25 | | private const int MaxGjkIterations = 32; |
| | | 26 | | private const int MaxConservativeAdvancementIterations = 32; |
| | 1 | 27 | | private static readonly Fixed64 DistanceTolerance = |
| | 1 | 28 | | Fixed64.FromFraction(1, 1_048_576); |
| | 1 | 29 | | internal static readonly Fixed64 ContactTolerance = Fixed64.FromFraction(1, 4096); |
| | 1 | 30 | | private static readonly SweepTriangleCandidateComparer SweepTriangleComparer = new(); |
| | 1 | 31 | | private static readonly FixedPointAnchor ZeroAnchor = |
| | 1 | 32 | | new( |
| | 1 | 33 | | Vector3d.Zero, |
| | 1 | 34 | | FixedQuaternion.Identity, |
| | 1 | 35 | | Vector3d.Zero); |
| | | 36 | | |
| | 20399 | 37 | | private readonly SupportPoint[] _simplex = new SupportPoint[4]; |
| | 20399 | 38 | | private readonly SwiftList<int> _triangleCandidates = new(16); |
| | 20399 | 39 | | private readonly SwiftList<SweepTriangleCandidate> _sweepTriangleCandidates = new(16); |
| | | 40 | | private readonly int _maxConservativeAdvancementIterations; |
| | | 41 | | |
| | | 42 | | private LSCollider? _source; |
| | | 43 | | private ConvexShape _sourceShape; |
| | | 44 | | private bool _hasSource; |
| | | 45 | | private Vector3d _displacement; |
| | | 46 | | private Vector3d _outputDirection; |
| | | 47 | | private Vector3d _sweptSourceBoundsMin; |
| | | 48 | | private Vector3d _sweptSourceBoundsMax; |
| | | 49 | | private FixedPointAnchor _displacementAnchor; |
| | | 50 | | private FixedSegment _chord; |
| | | 51 | | private Fixed64 _length; |
| | | 52 | | |
| | | 53 | | internal int LastMeshTriangleCandidateCount { get; private set; } |
| | | 54 | | |
| | 20399 | 55 | | internal ConvexSweepQueryWorker( |
| | 20399 | 56 | | int maxConservativeAdvancementIterations = MaxConservativeAdvancementIterations) => |
| | 20399 | 57 | | _maxConservativeAdvancementIterations = maxConservativeAdvancementIterations; |
| | | 58 | | |
| | | 59 | | public void PrepareConvexMeshSource(LSMeshCollider source, Vector3d displacement) |
| | | 60 | | { |
| | 23 | 61 | | SwiftThrowHelper.ThrowIfNull(source, nameof(source)); |
| | 23 | 62 | | ThrowIfConcaveSource(source); |
| | 22 | 63 | | Prepare(source, displacement); |
| | 22 | 64 | | } |
| | | 65 | | |
| | | 66 | | public void PrepareCompoundSource(LSCompoundCollider source, Vector3d displacement) |
| | | 67 | | { |
| | 15 | 68 | | SwiftThrowHelper.ThrowIfNull(source, nameof(source)); |
| | 15 | 69 | | Prepare(source, displacement); |
| | 15 | 70 | | } |
| | | 71 | | |
| | | 72 | | public void PreparePrimitiveSource(LSCollider source, Vector3d displacement) |
| | | 73 | | { |
| | 148 | 74 | | SwiftThrowHelper.ThrowIfNull(source, nameof(source)); |
| | 148 | 75 | | if (!ConvexColliderSupport.IsSupported(source)) |
| | 1 | 76 | | throw new NotSupportedException( |
| | 1 | 77 | | $"Convex swept queries do not support {source.GetType().Name} sources."); |
| | | 78 | | |
| | 147 | 79 | | Prepare(source, displacement); |
| | 147 | 80 | | } |
| | | 81 | | |
| | | 82 | | public void PrepareCircleSlabSource(Vector3d center, Fixed64 radius, Fixed64 halfHeight, Vector3d displacement) |
| | | 83 | | { |
| | 18 | 84 | | SwiftThrowHelper.ThrowIfArgument(radius <= Fixed64.Zero, nameof(radius), "Circle-slab sweep radius must be great |
| | 17 | 85 | | SwiftThrowHelper.ThrowIfArgument(halfHeight <= Fixed64.Zero, nameof(halfHeight), "Circle-slab sweep half-height |
| | 16 | 86 | | _source = null; |
| | 16 | 87 | | Prepare(ConvexShape.CreateCircleSlab(center, radius, halfHeight), displacement); |
| | 16 | 88 | | } |
| | | 89 | | |
| | | 90 | | internal void PrepareSphereSource( |
| | | 91 | | Vector3d center, |
| | | 92 | | Fixed64 radius, |
| | | 93 | | Vector3d displacement) |
| | | 94 | | { |
| | 14467 | 95 | | SwiftThrowHelper.ThrowIfArgument( |
| | 14467 | 96 | | radius < Fixed64.Zero, |
| | 14467 | 97 | | nameof(radius), |
| | 14467 | 98 | | "Sphere sweep radius cannot be negative."); |
| | 14467 | 99 | | _source = null; |
| | 14467 | 100 | | Prepare(ConvexShape.CreateSphere(center, radius), displacement); |
| | 14467 | 101 | | } |
| | | 102 | | |
| | | 103 | | public bool TrySweepPreparedSource(LSCollider target, out Physics3DHit hit) |
| | | 104 | | { |
| | 196 | 105 | | LastMeshTriangleCandidateCount = 0; |
| | 196 | 106 | | hit = default; |
| | 196 | 107 | | if (!_hasSource |
| | 196 | 108 | | || _length <= Fixed64.Epsilon |
| | 196 | 109 | | || !SweepBoundsUtility.OverlapsInclusive(_sweptSourceBoundsMin, _sweptSourceBoundsMax, target.BoundsMin, tar |
| | | 110 | | { |
| | 40 | 111 | | return false; |
| | | 112 | | } |
| | | 113 | | |
| | 156 | 114 | | if (_source is LSCompoundCollider compound) |
| | 12 | 115 | | return TrySweepCompoundSource(compound, target, out hit); |
| | | 116 | | |
| | 144 | 117 | | return TrySweepSourceShape( |
| | 144 | 118 | | _sourceShape, |
| | 144 | 119 | | target, |
| | 144 | 120 | | out hit, |
| | 144 | 121 | | out _); |
| | | 122 | | } |
| | | 123 | | |
| | | 124 | | private void Prepare(LSCollider source, Vector3d displacement) |
| | | 125 | | { |
| | 184 | 126 | | _source = source; |
| | 184 | 127 | | Prepare(CreateColliderShape(source, Vector3d.Zero), displacement); |
| | 184 | 128 | | } |
| | | 129 | | |
| | | 130 | | private void Prepare(ConvexShape sourceShape, Vector3d displacement) |
| | | 131 | | { |
| | 14667 | 132 | | _sourceShape = sourceShape; |
| | 14667 | 133 | | _displacement = displacement; |
| | 14667 | 134 | | _displacementAnchor = new FixedPointAnchor( |
| | 14667 | 135 | | Vector3d.Zero, |
| | 14667 | 136 | | FixedQuaternion.Identity, |
| | 14667 | 137 | | displacement); |
| | 14667 | 138 | | _chord = new FixedSegment(Vector3d.Zero, displacement); |
| | 14667 | 139 | | _hasSource = |
| | 14667 | 140 | | Vector3d.TryGetMagnitude(displacement, out _length) |
| | 14667 | 141 | | && sourceShape.CanTranslateCenter(displacement); |
| | 14667 | 142 | | if (!_hasSource) |
| | | 143 | | { |
| | 5 | 144 | | _outputDirection = Vector3d.Zero; |
| | 5 | 145 | | return; |
| | | 146 | | } |
| | | 147 | | |
| | 14662 | 148 | | _outputDirection = |
| | 14662 | 149 | | _length <= Fixed64.Epsilon |
| | 14662 | 150 | | ? Vector3d.Zero |
| | 14662 | 151 | | : displacement.Normalized; |
| | 14662 | 152 | | sourceShape.GetSourceBounds(out Vector3d sourceMin, out Vector3d sourceMax); |
| | | 153 | | // Bounds are broad-phase clips, not canonical pose coordinates. A |
| | | 154 | | // valid center may have support beyond a scalar face. |
| | 14662 | 155 | | SweepBoundsUtility.CreateSweptBounds( |
| | 14662 | 156 | | sourceMin, |
| | 14662 | 157 | | sourceMax, |
| | 14662 | 158 | | displacement, |
| | 14662 | 159 | | ContactTolerance, |
| | 14662 | 160 | | out _sweptSourceBoundsMin, |
| | 14662 | 161 | | out _sweptSourceBoundsMax); |
| | 14662 | 162 | | } |
| | | 163 | | |
| | | 164 | | private bool TrySweepCompoundSource(LSCompoundCollider source, LSCollider target, out Physics3DHit hit) |
| | | 165 | | { |
| | 12 | 166 | | hit = default; |
| | 12 | 167 | | bool found = false; |
| | 12 | 168 | | Fixed64 closestNumerator = Fixed64.MaxValue; |
| | 12 | 169 | | int closestPartIndex = int.MaxValue; |
| | | 170 | | |
| | 62 | 171 | | for (int i = 0; i < source.PartCount; i++) |
| | | 172 | | { |
| | 19 | 173 | | LSCollider part = source.GetPartCollider(i); |
| | 19 | 174 | | if (!TrySweepSourceShape( |
| | 19 | 175 | | CreateColliderShape(part, Vector3d.Zero), |
| | 19 | 176 | | target, |
| | 19 | 177 | | out Physics3DHit candidate, |
| | 19 | 178 | | out Fixed64 candidateNumerator) |
| | 19 | 179 | | || !ComesBeforeReducerCandidate( |
| | 19 | 180 | | candidateNumerator, |
| | 19 | 181 | | i, |
| | 19 | 182 | | found, |
| | 19 | 183 | | closestNumerator, |
| | 19 | 184 | | closestPartIndex)) |
| | | 185 | | { |
| | | 186 | | continue; |
| | | 187 | | } |
| | | 188 | | |
| | 13 | 189 | | hit = candidate; |
| | 13 | 190 | | closestNumerator = candidateNumerator; |
| | 13 | 191 | | closestPartIndex = i; |
| | 13 | 192 | | found = true; |
| | | 193 | | } |
| | | 194 | | |
| | 12 | 195 | | return found; |
| | | 196 | | } |
| | | 197 | | |
| | | 198 | | private bool TrySweepSourceShape( |
| | | 199 | | ConvexShape sourceShape, |
| | | 200 | | LSCollider target, |
| | | 201 | | out Physics3DHit hit, |
| | | 202 | | out Fixed64 hitNumerator) |
| | | 203 | | { |
| | 169 | 204 | | hit = default; |
| | 169 | 205 | | hitNumerator = default; |
| | | 206 | | |
| | 169 | 207 | | if (!CanSweptSourceShapeReachTarget(sourceShape, target)) |
| | 6 | 208 | | return false; |
| | | 209 | | |
| | 163 | 210 | | if (target is LSCompoundCollider compound) |
| | | 211 | | { |
| | 3 | 212 | | return TrySweepTargetCompound( |
| | 3 | 213 | | sourceShape, |
| | 3 | 214 | | compound, |
| | 3 | 215 | | out hit, |
| | 3 | 216 | | out hitNumerator); |
| | | 217 | | } |
| | | 218 | | |
| | 160 | 219 | | if (target is LSMeshCollider mesh && mesh.Mode == MeshColliderMode.Concave) |
| | | 220 | | { |
| | 19 | 221 | | return TrySweepConcaveMeshTarget( |
| | 19 | 222 | | sourceShape, |
| | 19 | 223 | | mesh, |
| | 19 | 224 | | out hit, |
| | 19 | 225 | | out hitNumerator); |
| | | 226 | | } |
| | | 227 | | |
| | 141 | 228 | | return TrySweepConvexTarget( |
| | 141 | 229 | | sourceShape, |
| | 141 | 230 | | CreateColliderShape(target, Vector3d.Zero), |
| | 141 | 231 | | target, |
| | 141 | 232 | | out hit, |
| | 141 | 233 | | out hitNumerator); |
| | | 234 | | } |
| | | 235 | | |
| | | 236 | | private bool TrySweepTargetCompound( |
| | | 237 | | ConvexShape sourceShape, |
| | | 238 | | LSCompoundCollider compound, |
| | | 239 | | out Physics3DHit hit, |
| | | 240 | | out Fixed64 hitNumerator) |
| | | 241 | | { |
| | 3 | 242 | | hit = default; |
| | 3 | 243 | | hitNumerator = default; |
| | 3 | 244 | | bool found = false; |
| | 3 | 245 | | Fixed64 closestNumerator = Fixed64.MaxValue; |
| | 3 | 246 | | int closestPartIndex = int.MaxValue; |
| | | 247 | | |
| | 18 | 248 | | for (int i = 0; i < compound.PartCount; i++) |
| | | 249 | | { |
| | 6 | 250 | | LSCollider part = compound.GetPartCollider(i); |
| | 6 | 251 | | if (!TrySweepSourceShape( |
| | 6 | 252 | | sourceShape, |
| | 6 | 253 | | part, |
| | 6 | 254 | | out Physics3DHit partHit, |
| | 6 | 255 | | out Fixed64 partNumerator) |
| | 6 | 256 | | || !ComesBeforeReducerCandidate( |
| | 6 | 257 | | partNumerator, |
| | 6 | 258 | | i, |
| | 6 | 259 | | found, |
| | 6 | 260 | | closestNumerator, |
| | 6 | 261 | | closestPartIndex)) |
| | | 262 | | { |
| | | 263 | | continue; |
| | | 264 | | } |
| | | 265 | | |
| | 2 | 266 | | hit = new Physics3DHit(compound, partHit.Anchor, partHit.Normal, partHit.Distance, partHit.Direction); |
| | 2 | 267 | | hitNumerator = partNumerator; |
| | 2 | 268 | | closestNumerator = partNumerator; |
| | 2 | 269 | | closestPartIndex = i; |
| | 2 | 270 | | found = true; |
| | | 271 | | } |
| | | 272 | | |
| | 3 | 273 | | return found; |
| | | 274 | | } |
| | | 275 | | |
| | | 276 | | private bool TrySweepConcaveMeshTarget( |
| | | 277 | | ConvexShape sourceShape, |
| | | 278 | | LSMeshCollider mesh, |
| | | 279 | | out Physics3DHit hit, |
| | | 280 | | out Fixed64 hitNumerator) |
| | | 281 | | { |
| | 19 | 282 | | hit = default; |
| | 19 | 283 | | hitNumerator = default; |
| | 19 | 284 | | bool found = false; |
| | 19 | 285 | | Fixed64 closestNumerator = Fixed64.MaxValue; |
| | 19 | 286 | | int closestTriangleIndex = int.MaxValue; |
| | | 287 | | |
| | 19 | 288 | | CreateSweptSourceBoundsInMeshFrame( |
| | 19 | 289 | | sourceShape, |
| | 19 | 290 | | mesh, |
| | 19 | 291 | | out Vector3d min, |
| | 19 | 292 | | out Vector3d max); |
| | | 293 | | |
| | 19 | 294 | | mesh.Mesh.GetTrianglesInLocalBounds( |
| | 19 | 295 | | new FixedBoundVolume(min, max), |
| | 19 | 296 | | _triangleCandidates); |
| | 19 | 297 | | LastMeshTriangleCandidateCount += _triangleCandidates.Count; |
| | 19 | 298 | | BuildOrderedSweepTriangleCandidates(sourceShape, mesh); |
| | | 299 | | |
| | 92 | 300 | | for (int i = 0; i < _sweepTriangleCandidates.Count; i++) |
| | | 301 | | { |
| | 28 | 302 | | SweepTriangleCandidate sweepCandidate = _sweepTriangleCandidates[i]; |
| | 28 | 303 | | if (RemainingSweepTrianglesCannotBeat( |
| | 28 | 304 | | sweepCandidate.LowerBoundNumerator, |
| | 28 | 305 | | found, |
| | 28 | 306 | | closestNumerator)) |
| | | 307 | | { |
| | | 308 | | break; |
| | | 309 | | } |
| | | 310 | | |
| | 27 | 311 | | int triangleIndex = sweepCandidate.TriangleIndex; |
| | 27 | 312 | | ConvexShape triangle = CreateTriangleShape(mesh, triangleIndex); |
| | 27 | 313 | | if (!TrySweepConvexTarget( |
| | 27 | 314 | | sourceShape, |
| | 27 | 315 | | triangle, |
| | 27 | 316 | | mesh, |
| | 27 | 317 | | out Physics3DHit candidate, |
| | 27 | 318 | | out Fixed64 candidateNumerator) |
| | 27 | 319 | | || !ComesBeforeReducerCandidate( |
| | 27 | 320 | | candidateNumerator, |
| | 27 | 321 | | triangleIndex, |
| | 27 | 322 | | found, |
| | 27 | 323 | | closestNumerator, |
| | 27 | 324 | | closestTriangleIndex)) |
| | | 325 | | { |
| | | 326 | | continue; |
| | | 327 | | } |
| | | 328 | | |
| | 16 | 329 | | hit = candidate; |
| | 16 | 330 | | hitNumerator = candidateNumerator; |
| | 16 | 331 | | closestNumerator = candidateNumerator; |
| | 16 | 332 | | closestTriangleIndex = triangleIndex; |
| | 16 | 333 | | found = true; |
| | | 334 | | } |
| | | 335 | | |
| | 19 | 336 | | return found; |
| | | 337 | | } |
| | | 338 | | |
| | | 339 | | private void BuildOrderedSweepTriangleCandidates(ConvexShape sourceShape, LSMeshCollider mesh) |
| | | 340 | | { |
| | 19 | 341 | | _sweepTriangleCandidates.FastClear(); |
| | 96 | 342 | | for (int i = 0; i < _triangleCandidates.Count; i++) |
| | | 343 | | { |
| | 29 | 344 | | int triangleIndex = _triangleCandidates[i]; |
| | 29 | 345 | | ConvexShape triangle = CreateTriangleShape(mesh, triangleIndex); |
| | 29 | 346 | | if (TryComputeSweepLowerBoundNumerator( |
| | 29 | 347 | | sourceShape, |
| | 29 | 348 | | triangle, |
| | 29 | 349 | | out Fixed64 lowerBoundNumerator)) |
| | | 350 | | { |
| | 28 | 351 | | _sweepTriangleCandidates.Add( |
| | 28 | 352 | | new SweepTriangleCandidate( |
| | 28 | 353 | | triangleIndex, |
| | 28 | 354 | | lowerBoundNumerator)); |
| | | 355 | | } |
| | | 356 | | } |
| | | 357 | | |
| | 19 | 358 | | _sweepTriangleCandidates.SortInPlace(SweepTriangleComparer); |
| | 19 | 359 | | } |
| | | 360 | | |
| | | 361 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 362 | | private bool TryComputeSweepLowerBoundNumerator( |
| | | 363 | | ConvexShape sourceShape, |
| | | 364 | | ConvexShape targetShape, |
| | | 365 | | out Fixed64 lowerBoundNumerator) |
| | | 366 | | { |
| | 29 | 367 | | sourceShape.GetSourceBounds( |
| | 29 | 368 | | out Vector3d sourceMin, |
| | 29 | 369 | | out Vector3d sourceMax); |
| | 29 | 370 | | targetShape.GetBounds( |
| | 29 | 371 | | out Vector3d targetMin, |
| | 29 | 372 | | out Vector3d targetMax); |
| | | 373 | | |
| | 29 | 374 | | Vector3d padding = Vector3d.One * ContactTolerance; |
| | 29 | 375 | | sourceMin -= padding; |
| | 29 | 376 | | sourceMax += padding; |
| | 29 | 377 | | return TryComputeSweepLowerBoundNumerator( |
| | 29 | 378 | | sourceMin, |
| | 29 | 379 | | sourceMax, |
| | 29 | 380 | | targetMin, |
| | 29 | 381 | | targetMax, |
| | 29 | 382 | | _displacement, |
| | 29 | 383 | | _length, |
| | 29 | 384 | | out lowerBoundNumerator); |
| | | 385 | | } |
| | | 386 | | |
| | | 387 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 388 | | internal static bool TryComputeSweepLowerBoundNumerator( |
| | | 389 | | Vector3d sourceMin, |
| | | 390 | | Vector3d sourceMax, |
| | | 391 | | Vector3d targetMin, |
| | | 392 | | Vector3d targetMax, |
| | | 393 | | Vector3d displacement, |
| | | 394 | | Fixed64 length, |
| | | 395 | | out Fixed64 lowerBoundNumerator) |
| | | 396 | | { |
| | 34 | 397 | | lowerBoundNumerator = Fixed64.Zero; |
| | 34 | 398 | | return IncludeAxisEntryNumerator( |
| | 34 | 399 | | sourceMin.X, |
| | 34 | 400 | | sourceMax.X, |
| | 34 | 401 | | targetMin.X, |
| | 34 | 402 | | targetMax.X, |
| | 34 | 403 | | displacement.X, |
| | 34 | 404 | | length, |
| | 34 | 405 | | ref lowerBoundNumerator) |
| | 34 | 406 | | && IncludeAxisEntryNumerator( |
| | 34 | 407 | | sourceMin.Y, |
| | 34 | 408 | | sourceMax.Y, |
| | 34 | 409 | | targetMin.Y, |
| | 34 | 410 | | targetMax.Y, |
| | 34 | 411 | | displacement.Y, |
| | 34 | 412 | | length, |
| | 34 | 413 | | ref lowerBoundNumerator) |
| | 34 | 414 | | && IncludeAxisEntryNumerator( |
| | 34 | 415 | | sourceMin.Z, |
| | 34 | 416 | | sourceMax.Z, |
| | 34 | 417 | | targetMin.Z, |
| | 34 | 418 | | targetMax.Z, |
| | 34 | 419 | | displacement.Z, |
| | 34 | 420 | | length, |
| | 34 | 421 | | ref lowerBoundNumerator); |
| | | 422 | | } |
| | | 423 | | |
| | | 424 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 425 | | private static bool IncludeAxisEntryNumerator( |
| | | 426 | | Fixed64 sourceMin, |
| | | 427 | | Fixed64 sourceMax, |
| | | 428 | | Fixed64 targetMin, |
| | | 429 | | Fixed64 targetMax, |
| | | 430 | | Fixed64 displacement, |
| | | 431 | | Fixed64 length, |
| | | 432 | | ref Fixed64 entryNumerator) |
| | | 433 | | { |
| | 98 | 434 | | if (sourceMax >= targetMin |
| | 98 | 435 | | && sourceMin <= targetMax) |
| | | 436 | | { |
| | 52 | 437 | | return true; |
| | | 438 | | } |
| | | 439 | | |
| | | 440 | | Fixed64 gap; |
| | | 441 | | Fixed64 span; |
| | 46 | 442 | | if (sourceMax < targetMin |
| | 46 | 443 | | && displacement > Fixed64.Zero) |
| | | 444 | | { |
| | | 445 | | // Saturation is safe here: an unrepresentable positive gap is |
| | | 446 | | // necessarily farther than the representable sweep span. |
| | 41 | 447 | | gap = targetMin - sourceMax; |
| | 41 | 448 | | span = displacement; |
| | | 449 | | } |
| | 5 | 450 | | else if (sourceMin > targetMax |
| | 5 | 451 | | && displacement < Fixed64.Zero) |
| | | 452 | | { |
| | 3 | 453 | | gap = sourceMin - targetMax; |
| | | 454 | | // Prepare admits only representable chord magnitudes, so no |
| | | 455 | | // component can be Fixed64.MinValue here. |
| | 3 | 456 | | span = -displacement; |
| | | 457 | | } |
| | | 458 | | else |
| | | 459 | | { |
| | 2 | 460 | | return false; |
| | | 461 | | } |
| | | 462 | | |
| | 44 | 463 | | if (gap > span) |
| | 1 | 464 | | return false; |
| | | 465 | | |
| | | 466 | | // With 0 <= gap <= span, the fused result is bounded by the already |
| | | 467 | | // representable chord length. |
| | 43 | 468 | | _ = Fixed64.TryMultiplyDivide( |
| | 43 | 469 | | length, |
| | 43 | 470 | | gap, |
| | 43 | 471 | | span, |
| | 43 | 472 | | out Fixed64 axisNumerator); |
| | | 473 | | |
| | | 474 | | // Fused conversion keeps u = numerator / chord length exact until the |
| | | 475 | | // final Q32.32 rounding. One raw unit makes that rounded result a |
| | | 476 | | // conservative lower bound without moving an underflowed zero. |
| | 43 | 477 | | axisNumerator = FixedMath.Max( |
| | 43 | 478 | | Fixed64.Zero, |
| | 43 | 479 | | axisNumerator - Fixed64.Epsilon); |
| | | 480 | | |
| | 43 | 481 | | if (axisNumerator > entryNumerator) |
| | 30 | 482 | | entryNumerator = axisNumerator; |
| | 43 | 483 | | return true; |
| | | 484 | | } |
| | | 485 | | |
| | | 486 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 487 | | internal static bool RemainingSweepTrianglesCannotBeat( |
| | | 488 | | Fixed64 candidateLowerBoundNumerator, |
| | | 489 | | bool found, |
| | | 490 | | Fixed64 closestNumerator) |
| | | 491 | | { |
| | 32 | 492 | | if (!found) |
| | 19 | 493 | | return false; |
| | | 494 | | |
| | 13 | 495 | | return candidateLowerBoundNumerator |
| | 13 | 496 | | > closestNumerator + Fixed64.Epsilon; |
| | | 497 | | } |
| | | 498 | | |
| | | 499 | | private bool TrySweepConvexTarget( |
| | | 500 | | ConvexShape sourceShape, |
| | | 501 | | ConvexShape targetShape, |
| | | 502 | | LSCollider targetCollider, |
| | | 503 | | out Physics3DHit hit, |
| | | 504 | | out Fixed64 hitNumerator) |
| | | 505 | | { |
| | 168 | 506 | | hit = default; |
| | 168 | 507 | | hitNumerator = default; |
| | | 508 | | // u is retained as travelNumerator / _length. Keeping the common |
| | | 509 | | // denominator exact preserves small chord components and physical |
| | | 510 | | // distance resolution without exposing wide arithmetic. |
| | 168 | 511 | | Fixed64 travelNumerator = Fixed64.Zero; |
| | 168 | 512 | | Vector3d normal = Vector3d.Zero; |
| | 168 | 513 | | GjkResult result = default; |
| | | 514 | | |
| | 622 | 515 | | for (int i = 0; i < _maxConservativeAdvancementIterations; i++) |
| | | 516 | | { |
| | 308 | 517 | | ConvexShape movedSource = |
| | 308 | 518 | | sourceShape.WithSourceOffset( |
| | 308 | 519 | | GetChordOffset(travelNumerator)); |
| | 308 | 520 | | result = ComputeDistance(movedSource, targetShape); |
| | 308 | 521 | | if (result.Intersects || result.Distance <= ContactTolerance) |
| | | 522 | | { |
| | 149 | 523 | | ContactAnchor anchor = ResolveHitAnchor( |
| | 149 | 524 | | targetShape, |
| | 149 | 525 | | targetCollider, |
| | 149 | 526 | | movedSource, |
| | 149 | 527 | | result, |
| | 149 | 528 | | out Vector3d point, |
| | 149 | 529 | | out bool hasMaterializedPoint, |
| | 149 | 530 | | out bool hasRefinedSurfaceNormal); |
| | 149 | 531 | | Vector3d hitNormal = ResolveHitNormal( |
| | 149 | 532 | | targetShape, |
| | 149 | 533 | | targetCollider, |
| | 149 | 534 | | point, |
| | 149 | 535 | | result.Normal, |
| | 149 | 536 | | normal, |
| | 149 | 537 | | hasRefinedSurfaceNormal, |
| | 149 | 538 | | hasMaterializedPoint); |
| | | 539 | | |
| | 149 | 540 | | hit = new Physics3DHit( |
| | 149 | 541 | | targetCollider, |
| | 149 | 542 | | anchor, |
| | 149 | 543 | | hitNormal, |
| | 149 | 544 | | travelNumerator, |
| | 149 | 545 | | _outputDirection); |
| | 149 | 546 | | hitNumerator = travelNumerator; |
| | 149 | 547 | | return true; |
| | | 548 | | } |
| | | 549 | | |
| | 159 | 550 | | normal = result.Normal; |
| | | 551 | | // Projecting a representable chord onto a unit normal is bounded |
| | | 552 | | // by the admitted chord length. |
| | 159 | 553 | | _ = _displacementAnchor.TryGetProjectedOffsetFrom( |
| | 159 | 554 | | ZeroAnchor, |
| | 159 | 555 | | -normal, |
| | 159 | 556 | | out Fixed64 closingPerFraction); |
| | 159 | 557 | | if (closingPerFraction <= Fixed64.Epsilon) |
| | | 558 | | { |
| | 9 | 559 | | return TryResolveEndpointBracket( |
| | 9 | 560 | | sourceShape, |
| | 9 | 561 | | targetShape, |
| | 9 | 562 | | targetCollider, |
| | 9 | 563 | | travelNumerator, |
| | 9 | 564 | | out hit, |
| | 9 | 565 | | out hitNumerator); |
| | | 566 | | } |
| | 150 | 567 | | Fixed64 boundedDistance = |
| | 150 | 568 | | FixedMath.Min( |
| | 150 | 569 | | result.Distance, |
| | 150 | 570 | | closingPerFraction); |
| | | 571 | | // Capping the advancement ratio at one preserves the endpoint |
| | | 572 | | // decision while keeping the fused result inside the chord length. |
| | 150 | 573 | | _ = Fixed64.TryMultiplyDivide( |
| | 150 | 574 | | boundedDistance, |
| | 150 | 575 | | _length, |
| | 150 | 576 | | closingPerFraction, |
| | 150 | 577 | | out Fixed64 stepNumerator); |
| | 150 | 578 | | Fixed64 nextTravelNumerator = |
| | 150 | 579 | | travelNumerator + stepNumerator; |
| | 150 | 580 | | if (result.Distance > closingPerFraction |
| | 150 | 581 | | || nextTravelNumerator > _length) |
| | | 582 | | { |
| | 7 | 583 | | ConvexShape endpointSource = sourceShape.WithSourceOffset(_displacement); |
| | 7 | 584 | | GjkResult endpointResult = ComputeDistance(endpointSource, targetShape); |
| | 7 | 585 | | if (!endpointResult.Intersects && endpointResult.Distance > ContactTolerance) |
| | 6 | 586 | | return false; |
| | | 587 | | |
| | 1 | 588 | | ContactAnchor anchor = ResolveHitAnchor( |
| | 1 | 589 | | targetShape, |
| | 1 | 590 | | targetCollider, |
| | 1 | 591 | | endpointSource, |
| | 1 | 592 | | endpointResult, |
| | 1 | 593 | | out Vector3d point, |
| | 1 | 594 | | out bool hasMaterializedPoint, |
| | 1 | 595 | | out bool hasRefinedSurfaceNormal); |
| | 1 | 596 | | Vector3d hitNormal = ResolveHitNormal( |
| | 1 | 597 | | targetShape, |
| | 1 | 598 | | targetCollider, |
| | 1 | 599 | | point, |
| | 1 | 600 | | endpointResult.Normal, |
| | 1 | 601 | | normal, |
| | 1 | 602 | | hasRefinedSurfaceNormal, |
| | 1 | 603 | | hasMaterializedPoint); |
| | 1 | 604 | | hit = new Physics3DHit( |
| | 1 | 605 | | targetCollider, |
| | 1 | 606 | | anchor, |
| | 1 | 607 | | hitNormal, |
| | 1 | 608 | | _length, |
| | 1 | 609 | | _outputDirection); |
| | 1 | 610 | | hitNumerator = _length; |
| | 1 | 611 | | return true; |
| | | 612 | | } |
| | | 613 | | |
| | 143 | 614 | | travelNumerator = nextTravelNumerator; |
| | | 615 | | } |
| | | 616 | | |
| | 3 | 617 | | return TryResolveEndpointBracket( |
| | 3 | 618 | | sourceShape, |
| | 3 | 619 | | targetShape, |
| | 3 | 620 | | targetCollider, |
| | 3 | 621 | | travelNumerator, |
| | 3 | 622 | | out hit, |
| | 3 | 623 | | out hitNumerator); |
| | | 624 | | } |
| | | 625 | | |
| | | 626 | | private bool TryResolveEndpointBracket( |
| | | 627 | | ConvexShape sourceShape, |
| | | 628 | | ConvexShape targetShape, |
| | | 629 | | LSCollider targetCollider, |
| | | 630 | | Fixed64 lowerNumerator, |
| | | 631 | | out Physics3DHit hit, |
| | | 632 | | out Fixed64 hitNumerator) |
| | | 633 | | { |
| | 12 | 634 | | hit = default; |
| | 12 | 635 | | hitNumerator = default; |
| | 12 | 636 | | Fixed64 upperNumerator = _length; |
| | 12 | 637 | | ConvexShape upperSource = |
| | 12 | 638 | | sourceShape.WithSourceOffset(_displacement); |
| | 12 | 639 | | GjkResult upperResult = |
| | 12 | 640 | | ComputeDistance(upperSource, targetShape); |
| | 12 | 641 | | if (!upperResult.Intersects |
| | 12 | 642 | | && upperResult.Distance > ContactTolerance) |
| | | 643 | | { |
| | 10 | 644 | | return false; |
| | | 645 | | } |
| | | 646 | | |
| | 2 | 647 | | for (int iteration = 0; |
| | 4 | 648 | | iteration < _maxConservativeAdvancementIterations |
| | 4 | 649 | | && upperNumerator - lowerNumerator > DistanceTolerance; |
| | 2 | 650 | | iteration++) |
| | | 651 | | { |
| | 2 | 652 | | Fixed64 middleNumerator = |
| | 2 | 653 | | FixedMath.Midpoint( |
| | 2 | 654 | | lowerNumerator, |
| | 2 | 655 | | upperNumerator); |
| | 2 | 656 | | ConvexShape middleSource = |
| | 2 | 657 | | sourceShape.WithSourceOffset( |
| | 2 | 658 | | GetChordOffset(middleNumerator)); |
| | 2 | 659 | | GjkResult middleResult = |
| | 2 | 660 | | ComputeDistance(middleSource, targetShape); |
| | 2 | 661 | | if (middleResult.Intersects |
| | 2 | 662 | | || middleResult.Distance <= ContactTolerance) |
| | | 663 | | { |
| | 1 | 664 | | upperNumerator = middleNumerator; |
| | 1 | 665 | | upperSource = middleSource; |
| | 1 | 666 | | upperResult = middleResult; |
| | | 667 | | } |
| | | 668 | | else |
| | | 669 | | { |
| | 1 | 670 | | lowerNumerator = middleNumerator; |
| | | 671 | | } |
| | | 672 | | } |
| | | 673 | | |
| | 2 | 674 | | ContactAnchor anchor = ResolveHitAnchor( |
| | 2 | 675 | | targetShape, |
| | 2 | 676 | | targetCollider, |
| | 2 | 677 | | upperSource, |
| | 2 | 678 | | upperResult, |
| | 2 | 679 | | out Vector3d point, |
| | 2 | 680 | | out bool hasMaterializedPoint, |
| | 2 | 681 | | out bool hasRefinedSurfaceNormal); |
| | 2 | 682 | | Vector3d hitNormal = ResolveHitNormal( |
| | 2 | 683 | | targetShape, |
| | 2 | 684 | | targetCollider, |
| | 2 | 685 | | point, |
| | 2 | 686 | | upperResult.Normal, |
| | 2 | 687 | | Vector3d.Zero, |
| | 2 | 688 | | hasRefinedSurfaceNormal, |
| | 2 | 689 | | hasMaterializedPoint); |
| | 2 | 690 | | hit = new Physics3DHit( |
| | 2 | 691 | | targetCollider, |
| | 2 | 692 | | anchor, |
| | 2 | 693 | | hitNormal, |
| | 2 | 694 | | upperNumerator, |
| | 2 | 695 | | _outputDirection); |
| | 2 | 696 | | hitNumerator = upperNumerator; |
| | 2 | 697 | | return true; |
| | | 698 | | } |
| | | 699 | | |
| | | 700 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 701 | | private Vector3d GetChordOffset(Fixed64 numerator) => |
| | 310 | 702 | | _chord.GetPointAtDistance( |
| | 310 | 703 | | numerator, |
| | 310 | 704 | | _length); |
| | | 705 | | |
| | | 706 | | private ContactAnchor ResolveHitAnchor( |
| | | 707 | | ConvexShape targetShape, |
| | | 708 | | LSCollider targetCollider, |
| | | 709 | | ConvexShape movedSource, |
| | | 710 | | GjkResult result, |
| | | 711 | | out Vector3d point, |
| | | 712 | | out bool hasMaterializedPoint, |
| | | 713 | | out bool hasRefinedSurfaceNormal) |
| | | 714 | | { |
| | 152 | 715 | | hasMaterializedPoint = true; |
| | 152 | 716 | | hasRefinedSurfaceNormal = false; |
| | 152 | 717 | | if (targetCollider is LSSphereCollider |
| | 152 | 718 | | && movedSource.TryGetClosestPointOnSurface( |
| | 152 | 719 | | targetCollider.Center, |
| | 152 | 720 | | out Vector3d sourcePoint) |
| | 152 | 721 | | && Vector3d.TrySubtract( |
| | 152 | 722 | | sourcePoint, |
| | 152 | 723 | | targetCollider.Center, |
| | 152 | 724 | | out Vector3d centerToSource) |
| | 152 | 725 | | && centerToSource.MagnitudeSquared > Fixed64.Epsilon) |
| | | 726 | | { |
| | | 727 | | // A sphere's closest pair is defined by its center and the closest |
| | | 728 | | // source feature. Refining that feature removes arbitrary support |
| | | 729 | | // tie bias without changing the conservative TOI. |
| | 55 | 730 | | FixedPointAnchor sphereAnchor = |
| | 55 | 731 | | ConvexColliderSupport.GetSupportAnchor( |
| | 55 | 732 | | targetCollider, |
| | 55 | 733 | | centerToSource, |
| | 55 | 734 | | Vector3d.Zero); |
| | 55 | 735 | | hasMaterializedPoint = sphereAnchor.TryGetPoint(out point); |
| | 55 | 736 | | hasRefinedSurfaceNormal = hasMaterializedPoint; |
| | 55 | 737 | | return new ContactAnchor(sphereAnchor); |
| | | 738 | | } |
| | | 739 | | |
| | 97 | 740 | | FixedPointAnchor movedSourceCenter = movedSource.GetCenterAnchor(); |
| | 97 | 741 | | FixedPointAnchor targetCenter = targetShape.GetCenterAnchor(); |
| | 97 | 742 | | if (movedSourceCenter.TryGetOffsetFrom( |
| | 97 | 743 | | targetCenter, |
| | 97 | 744 | | out Vector3d centerDifference) |
| | 97 | 745 | | && centerDifference.MagnitudeSquared <= Fixed64.Epsilon) |
| | | 746 | | { |
| | 7 | 747 | | FixedPointAnchor fallbackAnchor = |
| | 7 | 748 | | targetShape.GetFallbackSurfaceAnchor(-_displacement); |
| | 7 | 749 | | hasMaterializedPoint = fallbackAnchor.TryGetPoint(out point); |
| | 7 | 750 | | return new ContactAnchor(fallbackAnchor); |
| | | 751 | | } |
| | | 752 | | |
| | | 753 | | // GJK's target witness identifies the feature that stopped the sweep. |
| | | 754 | | // Center-to-center projection can select an unrelated feature for long, |
| | | 755 | | // offset shapes and therefore produce a non-physical response normal. |
| | 90 | 756 | | if (!result.PointB.TryGetPoint(out point)) |
| | | 757 | | { |
| | 1 | 758 | | point = default; |
| | 1 | 759 | | hasMaterializedPoint = false; |
| | 1 | 760 | | return new ContactAnchor(result.PointB); |
| | | 761 | | } |
| | | 762 | | |
| | 89 | 763 | | return new ContactAnchor(result.PointB); |
| | | 764 | | } |
| | | 765 | | |
| | | 766 | | private Vector3d ResolveHitNormal( |
| | | 767 | | ConvexShape targetShape, |
| | | 768 | | LSCollider targetCollider, |
| | | 769 | | Vector3d point, |
| | | 770 | | Vector3d resultNormal, |
| | | 771 | | Vector3d fallbackNormal, |
| | | 772 | | bool hasRefinedSurfaceNormal, |
| | | 773 | | bool hasMaterializedPoint) |
| | | 774 | | { |
| | 152 | 775 | | Vector3d planarNormal = Vector3d.Zero; |
| | 152 | 776 | | if (hasMaterializedPoint) |
| | 151 | 777 | | targetShape.TryGetPlanarSurfaceNormal(point, out planarNormal); |
| | 152 | 778 | | return ConvexSweepHitPolicy.ResolveHitNormal( |
| | 152 | 779 | | targetCollider, |
| | 152 | 780 | | point, |
| | 152 | 781 | | resultNormal, |
| | 152 | 782 | | fallbackNormal, |
| | 152 | 783 | | _displacement, |
| | 152 | 784 | | planarNormal, |
| | 152 | 785 | | hasRefinedSurfaceNormal, |
| | 152 | 786 | | hasMaterializedPoint); |
| | | 787 | | } |
| | | 788 | | |
| | | 789 | | private static ConvexShape CreateColliderShape(LSCollider collider, Vector3d offset) |
| | | 790 | | { |
| | 344 | 791 | | return new ConvexShape(collider, offset); |
| | | 792 | | } |
| | | 793 | | |
| | | 794 | | private static ConvexShape CreateTriangleShape(LSMeshCollider mesh, int triangleIndex) |
| | | 795 | | { |
| | 56 | 796 | | mesh.Mesh.GetLocalTriangleVertices( |
| | 56 | 797 | | triangleIndex, |
| | 56 | 798 | | out Vector3d first, |
| | 56 | 799 | | out Vector3d second, |
| | 56 | 800 | | out Vector3d third); |
| | 56 | 801 | | return new ConvexShape(mesh, triangleIndex, first, second, third); |
| | | 802 | | } |
| | | 803 | | |
| | | 804 | | private static void ThrowIfConcaveSource(LSMeshCollider source) |
| | | 805 | | { |
| | 23 | 806 | | if (source.Mode == MeshColliderMode.Concave) |
| | 1 | 807 | | throw CreateConcaveSourceException(source); |
| | 22 | 808 | | } |
| | | 809 | | |
| | | 810 | | private void CreateSweptSourceBoundsInMeshFrame( |
| | | 811 | | ConvexShape sourceShape, |
| | | 812 | | LSMeshCollider mesh, |
| | | 813 | | out Vector3d min, |
| | | 814 | | out Vector3d max) |
| | | 815 | | { |
| | | 816 | | // Concave-target dispatch receives only the source's committed shape; |
| | | 817 | | // iterative chord offsets are introduced after broad-phase collection. |
| | 19 | 818 | | _ = sourceShape.TryGetBoundsRelativeTo( |
| | 19 | 819 | | mesh.Mesh.Origin, |
| | 19 | 820 | | mesh.Mesh.Rotation, |
| | 19 | 821 | | out Vector3d sourceMin, |
| | 19 | 822 | | out Vector3d sourceMax); |
| | | 823 | | |
| | | 824 | | // Prepare admitted the chord magnitude, so a unit rotation preserves |
| | | 825 | | // representability. |
| | 19 | 826 | | _ = mesh.Mesh.Rotation.Inverse().TryRotate( |
| | 19 | 827 | | _displacement, |
| | 19 | 828 | | out Vector3d localDisplacement); |
| | 19 | 829 | | SweepBoundsUtility.CreateSweptBounds( |
| | 19 | 830 | | sourceMin, |
| | 19 | 831 | | sourceMax, |
| | 19 | 832 | | localDisplacement, |
| | 19 | 833 | | ContactTolerance, |
| | 19 | 834 | | out min, |
| | 19 | 835 | | out max); |
| | 19 | 836 | | } |
| | | 837 | | |
| | | 838 | | private bool CanSweptSourceShapeReachTarget(ConvexShape sourceShape, LSCollider target) |
| | | 839 | | { |
| | 169 | 840 | | if (!sourceShape.CanTranslateCenter(_displacement)) |
| | 1 | 841 | | return false; |
| | | 842 | | |
| | 168 | 843 | | sourceShape.GetSourceBounds(out Vector3d sourceMin, out Vector3d sourceMax); |
| | 168 | 844 | | SweepBoundsUtility.CreateSweptBounds( |
| | 168 | 845 | | sourceMin, |
| | 168 | 846 | | sourceMax, |
| | 168 | 847 | | _displacement, |
| | 168 | 848 | | ContactTolerance, |
| | 168 | 849 | | out Vector3d min, |
| | 168 | 850 | | out Vector3d max); |
| | 168 | 851 | | return SweepBoundsUtility.OverlapsInclusive( |
| | 168 | 852 | | min, |
| | 168 | 853 | | max, |
| | 168 | 854 | | target.BoundsMin, |
| | 168 | 855 | | target.BoundsMax); |
| | | 856 | | } |
| | | 857 | | |
| | | 858 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 859 | | private static bool ComesBeforeReducerCandidate( |
| | | 860 | | Fixed64 hitNumerator, |
| | | 861 | | int candidateOrdinal, |
| | | 862 | | bool found, |
| | | 863 | | Fixed64 closestNumerator, |
| | | 864 | | int closestOrdinal) |
| | | 865 | | { |
| | 44 | 866 | | if (!found) |
| | 28 | 867 | | return true; |
| | | 868 | | |
| | 16 | 869 | | int numeratorCompare = |
| | 16 | 870 | | hitNumerator.CompareTo(closestNumerator); |
| | 16 | 871 | | if (numeratorCompare != 0) |
| | 7 | 872 | | return numeratorCompare < 0; |
| | | 873 | | |
| | 9 | 874 | | return candidateOrdinal < closestOrdinal; |
| | | 875 | | } |
| | | 876 | | |
| | | 877 | | private static ArgumentException CreateConcaveSourceException(LSMeshCollider source) => |
| | 1 | 878 | | new("Concave mesh sources are not supported by swept query APIs. Use an LSCompoundCollider built from authored c |
| | | 879 | | } |