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