| | | 1 | | //======================================================================= |
| | | 2 | | // DynamicMixedIntervalHit.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 Gravitas.CollisionHandling; |
| | | 10 | | using Gravitas.Queries; |
| | | 11 | | |
| | | 12 | | namespace Gravitas; |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// Stores a moving mixed-pair search result and orders candidates by safe travel distance and contact evidence. |
| | | 16 | | /// </summary> |
| | | 17 | | internal readonly struct DynamicMixedIntervalHit |
| | | 18 | | { |
| | | 19 | | internal DynamicMixedIntervalHit( |
| | | 20 | | IntervalSearchStatus status, |
| | | 21 | | PhysicsMixedHit exactHit, |
| | | 22 | | Fixed64 safeDistance, |
| | | 23 | | Fixed64 closingSpeed, |
| | | 24 | | int targetId) |
| | | 25 | | { |
| | 168 | 26 | | Status = status; |
| | 168 | 27 | | ExactHit = exactHit; |
| | 168 | 28 | | SafeDistance = safeDistance; |
| | 168 | 29 | | ClosingSpeed = closingSpeed; |
| | 168 | 30 | | TargetId = targetId; |
| | 168 | 31 | | } |
| | | 32 | | |
| | | 33 | | internal IntervalSearchStatus Status { get; } |
| | | 34 | | |
| | | 35 | | internal PhysicsMixedHit ExactHit { get; } |
| | | 36 | | |
| | | 37 | | internal Fixed64 SafeDistance { get; } |
| | | 38 | | |
| | | 39 | | internal Fixed64 ClosingSpeed { get; } |
| | | 40 | | |
| | | 41 | | internal int TargetId { get; } |
| | | 42 | | |
| | | 43 | | internal static bool ShouldReplace( |
| | | 44 | | DynamicMixedIntervalHit candidate, |
| | | 45 | | DynamicMixedIntervalHit current, |
| | | 46 | | bool hasCurrent) |
| | | 47 | | { |
| | 126 | 48 | | if (!hasCurrent) |
| | 117 | 49 | | return true; |
| | | 50 | | |
| | 9 | 51 | | int distance = candidate.SafeDistance.CompareTo(current.SafeDistance); |
| | 9 | 52 | | if (distance != 0) |
| | 5 | 53 | | return distance < 0; |
| | | 54 | | |
| | 4 | 55 | | bool candidateIsExact = |
| | 4 | 56 | | candidate.Status == IntervalSearchStatus.ExactHit; |
| | 4 | 57 | | bool currentIsExact = |
| | 4 | 58 | | current.Status == IntervalSearchStatus.ExactHit; |
| | 4 | 59 | | if (candidateIsExact != currentIsExact) |
| | 2 | 60 | | return candidateIsExact; |
| | | 61 | | |
| | 2 | 62 | | if (candidateIsExact) |
| | | 63 | | { |
| | 1 | 64 | | return ContinuousCollisionCandidateOrdering.ShouldReplaceMixedHit( |
| | 1 | 65 | | candidate.ExactHit, |
| | 1 | 66 | | candidate.ClosingSpeed, |
| | 1 | 67 | | true, |
| | 1 | 68 | | true, |
| | 1 | 69 | | current.ExactHit, |
| | 1 | 70 | | current.ClosingSpeed); |
| | | 71 | | } |
| | | 72 | | |
| | 1 | 73 | | return candidate.TargetId < current.TargetId; |
| | | 74 | | } |
| | | 75 | | |
| | | 76 | | internal static DynamicMixedIntervalHit Select( |
| | | 77 | | DynamicMixedIntervalHit candidate, |
| | | 78 | | DynamicMixedIntervalHit current, |
| | | 79 | | ref bool hasCurrent) |
| | | 80 | | { |
| | 119 | 81 | | if (!ShouldReplace(candidate, current, hasCurrent)) |
| | 3 | 82 | | return current; |
| | | 83 | | |
| | 116 | 84 | | hasCurrent = true; |
| | 116 | 85 | | return candidate; |
| | | 86 | | } |
| | | 87 | | |
| | | 88 | | internal static bool ShouldReplaceStatic( |
| | | 89 | | DynamicMixedIntervalHit candidate, |
| | | 90 | | bool hasCandidate, |
| | | 91 | | PhysicsMixedHit current, |
| | | 92 | | bool hasCurrent) |
| | | 93 | | { |
| | 2406 | 94 | | if (!hasCandidate) |
| | 2287 | 95 | | return false; |
| | 119 | 96 | | if (!hasCurrent) |
| | 116 | 97 | | return true; |
| | 3 | 98 | | if (candidate.SafeDistance < current.Distance) |
| | 1 | 99 | | return true; |
| | 2 | 100 | | if (candidate.Status != IntervalSearchStatus.ExactHit) |
| | 1 | 101 | | return false; |
| | | 102 | | |
| | 1 | 103 | | return ContinuousCollisionCandidateOrdering.ShouldReplaceMixedHit( |
| | 1 | 104 | | candidate.ExactHit, |
| | 1 | 105 | | candidate.ClosingSpeed, |
| | 1 | 106 | | true, |
| | 1 | 107 | | true, |
| | 1 | 108 | | current, |
| | 1 | 109 | | Fixed64.Zero); |
| | | 110 | | } |
| | | 111 | | |
| | | 112 | | internal static bool ShouldSelect2D( |
| | | 113 | | bool has2D, |
| | | 114 | | Fixed64 distance2D, |
| | | 115 | | bool hasMixed, |
| | | 116 | | Fixed64 distanceMixed) |
| | | 117 | | { |
| | 983 | 118 | | if (!has2D) |
| | 763 | 119 | | return false; |
| | 220 | 120 | | if (!hasMixed) |
| | 218 | 121 | | return true; |
| | | 122 | | |
| | 2 | 123 | | return ContinuousCollisionCandidateOrdering.Is2DHitFirst( |
| | 2 | 124 | | distance2D, |
| | 2 | 125 | | distanceMixed); |
| | | 126 | | } |
| | | 127 | | |
| | | 128 | | internal static bool ShouldSelect3D( |
| | | 129 | | bool has3D, |
| | | 130 | | Fixed64 distance3D, |
| | | 131 | | bool hasMixed, |
| | | 132 | | Fixed64 distanceMixed) |
| | | 133 | | { |
| | 1424 | 134 | | if (!has3D) |
| | 987 | 135 | | return false; |
| | 437 | 136 | | if (!hasMixed) |
| | 435 | 137 | | return true; |
| | | 138 | | |
| | 2 | 139 | | return !ContinuousCollisionCandidateOrdering.Is2DHitFirst( |
| | 2 | 140 | | distanceMixed, |
| | 2 | 141 | | distance3D); |
| | | 142 | | } |
| | | 143 | | } |