< Summary

Information
Class: Gravitas.Colliders.LSSphereCollider
Assembly: Gravitas
File(s): /home/runner/work/Gravitas/Gravitas/src/Gravitas/Colliders/3D/Shapes/LSSphereCollider.cs
Line coverage
100%
Covered lines: 81
Uncovered lines: 0
Coverable lines: 81
Total lines: 159
Line coverage: 100%
Branch coverage
100%
Covered branches: 6
Total branches: 6
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor()100%11100%
.ctor(...)100%11100%
get_Shape()100%11100%
get_Priority()100%11100%
OnRadiusChanged()100%11100%
get_ScaledRadius()100%11100%
PrepareShape(...)100%11100%
PublishShape()100%11100%
CalculateMassPropertyWeight()100%11100%
GetMassPropertyRadius()100%22100%
CalculatePreparedMassPropertyWeight()100%11100%
CalculateCenterOfMassInertiaTensor(...)100%11100%
ClosestPointOnSurface(...)100%22100%
GetNormalAtPoint(...)100%22100%
GetClosestSurfaceAnchor(...)100%11100%
GetClosestSurfaceAnchor(...)100%11100%
ColliderOverlapsRay(...)100%11100%

File(s)

/home/runner/work/Gravitas/Gravitas/src/Gravitas/Colliders/3D/Shapes/LSSphereCollider.cs

