| | | 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 System; |
| | | 9 | | using System.Runtime.CompilerServices; |
| | | 10 | | using FixedMathSharp; |
| | | 11 | | using Gravitas.Colliders; |
| | | 12 | | using Gravitas.CollisionHandling; |
| | | 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) |
| | 2285 | 25 | | : this( |
| | 2285 | 26 | | collider, |
| | 2285 | 27 | | ContactAnchor2D.FromWorldPoint(point), |
| | 2285 | 28 | | normal, |
| | 2285 | 29 | | distance) |
| | | 30 | | { |
| | 2285 | 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 | | { |
| | 52544 | 42 | | Collider = collider; |
| | 52544 | 43 | | Body = collider.Body; |
| | 52544 | 44 | | Anchor = anchor; |
| | 52544 | 45 | | Normal = normal; |
| | 52544 | 46 | | Distance = distance; |
| | 52544 | 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 | | /// <remarks> |
| | | 68 | | /// A contained-start ray reports its start point at zero distance. That point |
| | | 69 | | /// may lie inside the target rather than on its surface. |
| | | 70 | | /// </remarks> |
| | | 71 | | /// <exception cref="InvalidOperationException"> |
| | | 72 | | /// The conceptual witness lies outside the representable coordinate range. |
| | | 73 | | /// </exception> |
| | | 74 | | public Vector2d Point |
| | | 75 | | { |
| | | 76 | | get |
| | | 77 | | { |
| | 304 | 78 | | if (TryGetPoint(out Vector2d point)) |
| | 303 | 79 | | return point; |
| | | 80 | | |
| | 1 | 81 | | throw new InvalidOperationException( |
| | 1 | 82 | | "Point is outside the representable coordinate range. Use TryGetPoint."); |
| | | 83 | | } |
| | | 84 | | } |
| | | 85 | | |
| | | 86 | | /// <summary> |
| | | 87 | | /// Attempts to materialize the world-space query witness without saturation. |
| | | 88 | | /// </summary> |
| | | 89 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 667 | 90 | | public bool TryGetPoint(out Vector2d point) => Anchor.TryGetWorldPoint(out point); |
| | | 91 | | |
| | | 92 | | /// <summary> |
| | | 93 | | /// Gets the world-space surface normal. |
| | | 94 | | /// </summary> |
| | | 95 | | /// <remarks> |
| | | 96 | | /// For a contained-start ray, this is the target's outward geometric |
| | | 97 | | /// separation normal, independent of ray direction. |
| | | 98 | | /// </remarks> |
| | | 99 | | public Vector2d Normal { get; } |
| | | 100 | | |
| | | 101 | | /// <summary> |
| | | 102 | | /// Gets the query-specific distance used to rank this hit. |
| | | 103 | | /// </summary> |
| | | 104 | | /// <remarks> |
| | | 105 | | /// Rays and sweeps report query travel. Overlaps rank by surface distance, |
| | | 106 | | /// clamped to zero for containment, rather than origin-to-witness distance. |
| | | 107 | | /// </remarks> |
| | | 108 | | public Fixed64 Distance { get; } |
| | | 109 | | } |