| | | 1 | | //======================================================================= |
| | | 2 | | // ContinuousCollisionCandidateOrdering.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.Colliders; |
| | | 10 | | using Gravitas.Queries; |
| | | 11 | | using System.Runtime.CompilerServices; |
| | | 12 | | |
| | | 13 | | namespace Gravitas.CollisionHandling; |
| | | 14 | | |
| | | 15 | | internal static class ContinuousCollisionCandidateOrdering |
| | | 16 | | { |
| | | 17 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 18 | | public static bool Is2DHitFirst(Fixed64 distance2D, Fixed64 distance3D) => |
| | 9 | 19 | | distance2D <= distance3D; |
| | | 20 | | |
| | | 21 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 22 | | public static bool ShouldReplaceHit( |
| | | 23 | | Physics3DHit candidate, |
| | | 24 | | Fixed64 candidateClosingSpeed, |
| | | 25 | | bool hasCandidate, |
| | | 26 | | bool hasCurrent, |
| | | 27 | | Physics3DHit current, |
| | | 28 | | Fixed64 currentClosingSpeed) |
| | | 29 | | { |
| | 498 | 30 | | if (!hasCandidate) |
| | 385 | 31 | | return false; |
| | 113 | 32 | | if (!hasCurrent) |
| | 95 | 33 | | return true; |
| | | 34 | | |
| | 18 | 35 | | int distance = candidate.Distance.CompareTo(current.Distance); |
| | 18 | 36 | | if (distance != 0) |
| | 13 | 37 | | return distance < 0; |
| | | 38 | | |
| | 5 | 39 | | int closing = candidateClosingSpeed.CompareTo(currentClosingSpeed); |
| | 5 | 40 | | if (closing != 0) |
| | 1 | 41 | | return closing > 0; |
| | | 42 | | |
| | 4 | 43 | | int candidateId = candidate.Collider?.Id ?? -1; |
| | 4 | 44 | | int currentId = current.Collider?.Id ?? -1; |
| | 4 | 45 | | return candidateId < currentId; |
| | | 46 | | } |
| | | 47 | | |
| | | 48 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 49 | | public static bool ShouldReplaceHit( |
| | | 50 | | Physics2DHit candidate, |
| | | 51 | | Fixed64 candidateClosingSpeed, |
| | | 52 | | bool hasCandidate, |
| | | 53 | | bool hasCurrent, |
| | | 54 | | Physics2DHit current, |
| | | 55 | | Fixed64 currentClosingSpeed) |
| | | 56 | | { |
| | 375 | 57 | | if (!hasCandidate) |
| | 286 | 58 | | return false; |
| | 89 | 59 | | if (!hasCurrent) |
| | 73 | 60 | | return true; |
| | | 61 | | |
| | 16 | 62 | | int distance = candidate.Distance.CompareTo(current.Distance); |
| | 16 | 63 | | if (distance != 0) |
| | 13 | 64 | | return distance < 0; |
| | | 65 | | |
| | 3 | 66 | | int closing = candidateClosingSpeed.CompareTo(currentClosingSpeed); |
| | 3 | 67 | | if (closing != 0) |
| | 1 | 68 | | return closing > 0; |
| | | 69 | | |
| | 2 | 70 | | return candidate.Collider.Id < current.Collider.Id; |
| | | 71 | | } |
| | | 72 | | |
| | | 73 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 74 | | public static bool ShouldReplaceMixedHit( |
| | | 75 | | PhysicsMixedHit candidate, |
| | | 76 | | Fixed64 candidateClosingSpeed, |
| | | 77 | | bool hasCandidate, |
| | | 78 | | bool hasCurrent, |
| | | 79 | | PhysicsMixedHit current, |
| | | 80 | | Fixed64 currentClosingSpeed) |
| | | 81 | | { |
| | 15 | 82 | | if (!hasCandidate) |
| | 1 | 83 | | return false; |
| | 14 | 84 | | if (!hasCurrent) |
| | 1 | 85 | | return true; |
| | | 86 | | |
| | 13 | 87 | | int distance = candidate.Distance.CompareTo(current.Distance); |
| | 13 | 88 | | if (distance != 0) |
| | 4 | 89 | | return distance < 0; |
| | | 90 | | |
| | 9 | 91 | | int closing = candidateClosingSpeed.CompareTo(currentClosingSpeed); |
| | 9 | 92 | | if (closing != 0) |
| | 1 | 93 | | return closing > 0; |
| | | 94 | | |
| | 8 | 95 | | int reducer = candidate.ReducerKind.CompareTo(current.ReducerKind); |
| | 8 | 96 | | if (reducer != 0) |
| | 1 | 97 | | return reducer < 0; |
| | | 98 | | |
| | 7 | 99 | | int candidate3D = candidate.Collider3D?.Id ?? -1; |
| | 7 | 100 | | int current3D = current.Collider3D?.Id ?? -1; |
| | 7 | 101 | | int collider3D = candidate3D.CompareTo(current3D); |
| | 7 | 102 | | if (collider3D != 0) |
| | 3 | 103 | | return collider3D < 0; |
| | | 104 | | |
| | 4 | 105 | | int candidate2D = candidate.Collider2D?.Id ?? -1; |
| | 4 | 106 | | int current2D = current.Collider2D?.Id ?? -1; |
| | 4 | 107 | | return candidate2D < current2D; |
| | | 108 | | } |
| | | 109 | | |
| | | 110 | | public static bool IsIgnoredTarget(LSCollider hitCollider, LSCollider? ignored) |
| | | 111 | | { |
| | 13661 | 112 | | if (ignored == null) |
| | 13406 | 113 | | return false; |
| | | 114 | | |
| | 255 | 115 | | if (ReferenceEquals(hitCollider, ignored)) |
| | 124 | 116 | | return true; |
| | | 117 | | |
| | 131 | 118 | | SolidBody? ignoredBody = ignored.Body; |
| | 131 | 119 | | if (ignoredBody != null && ReferenceEquals(hitCollider.Body, ignoredBody)) |
| | 1 | 120 | | return true; |
| | | 121 | | |
| | 130 | 122 | | LSCollider? hitTopParent = hitCollider.TopParent3D; |
| | 130 | 123 | | LSCollider? ignoredTopParent = ignored.TopParent3D; |
| | 130 | 124 | | return (hitTopParent != null && ReferenceEquals(hitTopParent, ignored)) |
| | 130 | 125 | | || (ignoredTopParent != null && ReferenceEquals(hitCollider, ignoredTopParent)) |
| | 130 | 126 | | || (hitTopParent != null && ignoredTopParent != null && ReferenceEquals(hitTopParent, ignoredTopParent)); |
| | | 127 | | } |
| | | 128 | | |
| | | 129 | | public static bool IsIgnoredTarget(LSCollider2D hitCollider, LSCollider2D? ignored) |
| | | 130 | | { |
| | 1523 | 131 | | if (ignored == null) |
| | 1280 | 132 | | return false; |
| | | 133 | | |
| | 243 | 134 | | if (ReferenceEquals(hitCollider, ignored)) |
| | 149 | 135 | | return true; |
| | | 136 | | |
| | 94 | 137 | | SolidBody2D? ignoredBody = ignored.Body; |
| | 94 | 138 | | if (ignoredBody != null && ReferenceEquals(hitCollider.Body, ignoredBody)) |
| | 1 | 139 | | return true; |
| | | 140 | | |
| | 93 | 141 | | LSCollider2D? hitTopParent = hitCollider.TopParent2D; |
| | 93 | 142 | | LSCollider2D? ignoredTopParent = ignored.TopParent2D; |
| | 93 | 143 | | return (hitTopParent != null && ReferenceEquals(hitTopParent, ignored)) |
| | 93 | 144 | | || (ignoredTopParent != null && ReferenceEquals(hitCollider, ignoredTopParent)) |
| | 93 | 145 | | || (hitTopParent != null && ignoredTopParent != null && ReferenceEquals(hitTopParent, ignoredTopParent)); |
| | | 146 | | } |
| | | 147 | | } |