| | | 1 | | //======================================================================= |
| | | 2 | | // Physics3DHit.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 3D physics query. |
| | | 18 | | /// </summary> |
| | | 19 | | public readonly struct Physics3DHit |
| | | 20 | | { |
| | | 21 | | /// <summary> |
| | | 22 | | /// Creates a 3D query hit from a world-space surface witness. |
| | | 23 | | /// </summary> |
| | | 24 | | public Physics3DHit(LSCollider? collider, Vector3d point, Vector3d normal, Fixed64 distance, Vector3d direction) |
| | 2488 | 25 | | : this( |
| | 2488 | 26 | | collider, |
| | 2488 | 27 | | ContactAnchor.FromWorldPoint(point), |
| | 2488 | 28 | | normal, |
| | 2488 | 29 | | distance, |
| | 2488 | 30 | | direction) |
| | | 31 | | { |
| | 2488 | 32 | | } |
| | | 33 | | |
| | | 34 | | /// <summary> |
| | | 35 | | /// Creates a query hit with a rigid-frame surface witness. |
| | | 36 | | /// </summary> |
| | | 37 | | public Physics3DHit( |
| | | 38 | | LSCollider? collider, |
| | | 39 | | ContactAnchor anchor, |
| | | 40 | | Vector3d normal, |
| | | 41 | | Fixed64 distance, |
| | | 42 | | Vector3d direction) |
| | | 43 | | { |
| | 10304 | 44 | | if (!anchor.IsValid) |
| | 1 | 45 | | throw new ArgumentException("The hit anchor must be valid.", nameof(anchor)); |
| | | 46 | | |
| | 10303 | 47 | | Collider = collider; |
| | 10303 | 48 | | Body = collider?.Body; |
| | 10303 | 49 | | Anchor = anchor; |
| | 10303 | 50 | | Normal = normal; |
| | 10303 | 51 | | Distance = distance; |
| | 10303 | 52 | | Direction = direction; |
| | 10303 | 53 | | } |
| | | 54 | | |
| | | 55 | | /// <summary> |
| | | 56 | | /// Gets the collider reported by the query, if any. |
| | | 57 | | /// </summary> |
| | | 58 | | public LSCollider? Collider { get; } |
| | | 59 | | |
| | | 60 | | /// <summary> |
| | | 61 | | /// Gets the collider's body, or <see langword="null"/> for a bodyless hit. |
| | | 62 | | /// </summary> |
| | | 63 | | public SolidBody? Body { get; } |
| | | 64 | | |
| | | 65 | | /// <summary> |
| | | 66 | | /// Gets the authoritative rigid-frame query witness. |
| | | 67 | | /// </summary> |
| | | 68 | | public ContactAnchor Anchor { get; } |
| | | 69 | | |
| | | 70 | | /// <summary> |
| | | 71 | | /// Gets the materialized world-space query witness. |
| | | 72 | | /// </summary> |
| | | 73 | | /// <exception cref="InvalidOperationException"> |
| | | 74 | | /// The conceptual witness lies outside the representable coordinate range. |
| | | 75 | | /// </exception> |
| | | 76 | | public Vector3d Point |
| | | 77 | | { |
| | | 78 | | get |
| | | 79 | | { |
| | 45 | 80 | | if (TryGetPoint(out Vector3d point)) |
| | 44 | 81 | | return point; |
| | | 82 | | |
| | 1 | 83 | | throw new InvalidOperationException( |
| | 1 | 84 | | "Point is outside the representable coordinate range. Use TryGetPoint."); |
| | | 85 | | } |
| | | 86 | | } |
| | | 87 | | |
| | | 88 | | /// <summary> |
| | | 89 | | /// Attempts to materialize the world-space query witness without |
| | | 90 | | /// saturation. |
| | | 91 | | /// </summary> |
| | | 92 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 55 | 93 | | public bool TryGetPoint(out Vector3d point) => Anchor.TryGetWorldPoint(out point); |
| | | 94 | | |
| | | 95 | | /// <summary> |
| | | 96 | | /// Gets the world-space surface normal. |
| | | 97 | | /// </summary> |
| | | 98 | | public Vector3d Normal { get; } |
| | | 99 | | |
| | | 100 | | /// <summary> |
| | | 101 | | /// Gets the distance from the query origin to the hit. |
| | | 102 | | /// </summary> |
| | | 103 | | public Fixed64 Distance { get; } |
| | | 104 | | |
| | | 105 | | /// <summary> |
| | | 106 | | /// Gets the query direction associated with the hit. |
| | | 107 | | /// </summary> |
| | | 108 | | public Vector3d Direction { get; } |
| | | 109 | | } |