| | | 1 | | //======================================================================= |
| | | 2 | | // Contact2D.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 System; |
| | | 10 | | |
| | | 11 | | namespace Gravitas.CollisionHandling; |
| | | 12 | | |
| | | 13 | | /// <summary> |
| | | 14 | | /// Deterministic pure 2D contact data generated by the 2D narrow phase. |
| | | 15 | | /// </summary> |
| | | 16 | | internal readonly struct Contact2D |
| | | 17 | | { |
| | | 18 | | public Contact2D( |
| | | 19 | | Vector2d pointA, |
| | | 20 | | Vector2d pointB, |
| | | 21 | | Vector2d normal, |
| | | 22 | | Fixed64 depth, |
| | | 23 | | bool depthIsClamped = false) |
| | 5 | 24 | | : this( |
| | 5 | 25 | | ContactAnchor2D.FromWorldPoint(pointA), |
| | 5 | 26 | | ContactAnchor2D.FromWorldPoint(pointB), |
| | 5 | 27 | | normal, |
| | 5 | 28 | | depth, |
| | 5 | 29 | | depthIsClamped) |
| | 5 | 30 | | { } |
| | | 31 | | |
| | | 32 | | public Contact2D( |
| | | 33 | | ContactAnchor2D anchorA, |
| | | 34 | | ContactAnchor2D anchorB, |
| | | 35 | | Vector2d normal, |
| | | 36 | | Fixed64 depth, |
| | | 37 | | bool depthIsClamped = false) |
| | | 38 | | { |
| | 1538 | 39 | | AnchorA = anchorA; |
| | 1538 | 40 | | AnchorB = anchorB; |
| | 1538 | 41 | | Normal = normal; |
| | 1538 | 42 | | Depth = depth; |
| | 1538 | 43 | | DepthIsClamped = depthIsClamped; |
| | 1538 | 44 | | HasContact = true; |
| | 1538 | 45 | | } |
| | | 46 | | |
| | | 47 | | public bool HasContact { get; } |
| | | 48 | | |
| | | 49 | | public ContactAnchor2D AnchorA { get; } |
| | | 50 | | |
| | | 51 | | public ContactAnchor2D AnchorB { get; } |
| | | 52 | | |
| | 12 | 53 | | public Vector2d PointA => GetRequiredWorldPoint(AnchorA, nameof(PointA)); |
| | | 54 | | |
| | 11 | 55 | | public Vector2d PointB => GetRequiredWorldPoint(AnchorB, nameof(PointB)); |
| | | 56 | | |
| | | 57 | | /// <summary> |
| | | 58 | | /// Contact normal pointing from collider A toward collider B. |
| | | 59 | | /// </summary> |
| | | 60 | | public Vector2d Normal { get; } |
| | | 61 | | |
| | | 62 | | public Fixed64 Depth { get; } |
| | | 63 | | |
| | | 64 | | public bool DepthIsClamped { get; } |
| | | 65 | | |
| | | 66 | | private static Vector2d GetRequiredWorldPoint(ContactAnchor2D anchor, string propertyName) |
| | | 67 | | { |
| | 23 | 68 | | if (anchor.TryGetWorldPoint(out Vector2d point)) |
| | 22 | 69 | | return point; |
| | | 70 | | |
| | 1 | 71 | | throw new InvalidOperationException( |
| | 1 | 72 | | $"{propertyName} is outside the representable coordinate range."); |
| | | 73 | | } |
| | | 74 | | |
| | | 75 | | } |