< Summary

Information
Class: Gravitas.Colliders.LSCuboidCollider
Assembly: Gravitas
File(s): /home/runner/work/Gravitas/Gravitas/src/Gravitas/Colliders/3D/Shapes/LSCuboidCollider.cs
Line coverage
100%
Covered lines: 107
Uncovered lines: 0
Coverable lines: 107
Total lines: 223
Line coverage: 100%
Branch coverage
100%
Covered branches: 20
Total branches: 20
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/3D/Shapes/LSCuboidCollider.cs

#LineLine coverage
 1//=======================================================================
 2// LSCuboidCollider.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 cuboid collider.</summary>
 17public sealed class LSCuboidCollider : LSCollider
 18{
 19    private FixedOrientedBox _orientedBox;
 20    private FixedOrientedBox _preparedOrientedBox;
 21    private Fixed64 _preparedArea;
 22
 23    /// <inheritdoc/>
 57064524    public override ColliderType Shape => Rotation == FixedQuaternion.Identity ? ColliderType.AABox : ColliderType.OBBox
 25
 26    /// <inheritdoc/>
 832427    public override int Priority => ColliderSettings.GetPriority(Shape);
 28
 29    /// <summary>
 30    /// Gets the canonical committed oriented-box geometry.
 31    /// </summary>
 8148132    public FixedOrientedBox OrientedBox => _orientedBox;
 33
 34    /// <inheritdoc/>
 35    public override Fixed64 ScaledRadius =>
 387136        HasCommittedShape
 387137            ? CanonicalCenteredProxyRadius
 387138            : ColliderCanonicalBounds
 387139                .GetCurrentCenteredProxyRadius(this);
 40
 41    internal override ExactMassWeight CalculateMassPropertyWeight()
 42    {
 14743        Vector3d halfExtents = GetMassPropertyHalfExtents();
 14744        return ExactMassWeight.FromProduct(
 14745            halfExtents.X,
 14746            halfExtents.Y,
 14747            halfExtents.Z,
 14748            (Fixed64)8);
 49    }
 50
 51    internal Vector3d GetMassPropertyHalfExtents()
 52    {
 440453        if (HasCommittedShape)
 435954            return _orientedBox.HalfExtents;
 55
 4556        GetCurrentShapeScales(out Vector3d ownerScale, out Vector3d partScale);
 4557        return ColliderScalePolicy.ScalePositive(Size, ownerScale, partScale, Fixed64.Two);
 58    }
 59
 60    internal override ExactMassWeight CalculatePreparedMassPropertyWeight()
 61    {
 600262        Vector3d halfExtents = _preparedOrientedBox.HalfExtents;
 600263        return ExactMassWeight.FromProduct(
 600264            halfExtents.X,
 600265            halfExtents.Y,
 600266            halfExtents.Z,
 600267            (Fixed64)8);
 68    }
 69
 70    /// <summary>Creates a cuboid collider with default dimensions.</summary>
 446471    public LSCuboidCollider() { }
 72
 73    /// <summary>Creates a runtime cuboid from an authored shape definition.</summary>
 74    public LSCuboidCollider(ColliderShapeDefinition definition)
 20775        : this()
 76    {
 20777        definition.EnsureKind(ColliderShapeDefinitionKind.Cuboid);
 20778        Material = definition.Material;
 20779        Size = definition.Size;
 20780    }
 81
 82    private protected override void PrepareShape(in ColliderShapeSnapshot snapshot)
 83    {
 2731784        Vector3d halfExtents = ColliderScalePolicy.ScalePositive(
 2731785            snapshot.Size,
 2731786            snapshot.OwnerScale,
 2731787            snapshot.PartScale,
 2731788            Fixed64.Two);
 2731789        SetPreparedGeometryReach(HasCommittedShape && halfExtents == _orientedBox.HalfExtents
 2731790            ? CanonicalCenteredProxyRadius : ColliderGeometryPolicy.Magnitude(halfExtents));
 2731791        _preparedOrientedBox = new FixedOrientedBox(
 2731792            snapshot.Center,
 2731793            snapshot.Rotation,
 2731794            halfExtents);
 2731795        _preparedArea = GetSurfaceArea(halfExtents);
 2731796        SetPreparedBounds(_preparedOrientedBox.GetBoundsClippedToDomain());
 2731797    }
 98
 99    private protected override void PublishShape()
 100    {
 27288101        _orientedBox = _preparedOrientedBox;
 27288102        Area = _preparedArea;
 27288103    }
 104
 105    internal override Fixed3x3 CalculateCenterOfMassInertiaTensor(Fixed64 mass)
 106    {
 4192107        Vector3d halfExtents = GetMassPropertyHalfExtents();
 4192108        Signed192 xSquared = Fixed64.GetExactRawProduct(halfExtents.X, halfExtents.X);
 4192109        Signed192 ySquared = Fixed64.GetExactRawProduct(halfExtents.Y, halfExtents.Y);
 4192110        Signed192 zSquared = Fixed64.GetExactRawProduct(halfExtents.Z, halfExtents.Z);
 4192111        Fixed64 xx = ExactMassProperties.InertiaBoxMoment(mass, ySquared, zSquared);
 4192112        Fixed64 yy = ExactMassProperties.InertiaBoxMoment(mass, xSquared, zSquared);
 4192113        Fixed64 zz = ExactMassProperties.InertiaBoxMoment(mass, xSquared, ySquared);
 114
 4192115        Fixed3x3 tensor = new(
 4192116            xx, Fixed64.Zero, Fixed64.Zero,
 4192117            Fixed64.Zero, yy, Fixed64.Zero,
 4192118            Fixed64.Zero, Fixed64.Zero, zz
 4192119        );
 4192120        return tensor;
 121    }
 122
 123    /// <inheritdoc/>
 124    public override Fixed64 GetFrontalArea(Vector3d direction)
 125    {
 591126        if (direction.MagnitudeSquared <= Fixed64.Epsilon)
 1127            return Area;
 128
 590129        direction = direction.Normalized;
 590130        _orientedBox.GetAxes(out Vector3d axisX, out Vector3d axisY, out Vector3d axisZ);
 590131        Fixed64 dotX = Vector3d.Dot(direction, axisX).Abs();
 590132        Fixed64 dotY = Vector3d.Dot(direction, axisY).Abs();
 590133        Fixed64 dotZ = Vector3d.Dot(direction, axisZ).Abs();
 590134        Vector3d halfExtents = _orientedBox.HalfExtents;
 135
 136        // The orthographic projection of a box is the sum of each face area's
 137        // contribution along the view direction.
 590138        return GetProjectedFaceArea(halfExtents.Y, halfExtents.Z, dotX)
 590139            + GetProjectedFaceArea(halfExtents.X, halfExtents.Z, dotY)
 590140            + GetProjectedFaceArea(halfExtents.X, halfExtents.Y, dotZ);
 141    }
 142
 143    /// <inheritdoc/>
 144    public override Vector3d ClosestPointOnSurface(Vector3d other)
 145    {
 218146        FixedPointAnchor anchor =
 218147            GetClosestSurfaceAnchor(other, out _);
 218148        if (anchor.TryGetPoint(out Vector3d closestPoint))
 217149            return closestPoint;
 150
 1151        throw new InvalidOperationException(
 1152            "The selected cuboid surface point is outside the representable coordinate domain.");
 153    }
 154
 155    /// <inheritdoc/>
 156    public override Vector3d GetNormalAtPoint(Vector3d point) =>
 685157        _orientedBox.GetNearestFaceNormal(point);
 158
 159    internal override FixedPointAnchor GetClosestSurfaceAnchor(
 160        Vector3d point,
 161        out Vector3d normal) =>
 441162        GetClosestSurfaceAnchor(point, out normal, out _);
 163
 164    internal override FixedPointAnchor GetClosestSurfaceAnchor(
 165        Vector3d point, out Vector3d normal, out bool contained)
 166    {
 1603167        contained = _orientedBox.Contains(point);
 1603168        FixedPointAnchor surface = _orientedBox.GetClosestPointAnchor(point);
 169        // A contained start exits through its face; an exterior corner uses the closest-feature direction.
 1603170        normal = contained ? _orientedBox.GetNearestFaceNormal(point)
 1603171            : WidePointAnchor3d.GetDirection(
 1603172                new FixedPointAnchor(point, FixedQuaternion.Identity, Vector3d.Zero), surface);
 1603173        return surface;
 174    }
 175
 176    internal override bool TryGetPlanarSurfaceNormal(Vector3d point, out Vector3d normal)
 177    {
 29178        normal = GetNormalAtPoint(point);
 29179        return true;
 180    }
 181
 182    private static Fixed64 GetSurfaceArea(Vector3d halfExtents) =>
 27317183        GetScaledProduct(halfExtents.X, halfExtents.Y, (Fixed64)8)
 27317184        + GetScaledProduct(halfExtents.X, halfExtents.Z, (Fixed64)8)
 27317185        + GetScaledProduct(halfExtents.Y, halfExtents.Z, (Fixed64)8);
 186
 187    private static Fixed64 GetProjectedFaceArea(
 188        Fixed64 firstHalfExtent,
 189        Fixed64 secondHalfExtent,
 190        Fixed64 alignment)
 191    {
 192        // Published half-extents are bounded by admission; a projected face fits Fixed64.
 1770193        _ = Fixed64.TryMultiplyDivide(
 1770194            firstHalfExtent,
 1770195            secondHalfExtent,
 1770196            alignment,
 1770197            Fixed64.One / (Fixed64)4,
 1770198            out Fixed64 area);
 1770199        return area;
 200    }
 201
 202    private static Fixed64 GetScaledProduct(
 203        Fixed64 first,
 204        Fixed64 second,
 205        Fixed64 multiplier) =>
 81951206        Fixed64.TryMultiplyDivide(
 81951207            first,
 81951208            second,
 81951209            multiplier,
 81951210            Fixed64.One,
 81951211            out Fixed64 product)
 81951212            ? product
 81951213            : Fixed64.MaxValue;
 214
 215    /// <inheritdoc/>
 216    public override bool ColliderOverlapsRay(RaycastSegmentWorker worker, ref SwiftList<Vector3d> outputIntersectionPoin
 217    {
 452218        if (Shape == ColliderType.AABox)
 439219            return worker.CheckAABBoxOverlaps(this, ref outputIntersectionPoints);
 220
 13221        return worker.CheckOBBoxOverlaps(this, ref outputIntersectionPoints);
 222    }
 223}