| | | 1 | | //======================================================================= |
| | | 2 | | // Physics2DHit.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.Colliders; |
| | | 10 | | using Gravitas.CollisionHandling; |
| | | 11 | | using System; |
| | | 12 | | using System.Runtime.CompilerServices; |
| | | 13 | | |
| | | 14 | | namespace Gravitas.Queries; |
| | | 15 | | |
| | | 16 | | /// <summary> |
| | | 17 | | /// Result from a pure 2D query. |
| | | 18 | | /// </summary> |
| | | 19 | | public readonly struct Physics2DHit |
| | | 20 | | { |
| | | 21 | | /// <summary> |
| | | 22 | | /// Creates a 2D query hit from a world-space surface witness. |
| | | 23 | | /// </summary> |
| | | 24 | | public Physics2DHit(LSCollider2D collider, Vector2d point, Vector2d normal, Fixed64 distance) |
| | 647 | 25 | | : this( |
| | 647 | 26 | | collider, |
| | 647 | 27 | | ContactAnchor2D.FromWorldPoint(point), |
| | 647 | 28 | | normal, |
| | 647 | 29 | | distance) |
| | | 30 | | { |
| | 647 | 31 | | } |
| | | 32 | | |
| | | 33 | | /// <summary> |
| | | 34 | | /// Creates a 2D query hit with a rigid-frame surface witness. |
| | | 35 | | /// </summary> |
| | | 36 | | public Physics2DHit( |
| | | 37 | | LSCollider2D collider, |
| | | 38 | | ContactAnchor2D anchor, |
| | | 39 | | Vector2d normal, |
| | | 40 | | Fixed64 distance) |
| | | 41 | | { |
| | 45845 | 42 | | Collider = collider; |
| | 45845 | 43 | | Body = collider.Body; |
| | 45845 | 44 | | Anchor = anchor; |
| | 45845 | 45 | | Normal = normal; |
| | 45845 | 46 | | Distance = distance; |
| | 45845 | 47 | | } |
| | | 48 | | |
| | | 49 | | /// <summary> |
| | | 50 | | /// Gets the collider reported by the query. |
| | | 51 | | /// </summary> |
| | | 52 | | public LSCollider2D Collider { get; } |
| | | 53 | | |
| | | 54 | | /// <summary> |
| | | 55 | | /// Gets the collider's body, or <see langword="null"/> for a bodyless collider. |
| | | 56 | | /// </summary> |
| | | 57 | | public SolidBody2D? Body { get; } |
| | | 58 | | |
| | | 59 | | /// <summary> |
| | | 60 | | /// Gets the rigid-frame query witness. |
| | | 61 | | /// </summary> |
| | | 62 | | public ContactAnchor2D Anchor { get; } |
| | | 63 | | |
| | | 64 | | /// <summary> |
| | | 65 | | /// Gets the materialized world-space query witness. |
| | | 66 | | /// </summary> |
| | | 67 | | /// <exception cref="InvalidOperationException"> |
| | | 68 | | /// The conceptual witness lies outside the representable coordinate range. |
| | | 69 | | /// </exception> |
| | | 70 | | public Vector2d Point |
| | | 71 | | { |
| | | 72 | | get |
| | | 73 | | { |
| | 117 | 74 | | if (TryGetPoint(out Vector2d point)) |
| | 116 | 75 | | return point; |
| | | 76 | | |
| | 1 | 77 | | throw new InvalidOperationException( |
| | 1 | 78 | | "Point is outside the representable coordinate range. Use TryGetPoint."); |
| | | 79 | | } |
| | | 80 | | } |
| | | 81 | | |
| | | 82 | | /// <summary> |
| | | 83 | | /// Attempts to materialize the world-space query witness without saturation. |
| | | 84 | | /// </summary> |
| | | 85 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 162 | 86 | | public bool TryGetPoint(out Vector2d point) => Anchor.TryGetWorldPoint(out point); |
| | | 87 | | |
| | | 88 | | /// <summary> |
| | | 89 | | /// Gets the world-space surface normal. |
| | | 90 | | /// </summary> |
| | | 91 | | public Vector2d Normal { get; } |
| | | 92 | | |
| | | 93 | | /// <summary> |
| | | 94 | | /// Gets the distance from the query origin to the hit. |
| | | 95 | | /// </summary> |
| | | 96 | | public Fixed64 Distance { get; } |
| | | 97 | | } |