< Summary

Information
Class: Gravitas.Colliders.LSCylinderCollider
Assembly: Gravitas
File(s): /home/runner/work/Gravitas/Gravitas/src/Gravitas/Colliders/3D/Shapes/LSCylinderCollider.cs
Line coverage
100%
Covered lines: 115
Uncovered lines: 0
Coverable lines: 115
Total lines: 225
Line coverage: 100%
Branch coverage
100%
Covered branches: 14
Total branches: 14
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%
get_ScaledRadius()100%11100%
OnRadiusChanged()100%11100%
NormalizeSize(...)100%11100%
CalculateMassPropertyWeight()100%11100%
GetMassPropertyGeometry(...)100%22100%
CalculatePreparedMassPropertyWeight()100%11100%
GetInertiaGeometry(...)100%11100%
CalculateCenterOfMassInertiaTensor(...)100%11100%
ColliderOverlapsRay(...)100%11100%
ClosestPointOnSurface(...)100%22100%
GetNormalAtPoint(...)100%11100%
GetClosestSurfaceAnchor(...)100%11100%
GetClosestSurfaceAnchor(...)100%11100%
PrepareShape(...)100%66100%
PublishShape()100%11100%
GetFrontalArea(...)100%44100%

File(s)

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

#LineLine coverage
 1//=======================================================================
 2// LSCylinderCollider.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 finite runtime 3D cylinder whose local axis follows Y.</summary>
 17public sealed class LSCylinderCollider : LSCollider
 18{
 104419    private Fixed64 _scaledRadius = Fixed64.Half;
 20    private Fixed64 _preparedRadius;
 21    private Fixed64 _preparedHeight;
 22    private Vector3d _preparedAxis;
 23    private Fixed64 _preparedArea;
 24
 25    /// <summary>Creates a cylinder collider with default dimensions.</summary>
 160426    public LSCylinderCollider() { }
 27
 28    /// <summary>Creates a runtime cylinder from an authored shape definition.</summary>
 24229    public LSCylinderCollider(ColliderShapeDefinition definition)
 30    {
 24231        definition.EnsureKind(ColliderShapeDefinitionKind.Cylinder);
 24232        Material = definition.Material;
 24233        Radius = definition.Radius;
 24234        Size = definition.Size;
 24235    }
 36
 37    /// <inheritdoc/>
 10645038    public override ColliderType Shape => ColliderType.Cylinder;
 39    /// <inheritdoc/>
 245840    public override int Priority => ColliderSettings.GetPriority(Shape);
 41
 42    /// <inheritdoc/>
 2674943    public override Fixed64 ScaledRadius => _scaledRadius;
 44
 45    /// <summary>
 46    /// Gets the cylinder's full physical length along <see cref="WorldAxis"/>.
 47    /// </summary>
 48    public Fixed64 Height { get; private set; }
 49
 50    /// <summary>
 51    /// Gets the derived normalized world-space cylinder axis. Exact geometry
 52    /// remains authoritative in the collider's rigid frame.
 53    /// </summary>
 54    public Vector3d WorldAxis { get; private set; } = Vector3d.Up;
 55
 56    /// <inheritdoc/>
 57    protected override void OnRadiusChanged()
 58    {
 32459        Fixed64 diameter = _radius * 2;
 32460        _size = new Vector3d(diameter, _size.Y, diameter);
 32461    }
 62
 63    /// <inheritdoc/>
 64    protected override Vector3d NormalizeSize(Vector3d value) =>
 93765        new(_radius * 2, value.Y, _radius * 2);
 66
 67    internal override ExactMassWeight CalculateMassPropertyWeight()
 68    {
 4469        GetMassPropertyGeometry(out Fixed64 radius, out Fixed64 height);
 4470        return ExactMassWeight.FromProduct(
 4471            Fixed64.Pi,
 4472            radius,
 4473            radius,
 4474            height);
 75    }
 76
 77    private void GetMassPropertyGeometry(out Fixed64 radius, out Fixed64 height)
 78    {
 83179        if (HasCommittedShape)
 80        {
 82281            radius = _scaledRadius;
 82282            height = Height;
 82283            return;
 84        }
 85
 986        GetCurrentShapeScales(out Vector3d ownerScale, out Vector3d partScale);
 987        radius = FixedMath.Max(
 988            ColliderScalePolicy.ScalePositive(Radius, ownerScale.X, partScale.X),
 989            ColliderScalePolicy.ScalePositive(Radius, ownerScale.Z, partScale.Z));
 990        height = ColliderScalePolicy.ScalePositive(Size.Y, ownerScale.Y, partScale.Y);
 991    }
 92
 93    internal override ExactMassWeight CalculatePreparedMassPropertyWeight() =>
 533294        ExactMassWeight.FromProduct(
 533295            Fixed64.Pi,
 533296            _preparedRadius,
 533297            _preparedRadius,
 533298            _preparedHeight);
 99
 100    internal void GetInertiaGeometry(out Signed192 transverse, out Signed192 axial)
 101    {
 787102        GetMassPropertyGeometry(out Fixed64 radius, out Fixed64 height);
 787103        Fixed64 twelfth = Fixed64.FromFraction(1, 12);
 787104        transverse = WideArithmetic.AddSigned192(
 787105            ExactMassProperties.InertiaGeometryTerm(twelfth * (Fixed64)3, radius, radius),
 787106            ExactMassProperties.InertiaGeometryTerm(twelfth, height, height));
 787107        axial = ExactMassProperties.InertiaGeometryTerm(Fixed64.Half, radius, radius);
 787108    }
 109
 110    internal override Fixed3x3 CalculateCenterOfMassInertiaTensor(Fixed64 mass)
 111    {
 772112        GetInertiaGeometry(out Signed192 transverse, out Signed192 axial);
 772113        Fixed64 inertiaXZ = ExactMassProperties.InertiaMoment(mass, transverse);
 772114        Fixed64 inertiaY = ExactMassProperties.InertiaMoment(mass, axial);
 115
 772116        Fixed3x3 tensor = new(
 772117            inertiaXZ, Fixed64.Zero, Fixed64.Zero,
 772118            Fixed64.Zero, inertiaY, Fixed64.Zero,
 772119            Fixed64.Zero, Fixed64.Zero, inertiaXZ
 772120        );
 772121        return tensor;
 122    }
 123
 124    /// <inheritdoc/>
 125    public override bool ColliderOverlapsRay(RaycastSegmentWorker worker, ref SwiftList<Vector3d> outputIntersectionPoin
 126    {
 11127        return worker.CheckCylinderOverlaps(this, ref outputIntersectionPoints);
 128    }
 129
 130    /// <inheritdoc/>
 131    public override Vector3d ClosestPointOnSurface(Vector3d other)
 132    {
 9133        FixedPointAnchor surfaceAnchor =
 9134            GetClosestSurfaceAnchor(other, out _);
 9135        if (!surfaceAnchor.TryGetPoint(out Vector3d surfacePoint))
 136        {
 1137            throw new InvalidOperationException(
 1138                "The closest cylinder surface point is outside the representable coordinate domain.");
 139        }
 140
 8141        return surfacePoint;
 142    }
 143
 144    /// <inheritdoc/>
 145    public override Vector3d GetNormalAtPoint(Vector3d point)
 146    {
 696147        _ = GetClosestSurfaceAnchor(
 696148            point,
 696149            out Vector3d outwardNormal);
 696150        return outwardNormal;
 151    }
 152
 153    internal override FixedPointAnchor GetClosestSurfaceAnchor(
 154        Vector3d point,
 155        out Vector3d normal) =>
 1247156        GetClosestSurfaceAnchor(point, out normal, out _);
 157
 158    internal override FixedPointAnchor GetClosestSurfaceAnchor(
 159        Vector3d point, out Vector3d normal, out bool contained) =>
 1415160        WideFiniteAxisIntersection
 1415161            .GetClosestCenteredFiniteCylinderSurfaceAnchor(
 1415162                point,
 1415163                Center,
 1415164                Rotation,
 1415165                Vector3d.Up,
 1415166                Height,
 1415167                ScaledRadius,
 1415168                Vector3d.Right,
 1415169                out normal,
 1415170                out contained);
 171
 172    private protected override void PrepareShape(in ColliderShapeSnapshot snapshot)
 173    {
 7661174        Fixed64 radiusX = ColliderScalePolicy.ScalePositive(
 7661175            snapshot.Radius,
 7661176            snapshot.OwnerScale.X,
 7661177            snapshot.PartScale.X);
 7661178        Fixed64 radiusZ = ColliderScalePolicy.ScalePositive(
 7661179            snapshot.Radius,
 7661180            snapshot.OwnerScale.Z,
 7661181            snapshot.PartScale.Z);
 7661182        _preparedRadius = FixedMath.Max(radiusX, radiusZ);
 7661183        _preparedHeight = ColliderScalePolicy.ScalePositive(
 7661184            snapshot.Size.Y,
 7661185            snapshot.OwnerScale.Y,
 7661186            snapshot.PartScale.Y);
 7661187        SetPreparedGeometryReach(HasCommittedShape && _preparedRadius == _scaledRadius && _preparedHeight == Height
 7661188            ? CanonicalCenteredProxyRadius : ColliderGeometryPolicy.FiniteAxis(_preparedRadius, _preparedHeight));
 7661189        _preparedAxis = (snapshot.Rotation * Vector3d.Up).Normalized;
 7661190        _preparedArea = Fixed64.Two * Fixed64.Pi * _preparedRadius
 7661191            * (_preparedHeight + _preparedRadius);
 7661192        SetPreparedBounds(FixedBoundBox.FromCenteredFiniteCylinderClippedToDomain(
 7661193            snapshot.Center,
 7661194            snapshot.Rotation,
 7661195            Vector3d.Up,
 7661196            _preparedHeight,
 7661197            _preparedRadius));
 7661198    }
 199
 200    private protected override void PublishShape()
 201    {
 7655202        _scaledRadius = _preparedRadius;
 7655203        Height = _preparedHeight;
 7655204        WorldAxis = _preparedAxis;
 7655205        Area = _preparedArea;
 7655206    }
 207
 208    /// <inheritdoc/>
 209    public override Fixed64 GetFrontalArea(Vector3d direction)
 210    {
 3211        Fixed64 directionMagnitude = direction.Magnitude;
 3212        if (directionMagnitude <= Fixed64.Epsilon)
 1213            return Area;
 214
 2215        Vector3d normalizedDirection = direction / directionMagnitude;
 2216        Fixed64 axial = Rotation.Inverse()
 2217            .Rotate(normalizedDirection).Y.Abs();
 2218        Fixed64 radialFactorSqr = Fixed64.One - axial * axial;
 2219        Fixed64 radialFactor = radialFactorSqr <= Fixed64.Zero ? Fixed64.Zero : FixedMath.Sqrt(radialFactorSqr);
 220
 2221        return axial * Fixed64.Pi * ScaledRadiusSqr
 2222            + radialFactor * 2 * ScaledRadius * Height;
 223    }
 224
 225}