< Summary

Information
Class: Gravitas.CollisionHandling.ConvexColliderSupport
Assembly: Gravitas
File(s): /home/runner/work/Gravitas/Gravitas/src/Gravitas/CollisionHandling/Detection/3D/Convex/ConvexColliderSupport.cs
Line coverage
100%
Covered lines: 161
Uncovered lines: 0
Coverable lines: 161
Total lines: 277
Line coverage: 100%
Branch coverage
100%
Covered branches: 82
Total branches: 82
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
GetSupportAnchor(...)100%1818100%
IsSupported(...)100%1414100%
Intersects(...)100%2020100%
IntersectsConeVolume(...)100%1818100%
TrySupportMinkowski(...)100%44100%
TrySupportMinkowskiConeCollider(...)100%44100%
ResolveSupportDirection(...)100%22100%
GetLocalDisplacement(...)100%22100%

File(s)

/home/runner/work/Gravitas/Gravitas/src/Gravitas/CollisionHandling/Detection/3D/Convex/ConvexColliderSupport.cs

#LineLine coverage
 1//=======================================================================
 2// ConvexColliderSupport.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.CollisionHandling;
 15
 16/// <summary>
 17/// Produces support witnesses and tests intersections for supported convex 3D colliders.
 18/// </summary>
 19internal static class ConvexColliderSupport
 20{
 21    public static FixedPointAnchor GetSupportAnchor(
 22        LSCollider collider,
 23        Vector3d direction,
 24        Vector3d originOffset,
 25        bool preferAxialCenter = false)
 26    {
 4915827        Vector3d normal = ResolveSupportDirection(direction);
 4915828        Vector3d localTranslation =
 4915829            GetLocalDisplacement(collider.Rotation, originOffset);
 4915730        if (collider is LSSphereCollider sphere)
 31        {
 272932            return FixedSegment.GetCenteredCapsuleSupportAnchor(
 272933                    sphere.Center,
 272934                    sphere.Rotation,
 272935                    Fixed64.Zero,
 272936                    sphere.ScaledRadius,
 272937                    normal)
 272938                .WithLocalTranslation(localTranslation);
 39        }
 4642840        if (collider is LSCapsuleCollider capsule)
 41        {
 1308542            return FixedSegment.GetCenteredCapsuleSupportAnchor(
 1308543                    capsule.Center,
 1308544                    capsule.Rotation,
 1308545                    capsule.AxisLength,
 1308546                    capsule.ScaledRadius,
 1308547                    normal)
 1308548                .WithLocalTranslation(localTranslation);
 49        }
 3334350        if (collider is LSCuboidCollider cuboid)
 51        {
 1376352            return new FixedPointAnchor(
 1376353                cuboid.Center,
 1376354                cuboid.Rotation,
 1376355                cuboid.OrientedBox.GetLocalSupportPoint(normal))
 1376356                .WithLocalTranslation(localTranslation);
 57        }
 1958058        if (collider is LSCylinderCollider cylinder)
 59        {
 345860            FixedPointAnchor support = preferAxialCenter
 345861                ? WideGeometry.GetCenteredCylinderSupportAnchor(
 345862                    cylinder.Center, cylinder.Rotation, cylinder.Height,
 345863                    cylinder.ScaledRadius, normal, preferAxialCenter: true)
 345864                : FixedSegment.GetCenteredFiniteCylinderSupportAnchor(
 345865                    cylinder.Center, cylinder.Rotation, cylinder.Height,
 345866                    cylinder.ScaledRadius, normal);
 345867            return support.WithLocalTranslation(localTranslation);
 68        }
 1612269        if (collider is LSConeCollider cone)
 70        {
 740171            return FixedSegment.GetCenteredFiniteConeSupportAnchor(
 740172                    cone.Center,
 740173                    cone.Rotation,
 740174                    cone.Height,
 740175                    cone.ScaledRadius,
 740176                    normal)
 740177                .WithLocalTranslation(localTranslation);
 78        }
 872179        if (collider is LSMeshCollider { Mode: MeshColliderMode.Convex } mesh)
 80        {
 872081            FixedPointAnchor support = mesh.Mesh.CreatePointAnchor(mesh.Mesh.GetSupportVertexLocal(normal));
 872082            return support.WithLocalTranslation(support.Rotation == FixedQuaternion.Identity
 872083                ? originOffset : localTranslation);
 84        }
 85
 186        throw new NotSupportedException(
 187            $"Convex support mapping does not support {collider.GetType().Name}.");
 88    }
 89
 90    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 91    public static bool IsSupported(LSCollider collider) =>
 523192        collider is LSSphereCollider
 523193            or LSCapsuleCollider
 523194            or LSCuboidCollider
 523195            or LSCylinderCollider
 523196            or LSConeCollider
 523197            or LSMeshCollider { Mode: MeshColliderMode.Convex };
 98
 99    public static bool Intersects(LSCollider first, LSCollider second, int maxIterations = 32)
 100    {
 821101        if (!IsSupported(first) || !IsSupported(second))
 2102            return false;
 103
 819104        Span<Vector3d> simplex = stackalloc Vector3d[4];
 819105        int count = 0;
 819106        Vector3d direction = second.Center - first.Center;
 819107        if (direction.MagnitudeSquared <= Fixed64.Epsilon)
 124108            direction = Vector3d.Right;
 109
 819110        if (!TrySupportMinkowski(first, second, direction, out simplex[count]))
 1111            return false;
 818112        count++;
 818113        direction = -simplex[0];
 818114        if (direction.MagnitudeSquared <= Fixed64.Epsilon)
 4115            return true;
 116
 4150117        for (int i = 0; i < maxIterations; i++)
 118        {
 2074119            if (!TrySupportMinkowski(first, second, direction, out Vector3d point))
 1120                return false;
 2073121            if (GjkSimplexPolicy.HasSeparatingSupport(point, direction))
 550122                return false;
 123
 1523124            GjkSimplexPolicy.AddPoint(simplex, ref count, point);
 1523125            if (GjkSimplexPolicy.Update(simplex, ref count, ref direction))
 260126                return true;
 127
 1263128            if (direction.MagnitudeSquared <= Fixed64.Epsilon)
 2129                return true;
 130        }
 131
 132        // No separating support was found within the bounded search, so preserve touching/overlap.
 1133        return true;
 134    }
 135
 136    public static bool IntersectsConeVolume(
 137        LSCollider collider,
 138        Vector3d apex,
 139        Vector3d baseCenter,
 140        Vector3d axis,
 141        Fixed64 endRadius,
 142        int maxIterations = 32)
 143    {
 530144        if (!IsSupported(collider))
 1145            return false;
 146
 529147        Span<Vector3d> simplex = stackalloc Vector3d[4];
 529148        int count = 0;
 529149        Vector3d coneCenter = Vector3d.Midpoint(apex, baseCenter);
 529150        Vector3d direction = collider.Center - coneCenter;
 529151        if (direction.MagnitudeSquared <= Fixed64.Epsilon)
 12152            direction = Vector3d.Right;
 153
 529154        if (!TrySupportMinkowskiConeCollider(
 529155                collider,
 529156                apex,
 529157                baseCenter,
 529158                axis,
 529159                endRadius,
 529160                direction,
 529161                out simplex[count]))
 162        {
 1163            return false;
 164        }
 528165        count++;
 528166        direction = -simplex[0];
 528167        if (direction.MagnitudeSquared <= Fixed64.Epsilon)
 17168            return true;
 169
 2994170        for (int i = 0; i < maxIterations; i++)
 171        {
 1496172            if (!TrySupportMinkowskiConeCollider(
 1496173                    collider,
 1496174                    apex,
 1496175                    baseCenter,
 1496176                    axis,
 1496177                    endRadius,
 1496178                    direction,
 1496179                    out Vector3d point))
 180            {
 1181                return false;
 182            }
 1495183            if (GjkSimplexPolicy.HasSeparatingSupport(point, direction))
 49184                return false;
 185
 1446186            GjkSimplexPolicy.AddPoint(simplex, ref count, point);
 1446187            if (GjkSimplexPolicy.Update(simplex, ref count, ref direction))
 459188                return true;
 189
 987190            if (direction.MagnitudeSquared <= Fixed64.Epsilon)
 1191                return true;
 192        }
 193
 194        // No separating support was found within the bounded search, so preserve touching/overlap.
 1195        return true;
 196    }
 197
 198    private static bool TrySupportMinkowski(
 199        LSCollider first,
 200        LSCollider second,
 201        Vector3d direction,
 202        out Vector3d difference)
 203    {
 2893204        Vector3d normal = ResolveSupportDirection(direction);
 2893205        if (first is LSCuboidCollider firstBox
 2893206            && second is LSCuboidCollider secondBox)
 207        {
 3208            return firstBox.OrientedBox.TryGetSupportDifference(
 3209                secondBox.OrientedBox,
 3210                normal,
 3211                out difference);
 212        }
 213
 2890214        FixedPointAnchor supportA =
 2890215            GetSupportAnchor(first, normal, Vector3d.Zero);
 2890216        FixedPointAnchor supportB =
 2890217            GetSupportAnchor(second, -normal, Vector3d.Zero);
 2890218        return supportA.TryGetOffsetFrom(supportB, out difference);
 219    }
 220
 221    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 222    private static bool TrySupportMinkowskiConeCollider(
 223        LSCollider collider,
 224        Vector3d apex,
 225        Vector3d baseCenter,
 226        Vector3d axis,
 227        Fixed64 endRadius,
 228        Vector3d direction,
 229        out Vector3d difference)
 230    {
 2025231        Vector3d normal = ResolveSupportDirection(direction);
 2025232        Vector3d radialDirection =
 2025233            Vector3d.GetNormalizedProjectionOnPlane(normal, axis);
 2025234        var apexAnchor = new FixedPointAnchor(
 2025235            apex,
 2025236            FixedQuaternion.Identity,
 2025237            Vector3d.Zero);
 2025238        var baseAnchor = new FixedPointAnchor(
 2025239            baseCenter,
 2025240            FixedQuaternion.Identity,
 2025241            radialDirection * endRadius);
 2025242        FixedPointAnchor coneSupport =
 2025243            baseAnchor.ProjectNonNegativeOffsetFrom(apexAnchor, normal)
 2025244                > Fixed64.Zero
 2025245            || apexAnchor.ProjectNonNegativeOffsetFrom(baseAnchor, normal)
 2025246                == Fixed64.Zero
 2025247                ? baseAnchor
 2025248                : apexAnchor;
 2025249        FixedPointAnchor colliderSupport =
 2025250            GetSupportAnchor(collider, -normal, Vector3d.Zero);
 2025251        return coneSupport.TryGetOffsetFrom(
 2025252            colliderSupport,
 2025253            out difference);
 254    }
 255
 256    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 257    private static Vector3d ResolveSupportDirection(Vector3d direction) =>
 54076258        direction != Vector3d.Zero
 54076259            ? direction.Normalized
 54076260            : Vector3d.Right;
 261
 262    internal static Vector3d GetLocalDisplacement(
 263        FixedQuaternion rotation,
 264        Vector3d worldDisplacement)
 265    {
 49170266        if (rotation.Inverse().TryRotate(
 49170267                worldDisplacement,
 49170268                out Vector3d localDisplacement))
 269        {
 49169270            return localDisplacement;
 271        }
 272
 1273        throw new InvalidOperationException(
 1274            "The sweep displacement cannot be represented in the collider's canonical frame.");
 275    }
 276
 277}