| | | 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 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 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) |
| | 4000 | 25 | | : this( |
| | 4000 | 26 | | collider, |
| | 4000 | 27 | | ContactAnchor.FromWorldPoint(point), |
| | 4000 | 28 | | normal, |
| | 4000 | 29 | | distance, |
| | 4000 | 30 | | direction) |
| | | 31 | | { |
| | 4000 | 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 | | { |
| | 22079 | 44 | | if (!anchor.IsValid) |
| | 1 | 45 | | throw new ArgumentException("The hit anchor must be valid.", nameof(anchor)); |
| | | 46 | | |
| | 22078 | 47 | | Collider = collider; |
| | 22078 | 48 | | Body = collider?.Body; |
| | 22078 | 49 | | Anchor = anchor; |
| | 22078 | 50 | | Normal = normal; |
| | 22078 | 51 | | Distance = distance; |
| | 22078 | 52 | | Direction = direction; |
| | 22078 | 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 | | { |
| | 561 | 80 | | if (TryGetPoint(out Vector3d point)) |
| | 560 | 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)] |
| | 818 | 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 query-specific distance used to rank this hit. |
| | | 102 | | /// </summary> |
| | | 103 | | /// <remarks> |
| | | 104 | | /// Rays and sweeps report query travel; cone volumes report axial distance. |
| | | 105 | | /// Projected overlaps use their overlap ranking distance, which can be zero |
| | | 106 | | /// for a contained origin. This need not equal origin-to-witness distance. |
| | | 107 | | /// </remarks> |
| | | 108 | | public Fixed64 Distance { get; } |
| | | 109 | | |
| | | 110 | | /// <summary> |
| | | 111 | | /// Gets the query direction associated with the hit. |
| | | 112 | | /// </summary> |
| | | 113 | | public Vector3d Direction { get; } |
| | | 114 | | } |