< Summary

Line coverage
100%
Covered lines: 880
Uncovered lines: 0
Coverable lines: 880
Total lines: 1561
Line coverage: 100%
Branch coverage
100%
Covered branches: 230
Total branches: 230
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
File 1: TryRaycastCompound(...)100%66100%
File 1: TrySweepCircleCompound(...)100%66100%
File 1: TrySweepMoverCompound(...)100%44100%
File 1: TrySweepMoverAgainstCompound(...)100%66100%
File 2: TrySweepCapsuleMover(...)100%88100%
File 2: TrySweepConvexMoverAgainstCapsule(...)100%44100%
File 2: TrySweepConvexMoverAgainstConvex(...)100%44100%
File 2: TryBuildCapsuleTargetSweepHit(...)100%22100%
File 3: TryOverlapCircle(...)100%1212100%
File 3: TryOverlapCircleCenteredCapsule(...)100%22100%
File 3: TryOverlapPolygon(...)100%11100%
File 3: ValidateAabbSize(...)100%22100%
File 3: ValidateConvexQueryPolygon(...)100%11100%
File 3: CalculateAverageCenter(...)100%11100%
File 3: TryRaycast(...)100%88100%
File 3: TryRaycast(...)100%22100%
File 3: TryRaycastPrepared(...)100%1414100%
File 3: TrySweepCircle(...)100%1010100%
File 3: TrySweepCircle(...)100%22100%
File 3: TrySweepCirclePrepared(...)100%1212100%
File 3: TrySweepMoverShape(...)100%1818100%
File 3: TryRaycastCircle(...)100%22100%
File 3: TrySweepCircleCircle(...)100%22100%
File 3: TryRaycastCapsule(...)100%22100%
File 3: TrySweepCircleCapsule(...)100%44100%
File 3: TryOverlapCircleCompound(...)100%66100%
File 3: TryOverlapConvexArea(...)100%66100%
File 3: TryOverlapConvexAreaCompound(...)100%66100%
File 3: TryOverlapConvexAreaCircle(...)100%22100%
File 3: TryOverlapConvexAreaCapsule(...)100%22100%
File 3: TryOverlapConvexAreaConvex(...)100%22100%
File 3: TryBuildCenteredCapsuleAreaHit(...)100%22100%
File 3: GetCenteredCapsuleSurfaceAnchor(...)100%11100%
File 3: TryBuildConvexAreaHit(...)100%66100%
File 3: TryRaycastConvex(...)100%22100%
File 3: TrySweepCircleConvex(...)100%22100%
File 3: TrySweepConvexMoverAgainstCircle(...)100%44100%
File 4: TryOffsetPoint(...)100%22100%
File 4: ContainsPointExact(...)100%88100%
File 4: TryGetContainedRayNormal(...)100%88100%
File 4: GetConvexVertexOffsets(...)100%44100%
File 4: TryKeepEarlierHit(...)100%22100%
File 4: TryKeepCloserHit(...)100%22100%
File 4: SegmentBoundsOverlap(...)100%66100%
File 4: SweepBoundsOverlap(...)100%66100%
File 4: ResolveQueryFallbackNormal(...)100%22100%
File 5: TryFindSupport(...)100%88100%
File 5: TryFindLeafSupport(...)100%1010100%

File(s)

/home/runner/work/Gravitas/Gravitas/src/Gravitas/Queries/2D/Detection/QueryDetection2D.Compound.cs

#LineLine coverage
 1//=======================================================================
 2// QueryDetection2D.Compound.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
 8using FixedMathSharp;
 9using Gravitas.Colliders;
 10
 11namespace Gravitas.Queries;
 12
 13/// <content>Reduces planar raycasts and sweeps over the authored parts of compound colliders.</content>
 14internal static partial class QueryDetection2D
 15{
 16    private static bool TryRaycastCompound(
 17        Vector2d start,
 18        Vector2d end,
 19        Vector2d segment,
 20        Fixed64 segmentLength,
 21        LSCompoundCollider2D compound,
 22        out Physics2DHit hit)
 23    {
 43724        bool found = false;
 43725        Physics2DHit best = default;
 26
 346427        for (int i = 0; i < compound.PartCount; i++)
 28        {
 129529            LSCollider2D part = compound.GetPartCollider(i);
 129530            if (!TryRaycast(start, end, segment, segmentLength, part, out Physics2DHit candidate))
 31                continue;
 32
 86833            TryKeepEarlierHit(candidate, ref found, ref best);
 34        }
 35
 43736        if (!found)
 37        {
 138            hit = default;
 139            return false;
 40        }
 41
 43642        hit = new Physics2DHit(compound, best.Anchor, best.Normal, best.Distance);
 43643        return true;
 44    }
 45
 46    private static bool TrySweepCircleCompound(
 47        Vector2d start,
 48        Vector2d end,
 49        Vector2d segment,
 50        Fixed64 segmentLength,
 51        Fixed64 radius,
 52        LSCompoundCollider2D compound,
 53        out Physics2DHit hit)
 54    {
 2755        bool found = false;
 2756        Physics2DHit best = default;
 57
 17458        for (int i = 0; i < compound.PartCount; i++)
 59        {
 6060            LSCollider2D part = compound.GetPartCollider(i);
 6061            if (!TrySweepCircle(start, end, segment, segmentLength, radius, part, out Physics2DHit candidate))
 62                continue;
 63
 4164            TryKeepEarlierHit(candidate, ref found, ref best);
 65        }
 66
 2767        if (!found)
 68        {
 169            hit = default;
 170            return false;
 71        }
 72
 2673        hit = new Physics2DHit(compound, best.Anchor, best.Normal, best.Distance);
 2674        return true;
 75    }
 76
 77    private static bool TrySweepMoverCompound(
 78        LSCompoundCollider2D mover,
 79        Vector2d displacement,
 80        LSCollider2D target,
 81        out Physics2DHit hit)
 82    {
 483        bool found = false;
 484        Physics2DHit best = default;
 85
 2286        for (int i = 0; i < mover.PartCount; i++)
 87        {
 788            LSCollider2D part = mover.GetPartCollider(i);
 789            if (!TrySweepMoverShape(part, displacement, target, out Physics2DHit candidate))
 90                continue;
 91
 292            TryKeepEarlierHit(candidate, ref found, ref best);
 93        }
 94
 495        hit = best;
 496        return found;
 97    }
 98
 99    private static bool TrySweepMoverAgainstCompound(
 100        LSCollider2D mover,
 101        Vector2d displacement,
 102        LSCompoundCollider2D target,
 103        out Physics2DHit hit)
 104    {
 3105        bool found = false;
 3106        Physics2DHit best = default;
 107
 18108        for (int i = 0; i < target.PartCount; i++)
 109        {
 6110            LSCollider2D part = target.GetPartCollider(i);
 6111            if (!TrySweepMoverShape(mover, displacement, part, out Physics2DHit candidate))
 112                continue;
 113
 4114            TryKeepCloserHit(candidate, ref found, ref best);
 115        }
 116
 3117        if (!found)
 118        {
 1119            hit = default;
 1120            return false;
 121        }
 122
 2123        hit = new Physics2DHit(target, best.Anchor, best.Normal, best.Distance);
 2124        return true;
 125    }
 126}

/home/runner/work/Gravitas/Gravitas/src/Gravitas/Queries/2D/Detection/QueryDetection2D.ConvexSweeps.cs

