| | | 1 | | //======================================================================= |
| | | 2 | | // ConvexSweepHitPolicy.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 | | |
| | | 11 | | namespace Gravitas.Queries; |
| | | 12 | | |
| | | 13 | | internal static class ConvexSweepHitPolicy |
| | | 14 | | { |
| | | 15 | | internal static Vector3d ResolveHitNormal( |
| | | 16 | | LSCollider targetCollider, |
| | | 17 | | Vector3d point, |
| | | 18 | | Vector3d resultNormal, |
| | | 19 | | Vector3d fallbackNormal, |
| | | 20 | | Vector3d sweepDirection, |
| | | 21 | | Vector3d planarNormal, |
| | | 22 | | bool hasRefinedSurfaceNormal = false, |
| | | 23 | | bool hasMaterializedPoint = true) |
| | | 24 | | { |
| | 161 | 25 | | Vector3d surfaceNormal = hasRefinedSurfaceNormal |
| | 161 | 26 | | ? targetCollider.GetNormalAtPoint(point) |
| | 161 | 27 | | : Vector3d.Zero; |
| | 161 | 28 | | Vector3d normal = surfaceNormal.MagnitudeSquared > Fixed64.Epsilon |
| | 161 | 29 | | ? surfaceNormal |
| | 161 | 30 | | : planarNormal.MagnitudeSquared > Fixed64.Epsilon |
| | 161 | 31 | | ? planarNormal |
| | 161 | 32 | | : resultNormal.MagnitudeSquared > Fixed64.Epsilon |
| | 161 | 33 | | ? resultNormal |
| | 161 | 34 | | : fallbackNormal.MagnitudeSquared > Fixed64.Epsilon |
| | 161 | 35 | | ? fallbackNormal |
| | 161 | 36 | | : hasMaterializedPoint |
| | 161 | 37 | | ? targetCollider.GetNormalAtPoint(point) |
| | 161 | 38 | | : Vector3d.Zero; |
| | | 39 | | |
| | 161 | 40 | | if (normal.MagnitudeSquared <= Fixed64.Epsilon) |
| | | 41 | | { |
| | 3 | 42 | | return sweepDirection.MagnitudeSquared > Fixed64.Epsilon |
| | 3 | 43 | | ? -sweepDirection.Normalized |
| | 3 | 44 | | : Vector3d.Zero; |
| | | 45 | | } |
| | | 46 | | |
| | 158 | 47 | | normal = normal.Normalized; |
| | 158 | 48 | | return Vector3d.CompareProjection( |
| | 158 | 49 | | normal, |
| | 158 | 50 | | Vector3d.Zero, |
| | 158 | 51 | | sweepDirection) > 0 |
| | 158 | 52 | | ? -normal |
| | 158 | 53 | | : normal; |
| | | 54 | | } |
| | | 55 | | } |