#LineLine coverage
 1//=======================================================================
 2// LSSphereCollider.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.Queries;
 12using SwiftCollections;
 13
 14namespace Gravitas.Colliders;
 15
 16/// <summary>Represents a runtime 3D sphere collider.</summary>
 17public sealed class LSSphereCollider : LSCollider
 18{
 471319    private Fixed64 _scaledRadius = Fixed64.Half;
 20    private Fixed64 _preparedRadius;
 21    private Fixed64 _preparedArea;
 22
 23    /// <summary>Creates a sphere collider with default dimensions.</summary>
 853624    public LSSphereCollider() { }
 25
 26    /// <summary>Creates a runtime sphere from an authored shape definition.</summary>
 44527    public LSSphereCollider(ColliderShapeDefinition definition)
 28    {
 44529        definition.EnsureKind(ColliderShapeDefinitionKind.Sphere);
 44430        Material = definition.Material;
 44431        Radius = definition.Radius;
 44432    }
 33
 34    /// <inheritdoc/>
 119974435    public override ColliderType Shape => ColliderType.Sphere;
 36
 37    /// <inheritdoc/>
 1488538    public override int Priority => ColliderSettings.GetPriority(Shape);
 39
 40    /// <inheritdoc/>
 41    protected override void OnRadiusChanged()
 42    {
 87343        Fixed64 diameter = _radius * 2;
 87344        _size = new Vector3d(diameter, diameter, diameter);
 87345    }
 46
 47    /// <inheritdoc/>
 12970648    public override Fixed64 ScaledRadius => _scaledRadius;
 49
 50    private protected override void PrepareShape(in ColliderShapeSnapshot snapshot)
 51    {
 6115652        Fixed64 radiusX = ColliderScalePolicy.ScalePositive(
 6115653            snapshot.Radius,
 6115654            snapshot.OwnerScale.X,
 6115655            snapshot.PartScale.X);
 6115456        Fixed64 radiusY = ColliderScalePolicy.ScalePositive(
 6115457            snapshot.Radius,
 6115458            snapshot.OwnerScale.Y,
 6115459            snapshot.PartScale.Y);
 6115460        Fixed64 radiusZ = ColliderScalePolicy.ScalePositive(
 6115461            snapshot.Radius,
 6115462            snapshot.OwnerScale.Z,
 6115463            snapshot.PartScale.Z);
 6115464        _preparedRadius = FixedMath.Max(radiusX, FixedMath.Max(radiusY, radiusZ));
 6115465        SetPreparedGeometryReach(_preparedRadius);
 6115466        _preparedArea = Fixed64.Pi * _preparedRadius * _preparedRadius;
 6115467        SetPreparedBounds(FixedBoundBox.FromCenterAndScopeClippedToDomain(
 6115468            snapshot.Center,
 6115469            Vector3d.One * _preparedRadius));
 6115470    }
 71
 72    private protected override void PublishShape()
 73    {
 6048274        _scaledRadius = _preparedRadius;
 6048275        Area = _preparedArea;
 6048276    }
 77
 78    internal override ExactMassWeight CalculateMassPropertyWeight()
 79    {
 36080        Fixed64 radius = GetMassPropertyRadius();
 35981        return ExactMassWeight.FromProduct(
 35982            Fixed64.FromFraction(4, 3) * Fixed64.Pi,
 35983            radius,
 35984            radius,
 35985            radius);
 86    }
 87
 88    internal Fixed64 GetMassPropertyRadius() =>
 1804389        HasCommittedShape ? _scaledRadius : GetCurrentScaledRadius();
 90
 91    internal override ExactMassWeight CalculatePreparedMassPropertyWeight() =>
 1022692        ExactMassWeight.FromProduct(
 1022693            Fixed64.FromFraction(4, 3) * Fixed64.Pi,
 1022694            _preparedRadius,
 1022695            _preparedRadius,
 1022696            _preparedRadius);
 97
 98    internal override Fixed3x3 CalculateCenterOfMassInertiaTensor(Fixed64 mass)
 99    {
 100        // For a solid sphere, the inertia tensor is (2/5)*m*r^2 for the diagonal elements
 17549101        Fixed64 radius = GetMassPropertyRadius();
 17549102        Fixed64 diagonalElement = ExactMassProperties.InertiaMoment(
 17549103            mass, ExactMassProperties.InertiaGeometryTerm(Fixed64.FromFraction(2, 5), radius, radius));
 104
 17549105        Fixed3x3 tensor = new(
 17549106            diagonalElement, Fixed64.Zero, Fixed64.Zero,
 17549107            Fixed64.Zero, diagonalElement, Fixed64.Zero,
 17549108            Fixed64.Zero, Fixed64.Zero, diagonalElement
 17549109        );
 17549110        return tensor;
 111    }
 112
 113    /// <inheritdoc/>
 114    public override Vector3d ClosestPointOnSurface(Vector3d other)
 115    {
 113116        FixedPointAnchor anchor =
 113117            GetClosestSurfaceAnchor(other, out _);
 113118        if (anchor.TryGetPoint(out Vector3d point))
 112119            return point;
 120
 1121        throw new InvalidOperationException(
 1122            "The closest sphere surface point is outside the representable coordinate domain.");
 123    }
 124
 125    /// <inheritdoc/>
 126    public override Vector3d GetNormalAtPoint(Vector3d point)
 127    {
 1738128        if (point == Center)
 5129            return Vector3d.Zero;
 130
 1733131        _ = GetClosestSurfaceAnchor(
 1733132            point,
 1733133            out Vector3d normal);
 1733134        return normal;
 135    }
 136
 137    internal override FixedPointAnchor GetClosestSurfaceAnchor(
 138        Vector3d point,
 139        out Vector3d normal) =>
 10219140        GetClosestSurfaceAnchor(point, out normal, out _);
 141
 142    internal override FixedPointAnchor GetClosestSurfaceAnchor(
 143        Vector3d point, out Vector3d normal, out bool contained) =>
 10751144        WideFiniteAxisIntersection
 10751145            .GetClosestCenteredCapsuleSurfaceAnchor(
 10751146                point,
 10751147                Center,
 10751148                FixedQuaternion.Identity,
 10751149                Vector3d.Up,
 10751150                Fixed64.Zero,
 10751151                ScaledRadius,
 10751152                Vector3d.Right,
 10751153                out normal,
 10751154                out contained);
 155
 156    /// <inheritdoc/>
 157    public override bool ColliderOverlapsRay(RaycastSegmentWorker worker, ref SwiftList<Vector3d> outputIntersectionPoin
 1386158        worker.CheckSphereOverlaps(this, ref outputIntersectionPoints);
 159}