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