| | | 1 | | //======================================================================= |
| | | 2 | | // ManifoldContact.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.Materials; |
| | | 10 | | using System; |
| | | 11 | | using System.Runtime.CompilerServices; |
| | | 12 | | |
| | | 13 | | namespace Gravitas.CollisionHandling; |
| | | 14 | | |
| | | 15 | | /// <summary> |
| | | 16 | | /// A deterministic narrow-phase contact point for a collision manifold. |
| | | 17 | | /// </summary> |
| | | 18 | | public readonly struct ManifoldContact |
| | | 19 | | { |
| | | 20 | | /// <summary>Creates a deterministic contact from world-space witness points.</summary> |
| | | 21 | | public ManifoldContact( |
| | | 22 | | ulong contactId, |
| | | 23 | | Vector3d pointA, |
| | | 24 | | Vector3d pointB, |
| | | 25 | | Fixed64 depth, |
| | | 26 | | Vector3d normal, |
| | | 27 | | bool hasMaterialOverride = false, |
| | | 28 | | PhysicsMaterial materialA = default, |
| | | 29 | | PhysicsMaterial materialB = default, |
| | | 30 | | bool depthIsClamped = false) |
| | 3 | 31 | | : this( |
| | 3 | 32 | | contactId, |
| | 3 | 33 | | ContactAnchor.FromWorldPoint(pointA), |
| | 3 | 34 | | ContactAnchor.FromWorldPoint(pointB), |
| | 3 | 35 | | depth, |
| | 3 | 36 | | normal, |
| | 3 | 37 | | hasMaterialOverride, |
| | 3 | 38 | | materialA, |
| | 3 | 39 | | materialB, |
| | 3 | 40 | | depthIsClamped) |
| | 3 | 41 | | { } |
| | | 42 | | |
| | | 43 | | /// <summary> |
| | | 44 | | /// Creates a deterministic contact from authoritative rigid-frame anchors. |
| | | 45 | | /// </summary> |
| | | 46 | | public ManifoldContact( |
| | | 47 | | ulong contactId, |
| | | 48 | | ContactAnchor anchorA, |
| | | 49 | | ContactAnchor anchorB, |
| | | 50 | | Fixed64 depth, |
| | | 51 | | Vector3d normal, |
| | | 52 | | bool hasMaterialOverride = false, |
| | | 53 | | PhysicsMaterial materialA = default, |
| | | 54 | | PhysicsMaterial materialB = default, |
| | | 55 | | bool depthIsClamped = false) |
| | 9 | 56 | | : this( |
| | 9 | 57 | | contactId, |
| | 9 | 58 | | anchorA, |
| | 9 | 59 | | anchorB, |
| | 9 | 60 | | depth, |
| | 9 | 61 | | normal, |
| | 9 | 62 | | hasMaterialOverride, |
| | 9 | 63 | | materialA, |
| | 9 | 64 | | materialB, |
| | 9 | 65 | | depthIsClamped, |
| | 9 | 66 | | featureNamespaceA: 0, |
| | 9 | 67 | | featureNamespaceB: 0) |
| | 7 | 68 | | { } |
| | | 69 | | |
| | | 70 | | internal ManifoldContact( |
| | | 71 | | ulong contactId, |
| | | 72 | | ContactAnchor anchorA, |
| | | 73 | | ContactAnchor anchorB, |
| | | 74 | | Fixed64 depth, |
| | | 75 | | Vector3d normal, |
| | | 76 | | bool hasMaterialOverride, |
| | | 77 | | PhysicsMaterial materialA, |
| | | 78 | | PhysicsMaterial materialB, |
| | | 79 | | bool depthIsClamped, |
| | | 80 | | int featureNamespaceA, |
| | | 81 | | int featureNamespaceB) |
| | | 82 | | { |
| | 30389 | 83 | | if (!anchorA.IsValid) |
| | 1 | 84 | | throw new ArgumentException("Contact anchor A must be valid.", nameof(anchorA)); |
| | 30388 | 85 | | if (!anchorB.IsValid) |
| | 1 | 86 | | throw new ArgumentException("Contact anchor B must be valid.", nameof(anchorB)); |
| | | 87 | | |
| | 30387 | 88 | | ContactId = contactId; |
| | 30387 | 89 | | AnchorA = anchorA; |
| | 30387 | 90 | | AnchorB = anchorB; |
| | 30387 | 91 | | FeatureNamespaceA = featureNamespaceA; |
| | 30387 | 92 | | FeatureNamespaceB = featureNamespaceB; |
| | 30387 | 93 | | Depth = depth.Abs(); |
| | 30387 | 94 | | DepthIsClamped = depthIsClamped; |
| | 30387 | 95 | | Normal = normal.MagnitudeSquared > Fixed64.Epsilon |
| | 30387 | 96 | | ? normal.Normalized |
| | 30387 | 97 | | : Vector3d.Zero; |
| | 30387 | 98 | | HasMaterialOverride = hasMaterialOverride; |
| | 30387 | 99 | | MaterialA = hasMaterialOverride ? materialA : PhysicsMaterial.Default; |
| | 30387 | 100 | | MaterialB = hasMaterialOverride ? materialB : PhysicsMaterial.Default; |
| | 30387 | 101 | | } |
| | | 102 | | |
| | | 103 | | /// <summary> |
| | | 104 | | /// Stable identity derived from the unordered pair of pose-invariant |
| | | 105 | | /// canonical local feature terms. |
| | | 106 | | /// </summary> |
| | | 107 | | public ulong ContactId { get; } |
| | | 108 | | |
| | | 109 | | /// <summary> |
| | | 110 | | /// Authoritative canonical contact anchor on collider A. |
| | | 111 | | /// </summary> |
| | | 112 | | public ContactAnchor AnchorA { get; } |
| | | 113 | | |
| | | 114 | | /// <summary> |
| | | 115 | | /// Authoritative canonical contact anchor on collider B. |
| | | 116 | | /// </summary> |
| | | 117 | | public ContactAnchor AnchorB { get; } |
| | | 118 | | |
| | | 119 | | internal int FeatureNamespaceA { get; } |
| | | 120 | | |
| | | 121 | | internal int FeatureNamespaceB { get; } |
| | | 122 | | |
| | | 123 | | /// <summary> |
| | | 124 | | /// Gets the world-space contact point on collider A. |
| | | 125 | | /// </summary> |
| | | 126 | | /// <exception cref="InvalidOperationException"> |
| | | 127 | | /// The conceptual world point is outside the scalar range. |
| | | 128 | | /// </exception> |
| | 103 | 129 | | public Vector3d PointA => GetRequiredWorldPoint(AnchorA, nameof(PointA)); |
| | | 130 | | |
| | | 131 | | /// <summary> |
| | | 132 | | /// Gets the world-space contact point on collider B. |
| | | 133 | | /// </summary> |
| | | 134 | | /// <exception cref="InvalidOperationException"> |
| | | 135 | | /// The conceptual world point is outside the scalar range. |
| | | 136 | | /// </exception> |
| | 74 | 137 | | public Vector3d PointB => GetRequiredWorldPoint(AnchorB, nameof(PointB)); |
| | | 138 | | |
| | | 139 | | /// <summary> |
| | | 140 | | /// Attempts to materialize the world-space contact point on collider A. |
| | | 141 | | /// </summary> |
| | | 142 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 40 | 143 | | public bool TryGetPointA(out Vector3d point) => AnchorA.TryGetWorldPoint(out point); |
| | | 144 | | |
| | | 145 | | /// <summary> |
| | | 146 | | /// Attempts to materialize the world-space contact point on collider B. |
| | | 147 | | /// </summary> |
| | | 148 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 38 | 149 | | public bool TryGetPointB(out Vector3d point) => AnchorB.TryGetWorldPoint(out point); |
| | | 150 | | |
| | | 151 | | /// <summary> |
| | | 152 | | /// Penetration depth along <see cref="Normal"/>. |
| | | 153 | | /// </summary> |
| | | 154 | | public Fixed64 Depth { get; } |
| | | 155 | | |
| | | 156 | | /// <summary> |
| | | 157 | | /// Gets whether the conceptual penetration depth exceeded the scalar range. |
| | | 158 | | /// </summary> |
| | | 159 | | public bool DepthIsClamped { get; } |
| | | 160 | | |
| | | 161 | | /// <summary> |
| | | 162 | | /// Unit normal pointing from collider A toward collider B. |
| | | 163 | | /// </summary> |
| | | 164 | | public Vector3d Normal { get; } |
| | | 165 | | |
| | | 166 | | /// <summary> |
| | | 167 | | /// Gets whether this contact carries materials from private compound parts. |
| | | 168 | | /// </summary> |
| | | 169 | | public bool HasMaterialOverride { get; } |
| | | 170 | | |
| | | 171 | | /// <summary> |
| | | 172 | | /// Material for collider A when <see cref="HasMaterialOverride"/> is true. |
| | | 173 | | /// </summary> |
| | | 174 | | public PhysicsMaterial MaterialA { get; } |
| | | 175 | | |
| | | 176 | | /// <summary> |
| | | 177 | | /// Material for collider B when <see cref="HasMaterialOverride"/> is true. |
| | | 178 | | /// </summary> |
| | | 179 | | public PhysicsMaterial MaterialB { get; } |
| | | 180 | | |
| | | 181 | | private static Vector3d GetRequiredWorldPoint(ContactAnchor anchor, string propertyName) |
| | | 182 | | { |
| | 177 | 183 | | if (anchor.TryGetWorldPoint(out Vector3d point)) |
| | 176 | 184 | | return point; |
| | | 185 | | |
| | 1 | 186 | | throw new InvalidOperationException( |
| | 1 | 187 | | $"{propertyName} is outside the representable coordinate range. Use the corresponding TryGet method."); |
| | | 188 | | } |
| | | 189 | | } |