< Summary

Information
Class: Gravitas.CollisionHandling.ContinuousCollisionContactPolicy
Assembly: Gravitas
File(s): /home/runner/work/Gravitas/Gravitas/src/Gravitas/CollisionHandling/Continuous/ContinuousCollisionContactPolicy.cs
Line coverage
100%
Covered lines: 89
Uncovered lines: 0
Coverable lines: 89
Total lines: 164
Line coverage: 100%
Branch coverage
100%
Covered branches: 32
Total branches: 32
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
TryResolveSweptSphereContact(...)100%2424100%
ResolveSurfaceNormal(...)100%22100%
ResolveLegacyNormal(...)100%66100%

File(s)

/home/runner/work/Gravitas/Gravitas/src/Gravitas/CollisionHandling/Continuous/ContinuousCollisionContactPolicy.cs

#LineLine coverage
 1//=======================================================================
 2// ContinuousCollisionContactPolicy.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 FixedMathSharp.Geometry;
 10using Gravitas.Colliders;
 11
 12namespace Gravitas.CollisionHandling;
 13
 14/// <summary>Resolves target surface witnesses and normals for swept-sphere hits.</summary>
 15internal static class ContinuousCollisionContactPolicy
 16{
 17    internal static bool TryResolveSweptSphereContact(
 18        LSCollider target,
 19        Vector3d sphereCenterAtImpact,
 20        Vector3d direction,
 21        out ContactAnchor targetAnchor,
 22        out Vector3d normal)
 23    {
 1241324        if (target is LSSphereCollider or LSCapsuleCollider)
 25        {
 26            // Keep the same reducer's witness; an inverse-normal round trip can shift it.
 787227            FixedPointAnchor surface = target.GetClosestSurfaceAnchor(
 787228                sphereCenterAtImpact, out normal);
 787229            targetAnchor = new ContactAnchor(surface);
 787230            return true;
 31        }
 32
 33        // Solid-volume normals remain outward for contained starts: CCD must
 34        // permit escape and block deeper motion. Signed distance is not a flip.
 454135        if (target is LSCylinderCollider cylinder)
 36        {
 36337            if (!FixedSegment.TryGetClosestCenteredFiniteCylinderSurfaceAnchor(
 36338                    sphereCenterAtImpact,
 36339                    cylinder.Center,
 36340                    cylinder.Rotation,
 36341                    Vector3d.Up,
 36342                    cylinder.Height,
 36343                    cylinder.ScaledRadius,
 36344                    Vector3d.Right,
 36345                    out FixedPointAnchor targetPoint,
 36346                    out normal,
 36347                    out _))
 48            {
 249                targetAnchor = default;
 250                normal = default;
 251                return false;
 52            }
 53
 36154            targetAnchor = new ContactAnchor(targetPoint);
 36155            return true;
 56        }
 57
 417858        if (target is LSConeCollider cone)
 59        {
 35460            if (!FixedSegment.TryGetClosestCenteredFiniteConeSurfaceAnchor(
 35461                    sphereCenterAtImpact,
 35462                    cone.Center,
 35463                    cone.Rotation,
 35464                    Vector3d.Up,
 35465                    cone.Height,
 35466                    cone.ScaledRadius,
 35467                    Vector3d.Right,
 35468                    out FixedPointAnchor targetPoint,
 35469                    out normal,
 35470                    out _))
 71            {
 172                targetAnchor = default;
 173                normal = default;
 174                return false;
 75            }
 76
 35377            targetAnchor = new ContactAnchor(targetPoint);
 35378            return true;
 79        }
 80
 382481        if (target is LSCuboidCollider cuboid)
 82        {
 93383            FixedPointAnchor targetPoint = cuboid.GetClosestSurfaceAnchor(
 93384                sphereCenterAtImpact, out normal, out _);
 93385            targetAnchor = new ContactAnchor(targetPoint);
 93386            return true;
 87        }
 88
 289189        if (target is LSMeshCollider mesh)
 90        {
 166391            mesh.FindClosestPointAnchor(
 166392                sphereCenterAtImpact,
 166393                out FixedPointAnchor meshPoint,
 166394                out normal);
 166395            normal = ResolveSurfaceNormal(meshPoint, sphereCenterAtImpact, normal);
 166396            targetAnchor = new ContactAnchor(meshPoint);
 166397            return true;
 98        }
 99
 1228100        if (target is LSCompoundCollider compound)
 101        {
 1223102            FixedPointAnchor surfacePoint = compound.GetClosestSurfaceAnchor(
 1223103                sphereCenterAtImpact, out normal, out LSCollider surfaceOwner);
 1223104            if (surfaceOwner is LSMeshCollider)
 541105                normal = ResolveSurfaceNormal(surfacePoint, sphereCenterAtImpact, normal);
 106            // Keep the selected part's witness and outward primitive normal.
 107            // Re-querying a materialized point can select another part at a
 108            // shared boundary or lose an otherwise valid far-domain anchor.
 1223109            targetAnchor = new ContactAnchor(surfacePoint);
 1223110            return true;
 111        }
 112
 5113        Vector3d point = target.ClosestPointOnSurface(sphereCenterAtImpact);
 5114        normal = ResolveLegacyNormal(target, point, sphereCenterAtImpact, direction);
 5115        var worldPoint = new FixedPointAnchor(
 5116            point,
 5117            FixedQuaternion.Identity,
 5118            Vector3d.Zero);
 5119        if (worldPoint.TryGetLocalPointIn(
 5120                target.Center,
 5121                target.Rotation,
 5122                out Vector3d localPoint))
 123        {
 4124            targetAnchor = new ContactAnchor(
 4125                target.Center,
 4126                target.Rotation,
 4127                localPoint);
 4128            return true;
 129        }
 130
 1131        targetAnchor = default;
 1132        normal = default;
 1133        return false;
 134    }
 135
 136    internal static Vector3d ResolveSurfaceNormal(
 137        in FixedPointAnchor surfacePoint, Vector3d sourceCenter, Vector3d outwardNormal)
 138    {
 139        // Exterior apices and two-sided meshes need the exact closest-feature
 140        // direction; only coincidence retains the selected feature's normal.
 2471141        var center = new FixedPointAnchor(sourceCenter, FixedQuaternion.Identity, Vector3d.Zero);
 2471142        Vector3d normal = WidePointAnchor3d.GetDirection(center, surfacePoint);
 2471143        return normal == Vector3d.Zero ? outwardNormal : normal;
 144    }
 145
 146    private static Vector3d ResolveLegacyNormal(
 147        LSCollider target,
 148        Vector3d point,
 149        Vector3d sphereCenterAtImpact,
 150        Vector3d direction)
 151    {
 5152        Vector3d fromPointToSphereCenter = sphereCenterAtImpact - point;
 5153        Vector3d normal = target.GetNormalAtPoint(point);
 5154        if (normal.MagnitudeSquared > Fixed64.Epsilon)
 1155            return normal.Normalized;
 156
 4157        if (fromPointToSphereCenter.MagnitudeSquared > Fixed64.Epsilon)
 1158            return fromPointToSphereCenter.Normalized;
 159
 3160        return direction.MagnitudeSquared > Fixed64.Epsilon
 3161            ? -direction.Normalized
 3162            : Vector3d.Zero;
 163    }
 164}