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