< Summary

Information
Class: Gravitas.Colliders.CompoundMomentMath2D
Assembly: Gravitas
File(s): /home/runner/work/Gravitas/Gravitas/src/Gravitas/Colliders/MassProperties/CompoundMomentMath2D.cs
Line coverage
100%
Covered lines: 34
Uncovered lines: 0
Coverable lines: 34
Total lines: 71
Line coverage: 100%
Branch coverage
100%
Covered branches: 10
Total branches: 10
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
GetWeightedMoment(...)100%1010100%

File(s)

/home/runner/work/Gravitas/Gravitas/src/Gravitas/Colliders/MassProperties/CompoundMomentMath2D.cs

#LineLine coverage
 1//=======================================================================
 2// CompoundMomentMath2D.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;
 9
 10namespace Gravitas.Colliders;
 11
 12/// <summary>
 13/// Retains area-weighted intrinsic moments in Q224 with the common factor sixty,
 14/// so compound mass is applied once after geometry and parallel-axis reduction.
 15/// </summary>
 16internal static class CompoundMomentMath2D
 17{
 18    internal static Signed576 GetWeightedMoment(LSCollider2D part, ExactMassWeight weight,
 19        Fixed64 rotation, Signed320 dx, Signed320 dy, out Signed576 firstMomentContribution)
 20    {
 31221        firstMomentContribution = default;
 31222        if (part is LSCircleCollider2D circle)
 23        {
 17224            Fixed64 radius = circle.ScaledRadius;
 17225            return WideArithmetic.MultiplySigned576(
 17226                WideArithmetic.MultiplySigned320(weight.Numerator,
 17227                    ExactMassProperties.InertiaGeometryTerm(Fixed64.Half, radius, radius)), 60);
 28        }
 14029        if (part is LSAABBoxCollider2D box)
 30        {
 5431            Vector2d half = box.ScaledHalfExtents;
 5432            Signed192 squared = WideArithmetic.AddSigned192(
 5433                Fixed64.GetExactRawProduct(half.X, half.X),
 5434                Fixed64.GetExactRawProduct(half.Y, half.Y));
 5435            return WideArithmetic.MultiplySigned576(
 5436                WideArithmetic.MultiplySigned320(weight.Numerator, squared), Signed192.One, Signed192.Signed(20));
 37        }
 8638        if (part is LSPolygonCollider2D polygon)
 39        {
 6540            PolygonMassProperties2D properties = polygon.GetMassProperties();
 6541            firstMomentContribution = properties.CalculateWeightedFirstMomentContribution(rotation, dx, dy);
 6542            return properties.CalculateWeightedMoment();
 43        }
 44
 45        // Authored compound definitions admit only circle, box, polygon and capsule parts.
 2146        var capsule = (LSCapsuleCollider2D)part;
 2147        capsule.GetMassPropertyGeometry(out Fixed64 radiusValue, out Fixed64 length);
 2148        if (length <= Fixed64.Epsilon)
 49        {
 350            return WideArithmetic.MultiplySigned576(
 351                WideArithmetic.MultiplySigned320(weight.Numerator,
 352                    ExactMassProperties.InertiaGeometryTerm(Fixed64.Half, radiusValue, radiusValue)), 60);
 53        }
 1854        if (radiusValue == Fixed64.Zero)
 55        {
 56            // A collapsed detached stadium has zero geometric area. The compound
 57            // supplies nominal weight, while its intrinsic moment retains the rod limit.
 558            return WideArithmetic.MultiplySigned576(
 559                WideArithmetic.MultiplySigned320(weight.Numerator,
 560                    ExactMassProperties.InertiaGeometryTerm(Fixed64.One, length, length)), 5);
 61        }
 62
 1363        ExactMassWeight rectangleWeight = ExactMassWeight.FromProduct(Fixed64.Two, radiusValue, length);
 1364        ExactMassWeight capWeight = ExactMassWeight.FromProduct(Fixed64.Pi, radiusValue, radiusValue);
 1365        LSCapsuleCollider2D.GetCenteredMomentGeometry(radiusValue, length,
 1366            out Signed192 rectangleGeometry, out Signed192 capGeometry);
 1367        return WideArithmetic.MultiplySigned576(WideArithmetic.AddSigned576(
 1368            WideArithmetic.MultiplySigned320(rectangleWeight.Numerator, rectangleGeometry),
 1369            WideArithmetic.MultiplySigned320(capWeight.Numerator, capGeometry)), 5);
 70    }
 71}