#LineLine coverage
 1//=======================================================================
 2// QueryDetection2D.ConvexSweeps.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
 8using System;
 9using FixedMathSharp;
 10using FixedMathSharp.Geometry;
 11using Gravitas.Colliders;
 12using Gravitas.CollisionHandling;
 13
 14namespace Gravitas.Queries;
 15
 16/// <content>Tests planar sweeps between convex shapes and constructs the earliest accepted hit.</content>
 17internal static partial class QueryDetection2D
 18{
 19    private static bool TrySweepCapsuleMover(
 20        LSCapsuleCollider2D mover,
 21        Vector2d displacement,
 22        Fixed64 length,
 23        LSCollider2D target,
 24        out Physics2DHit hit)
 25    {
 29426        if (CollisionDetection2D.TryCollide(mover, target, out Contact2D overlap))
 27        {
 4828            hit = new Physics2DHit(target, overlap.AnchorB, -overlap.Normal, Fixed64.Zero);
 4829            return true;
 30        }
 31
 24632        if (target is LSCircleCollider2D circle)
 33        {
 534            bool found = WideFiniteAxisIntersection.TryGetSweptCenteredCapsuleSegmentFirstDistance(
 535                mover.Center,
 536                mover.WorldAxis,
 537                mover.AxisLength,
 538                mover.ScaledRadius,
 539                displacement,
 540                length,
 541                new FixedSegment2d(circle.Center, circle.Center),
 542                circle.ScaledRadius,
 543                out Fixed64 distance,
 544                out Vector2d normal,
 545                authoredDisplacement: true);
 546            return TryBuildCapsuleTargetSweepHit(
 547                mover,
 548                displacement,
 549                length,
 550                target,
 551                circle.Center,
 552                circle.Rotation,
 553                Fixed64.Zero,
 554                circle.ScaledRadius,
 555                found,
 556                distance,
 557                normal,
 558                out hit);
 59        }
 60
 24161        if (target is LSCapsuleCollider2D targetCapsule)
 62        {
 763            bool found = WideFiniteAxisIntersection.TryGetSweptCenteredCapsulesFirstDistance(
 764                mover.Center,
 765                mover.WorldAxis,
 766                mover.AxisLength,
 767                mover.ScaledRadius,
 768                displacement,
 769                length,
 770                targetCapsule.Center,
 771                targetCapsule.WorldAxis,
 772                targetCapsule.AxisLength,
 773                targetCapsule.ScaledRadius,
 774                out Fixed64 distance,
 775                out Vector2d normal,
 776                authoredDisplacement: true);
 777            return TryBuildCapsuleTargetSweepHit(
 778                mover,
 779                displacement,
 780                length,
 781                target,
 782                targetCapsule.Center,
 783                targetCapsule.Rotation,
 784                targetCapsule.AxisLength,
 785                targetCapsule.ScaledRadius,
 786                found,
 787                distance,
 788                normal,
 789                out hit);
 90        }
 91
 23492        Span<Vector2d> scratch = stackalloc Vector2d[4];
 23493        ReadOnlySpan<Vector2d> targetOffsets =
 23494            GetConvexVertexOffsets(target, scratch);
 23495        if (!FixedConvex2dRelations.TryGetSweptCenteredCapsuleFirstDistanceCore(
 23496                mover.Center,
 23497                mover.WorldAxis,
 23498                mover.AxisLength,
 23499                mover.ScaledRadius,
 234100                displacement,
 234101                length,
 234102                target.Center,
 234103                target.ConvexRotation,
 234104                targetOffsets,
 234105                out Fixed64 convexDistance,
 234106                out Vector2d convexNormal,
 234107                out Vector2d targetContactOffset,
 234108                authoredDisplacement: true))
 109        {
 6110            hit = default;
 6111            return false;
 112        }
 113
 228114        hit = new Physics2DHit(
 228115            target,
 228116            new ContactAnchor2D(new FixedPointAnchor2d(target.Center, target.ConvexRotation, targetContactOffset)),
 228117            convexNormal,
 228118            convexDistance);
 228119        return true;
 120    }
 121
 122    private static bool TrySweepConvexMoverAgainstCapsule(
 123        LSCollider2D mover,
 124        Vector2d displacement,
 125        Fixed64 displacementLength,
 126        LSCapsuleCollider2D target,
 127        out Physics2DHit hit)
 128    {
 4129        if (CollisionDetection2D.TryCollide(mover, target, out Contact2D overlap))
 130        {
 1131            hit = new Physics2DHit(target, overlap.AnchorB, -overlap.Normal, Fixed64.Zero);
 1132            return true;
 133        }
 134
 3135        if (!TrySweepCapsuleMover(
 3136                target,
 3137                -displacement,
 3138                displacementLength,
 3139                mover,
 3140                out Physics2DHit reverseHit))
 141        {
 1142            hit = default;
 1143            return false;
 144        }
 145
 146        // A reported first hit lies between the admitted start center and the
 147        // in-domain target feature, so the impact center is representable.
 2148        Vector2d reverseOffset = new FixedSegment2d(Vector2d.Zero, -displacement)
 2149            .GetPointAtDistance(reverseHit.Distance, displacementLength);
 2150        _ = Vector2d.TryAdd(target.Center, reverseOffset, out Vector2d movedCapsuleCenter);
 151
 2152        Span<Vector2d> moverScratch = stackalloc Vector2d[4];
 2153        ReadOnlySpan<Vector2d> moverOffsets =
 2154            GetConvexVertexOffsets(mover, moverScratch);
 2155        Span<FixedPointAnchor2d> capsuleContacts =
 2156            stackalloc FixedPointAnchor2d[2];
 2157        Span<FixedPointAnchor2d> convexContacts =
 2158            stackalloc FixedPointAnchor2d[2];
 2159        _ = FixedSegment2d.TryGetCenteredCapsuleConvexContacts(
 2160            movedCapsuleCenter,
 2161            target.Rotation,
 2162            Vector2d.Forward,
 2163            target.AxisLength,
 2164            target.ScaledRadius,
 2165            mover.Center,
 2166            mover.ConvexRotation,
 2167            moverOffsets,
 2168            capsuleContacts,
 2169            convexContacts,
 2170            out int contactCount,
 2171            out Vector2d normal,
 2172            out _,
 2173            out _);
 174
 2175        hit = new Physics2DHit(
 2176            target,
 2177            new ContactAnchor2D(
 2178                target.Center,
 2179                capsuleContacts[contactCount - 1].Rotation,
 2180                capsuleContacts[contactCount - 1].LocalPoint,
 2181                capsuleContacts[contactCount - 1].LocalDisplacement),
 2182            normal,
 2183            reverseHit.Distance);
 2184        return true;
 185    }
 186
 187    private static bool TrySweepConvexMoverAgainstConvex(
 188        LSCollider2D mover,
 189        Vector2d displacement,
 190        Fixed64 segmentLength,
 191        LSCollider2D target,
 192        out Physics2DHit hit)
 193    {
 239194        if (CollisionDetection2D.TryCollide(mover, target, out Contact2D overlap))
 195        {
 226196            hit = new Physics2DHit(target, overlap.AnchorB, -overlap.Normal, Fixed64.Zero);
 226197            return true;
 198        }
 199
 13200        Span<Vector2d> moverScratch = stackalloc Vector2d[4];
 13201        Span<Vector2d> targetScratch = stackalloc Vector2d[4];
 13202        ReadOnlySpan<Vector2d> moverOffsets =
 13203            GetConvexVertexOffsets(mover, moverScratch);
 13204        ReadOnlySpan<Vector2d> targetOffsets =
 13205            GetConvexVertexOffsets(target, targetScratch);
 13206        if (!FixedConvex2dRelations.TryGetSweptConvexFirstDistanceCore(
 13207                mover.Center,
 13208                mover.ConvexRotation,
 13209                moverOffsets,
 13210                displacement,
 13211                segmentLength,
 13212                target.Center,
 13213                target.ConvexRotation,
 13214                targetOffsets,
 13215                out Fixed64 distance,
 13216                out Vector2d normal,
 13217                out Vector2d targetContactOffset,
 13218                authoredDisplacement: true))
 219        {
 8220            hit = default;
 8221            return false;
 222        }
 223
 5224        hit = new Physics2DHit(
 5225            target,
 5226            new ContactAnchor2D(new FixedPointAnchor2d(target.Center, target.ConvexRotation, targetContactOffset)),
 5227            normal,
 5228            distance);
 5229        return true;
 230    }
 231
 232    private static bool TryBuildCapsuleTargetSweepHit(
 233        LSCapsuleCollider2D mover,
 234        Vector2d displacement,
 235        Fixed64 length,
 236        LSCollider2D target,
 237        Vector2d targetCenter,
 238        Fixed64 targetRotation,
 239        Fixed64 targetAxisLength,
 240        Fixed64 targetRadius,
 241        bool found,
 242        Fixed64 distance,
 243        Vector2d moverToTargetNormal,
 244        out Physics2DHit hit)
 245    {
 12246        if (!found)
 247        {
 1248            hit = default;
 1249            return false;
 250        }
 251
 252        // The swept-capsule solver only reports a first distance whose impact
 253        // center and exact contact are representable in the admitted frames.
 11254        Vector2d movedOffset = new FixedSegment2d(Vector2d.Zero, displacement)
 11255            .GetPointAtDistance(distance, length);
 11256        _ = Vector2d.TryAdd(mover.Center, movedOffset, out Vector2d movedCenter);
 11257        _ = FixedSegment2d.TryGetCenteredCapsulesContact(
 11258            movedCenter,
 11259            mover.Rotation,
 11260            mover.AxisLength,
 11261            mover.ScaledRadius,
 11262            targetCenter,
 11263            targetRotation,
 11264            targetAxisLength,
 11265            targetRadius,
 11266            moverToTargetNormal,
 11267            out FixedContactAnchors2d contact);
 268
 11269        hit = new Physics2DHit(
 11270            target,
 11271            new ContactAnchor2D(contact.SecondAnchor),
 11272            -contact.Normal,
 11273            distance);
 11274        return true;
 275    }
 276}

/home/runner/work/Gravitas/Gravitas/src/Gravitas/Queries/2D/Detection/QueryDetection2D.cs

