< Summary

Information
Class: Gravitas.Colliders.LSAABBoxCollider2D
Assembly: Gravitas
File(s): /home/runner/work/Gravitas/Gravitas/src/Gravitas/Colliders/2D/Shapes/LSAABBoxCollider2D.cs
Line coverage
100%
Covered lines: 120
Uncovered lines: 0
Coverable lines: 120
Total lines: 226
Line coverage: 100%
Branch coverage
100%
Covered branches: 30
Total branches: 30
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

File(s)

/home/runner/work/Gravitas/Gravitas/src/Gravitas/Colliders/2D/Shapes/LSAABBoxCollider2D.cs

#LineLine coverage
 1//=======================================================================
 2// LSAABBoxCollider2D.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 Chronicler;
 11using FixedMathSharp;
 12using FixedMathSharp.Geometry;
 13
 14namespace Gravitas.Colliders;
 15
 16/// <summary>
 17/// Pure 2D axis-aligned box collider.
 18/// </summary>
 19public sealed class LSAABBoxCollider2D : LSCollider2D, IConvexVertexSource2D
 20{
 21    private Vector2d _size;
 22    private Vector2d _halfExtents;
 23    private Vector2d _scaledHalfExtents;
 24    private Vector2d _preparedHalfExtents;
 25
 26    /// <summary>Creates a pure 2D axis-aligned box with an authored size.</summary>
 293227    public LSAABBoxCollider2D(Vector2d size)
 28    {
 293229        Size = size;
 293030    }
 31
 32    /// <summary>Creates a runtime box from an authored shape definition.</summary>
 15633    public LSAABBoxCollider2D(ColliderShapeDefinition2D definition)
 34    {
 15635        definition.EnsureKind(ColliderShapeDefinition2DKind.AABBox);
 15636        Material = definition.Material;
 15637        Size = definition.Size;
 15638    }
 39
 40    /// <inheritdoc/>
 23174641    public override ColliderType2D Shape => ColliderType2D.AABox;
 42
 43    /// <summary>Gets or sets the unscaled full box size.</summary>
 44    public Vector2d Size
 45    {
 46        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 423747        get => _size;
 48        set
 49        {
 310150            ChronicleGuard.ThrowIfArgument(
 310151                value.X <= Fixed64.Zero || value.Y <= Fixed64.Zero,
 310152                nameof(value),
 310153                "2D AABB size components must be greater than zero.");
 309954            if (_size == value)
 255                return;
 56
 309757            ValidateGeometryMutation(size: value);
 309558            _size = value;
 309559            _halfExtents = value * Fixed64.Half;
 309560            MarkShapeDirty();
 309561        }
 62    }
 63
 64    /// <summary>Gets half of the authored unscaled size.</summary>
 665    public Vector2d HalfExtents => _halfExtents;
 66
 67    /// <summary>Gets the half extents after owner and compound-part scaling.</summary>
 68    public Vector2d ScaledHalfExtents
 69    {
 70        get
 71        {
 17944972            if (HasCommittedShape)
 16719873                return _scaledHalfExtents;
 74
 1225175            GetCurrentScaleFactors(
 1225176                out Vector2d ownerScale,
 1225177                out Vector2d partScale);
 1225178            return ColliderScalePolicy.Scale(
 1225179                _size,
 1225180                ownerScale,
 1225181                partScale,
 1225182                Fixed64.Two);
 83        }
 84    }
 85
 11527986    int IConvexVertexSource2D.VertexCount => 4;
 87
 11060188    Fixed64 IConvexVertexSource2D.Rotation => Fixed64.Zero;
 89
 90    /// <inheritdoc/>
 91    public override bool ContainsPoint(Vector2d point)
 92    {
 457093        Span<Vector2d> offsets = stackalloc Vector2d[4];
 457094        GetCenterRelativeVertices(offsets);
 457095        return FixedConvex2dRelations.ContainsPoint(point, Center, offsets);
 96    }
 97
 98    /// <inheritdoc/>
 99    public override Vector2d GetClosestPoint(Vector2d point)
 100    {
 4101        Span<Vector2d> offsets = stackalloc Vector2d[4];
 4102        GetCenterRelativeVertices(offsets);
 4103        if (FixedConvex2dRelations.ContainsPoint(point, Center, offsets))
 1104            return point;
 105
 3106        Vector2d offset =
 3107            FixedConvex2dRelations.GetClosestPointOffset(
 3108                point,
 3109                Center,
 3110                offsets);
 111        // An exterior closest point lies on the segment from the representable
 112        // query to the representable box center, so its final sum is in range.
 3113        _ = Vector2d.TryAdd(Center, offset, out Vector2d closest);
 3114        return closest;
 115    }
 116
 117    /// <inheritdoc/>
 118    public override Vector2d GetSupportPoint(Vector2d direction)
 119    {
 10120        Vector2d halfExtents = ScaledHalfExtents;
 10121        Vector2d offset = new(
 10122            direction.X >= Fixed64.Zero
 10123                ? halfExtents.X
 10124                : -halfExtents.X,
 10125            direction.Y >= Fixed64.Zero
 10126                ? halfExtents.Y
 10127                : -halfExtents.Y);
 10128        if (Vector2d.TryAdd(Center, offset, out Vector2d support))
 8129            return support;
 130
 2131        throw new System.InvalidOperationException(
 2132            "The box support point is outside the Fixed64 coordinate domain.");
 133    }
 134
 135    Vector2d IConvexVertexSource2D.GetScaledLocalVertexUnchecked(int index)
 136    {
 162758137        Vector2d halfExtents = ScaledHalfExtents;
 162758138        return index switch
 162758139        {
 44623140            0 => new Vector2d(-halfExtents.X, -halfExtents.Y),
 39832141            1 => new Vector2d(halfExtents.X, -halfExtents.Y),
 38429142            2 => new Vector2d(halfExtents.X, halfExtents.Y),
 39874143            _ => new Vector2d(-halfExtents.X, halfExtents.Y)
 162758144        };
 145    }
 146
 147    FixedPointAnchor2d IConvexVertexSource2D.GetSupportAnchor(Vector2d direction)
 148    {
 5149        Vector2d halfExtents = ScaledHalfExtents;
 5150        return new FixedPointAnchor2d(
 5151            Center,
 5152            Fixed64.Zero,
 5153            new Vector2d(
 5154                direction.X >= Fixed64.Zero
 5155                    ? halfExtents.X
 5156                    : -halfExtents.X,
 5157                direction.Y >= Fixed64.Zero
 5158                    ? halfExtents.Y
 5159                    : -halfExtents.Y));
 160    }
 161
 162    private void GetCenterRelativeVertices(Span<Vector2d> offsets)
 163    {
 4574164        Vector2d halfExtents = ScaledHalfExtents;
 4574165        offsets[0] = new Vector2d(-halfExtents.X, -halfExtents.Y);
 4574166        offsets[1] = new Vector2d(halfExtents.X, -halfExtents.Y);
 4574167        offsets[2] = new Vector2d(halfExtents.X, halfExtents.Y);
 4574168        offsets[3] = new Vector2d(-halfExtents.X, halfExtents.Y);
 4574169    }
 170
 171    internal override ExactMassWeight CalculateAreaForMassProperties()
 172    {
 155173        Vector2d halfExtents = ScaledHalfExtents;
 155174        return ExactMassWeight.FromProduct(
 155175            (Fixed64)4,
 155176            halfExtents.X,
 155177            halfExtents.Y);
 178    }
 179
 180    internal override ExactMassWeight CalculatePreparedAreaForMassProperties() =>
 386181        ExactMassWeight.FromProduct(
 386182            (Fixed64)4,
 386183            _preparedHalfExtents.X,
 386184            _preparedHalfExtents.Y);
 185
 186    internal override Fixed64 CalculateCenterOfMassMoment(Fixed64 mass)
 187    {
 1565188        Vector2d halfExtents = ScaledHalfExtents;
 1565189        return ExactMassProperties.InertiaBoxMoment(mass,
 1565190            Fixed64.GetExactRawProduct(halfExtents.X, halfExtents.X),
 1565191            Fixed64.GetExactRawProduct(halfExtents.Y, halfExtents.Y));
 192    }
 193
 194    private protected override void PrepareShape(in ColliderShapeSnapshot2D snapshot)
 195    {
 4197196        _preparedHalfExtents = ColliderScalePolicy.ScalePositive(
 4197197            _size,
 4197198            snapshot.OwnerScale,
 4197199            snapshot.PartScale,
 4197200            Fixed64.Two);
 4197201        SetPreparedGeometryReach(HasCommittedShape && _preparedHalfExtents == _scaledHalfExtents
 4197202            ? CanonicalCenteredProxyRadius : ColliderGeometryPolicy.Magnitude(_preparedHalfExtents));
 4197203        SetPreparedBounds(FixedBoundArea.FromCenterAndScopeClippedToDomain(
 4197204            snapshot.Center,
 4197205            _preparedHalfExtents));
 4197206    }
 207
 208    private protected override void PublishShape() =>
 4126209        _scaledHalfExtents = _preparedHalfExtents;
 210
 211    /// <inheritdoc/>
 212    protected override void RecordShapeData(IChronicler chronicler)
 213    {
 9214        Vector2d size = _size;
 9215        RecordValues.Look(chronicler, ref size, "Size", Vector2d.One);
 9216        if (chronicler.Mode == SerializationMode.Loading)
 217        {
 6218            ChronicleGuard.ThrowIfArgument(
 6219                size.X <= Fixed64.Zero || size.Y <= Fixed64.Zero,
 6220                nameof(size),
 6221                "2D AABB size components must be greater than zero.");
 4222            _size = size;
 4223            _halfExtents = size * Fixed64.Half;
 224        }
 7225    }
 226}