| | | 1 | | //======================================================================= |
| | | 2 | | // MixedContact.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; |
| | | 9 | | using System.Runtime.CompilerServices; |
| | | 10 | | using FixedMathSharp; |
| | | 11 | | using FixedMathSharp.Geometry; |
| | | 12 | | using Gravitas.Colliders; |
| | | 13 | | using Gravitas.Materials; |
| | | 14 | | |
| | | 15 | | namespace Gravitas.CollisionHandling; |
| | | 16 | | |
| | | 17 | | /// <summary> |
| | | 18 | | /// Contact generated between one 3D collider and one embedded 2D collider. |
| | | 19 | | /// </summary> |
| | | 20 | | /// <remarks>A single contact may represent one row of a larger manifold. Its depth does not guarantee escape from a non |
| | | 21 | | public readonly struct MixedContact |
| | | 22 | | { |
| | | 23 | | /// <summary>Creates a mixed contact from world-space witness points.</summary> |
| | | 24 | | public MixedContact( |
| | | 25 | | Vector3d point3D, |
| | | 26 | | Vector3d point2D, |
| | | 27 | | Vector3d normal3DTo2D, |
| | | 28 | | Fixed64 depth, |
| | | 29 | | bool depthIsClamped = false) |
| | 91 | 30 | | : this( |
| | 91 | 31 | | ContactAnchor.FromWorldPoint(point3D), |
| | 91 | 32 | | ContactAnchor.FromWorldPoint(point2D), |
| | 91 | 33 | | normal3DTo2D, |
| | 91 | 34 | | depth, |
| | 91 | 35 | | depthIsClamped, |
| | 91 | 36 | | hasMaterialOverride: false, |
| | 91 | 37 | | default, |
| | 91 | 38 | | default) |
| | 91 | 39 | | { } |
| | | 40 | | |
| | | 41 | | /// <summary> |
| | | 42 | | /// Creates a mixed contact with authoritative rigid-frame witnesses. |
| | | 43 | | /// </summary> |
| | | 44 | | /// <exception cref="ArgumentException"> |
| | | 45 | | /// Either rigid-frame anchor is invalid. |
| | | 46 | | /// </exception> |
| | | 47 | | public MixedContact( |
| | | 48 | | ContactAnchor anchor3D, |
| | | 49 | | ContactAnchor anchor2D, |
| | | 50 | | Vector3d normal3DTo2D, |
| | | 51 | | Fixed64 depth, |
| | | 52 | | bool depthIsClamped = false) |
| | 9648 | 53 | | : this( |
| | 9648 | 54 | | anchor3D, |
| | 9648 | 55 | | anchor2D, |
| | 9648 | 56 | | normal3DTo2D, |
| | 9648 | 57 | | depth, |
| | 9648 | 58 | | depthIsClamped, |
| | 9648 | 59 | | hasMaterialOverride: false, |
| | 9648 | 60 | | default, |
| | 9648 | 61 | | default) |
| | 9646 | 62 | | { } |
| | | 63 | | |
| | | 64 | | internal MixedContact( |
| | | 65 | | Vector3d point3D, |
| | | 66 | | Vector3d point2D, |
| | | 67 | | Vector3d normal3DTo2D, |
| | | 68 | | Fixed64 depth, |
| | | 69 | | PhysicsMaterial material3D, |
| | | 70 | | PhysicsMaterial material2D, |
| | | 71 | | bool depthIsClamped = false) |
| | 2 | 72 | | : this( |
| | 2 | 73 | | ContactAnchor.FromWorldPoint(point3D), |
| | 2 | 74 | | ContactAnchor.FromWorldPoint(point2D), |
| | 2 | 75 | | normal3DTo2D, |
| | 2 | 76 | | depth, |
| | 2 | 77 | | depthIsClamped, |
| | 2 | 78 | | hasMaterialOverride: true, |
| | 2 | 79 | | material3D, |
| | 2 | 80 | | material2D) |
| | 2 | 81 | | { } |
| | | 82 | | |
| | | 83 | | internal MixedContact( |
| | | 84 | | ContactAnchor anchor3D, |
| | | 85 | | ContactAnchor anchor2D, |
| | | 86 | | Vector3d normal3DTo2D, |
| | | 87 | | Fixed64 depth, |
| | | 88 | | PhysicsMaterial material3D, |
| | | 89 | | PhysicsMaterial material2D, |
| | | 90 | | bool depthIsClamped = false) |
| | 1797 | 91 | | : this( |
| | 1797 | 92 | | anchor3D, |
| | 1797 | 93 | | anchor2D, |
| | 1797 | 94 | | normal3DTo2D, |
| | 1797 | 95 | | depth, |
| | 1797 | 96 | | depthIsClamped, |
| | 1797 | 97 | | hasMaterialOverride: true, |
| | 1797 | 98 | | material3D, |
| | 1797 | 99 | | material2D) |
| | 1797 | 100 | | { } |
| | | 101 | | |
| | | 102 | | private MixedContact( |
| | | 103 | | ContactAnchor anchor3D, |
| | | 104 | | ContactAnchor anchor2D, |
| | | 105 | | Vector3d normal3DTo2D, |
| | | 106 | | Fixed64 depth, |
| | | 107 | | bool depthIsClamped, |
| | | 108 | | bool hasMaterialOverride, |
| | | 109 | | PhysicsMaterial material3D, |
| | | 110 | | PhysicsMaterial material2D) |
| | | 111 | | { |
| | 384785 | 112 | | if (!anchor3D.IsValid) |
| | 1 | 113 | | throw new ArgumentException("The 3D contact anchor must be valid.", nameof(anchor3D)); |
| | 384784 | 114 | | if (!anchor2D.IsValid) |
| | 1 | 115 | | throw new ArgumentException("The 2D contact anchor must be valid.", nameof(anchor2D)); |
| | | 116 | | |
| | 384783 | 117 | | Anchor3D = anchor3D; |
| | 384783 | 118 | | Anchor2D = anchor2D; |
| | 384783 | 119 | | Normal3DTo2D = normal3DTo2D; |
| | 384783 | 120 | | Depth = depth; |
| | 384783 | 121 | | DepthIsClamped = depthIsClamped; |
| | 384783 | 122 | | HasMaterialOverride = hasMaterialOverride; |
| | 384783 | 123 | | Material3D = hasMaterialOverride ? material3D : PhysicsMaterial.Default; |
| | 384783 | 124 | | Material2D = hasMaterialOverride ? material2D : PhysicsMaterial.Default; |
| | 384783 | 125 | | HasContact = true; |
| | 384783 | 126 | | } |
| | | 127 | | |
| | | 128 | | /// <summary>Gets whether this value contains a valid mixed contact.</summary> |
| | | 129 | | public bool HasContact { get; } |
| | | 130 | | |
| | | 131 | | /// <summary>Gets the authoritative contact anchor on the 3D collider.</summary> |
| | | 132 | | public ContactAnchor Anchor3D { get; } |
| | | 133 | | |
| | | 134 | | /// <summary>Gets the authoritative contact anchor on the embedded 2D collider.</summary> |
| | | 135 | | public ContactAnchor Anchor2D { get; } |
| | | 136 | | |
| | | 137 | | /// <summary>Gets the world-space witness point on the 3D collider.</summary> |
| | 65 | 138 | | public Vector3d Point3D => GetRequiredWorldPoint(Anchor3D, nameof(Point3D)); |
| | | 139 | | |
| | | 140 | | /// <summary>Gets the world-space witness point on the embedded 2D collider.</summary> |
| | 75 | 141 | | public Vector3d Point2D => GetRequiredWorldPoint(Anchor2D, nameof(Point2D)); |
| | | 142 | | |
| | | 143 | | /// <summary>Attempts to resolve the 3D anchor to a world-space point.</summary> |
| | | 144 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 84 | 145 | | public bool TryGetPoint3D(out Vector3d point) => Anchor3D.TryGetWorldPoint(out point); |
| | | 146 | | |
| | | 147 | | /// <summary>Attempts to resolve the embedded 2D anchor to a world-space point.</summary> |
| | | 148 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 83 | 149 | | public bool TryGetPoint2D(out Vector3d point) => Anchor2D.TryGetWorldPoint(out point); |
| | | 150 | | |
| | | 151 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 152 | | internal bool TryGetPlanarOffset2DFrom( |
| | | 153 | | Vector2d origin, |
| | | 154 | | out Vector2d offset) => |
| | 1 | 155 | | TryGetPlanarOffset2DFrom( |
| | 1 | 156 | | origin, |
| | 1 | 157 | | Fixed64.Zero, |
| | 1 | 158 | | Vector2d.Zero, |
| | 1 | 159 | | out offset); |
| | | 160 | | |
| | | 161 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 162 | | internal bool TryGetPlanarOffset2DFrom( |
| | | 163 | | Vector2d origin, |
| | | 164 | | Fixed64 rotation, |
| | | 165 | | Vector2d localPoint, |
| | | 166 | | out Vector2d offset) |
| | | 167 | | { |
| | 991 | 168 | | ContactAnchor reference = |
| | 991 | 169 | | CreatePlanarReference(origin, rotation, localPoint); |
| | 991 | 170 | | if (!Anchor2D.TryGetOffsetFrom( |
| | 991 | 171 | | reference, |
| | 991 | 172 | | out Vector3d offset3D)) |
| | | 173 | | { |
| | 1 | 174 | | offset = default; |
| | 1 | 175 | | return false; |
| | | 176 | | } |
| | | 177 | | |
| | 990 | 178 | | offset = new Vector2d(offset3D.X, offset3D.Z); |
| | 990 | 179 | | return true; |
| | | 180 | | } |
| | | 181 | | |
| | | 182 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 183 | | internal ExactLever3D GetPlanarXZLeverFrom( |
| | | 184 | | Vector2d origin, |
| | | 185 | | Fixed64 rotation, |
| | | 186 | | Vector2d localPoint) => |
| | 58 | 187 | | Anchor2D.GetLeverFrom( |
| | 58 | 188 | | CreatePlanarReference(origin, rotation, localPoint)); |
| | | 189 | | |
| | | 190 | | /// <summary> |
| | | 191 | | /// Contact normal pointing from the 3D collider toward the embedded 2D collider volume. |
| | | 192 | | /// </summary> |
| | | 193 | | public Vector3d Normal3DTo2D { get; } |
| | | 194 | | |
| | | 195 | | /// <summary>Gets the penetration depth along <see cref="Normal3DTo2D"/>.</summary> |
| | | 196 | | public Fixed64 Depth { get; } |
| | | 197 | | |
| | | 198 | | /// <summary> |
| | | 199 | | /// Gets whether the exact penetration depth exceeded the scalar domain and |
| | | 200 | | /// <see cref="Depth"/> therefore contains <see cref="Fixed64.MaxValue"/>. |
| | | 201 | | /// </summary> |
| | | 202 | | public bool DepthIsClamped { get; } |
| | | 203 | | |
| | | 204 | | /// <summary>Gets whether the contact carries per-feature material values.</summary> |
| | | 205 | | public bool HasMaterialOverride { get; } |
| | | 206 | | |
| | | 207 | | /// <summary>Gets the material sampled from the 3D contact feature.</summary> |
| | | 208 | | public PhysicsMaterial Material3D { get; } |
| | | 209 | | |
| | | 210 | | /// <summary>Gets the material sampled from the embedded 2D contact feature.</summary> |
| | | 211 | | public PhysicsMaterial Material2D { get; } |
| | | 212 | | |
| | | 213 | | internal MixedContact WithMaterialOverride(PhysicsMaterial material3D, PhysicsMaterial material2D) => |
| | 1797 | 214 | | new( |
| | 1797 | 215 | | Anchor3D, |
| | 1797 | 216 | | Anchor2D, |
| | 1797 | 217 | | Normal3DTo2D, |
| | 1797 | 218 | | Depth, |
| | 1797 | 219 | | material3D, |
| | 1797 | 220 | | material2D, |
| | 1797 | 221 | | DepthIsClamped); |
| | | 222 | | |
| | | 223 | | internal MixedContact WithFallbackMaterials(PhysicsMaterial material3D, PhysicsMaterial material2D) => |
| | 1796 | 224 | | HasMaterialOverride ? this : WithMaterialOverride(material3D, material2D); |
| | | 225 | | |
| | | 226 | | internal static MixedContact FromManifoldContact(in ManifoldContact contact) => |
| | 373247 | 227 | | new(contact.AnchorA, contact.AnchorB, contact.Normal, contact.Depth, |
| | 373247 | 228 | | contact.DepthIsClamped, contact.HasMaterialOverride, contact.MaterialA, contact.MaterialB); |
| | | 229 | | |
| | | 230 | | internal ManifoldContact ToManifoldContact(int namespace3D = 0, int namespace2D = 0) => |
| | 9634 | 231 | | new(ContactManifold.CreateContactId(Anchor3D, namespace3D, Anchor2D, namespace2D), |
| | 9634 | 232 | | Anchor3D, Anchor2D, Depth, Normal3DTo2D, HasMaterialOverride, |
| | 9634 | 233 | | Material3D, Material2D, DepthIsClamped, namespace3D, namespace2D); |
| | | 234 | | |
| | | 235 | | internal static bool TrySelectClosingContact(ContactManifold manifold, |
| | | 236 | | ContactAnchor center3D, Vector3d linear3D, Vector3d angular3D, |
| | | 237 | | ContactAnchor center2D, Vector2d linear2D, Fixed64 angular2D, out MixedContact contact, |
| | | 238 | | LSCollider2D? owner2D = null) |
| | | 239 | | { |
| | | 240 | | // Preserve a geometric witness for conservative trajectory bounding even when every row separates. |
| | 1761 | 241 | | contact = manifold.HasContact ? FromManifoldContact(manifold.PrimaryContact) : default; |
| | 1761 | 242 | | Vector3d spatialLinear2D = ExactContactLever2D.ToSpatial(linear2D); |
| | 1761 | 243 | | Vector3d spatialAngular2D = new(Fixed64.Zero, -angular2D, Fixed64.Zero); |
| | 8695 | 244 | | foreach (ManifoldContact row in manifold) |
| | | 245 | | { |
| | 3248 | 246 | | if (!IsClosingContact(row, center3D, linear3D, angular3D, center2D, |
| | 3248 | 247 | | spatialLinear2D, spatialAngular2D, angular2D, owner2D)) continue; |
| | 1323 | 248 | | contact = FromManifoldContact(row); |
| | 1323 | 249 | | return true; |
| | | 250 | | } |
| | 438 | 251 | | return false; |
| | 1323 | 252 | | } |
| | | 253 | | |
| | | 254 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 255 | | internal static bool IsClosingContact(in ManifoldContact row, in ContactAnchor center3D, |
| | | 256 | | Vector3d linear3D, Vector3d angular3D, in ContactAnchor center2D, |
| | | 257 | | Vector3d spatialLinear2D, Vector3d spatialAngular2D, Fixed64 angular2D, LSCollider2D? owner2D) |
| | | 258 | | { |
| | 3354 | 259 | | ContactAnchor normalAnchor2D = owner2D == null ? row.AnchorB |
| | 3354 | 260 | | : ExactContactLever2D.GetDetectedNormalAnchor(owner2D, row.FeatureNamespaceB, center2D, row.AnchorB); |
| | 3354 | 261 | | ExactLever3D.GetRelativePointVelocityRatio(linear3D, angular3D, |
| | 3354 | 262 | | row.AnchorA.GetLeverFrom(center3D), spatialLinear2D, spatialAngular2D, |
| | 3354 | 263 | | normalAnchor2D.GetLeverFrom(center2D), row.Normal, |
| | 3354 | 264 | | out Signed832 numerator, out Signed832 denominator, secondPlanarAngularVelocity: angular2D); |
| | | 265 | | // A is always 3D and B is always 2D. Negative relative normal speed closes the row. |
| | 3354 | 266 | | return numerator.Sign < 0 && (!Fixed64.TryGetSignedRawRatio(numerator, denominator, 0, |
| | 3354 | 267 | | out Fixed64 speed) || speed < -Fixed64.Epsilon); |
| | | 268 | | } |
| | | 269 | | |
| | | 270 | | internal static bool IsClosingProjection(Vector3d displacement, Vector3d outwardNormal) |
| | | 271 | | { |
| | 747 | 272 | | if (Vector3d.TryDot(displacement, outwardNormal, out Fixed64 speed)) |
| | 743 | 273 | | return speed < -Fixed64.Epsilon; |
| | | 274 | | |
| | | 275 | | // A rounded unit normal can push a full-range projection beyond Q32.32. |
| | | 276 | | // Its exact sign still exists, and overflow already proves that its |
| | | 277 | | // magnitude exceeds the ordinary rounded closing-speed deadband. |
| | 4 | 278 | | return WideGeometry.GetDifferenceDotProduct3D( |
| | 4 | 279 | | displacement.X, Fixed64.Zero, displacement.Y, Fixed64.Zero, displacement.Z, Fixed64.Zero, |
| | 4 | 280 | | outwardNormal.X, Fixed64.Zero, outwardNormal.Y, Fixed64.Zero, outwardNormal.Z, Fixed64.Zero).Sign < 0; |
| | | 281 | | } |
| | | 282 | | |
| | | 283 | | private ContactAnchor CreatePlanarReference( |
| | | 284 | | Vector2d origin, |
| | | 285 | | Fixed64 rotation, |
| | | 286 | | Vector2d localPoint) => |
| | 1049 | 287 | | new( |
| | 1049 | 288 | | new Vector3d( |
| | 1049 | 289 | | origin.X, |
| | 1049 | 290 | | Anchor2D.Origin.Y, |
| | 1049 | 291 | | origin.Y), |
| | 1049 | 292 | | FixedQuaternion.FromAxisAngle(Vector3d.Up, -rotation), |
| | 1049 | 293 | | new Vector3d( |
| | 1049 | 294 | | localPoint.X, |
| | 1049 | 295 | | Fixed64.Zero, |
| | 1049 | 296 | | localPoint.Y)); |
| | | 297 | | |
| | | 298 | | private static Vector3d GetRequiredWorldPoint(ContactAnchor anchor, string propertyName) |
| | | 299 | | { |
| | 140 | 300 | | if (anchor.TryGetWorldPoint(out Vector3d point)) |
| | 138 | 301 | | return point; |
| | | 302 | | |
| | 2 | 303 | | throw new InvalidOperationException( |
| | 2 | 304 | | $"{propertyName} is outside the representable coordinate range. Use the corresponding TryGet method."); |
| | | 305 | | } |
| | | 306 | | } |