#LineLine coverage
 1//=======================================================================
 2// QueryDetection2D.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
 8using System;
 9using FixedMathSharp;
 10using FixedMathSharp.Geometry;
 11using Gravitas.Colliders;
 12using Gravitas.CollisionHandling;
 13
 14namespace Gravitas.Queries;
 15
 16/// <summary>
 17/// Deterministic pure 2D shape checks used by query services.
 18/// </summary>
 19/// <content>Owns overlap, raycast and circle-sweep detection for pure 2D shapes.</content>
 20internal static partial class QueryDetection2D
 21{
 22    internal static bool TryOverlapCircle(
 23        Vector2d center,
 24        Fixed64 radius,
 25        LSCollider2D collider,
 26        out Physics2DHit hit)
 27    {
 2389128        ChronicleGuard.ThrowIfArgument(radius < Fixed64.Zero, nameof(radius), "2D query radius cannot be negative.");
 29
 2389130        if (collider is LSCompoundCollider2D compound)
 831            return TryOverlapCircleCompound(center, radius, compound, out hit);
 2388332        if (collider is LSCircleCollider2D circle)
 2185533            return TryOverlapCircleCenteredCapsule(
 2185534                center,
 2185535                radius,
 2185536                circle.Center,
 2185537                circle.Rotation,
 2185538                Fixed64.Zero,
 2185539                circle.ScaledRadius,
 2185540                Vector2d.Right,
 2185541                circle,
 2185542                out hit);
 202843        if (collider is LSCapsuleCollider2D capsule)
 41044            return TryOverlapCircleCenteredCapsule(
 41045                center,
 41046                radius,
 41047                capsule.Center,
 41048                capsule.Rotation,
 41049                capsule.AxisLength,
 41050                capsule.ScaledRadius,
 41051                capsule.WorldAxis.RotatedRight,
 41052                capsule,
 41053                out hit);
 161854        if (collider is not IConvexVertexSource2D)
 55        {
 256            hit = default;
 257            return false;
 58        }
 59
 161660        Span<Vector2d> scratch = stackalloc Vector2d[4];
 161661        ReadOnlySpan<Vector2d> vertexOffsets =
 161662            GetConvexVertexOffsets(collider, scratch);
 161663        if (!FixedConvex2dRelations.TryGetCircleContact(
 161664                center,
 161665                Fixed64.Zero,
 161666                radius,
 161667                collider.Center,
 161668                collider.ConvexRotation,
 161669                vertexOffsets,
 161670                out _,
 161671                out FixedPointAnchor2d contactAnchor,
 161672                out Vector2d circleToColliderNormal,
 161673                out Fixed64 depth,
 161674                out _))
 75        {
 42876            hit = default;
 42877            return false;
 78        }
 79
 118880        bool containsCenter = FixedConvex2dRelations.ContainsPoint(
 118881            center,
 118882            collider.Center,
 118883            collider.ConvexRotation,
 118884            vertexOffsets);
 118885        Fixed64 distance = Fixed64.Zero;
 118886        Vector2d normal = -circleToColliderNormal;
 118887        if (!containsCenter)
 90188            distance = radius - depth;
 89
 118890        hit = new Physics2DHit(
 118891            collider,
 118892            new ContactAnchor2D(contactAnchor),
 118893            normal,
 118894            distance);
 118895        return true;
 96    }
 97
 98    private static bool TryOverlapCircleCenteredCapsule(
 99        Vector2d queryCenter,
 100        Fixed64 queryRadius,
 101        Vector2d targetCenter,
 102        Fixed64 targetRotation,
 103        Fixed64 targetAxisLength,
 104        Fixed64 targetRadius,
 105        Vector2d targetFallbackNormal,
 106        LSCollider2D target,
 107        out Physics2DHit hit)
 108    {
 22265109        if (!FixedSegment2d.TryGetCenteredCapsulesContact(
 22265110                queryCenter,
 22265111                Fixed64.Zero,
 22265112                Fixed64.Zero,
 22265113                queryRadius,
 22265114                targetCenter,
 22265115                targetRotation,
 22265116                targetAxisLength,
 22265117                targetRadius,
 22265118                -targetFallbackNormal,
 22265119                out FixedContactAnchors2d contact))
 120        {
 7408121            hit = default;
 7408122            return false;
 123        }
 124
 14857125        Fixed64 distance = FixedSegment2d.GetDistanceToCenteredCapsule(
 14857126            queryCenter,
 14857127            targetCenter,
 14857128            targetRotation,
 14857129            targetAxisLength,
 14857130            targetRadius);
 131
 14857132        hit = new Physics2DHit(
 14857133            target,
 14857134            new ContactAnchor2D(contact.SecondAnchor),
 14857135            -contact.Normal,
 14857136            distance);
 14857137        return true;
 138    }
 139
 140    internal static bool TryOverlapPolygon(
 141        ReadOnlySpan<Vector2d> vertices,
 142        Vector2d center,
 143        LSCollider2D collider,
 144        out Physics2DHit hit)
 145    {
 7802146        return TryOverlapConvexArea(center, vertices, collider, out hit);
 147    }
 148
 149    internal static Vector2d ValidateAabbSize(Vector2d size)
 150    {
 1691151        ChronicleGuard.ThrowIfArgument(
 1691152            size.X <= Fixed64.Zero || size.Y <= Fixed64.Zero,
 1691153            nameof(size),
 1691154            "2D AABB query size components must be greater than zero.");
 1689155        Vector2d halfExtents = size * Fixed64.Half;
 1689156        ColliderGeometryPolicy.ValidateReach(halfExtents, nameof(size));
 1686157        return halfExtents;
 158    }
 159
 160    internal static void ValidateConvexQueryPolygon(ReadOnlySpan<Vector2d> vertices)
 161    {
 34162        ChronicleGuard.ThrowIfArgument(vertices.Length < 3, nameof(vertices), "2D polygon query must contain at least th
 34163        ChronicleGuard.ThrowIfArgument(
 34164            !FixedConvex2dRelations.IsStrictlyConvex(vertices),
 34165            nameof(vertices),
 34166            "2D polygon query vertices must form a strictly convex boundary.");
 34167        ColliderGeometryPolicy.ValidateWorldPolygonReach(vertices);
 19168    }
 169
 170    internal static Vector2d CalculateAverageCenter(ReadOnlySpan<Vector2d> vertices) =>
 16171        Vector2d.GetAverage(vertices);
 172
 173    internal static bool TryRaycast(Vector2d start, Vector2d end, LSCollider2D collider, out Physics2DHit hit)
 174    {
 380175        if (!Vector2d.TrySubtract(end, start, out Vector2d segment))
 176        {
 1177            hit = default;
 1178            return false;
 179        }
 180
 379181        if (segment == Vector2d.Zero || !SegmentBoundsOverlap(start, end, collider))
 182        {
 33183            hit = default;
 33184            return false;
 185        }
 186
 346187        if (!Vector2d.TryGetMagnitude(segment, out Fixed64 segmentLength))
 188        {
 1189            hit = default;
 1190            return false;
 191        }
 192
 345193        return TryRaycastPrepared(start, end, segment, segmentLength, collider, out hit);
 194    }
 195
 196    // The caller has admitted the exact endpoint difference and its positive,
 197    // representable length. Keep shape bounds checks local to each compound part.
 198    internal static bool TryRaycast(
 199        Vector2d start, Vector2d end, Vector2d segment, Fixed64 segmentLength,
 200        LSCollider2D collider, out Physics2DHit hit)
 201    {
 18212202        if (!SegmentBoundsOverlap(start, end, collider))
 203        {
 432204            hit = default;
 432205            return false;
 206        }
 207
 17780208        return TryRaycastPrepared(start, end, segment, segmentLength, collider, out hit);
 209    }
 210
 211    private static bool TryRaycastPrepared(
 212        Vector2d start, Vector2d end, Vector2d segment, Fixed64 segmentLength,
 213        LSCollider2D collider, out Physics2DHit hit)
 214    {
 18125215        if (collider is LSCompoundCollider2D compound)
 437216            return TryRaycastCompound(start, end, segment, segmentLength, compound, out hit);
 217
 17688218        if (ContainsPointExact(collider, start))
 219        {
 220            // A contained ray has no entry normal. Reuse point/shape geometry
 221            // to describe an actual separating surface without contact anchors,
 222            // while retaining the ray's zero-distance/start-point contract.
 2264223            if (!TryGetContainedRayNormal(start, collider, out Vector2d normal))
 224            {
 1225                hit = default;
 1226                return false;
 227            }
 2263228            hit = new Physics2DHit(
 2263229                collider,
 2263230                start,
 2263231                normal,
 2263232                Fixed64.Zero);
 2263233            return true;
 234        }
 235
 15424236        if (collider is LSCircleCollider2D circle)
 14090237            return TryRaycastCircle(start, end, segmentLength, circle, out hit);
 1334238        if (collider is LSCapsuleCollider2D capsule)
 275239            return TryRaycastCapsule(start, end, segmentLength, capsule, out hit);
 1059240        if (collider is not LSAABBoxCollider2D
 1059241            && collider is not LSPolygonCollider2D)
 242        {
 8243            hit = default;
 8244            return false;
 245        }
 246
 1051247        return TryRaycastConvex(start, segment, segmentLength, collider, out hit);
 248    }
 249
 250    internal static bool TrySweepCircle(
 251        Vector2d start,
 252        Vector2d end,
 253        Fixed64 radius,
 254        LSCollider2D collider,
 255        out Physics2DHit hit)
 256    {
 129257        if (!Vector2d.TrySubtract(end, start, out Vector2d segment))
 258        {
 1259            hit = default;
 1260            return false;
 261        }
 262
 128263        if (segment == Vector2d.Zero || !SweepBoundsOverlap(start, end, radius, collider))
 264        {
 71265            hit = default;
 71266            return false;
 267        }
 268
 57269        if (!Vector2d.TryGetMagnitude(segment, out Fixed64 segmentLength)
 57270            || segmentLength <= Fixed64.Epsilon)
 271        {
 4272            hit = default;
 4273            return false;
 274        }
 275
 53276        return TrySweepCirclePrepared(start, end, segment, segmentLength, radius, collider, out hit);
 277    }
 278
 279    // Generic sweeps admit travel greater than Epsilon. Authored support queries
 280    // retain their separate distance domain and stationary-contact semantics.
 281    internal static bool TrySweepCircle(
 282        Vector2d start, Vector2d end, Vector2d segment, Fixed64 segmentLength,
 283        Fixed64 radius, LSCollider2D collider, out Physics2DHit hit)
 284    {
 9248285        if (!SweepBoundsOverlap(start, end, radius, collider))
 286        {
 32287            hit = default;
 32288            return false;
 289        }
 290
 9216291        return TrySweepCirclePrepared(start, end, segment, segmentLength, radius, collider, out hit);
 292    }
 293
 294    private static bool TrySweepCirclePrepared(
 295        Vector2d start, Vector2d end, Vector2d segment, Fixed64 segmentLength,
 296        Fixed64 radius, LSCollider2D collider, out Physics2DHit hit)
 297    {
 9269298        if (collider is LSCompoundCollider2D compound)
 27299            return TrySweepCircleCompound(start, end, segment, segmentLength, radius, compound, out hit);
 300
 9242301        if (TryOverlapCircle(start, radius, collider, out Physics2DHit overlapHit))
 302        {
 1440303            hit = new Physics2DHit(collider, overlapHit.Anchor, overlapHit.Normal, Fixed64.Zero);
 1440304            return true;
 305        }
 306
 7802307        if (collider is LSCircleCollider2D circle)
 7125308            return TrySweepCircleCircle(start, end, segmentLength, radius, circle, out hit);
 677309        if (collider is LSCapsuleCollider2D capsule)
 274310            return TrySweepCircleCapsule(start, end, segmentLength, radius, capsule, out hit);
 403311        if (collider is not LSAABBoxCollider2D
 403312            && collider is not LSPolygonCollider2D)
 313        {
 1314            hit = default;
 1315            return false;
 316        }
 317
 402318        return TrySweepCircleConvex(start, segment, segmentLength, radius, collider, out hit,
 402319            authoredDisplacement: true);
 320    }
 321
 322    internal static bool TrySweepMoverShape(
 323        LSCollider2D mover,
 324        Vector2d displacement,
 325        LSCollider2D target,
 326        out Physics2DHit hit)
 327    {
 795328        ChronicleGuard.ThrowIfNull(mover, nameof(mover));
 795329        ChronicleGuard.ThrowIfNull(target, nameof(target));
 330
 795331        if (!Vector2d.TryGetMagnitude(displacement, out Fixed64 displacementLength)
 795332            || displacementLength <= Fixed64.Epsilon)
 333        {
 2334            hit = default;
 2335            return false;
 336        }
 337
 793338        if (mover is LSCompoundCollider2D moverCompound)
 4339            return TrySweepMoverCompound(moverCompound, displacement, target, out hit);
 340
 789341        if (target is LSCompoundCollider2D targetCompound)
 3342            return TrySweepMoverAgainstCompound(mover, displacement, targetCompound, out hit);
 343
 786344        if (mover is LSCircleCollider2D circle)
 345        {
 211346            if (!Vector2d.TryAdd(
 211347                    circle.Center,
 211348                    displacement,
 211349                    out Vector2d end))
 350            {
 1351                hit = default;
 1352                return false;
 353            }
 354
 210355            return TrySweepCircle(
 210356                circle.Center,
 210357                end,
 210358                displacement,
 210359                displacementLength,
 210360                circle.ScaledRadius,
 210361                target,
 210362                out hit);
 363        }
 575364        if (mover is LSCapsuleCollider2D moverCapsule)
 291365            return TrySweepCapsuleMover(
 291366                moverCapsule,
 291367                displacement,
 291368                displacementLength,
 291369                target,
 291370                out hit);
 371
 284372        if (target is LSCircleCollider2D targetCircle)
 41373            return TrySweepConvexMoverAgainstCircle(mover, displacement, targetCircle, out hit);
 243374        if (target is LSCapsuleCollider2D targetCapsule)
 4375            return TrySweepConvexMoverAgainstCapsule(
 4376                mover,
 4377                displacement,
 4378                displacementLength,
 4379                targetCapsule,
 4380                out hit);
 381
 239382        return TrySweepConvexMoverAgainstConvex(
 239383            mover,
 239384            displacement,
 239385            displacementLength,
 239386            target,
 239387            out hit);
 388    }
 389
 390    private static bool TryRaycastCircle(
 391        Vector2d start,
 392        Vector2d end,
 393        Fixed64 segmentLength,
 394        LSCircleCollider2D circle,
 395        out Physics2DHit hit)
 396    {
 14090397        var query = new FixedSegment2d(start, end);
 14090398        if (!query.TryGetCircleIntersectionDistanceInterval(
 14090399                new FixedBoundCircle(circle.Center, circle.ScaledRadius),
 14090400                segmentLength,
 14090401                out Fixed64 distance,
 14090402                out _))
 403        {
 6404            hit = default;
 6405            return false;
 406        }
 407
 14084408        Vector2d point = query.GetPointAtDistance(distance, segmentLength);
 14084409        Vector2d normal = Vector2d.GetDirection(circle.Center, point);
 14084410        hit = new Physics2DHit(
 14084411            circle,
 14084412            new ContactAnchor2D(
 14084413                circle.Center,
 14084414                normal * circle.ScaledRadius),
 14084415            normal,
 14084416            distance);
 14084417        return true;
 418    }
 419
 420    private static bool TrySweepCircleCircle(
 421        Vector2d start,
 422        Vector2d end,
 423        Fixed64 segmentLength,
 424        Fixed64 radius,
 425        LSCircleCollider2D circle,
 426        out Physics2DHit hit)
 427    {
 7128428        var query = new FixedSegment2d(start, end);
 7128429        if (!query.TryGetCircleIntersectionDistanceInterval(
 7128430                new FixedBoundCircle(circle.Center, circle.ScaledRadius),
 7128431                radius,
 7128432                segmentLength,
 7128433                out Fixed64 distance,
 7128434                out _,
 7128435                out _,
 7128436                out _))
 437        {
 13438            hit = default;
 13439            return false;
 440        }
 441
 7115442        Vector2d sweptCenter = query.GetPointAtDistance(distance, segmentLength);
 7115443        Vector2d normal = Vector2d.GetDirection(circle.Center, sweptCenter);
 7115444        hit = new Physics2DHit(
 7115445            circle,
 7115446            new ContactAnchor2D(
 7115447                circle.Center,
 7115448                normal * circle.ScaledRadius),
 7115449            normal,
 7115450            distance);
 7115451        return true;
 452    }
 453
 454    private static bool TryRaycastCapsule(
 455        Vector2d start,
 456        Vector2d end,
 457        Fixed64 segmentLength,
 458        LSCapsuleCollider2D capsule,
 459        out Physics2DHit hit)
 460    {
 275461        var query = new FixedSegment2d(start, end);
 275462        if (!query.TryGetCapsuleIntersectionDistanceInterval(
 275463                capsule.Center,
 275464                capsule.Rotation,
 275465                capsule.AxisLength,
 275466                capsule.ScaledRadius,
 275467                Fixed64.Zero,
 275468                segmentLength,
 275469                out Fixed64 distance,
 275470                out _,
 275471                out _,
 275472                out _))
 473        {
 3474            hit = default;
 3475            return false;
 476        }
 477
 272478        Vector2d pointOnRay = query.GetPointAtDistance(distance, segmentLength);
 272479        Vector2d normal = capsule.GetNormalFromCenteredAxis(pointOnRay);
 272480        hit = new Physics2DHit(
 272481            capsule,
 272482            ContactAnchor2D.FromWorldPoint(pointOnRay),
 272483            normal,
 272484            distance);
 272485        return true;
 486    }
 487
 488    private static bool TrySweepCircleCapsule(
 489        Vector2d start,
 490        Vector2d end,
 491        Fixed64 segmentLength,
 492        Fixed64 radius,
 493        LSCapsuleCollider2D capsule,
 494        out Physics2DHit hit)
 495    {
 277496        var query = new FixedSegment2d(start, end);
 277497        if (!query.TryGetCapsuleIntersectionDistanceInterval(
 277498                capsule.Center,
 277499                capsule.Rotation,
 277500                capsule.AxisLength,
 277501                capsule.ScaledRadius,
 277502                radius,
 277503                segmentLength,
 277504                out Fixed64 distance,
 277505                out _,
 277506                out _,
 277507                out _))
 508        {
 2509            hit = default;
 2510            return false;
 511        }
 512
 275513        Vector2d sweptCenter = query.GetPointAtDistance(distance, segmentLength);
 275514        Vector2d normal = capsule.GetNormalFromCenteredAxis(sweptCenter);
 275515        bool hasPoint = TryOffsetPoint(
 275516            sweptCenter,
 275517            -normal,
 275518            radius,
 275519            out Vector2d point);
 520
 275521        hit = new Physics2DHit(
 275522            capsule,
 275523            hasPoint
 275524                ? ContactAnchor2D.FromWorldPoint(point)
 275525                : new ContactAnchor2D(
 275526                    sweptCenter,
 275527                    -normal * radius),
 275528            normal,
 275529            distance);
 275530        return true;
 531    }
 532
 533    private static bool TryOverlapCircleCompound(
 534        Vector2d center,
 535        Fixed64 radius,
 536        LSCompoundCollider2D compound,
 537        out Physics2DHit hit)
 538    {
 8539        bool found = false;
 8540        Physics2DHit best = default;
 541
 48542        for (int i = 0; i < compound.PartCount; i++)
 543        {
 16544            LSCollider2D part = compound.GetPartCollider(i);
 16545            if (!TryOverlapCircle(center, radius, part, out Physics2DHit candidate))
 546                continue;
 547
 10548            TryKeepEarlierHit(candidate, ref found, ref best);
 549        }
 550
 8551        if (!found)
 552        {
 3553            hit = default;
 3554            return false;
 555        }
 556
 5557        hit = new Physics2DHit(compound, best.Anchor, best.Normal, best.Distance);
 5558        return true;
 559    }
 560
 561    private static bool TryOverlapConvexArea(
 562        Vector2d center,
 563        ReadOnlySpan<Vector2d> vertices,
 564        LSCollider2D collider,
 565        out Physics2DHit hit)
 566    {
 7806567        if (collider is LSCompoundCollider2D compound)
 2568            return TryOverlapConvexAreaCompound(center, vertices, compound, out hit);
 569
 7804570        return collider switch
 7804571        {
 7804572            LSCircleCollider2D circle =>
 7506573                TryOverlapConvexAreaCircle(center, vertices, circle, out hit),
 7804574            LSCapsuleCollider2D capsule =>
 137575                TryOverlapConvexAreaCapsule(center, vertices, capsule, out hit),
 161576            _ => TryOverlapConvexAreaConvex(center, vertices, collider, out hit)
 7804577        };
 578    }
 579
 580    private static bool TryOverlapConvexAreaCompound(
 581        Vector2d center,
 582        ReadOnlySpan<Vector2d> vertices,
 583        LSCompoundCollider2D compound,
 584        out Physics2DHit hit)
 585    {
 2586        bool found = false;
 2587        Physics2DHit best = default;
 12588        for (int i = 0; i < compound.PartCount; i++)
 589        {
 4590            LSCollider2D part = compound.GetPartCollider(i);
 4591            if (!TryOverlapConvexArea(center, vertices, part, out Physics2DHit candidate))
 592                continue;
 593
 2594            TryKeepEarlierHit(candidate, ref found, ref best);
 595        }
 596
 2597        if (!found)
 598        {
 1599            hit = default;
 1600            return false;
 601        }
 602
 1603        hit = new Physics2DHit(compound, best.Anchor, best.Normal, best.Distance);
 1604        return true;
 605    }
 606
 607    private static bool TryOverlapConvexAreaCircle(
 608        Vector2d center,
 609        ReadOnlySpan<Vector2d> vertices,
 610        LSCircleCollider2D circle,
 611        out Physics2DHit hit)
 612    {
 7506613        if (!FixedConvex2dRelations.TryGetCircleContact(
 7506614                circle.Center,
 7506615                circle.Rotation,
 7506616                circle.ScaledRadius,
 7506617                Vector2d.Zero,
 7506618                Fixed64.Zero,
 7506619                vertices,
 7506620                out _,
 7506621                out _,
 7506622                out _,
 7506623                out _,
 7506624                out _))
 625        {
 8626            hit = default;
 8627            return false;
 628        }
 629
 7498630        return TryBuildCenteredCapsuleAreaHit(
 7498631            center,
 7498632            circle.Center,
 7498633            circle.Rotation,
 7498634            Fixed64.Zero,
 7498635            circle.ScaledRadius,
 7498636            circle,
 7498637            out hit);
 638    }
 639
 640    private static bool TryOverlapConvexAreaCapsule(
 641        Vector2d center,
 642        ReadOnlySpan<Vector2d> vertices,
 643        LSCapsuleCollider2D capsule,
 644        out Physics2DHit hit)
 645    {
 137646        if (!FixedSegment2d.TryGetCenteredCapsuleConvexMinimumTranslation(
 137647                capsule.Center,
 137648                capsule.Rotation,
 137649                capsule.AxisLength,
 137650                capsule.ScaledRadius,
 137651                Vector2d.Zero,
 137652                vertices,
 137653                out _,
 137654                out _))
 655        {
 3656            hit = default;
 3657            return false;
 658        }
 659
 134660        return TryBuildCenteredCapsuleAreaHit(
 134661            center,
 134662            capsule.Center,
 134663            capsule.Rotation,
 134664            capsule.AxisLength,
 134665            capsule.ScaledRadius,
 134666            capsule,
 134667            out hit);
 668    }
 669
 670    private static bool TryOverlapConvexAreaConvex(
 671        Vector2d center,
 672        ReadOnlySpan<Vector2d> vertices,
 673        LSCollider2D collider,
 674        out Physics2DHit hit)
 675    {
 161676        Span<Vector2d> targetScratch = stackalloc Vector2d[4];
 161677        ReadOnlySpan<Vector2d> targetOffsets =
 161678            GetConvexVertexOffsets(collider, targetScratch);
 161679        Span<FixedPointAnchor2d> queryContacts =
 161680            stackalloc FixedPointAnchor2d[2];
 161681        Span<FixedPointAnchor2d> targetContacts =
 161682            stackalloc FixedPointAnchor2d[2];
 161683        if (!FixedConvex2dRelations.TryGetConvexContacts(
 161684                Vector2d.Zero,
 161685                Fixed64.Zero,
 161686                vertices,
 161687                collider.Center,
 161688                collider.ConvexRotation,
 161689                targetOffsets,
 161690                queryContacts,
 161691                targetContacts,
 161692                out _,
 161693                out _,
 161694                out _,
 161695                out _))
 696        {
 9697            hit = default;
 9698            return false;
 699        }
 700
 152701        return TryBuildConvexAreaHit(
 152702            center,
 152703            collider,
 152704            targetOffsets,
 152705            out hit);
 706    }
 707
 708    private static bool TryBuildCenteredCapsuleAreaHit(
 709        Vector2d queryCenter,
 710        Vector2d targetCenter,
 711        Fixed64 targetRotation,
 712        Fixed64 targetAxisLength,
 713        Fixed64 targetRadius,
 714        LSCollider2D target,
 715        out Physics2DHit hit)
 716    {
 7632717        bool containsCenter = FixedSegment2d.ContainsPointInCenteredCapsule(
 7632718            queryCenter,
 7632719            targetCenter,
 7632720            targetRotation,
 7632721            targetAxisLength,
 7632722            targetRadius,
 7632723            Fixed64.Zero);
 7632724        if (containsCenter)
 725        {
 844726            hit = new Physics2DHit(
 844727                target,
 844728                ContactAnchor2D.FromWorldPoint(queryCenter),
 844729                ResolveQueryFallbackNormal(queryCenter, targetCenter),
 844730                Fixed64.Zero);
 844731            return true;
 732        }
 733
 6788734        Vector2d normal = FixedSegment2d.GetDirectionFromCenteredAxis(
 6788735            queryCenter,
 6788736            targetCenter,
 6788737            targetRotation,
 6788738            targetAxisLength);
 739
 6788740        hit = new Physics2DHit(
 6788741            target,
 6788742            new ContactAnchor2D(
 6788743                GetCenteredCapsuleSurfaceAnchor(
 6788744                    queryCenter,
 6788745                    targetCenter,
 6788746                    targetRotation,
 6788747                    targetAxisLength,
 6788748                    targetRadius,
 6788749                    normal)),
 6788750            normal,
 6788751            FixedSegment2d.GetDistanceToCenteredCapsule(
 6788752                queryCenter,
 6788753                targetCenter,
 6788754                targetRotation,
 6788755                targetAxisLength,
 6788756                targetRadius));
 6788757        return true;
 758    }
 759
 760    private static FixedPointAnchor2d GetCenteredCapsuleSurfaceAnchor(
 761        Vector2d point,
 762        Vector2d center,
 763        Fixed64 rotation,
 764        Fixed64 axisLength,
 765        Fixed64 radius,
 766        Vector2d worldNormal)
 767    {
 768        // A unit rotation preserves the admitted normalized direction.
 6788769        _ = Vector2d.TryRotate(
 6788770            worldNormal,
 6788771            -rotation,
 6788772            out Vector2d localNormal);
 6788773        return FixedSegment2d.GetSurfaceAnchorOnCenteredCapsule(
 6788774            point,
 6788775            center,
 6788776            rotation,
 6788777            Vector2d.Forward,
 6788778            axisLength,
 6788779            radius,
 6788780            localNormal.Normalized);
 781    }
 782
 783    private static bool TryBuildConvexAreaHit(
 784        Vector2d queryCenter,
 785        LSCollider2D target,
 786        ReadOnlySpan<Vector2d> targetVertexOffsets,
 787        out Physics2DHit hit)
 788    {
 152789        if (FixedConvex2dRelations.ContainsPoint(
 152790                queryCenter,
 152791                target.Center,
 152792                target.ConvexRotation,
 152793                targetVertexOffsets))
 794        {
 16795            hit = new Physics2DHit(
 16796                target,
 16797                ContactAnchor2D.FromWorldPoint(queryCenter),
 16798                ResolveQueryFallbackNormal(queryCenter, target.Center),
 16799                Fixed64.Zero);
 16800            return true;
 801        }
 802
 136803        FixedPointAnchor2d targetAnchor =
 136804            FixedConvex2dRelations.GetClosestPointAnchor(
 136805                queryCenter,
 136806                target.Center,
 136807                target.ConvexRotation,
 136808                targetVertexOffsets);
 809
 136810        var anchor = new ContactAnchor2D(targetAnchor);
 136811        if (!anchor.TryGetOffsetFrom(queryCenter, out Vector2d centerToTarget)
 136812            || !Vector2d.TryGetMagnitude(centerToTarget, out Fixed64 distance))
 813        {
 2814            hit = default;
 2815            return false;
 816        }
 817
 134818        Vector2d normal = -centerToTarget / distance;
 134819        hit = new Physics2DHit(target, anchor, normal, distance);
 134820        return true;
 821    }
 822
 823    private static bool TryRaycastConvex(
 824        Vector2d start,
 825        Vector2d direction,
 826        Fixed64 segmentLength,
 827        LSCollider2D collider,
 828        out Physics2DHit hit)
 829    {
 1051830        Span<Vector2d> scratch = stackalloc Vector2d[4];
 1051831        ReadOnlySpan<Vector2d> vertexOffsets =
 1051832            GetConvexVertexOffsets(collider, scratch);
 1051833        if (!FixedConvex2dRelations.TryGetSweptCenteredCapsuleFirstDistanceCore(
 1051834                start,
 1051835                Vector2d.Right,
 1051836                Fixed64.Zero,
 1051837                Fixed64.Zero,
 1051838                direction,
 1051839                segmentLength,
 1051840                collider.Center,
 1051841                collider.ConvexRotation,
 1051842                vertexOffsets,
 1051843                out Fixed64 distance,
 1051844                out Vector2d normal,
 1051845                out Vector2d contactOffset,
 1051846                authoredDisplacement: true))
 847        {
 1848            hit = default;
 1849            return false;
 850        }
 851
 1050852        hit = new Physics2DHit(
 1050853            collider,
 1050854            new ContactAnchor2D(new FixedPointAnchor2d(collider.Center, collider.ConvexRotation, contactOffset)),
 1050855            normal,
 1050856            distance);
 1050857        return true;
 858    }
 859
 860    private static bool TrySweepCircleConvex(
 861        Vector2d start,
 862        Vector2d direction,
 863        Fixed64 segmentLength,
 864        Fixed64 radius,
 865        LSCollider2D collider,
 866        out Physics2DHit hit,
 867        bool authoredDisplacement = false)
 868    {
 419869        Span<Vector2d> scratch = stackalloc Vector2d[4];
 419870        ReadOnlySpan<Vector2d> vertexOffsets =
 419871            GetConvexVertexOffsets(collider, scratch);
 419872        if (!FixedConvex2dRelations.TryGetSweptCenteredCapsuleFirstDistanceCore(
 419873                start,
 419874                Vector2d.Right,
 419875                Fixed64.Zero,
 419876                radius,
 419877                direction,
 419878                segmentLength,
 419879                collider.Center,
 419880                collider.ConvexRotation,
 419881                vertexOffsets,
 419882                out Fixed64 distance,
 419883                out Vector2d normal,
 419884                out Vector2d contactOffset,
 419885                authoredDisplacement))
 886        {
 65887            hit = default;
 65888            return false;
 889        }
 890
 354891        hit = new Physics2DHit(
 354892            collider,
 354893            new ContactAnchor2D(new FixedPointAnchor2d(collider.Center, collider.ConvexRotation, contactOffset)),
 354894            normal,
 354895            distance);
 354896        return true;
 897    }
 898
 899    private static bool TrySweepConvexMoverAgainstCircle(
 900        LSCollider2D mover,
 901        Vector2d displacement,
 902        LSCircleCollider2D target,
 903        out Physics2DHit hit)
 904    {
 41905        if (CollisionDetection2D.TryCollide(mover, target, out Contact2D overlap))
 906        {
 2907            Vector2d normal = -overlap.Normal;
 2908            hit = new Physics2DHit(
 2909                target,
 2910                overlap.AnchorB,
 2911                normal,
 2912                Fixed64.Zero);
 2913            return true;
 914        }
 915
 39916        if (!TrySweepCircle(
 39917                target.Center,
 39918                target.Center - displacement,
 39919                target.ScaledRadius,
 39920                mover,
 39921                out Physics2DHit reverseHit))
 922        {
 35923            hit = default;
 35924            return false;
 925        }
 926
 4927        Vector2d hitNormal = -reverseHit.Normal;
 4928        hit = new Physics2DHit(
 4929            target,
 4930            new ContactAnchor2D(
 4931                target.Center,
 4932                hitNormal * target.ScaledRadius),
 4933            hitNormal,
 4934            reverseHit.Distance);
 4935        return true;
 936    }
 937
 938}

/home/runner/work/Gravitas/Gravitas/src/Gravitas/Queries/2D/Detection/QueryDetection2D.Support.cs

#LineLine coverage
 1//=======================================================================
 2// QueryDetection2D.Support.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
 8using System;
 9using System.Runtime.CompilerServices;
 10using FixedMathSharp;
 11using FixedMathSharp.Geometry;
 12using Gravitas.Colliders;
 13
 14namespace Gravitas.Queries;
 15
 16/// <content>Owns shared shape containment, normals, convex frames and hit reduction helpers.</content>
 17internal static partial class QueryDetection2D
 18{
 19    private static bool TryOffsetPoint(
 20        Vector2d point,
 21        Vector2d direction,
 22        Fixed64 distance,
 23        out Vector2d result)
 24    {
 27525        bool representable = Fixed64.TryMultiplyAdd(direction.X, distance, point.X, out Fixed64 x)
 27526            & Fixed64.TryMultiplyAdd(direction.Y, distance, point.Y, out Fixed64 y);
 27527        result = representable ? new Vector2d(x, y) : default;
 27528        return representable;
 29    }
 30
 31    private static bool ContainsPointExact(
 32        LSCollider2D collider,
 33        Vector2d point)
 34    {
 1768835        if (collider is LSCircleCollider2D circle)
 36        {
 1500537            return FixedSegment2d.ContainsPointInCenteredCapsule(
 1500538                point,
 1500539                circle.Center,
 1500540                Vector2d.Right,
 1500541                Fixed64.Zero,
 1500542                circle.ScaledRadius,
 1500543                Fixed64.Zero);
 44        }
 45
 268346        if (collider is LSCapsuleCollider2D capsule)
 71547            return capsule.ContainsPoint(point);
 196848        if (collider is not LSAABBoxCollider2D
 196849            && collider is not LSPolygonCollider2D)
 50        {
 951            return collider.ContainsPoint(point);
 52        }
 53
 195954        Span<Vector2d> scratch = stackalloc Vector2d[4];
 195955        ReadOnlySpan<Vector2d> vertexOffsets =
 195956            GetConvexVertexOffsets(collider, scratch);
 195957        return FixedConvex2dRelations.ContainsPoint(
 195958            point,
 195959            collider.Center,
 195960            collider.ConvexRotation,
 195961            vertexOffsets);
 62    }
 63
 64    private static bool TryGetContainedRayNormal(Vector2d start, LSCollider2D collider, out Vector2d normal)
 65    {
 226466        if (collider is LSCircleCollider2D circle)
 67        {
 91568            normal = Vector2d.GetDirection(circle.Center, start);
 91569            if (normal == Vector2d.Zero)
 7070                normal = Vector2d.Right;
 91571            return true;
 72        }
 134973        if (collider is LSCapsuleCollider2D capsule)
 74        {
 44075            normal = capsule.GetNormalFromCenteredAxis(start);
 44076            return true;
 77        }
 90978        if (collider is not IConvexVertexSource2D)
 79        {
 180            normal = default;
 181            return false;
 82        }
 83
 90884        Span<Vector2d> scratch = stackalloc Vector2d[4];
 90885        bool found = WideCenteredCapsule2dRelations.TryGetPointMinimumTranslationNormal(
 90886            start,
 90887            collider.Center, collider.ConvexRotation, GetConvexVertexOffsets(collider, scratch),
 90888            out normal);
 89        // Upstream contact normals point from the source point to the target.
 90890        normal = -normal;
 90891        return found;
 92    }
 93
 94    private static ReadOnlySpan<Vector2d> GetConvexVertexOffsets(
 95        LSCollider2D collider,
 96        Span<Vector2d> scratch)
 97    {
 637698        if (collider is LSPolygonCollider2D polygon)
 169299            return polygon.ScaledLocalVertices;
 100
 4684101        int vertexCount = collider.VertexCount;
 46840102        for (int i = 0; i < vertexCount; i++)
 18736103            scratch[i] = collider.GetScaledLocalVertexUnchecked(i);
 4684104        return scratch.Slice(0, vertexCount);
 105    }
 106
 107    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 108    private static void TryKeepEarlierHit(Physics2DHit candidate, ref bool found, ref Physics2DHit best)
 109    {
 924110        if (!PhysicsHitSelectionPolicy.ShouldReplace(candidate, found, best))
 449111            return;
 112
 475113        found = true;
 475114        best = candidate;
 475115    }
 116
 117    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 118    private static void TryKeepCloserHit(Physics2DHit candidate, ref bool found, ref Physics2DHit best)
 119    {
 4120        if (!PhysicsHitSelectionPolicy.ShouldReplaceDistance(candidate.Distance, found, best.Distance))
 1121            return;
 122
 3123        found = true;
 3124        best = candidate;
 3125    }
 126
 127    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 128    private static bool SegmentBoundsOverlap(Vector2d start, Vector2d end, LSCollider2D collider)
 129    {
 18589130        Fixed64 minX = FixedMath.Min(start.X, end.X);
 18589131        Fixed64 maxX = FixedMath.Max(start.X, end.X);
 18589132        Fixed64 minY = FixedMath.Min(start.Y, end.Y);
 18589133        Fixed64 maxY = FixedMath.Max(start.Y, end.Y);
 18589134        return maxX >= collider.MinX
 18589135            && minX <= collider.MaxX
 18589136            && maxY >= collider.MinY
 18589137            && minY <= collider.MaxY;
 138    }
 139
 140    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 141    private static bool SweepBoundsOverlap(Vector2d start, Vector2d end, Fixed64 radius, LSCollider2D collider)
 142    {
 9375143        Fixed64 minX = FixedMath.Min(start.X, end.X) - radius;
 9375144        Fixed64 maxX = FixedMath.Max(start.X, end.X) + radius;
 9375145        Fixed64 minY = FixedMath.Min(start.Y, end.Y) - radius;
 9375146        Fixed64 maxY = FixedMath.Max(start.Y, end.Y) + radius;
 9375147        return maxX >= collider.MinX
 9375148            && minX <= collider.MaxX
 9375149            && maxY >= collider.MinY
 9375150            && minY <= collider.MaxY;
 151    }
 152
 153    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 154    private static Vector2d ResolveQueryFallbackNormal(Vector2d center, Vector2d colliderCenter)
 155    {
 860156        Vector2d direction = center - colliderCenter;
 860157        return direction.MagnitudeSquared > Fixed64.Epsilon
 860158            ? direction.Normalized
 860159            : Vector2d.Right;
 160    }
 161}

/home/runner/work/Gravitas/Gravitas/src/Gravitas/Queries/2D/Detection/QueryDetection2D.SupportQuery.cs

#LineLine coverage
 1//=======================================================================
 2// QueryDetection2D.SupportQuery.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
 8using FixedMathSharp;
 9using Gravitas.Colliders;
 10
 11namespace Gravitas.Queries;
 12
 13/// <content>Finds eligible support hits on planar collider shapes and compound parts.</content>
 14internal static partial class QueryDetection2D
 15{
 16    internal static bool TryFindSupport(in Physics2DSupportQuery request, Vector2d end,
 17        LSCollider2D collider, out Physics2DHit hit)
 18    {
 24319        if (collider is not LSCompoundCollider2D compound)
 24120            return TryFindLeafSupport(request, end, collider, out hit);
 21
 222        bool found = false;
 223        Physics2DHit best = default;
 1024        for (int i = 0; i < compound.PartCount; i++)
 25        {
 326            if (TryFindLeafSupport(request, end, compound.GetPartCollider(i), out Physics2DHit candidate))
 127                TryKeepEarlierHit(candidate, ref found, ref best);
 28        }
 229        hit = found ? new Physics2DHit(compound, best.Anchor, best.Normal, best.Distance) : default;
 230        return found;
 31    }
 32
 33    private static bool TryFindLeafSupport(in Physics2DSupportQuery request, Vector2d end,
 34        LSCollider2D collider, out Physics2DHit hit)
 35    {
 36        // The public generic sweep intentionally rejects tiny segments. Support has
 37        // an authored direction/distance and must also recognize stationary contact.
 24438        bool found = TryOverlapCircle(request.Center, request.Radius, collider, out hit);
 24439        if (found)
 22140            hit = new Physics2DHit(collider, hit.Anchor, hit.Normal, Fixed64.Zero);
 41        else
 42        {
 2343            found = collider switch
 2344            {
 345                LSCircleCollider2D circle => TrySweepCircleCircle(request.Center, end, request.Distance,
 346                    request.Radius, circle, out hit),
 347                LSCapsuleCollider2D capsule => TrySweepCircleCapsule(request.Center, end, request.Distance,
 348                    request.Radius, capsule, out hit),
 1749                _ => TrySweepCircleConvex(request.Center, -request.Up, request.Distance,
 1750                    request.Radius, collider, out hit)
 2351            };
 52        }
 24453        if (found && Vector2d.Dot(hit.Normal, request.Up) >= request.MinimumNormalDot)
 24154            return true;
 355        hit = default;
 356        return false;
 57    }
 58}
 59// MIT License, Copyright (c) 2026-present David Oravsky (mrdav30)
 60// See LICENSE file in the project root for full license information.

Methods/Properties

TryRaycastCompound(FixedMathSharp.Vector2d,FixedMathSharp.Vector2d,FixedMathSharp.Vector2d,FixedMathSharp.Fixed64,Gravitas.Colliders.LSCompoundCollider2D,Gravitas.Queries.Physics2DHit&)
TrySweepCircleCompound(FixedMathSharp.Vector2d,FixedMathSharp.Vector2d,FixedMathSharp.Vector2d,FixedMathSharp.Fixed64,FixedMathSharp.Fixed64,Gravitas.Colliders.LSCompoundCollider2D,Gravitas.Queries.Physics2DHit&)
TrySweepMoverCompound(Gravitas.Colliders.LSCompoundCollider2D,FixedMathSharp.Vector2d,Gravitas.Colliders.LSCollider2D,Gravitas.Queries.Physics2DHit&)
TrySweepMoverAgainstCompound(Gravitas.Colliders.LSCollider2D,FixedMathSharp.Vector2d,Gravitas.Colliders.LSCompoundCollider2D,Gravitas.Queries.Physics2DHit&)
TrySweepCapsuleMover(Gravitas.Colliders.LSCapsuleCollider2D,FixedMathSharp.Vector2d,FixedMathSharp.Fixed64,Gravitas.Colliders.LSCollider2D,Gravitas.Queries.Physics2DHit&)
TrySweepConvexMoverAgainstCapsule(Gravitas.Colliders.LSCollider2D,FixedMathSharp.Vector2d,FixedMathSharp.Fixed64,Gravitas.Colliders.LSCapsuleCollider2D,Gravitas.Queries.Physics2DHit&)
TrySweepConvexMoverAgainstConvex(Gravitas.Colliders.LSCollider2D,FixedMathSharp.Vector2d,FixedMathSharp.Fixed64,Gravitas.Colliders.LSCollider2D,Gravitas.Queries.Physics2DHit&)
TryBuildCapsuleTargetSweepHit(Gravitas.Colliders.LSCapsuleCollider2D,FixedMathSharp.Vector2d,FixedMathSharp.Fixed64,Gravitas.Colliders.LSCollider2D,FixedMathSharp.Vector2d,FixedMathSharp.Fixed64,FixedMathSharp.Fixed64,FixedMathSharp.Fixed64,System.Boolean,FixedMathSharp.Fixed64,FixedMathSharp.Vector2d,Gravitas.Queries.Physics2DHit&)
TryOverlapCircle(FixedMathSharp.Vector2d,FixedMathSharp.Fixed64,Gravitas.Colliders.LSCollider2D,Gravitas.Queries.Physics2DHit&)
TryOverlapCircleCenteredCapsule(FixedMathSharp.Vector2d,FixedMathSharp.Fixed64,FixedMathSharp.Vector2d,FixedMathSharp.Fixed64,FixedMathSharp.Fixed64,FixedMathSharp.Fixed64,FixedMathSharp.Vector2d,Gravitas.Colliders.LSCollider2D,Gravitas.Queries.Physics2DHit&)
TryOverlapPolygon(System.ReadOnlySpan`1<FixedMathSharp.Vector2d>,FixedMathSharp.Vector2d,Gravitas.Colliders.LSCollider2D,Gravitas.Queries.Physics2DHit&)
ValidateAabbSize(FixedMathSharp.Vector2d)
ValidateConvexQueryPolygon(System.ReadOnlySpan`1<FixedMathSharp.Vector2d>)
CalculateAverageCenter(System.ReadOnlySpan`1<FixedMathSharp.Vector2d>)
TryRaycast(FixedMathSharp.Vector2d,FixedMathSharp.Vector2d,Gravitas.Colliders.LSCollider2D,Gravitas.Queries.Physics2DHit&)
TryRaycast(FixedMathSharp.Vector2d,FixedMathSharp.Vector2d,FixedMathSharp.Vector2d,FixedMathSharp.Fixed64,Gravitas.Colliders.LSCollider2D,Gravitas.Queries.Physics2DHit&)
TryRaycastPrepared(FixedMathSharp.Vector2d,FixedMathSharp.Vector2d,FixedMathSharp.Vector2d,FixedMathSharp.Fixed64,Gravitas.Colliders.LSCollider2D,Gravitas.Queries.Physics2DHit&)
TrySweepCircle(FixedMathSharp.Vector2d,FixedMathSharp.Vector2d,FixedMathSharp.Fixed64,Gravitas.Colliders.LSCollider2D,Gravitas.Queries.Physics2DHit&)
TrySweepCircle(FixedMathSharp.Vector2d,FixedMathSharp.Vector2d,FixedMathSharp.Vector2d,FixedMathSharp.Fixed64,FixedMathSharp.Fixed64,Gravitas.Colliders.LSCollider2D,Gravitas.Queries.Physics2DHit&)
TrySweepCirclePrepared(FixedMathSharp.Vector2d,FixedMathSharp.Vector2d,FixedMathSharp.Vector2d,FixedMathSharp.Fixed64,FixedMathSharp.Fixed64,Gravitas.Colliders.LSCollider2D,Gravitas.Queries.Physics2DHit&)
TrySweepMoverShape(Gravitas.Colliders.LSCollider2D,FixedMathSharp.Vector2d,Gravitas.Colliders.LSCollider2D,Gravitas.Queries.Physics2DHit&)
TryRaycastCircle(FixedMathSharp.Vector2d,FixedMathSharp.Vector2d,FixedMathSharp.Fixed64,Gravitas.Colliders.LSCircleCollider2D,Gravitas.Queries.Physics2DHit&)
TrySweepCircleCircle(FixedMathSharp.Vector2d,FixedMathSharp.Vector2d,FixedMathSharp.Fixed64,FixedMathSharp.Fixed64,Gravitas.Colliders.LSCircleCollider2D,Gravitas.Queries.Physics2DHit&)
TryRaycastCapsule(FixedMathSharp.Vector2d,FixedMathSharp.Vector2d,FixedMathSharp.Fixed64,Gravitas.Colliders.LSCapsuleCollider2D,Gravitas.Queries.Physics2DHit&)
TrySweepCircleCapsule(FixedMathSharp.Vector2d,FixedMathSharp.Vector2d,FixedMathSharp.Fixed64,FixedMathSharp.Fixed64,Gravitas.Colliders.LSCapsuleCollider2D,Gravitas.Queries.Physics2DHit&)
TryOverlapCircleCompound(FixedMathSharp.Vector2d,FixedMathSharp.Fixed64,Gravitas.Colliders.LSCompoundCollider2D,Gravitas.Queries.Physics2DHit&)
TryOverlapConvexArea(FixedMathSharp.Vector2d,System.ReadOnlySpan`1<FixedMathSharp.Vector2d>,Gravitas.Colliders.LSCollider2D,Gravitas.Queries.Physics2DHit&)
TryOverlapConvexAreaCompound(FixedMathSharp.Vector2d,System.ReadOnlySpan`1<FixedMathSharp.Vector2d>,Gravitas.Colliders.LSCompoundCollider2D,Gravitas.Queries.Physics2DHit&)
TryOverlapConvexAreaCircle(FixedMathSharp.Vector2d,System.ReadOnlySpan`1<FixedMathSharp.Vector2d>,Gravitas.Colliders.LSCircleCollider2D,Gravitas.Queries.Physics2DHit&)
TryOverlapConvexAreaCapsule(FixedMathSharp.Vector2d,System.ReadOnlySpan`1<FixedMathSharp.Vector2d>,Gravitas.Colliders.LSCapsuleCollider2D,Gravitas.Queries.Physics2DHit&)
TryOverlapConvexAreaConvex(FixedMathSharp.Vector2d,System.ReadOnlySpan`1<FixedMathSharp.Vector2d>,Gravitas.Colliders.LSCollider2D,Gravitas.Queries.Physics2DHit&)
TryBuildCenteredCapsuleAreaHit(FixedMathSharp.Vector2d,FixedMathSharp.Vector2d,FixedMathSharp.Fixed64,FixedMathSharp.Fixed64,FixedMathSharp.Fixed64,Gravitas.Colliders.LSCollider2D,Gravitas.Queries.Physics2DHit&)
GetCenteredCapsuleSurfaceAnchor(FixedMathSharp.Vector2d,FixedMathSharp.Vector2d,FixedMathSharp.Fixed64,FixedMathSharp.Fixed64,FixedMathSharp.Fixed64,FixedMathSharp.Vector2d)
TryBuildConvexAreaHit(FixedMathSharp.Vector2d,Gravitas.Colliders.LSCollider2D,System.ReadOnlySpan`1<FixedMathSharp.Vector2d>,Gravitas.Queries.Physics2DHit&)
TryRaycastConvex(FixedMathSharp.Vector2d,FixedMathSharp.Vector2d,FixedMathSharp.Fixed64,Gravitas.Colliders.LSCollider2D,Gravitas.Queries.Physics2DHit&)
TrySweepCircleConvex(FixedMathSharp.Vector2d,FixedMathSharp.Vector2d,FixedMathSharp.Fixed64,FixedMathSharp.Fixed64,Gravitas.Colliders.LSCollider2D,Gravitas.Queries.Physics2DHit&,System.Boolean)
TrySweepConvexMoverAgainstCircle(Gravitas.Colliders.LSCollider2D,FixedMathSharp.Vector2d,Gravitas.Colliders.LSCircleCollider2D,Gravitas.Queries.Physics2DHit&)
TryOffsetPoint(FixedMathSharp.Vector2d,FixedMathSharp.Vector2d,FixedMathSharp.Fixed64,FixedMathSharp.Vector2d&)
ContainsPointExact(Gravitas.Colliders.LSCollider2D,FixedMathSharp.Vector2d)
TryGetContainedRayNormal(FixedMathSharp.Vector2d,Gravitas.Colliders.LSCollider2D,FixedMathSharp.Vector2d&)
GetConvexVertexOffsets(Gravitas.Colliders.LSCollider2D,System.Span`1<FixedMathSharp.Vector2d>)
TryKeepEarlierHit(Gravitas.Queries.Physics2DHit,System.Boolean&,Gravitas.Queries.Physics2DHit&)
TryKeepCloserHit(Gravitas.Queries.Physics2DHit,System.Boolean&,Gravitas.Queries.Physics2DHit&)
SegmentBoundsOverlap(FixedMathSharp.Vector2d,FixedMathSharp.Vector2d,Gravitas.Colliders.LSCollider2D)
SweepBoundsOverlap(FixedMathSharp.Vector2d,FixedMathSharp.Vector2d,FixedMathSharp.Fixed64,Gravitas.Colliders.LSCollider2D)
ResolveQueryFallbackNormal(FixedMathSharp.Vector2d,FixedMathSharp.Vector2d)
TryFindSupport(Gravitas.Queries.Physics2DSupportQuery&,FixedMathSharp.Vector2d,Gravitas.Colliders.LSCollider2D,Gravitas.Queries.Physics2DHit&)
TryFindLeafSupport(Gravitas.Queries.Physics2DSupportQuery&,FixedMathSharp.Vector2d,Gravitas.Colliders.LSCollider2D,Gravitas.Queries.Physics2DHit&)