| | | 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 | | |
| | | 8 | | using System; |
| | | 9 | | using FixedMathSharp; |
| | | 10 | | using FixedMathSharp.Geometry; |
| | | 11 | | using Gravitas.Queries; |
| | | 12 | | using SwiftCollections; |
| | | 13 | | |
| | | 14 | | namespace Gravitas.Colliders; |
| | | 15 | | |
| | | 16 | | /// <summary>Represents a runtime 3D cuboid collider.</summary> |
| | | 17 | | public sealed class LSCuboidCollider : LSCollider |
| | | 18 | | { |
| | | 19 | | private FixedOrientedBox _orientedBox; |
| | | 20 | | private FixedOrientedBox _preparedOrientedBox; |
| | | 21 | | private Fixed64 _preparedArea; |
| | | 22 | | |
| | | 23 | | /// <inheritdoc/> |
| | 570645 | 24 | | public override ColliderType Shape => Rotation == FixedQuaternion.Identity ? ColliderType.AABox : ColliderType.OBBox |
| | | 25 | | |
| | | 26 | | /// <inheritdoc/> |
| | 8324 | 27 | | public override int Priority => ColliderSettings.GetPriority(Shape); |
| | | 28 | | |
| | | 29 | | /// <summary> |
| | | 30 | | /// Gets the canonical committed oriented-box geometry. |
| | | 31 | | /// </summary> |
| | 81481 | 32 | | public FixedOrientedBox OrientedBox => _orientedBox; |
| | | 33 | | |
| | | 34 | | /// <inheritdoc/> |
| | | 35 | | public override Fixed64 ScaledRadius => |
| | 3871 | 36 | | HasCommittedShape |
| | 3871 | 37 | | ? CanonicalCenteredProxyRadius |
| | 3871 | 38 | | : ColliderCanonicalBounds |
| | 3871 | 39 | | .GetCurrentCenteredProxyRadius(this); |
| | | 40 | | |
| | | 41 | | internal override ExactMassWeight CalculateMassPropertyWeight() |
| | | 42 | | { |
| | 147 | 43 | | Vector3d halfExtents = GetMassPropertyHalfExtents(); |
| | 147 | 44 | | return ExactMassWeight.FromProduct( |
| | 147 | 45 | | halfExtents.X, |
| | 147 | 46 | | halfExtents.Y, |
| | 147 | 47 | | halfExtents.Z, |
| | 147 | 48 | | (Fixed64)8); |
| | | 49 | | } |
| | | 50 | | |
| | | 51 | | internal Vector3d GetMassPropertyHalfExtents() |
| | | 52 | | { |
| | 4404 | 53 | | if (HasCommittedShape) |
| | 4359 | 54 | | return _orientedBox.HalfExtents; |
| | | 55 | | |
| | 45 | 56 | | GetCurrentShapeScales(out Vector3d ownerScale, out Vector3d partScale); |
| | 45 | 57 | | return ColliderScalePolicy.ScalePositive(Size, ownerScale, partScale, Fixed64.Two); |
| | | 58 | | } |
| | | 59 | | |
| | | 60 | | internal override ExactMassWeight CalculatePreparedMassPropertyWeight() |
| | | 61 | | { |
| | 6002 | 62 | | Vector3d halfExtents = _preparedOrientedBox.HalfExtents; |
| | 6002 | 63 | | return ExactMassWeight.FromProduct( |
| | 6002 | 64 | | halfExtents.X, |
| | 6002 | 65 | | halfExtents.Y, |
| | 6002 | 66 | | halfExtents.Z, |
| | 6002 | 67 | | (Fixed64)8); |
| | | 68 | | } |
| | | 69 | | |
| | | 70 | | /// <summary>Creates a cuboid collider with default dimensions.</summary> |
| | 4464 | 71 | | public LSCuboidCollider() { } |
| | | 72 | | |
| | | 73 | | /// <summary>Creates a runtime cuboid from an authored shape definition.</summary> |
| | | 74 | | public LSCuboidCollider(ColliderShapeDefinition definition) |
| | 207 | 75 | | : this() |
| | | 76 | | { |
| | 207 | 77 | | definition.EnsureKind(ColliderShapeDefinitionKind.Cuboid); |
| | 207 | 78 | | Material = definition.Material; |
| | 207 | 79 | | Size = definition.Size; |
| | 207 | 80 | | } |
| | | 81 | | |
| | | 82 | | private protected override void PrepareShape(in ColliderShapeSnapshot snapshot) |
| | | 83 | | { |
| | 27317 | 84 | | Vector3d halfExtents = ColliderScalePolicy.ScalePositive( |
| | 27317 | 85 | | snapshot.Size, |
| | 27317 | 86 | | snapshot.OwnerScale, |
| | 27317 | 87 | | snapshot.PartScale, |
| | 27317 | 88 | | Fixed64.Two); |
| | 27317 | 89 | | SetPreparedGeometryReach(HasCommittedShape && halfExtents == _orientedBox.HalfExtents |
| | 27317 | 90 | | ? CanonicalCenteredProxyRadius : ColliderGeometryPolicy.Magnitude(halfExtents)); |
| | 27317 | 91 | | _preparedOrientedBox = new FixedOrientedBox( |
| | 27317 | 92 | | snapshot.Center, |
| | 27317 | 93 | | snapshot.Rotation, |
| | 27317 | 94 | | halfExtents); |
| | 27317 | 95 | | _preparedArea = GetSurfaceArea(halfExtents); |
| | 27317 | 96 | | SetPreparedBounds(_preparedOrientedBox.GetBoundsClippedToDomain()); |
| | 27317 | 97 | | } |
| | | 98 | | |
| | | 99 | | private protected override void PublishShape() |
| | | 100 | | { |
| | 27288 | 101 | | _orientedBox = _preparedOrientedBox; |
| | 27288 | 102 | | Area = _preparedArea; |
| | 27288 | 103 | | } |
| | | 104 | | |
| | | 105 | | internal override Fixed3x3 CalculateCenterOfMassInertiaTensor(Fixed64 mass) |
| | | 106 | | { |
| | 4192 | 107 | | Vector3d halfExtents = GetMassPropertyHalfExtents(); |
| | 4192 | 108 | | Signed192 xSquared = Fixed64.GetExactRawProduct(halfExtents.X, halfExtents.X); |
| | 4192 | 109 | | Signed192 ySquared = Fixed64.GetExactRawProduct(halfExtents.Y, halfExtents.Y); |
| | 4192 | 110 | | Signed192 zSquared = Fixed64.GetExactRawProduct(halfExtents.Z, halfExtents.Z); |
| | 4192 | 111 | | Fixed64 xx = ExactMassProperties.InertiaBoxMoment(mass, ySquared, zSquared); |
| | 4192 | 112 | | Fixed64 yy = ExactMassProperties.InertiaBoxMoment(mass, xSquared, zSquared); |
| | 4192 | 113 | | Fixed64 zz = ExactMassProperties.InertiaBoxMoment(mass, xSquared, ySquared); |
| | | 114 | | |
| | 4192 | 115 | | Fixed3x3 tensor = new( |
| | 4192 | 116 | | xx, Fixed64.Zero, Fixed64.Zero, |
| | 4192 | 117 | | Fixed64.Zero, yy, Fixed64.Zero, |
| | 4192 | 118 | | Fixed64.Zero, Fixed64.Zero, zz |
| | 4192 | 119 | | ); |
| | 4192 | 120 | | return tensor; |
| | | 121 | | } |
| | | 122 | | |
| | | 123 | | /// <inheritdoc/> |
| | | 124 | | public override Fixed64 GetFrontalArea(Vector3d direction) |
| | | 125 | | { |
| | 591 | 126 | | if (direction.MagnitudeSquared <= Fixed64.Epsilon) |
| | 1 | 127 | | return Area; |
| | | 128 | | |
| | 590 | 129 | | direction = direction.Normalized; |
| | 590 | 130 | | _orientedBox.GetAxes(out Vector3d axisX, out Vector3d axisY, out Vector3d axisZ); |
| | 590 | 131 | | Fixed64 dotX = Vector3d.Dot(direction, axisX).Abs(); |
| | 590 | 132 | | Fixed64 dotY = Vector3d.Dot(direction, axisY).Abs(); |
| | 590 | 133 | | Fixed64 dotZ = Vector3d.Dot(direction, axisZ).Abs(); |
| | 590 | 134 | | 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. |
| | 590 | 138 | | return GetProjectedFaceArea(halfExtents.Y, halfExtents.Z, dotX) |
| | 590 | 139 | | + GetProjectedFaceArea(halfExtents.X, halfExtents.Z, dotY) |
| | 590 | 140 | | + GetProjectedFaceArea(halfExtents.X, halfExtents.Y, dotZ); |
| | | 141 | | } |
| | | 142 | | |
| | | 143 | | /// <inheritdoc/> |
| | | 144 | | public override Vector3d ClosestPointOnSurface(Vector3d other) |
| | | 145 | | { |
| | 218 | 146 | | FixedPointAnchor anchor = |
| | 218 | 147 | | GetClosestSurfaceAnchor(other, out _); |
| | 218 | 148 | | if (anchor.TryGetPoint(out Vector3d closestPoint)) |
| | 217 | 149 | | return closestPoint; |
| | | 150 | | |
| | 1 | 151 | | throw new InvalidOperationException( |
| | 1 | 152 | | "The selected cuboid surface point is outside the representable coordinate domain."); |
| | | 153 | | } |
| | | 154 | | |
| | | 155 | | /// <inheritdoc/> |
| | | 156 | | public override Vector3d GetNormalAtPoint(Vector3d point) => |
| | 685 | 157 | | _orientedBox.GetNearestFaceNormal(point); |
| | | 158 | | |
| | | 159 | | internal override FixedPointAnchor GetClosestSurfaceAnchor( |
| | | 160 | | Vector3d point, |
| | | 161 | | out Vector3d normal) => |
| | 441 | 162 | | GetClosestSurfaceAnchor(point, out normal, out _); |
| | | 163 | | |
| | | 164 | | internal override FixedPointAnchor GetClosestSurfaceAnchor( |
| | | 165 | | Vector3d point, out Vector3d normal, out bool contained) |
| | | 166 | | { |
| | 1603 | 167 | | contained = _orientedBox.Contains(point); |
| | 1603 | 168 | | FixedPointAnchor surface = _orientedBox.GetClosestPointAnchor(point); |
| | | 169 | | // A contained start exits through its face; an exterior corner uses the closest-feature direction. |
| | 1603 | 170 | | normal = contained ? _orientedBox.GetNearestFaceNormal(point) |
| | 1603 | 171 | | : WidePointAnchor3d.GetDirection( |
| | 1603 | 172 | | new FixedPointAnchor(point, FixedQuaternion.Identity, Vector3d.Zero), surface); |
| | 1603 | 173 | | return surface; |
| | | 174 | | } |
| | | 175 | | |
| | | 176 | | internal override bool TryGetPlanarSurfaceNormal(Vector3d point, out Vector3d normal) |
| | | 177 | | { |
| | 29 | 178 | | normal = GetNormalAtPoint(point); |
| | 29 | 179 | | return true; |
| | | 180 | | } |
| | | 181 | | |
| | | 182 | | private static Fixed64 GetSurfaceArea(Vector3d halfExtents) => |
| | 27317 | 183 | | GetScaledProduct(halfExtents.X, halfExtents.Y, (Fixed64)8) |
| | 27317 | 184 | | + GetScaledProduct(halfExtents.X, halfExtents.Z, (Fixed64)8) |
| | 27317 | 185 | | + 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. |
| | 1770 | 193 | | _ = Fixed64.TryMultiplyDivide( |
| | 1770 | 194 | | firstHalfExtent, |
| | 1770 | 195 | | secondHalfExtent, |
| | 1770 | 196 | | alignment, |
| | 1770 | 197 | | Fixed64.One / (Fixed64)4, |
| | 1770 | 198 | | out Fixed64 area); |
| | 1770 | 199 | | return area; |
| | | 200 | | } |
| | | 201 | | |
| | | 202 | | private static Fixed64 GetScaledProduct( |
| | | 203 | | Fixed64 first, |
| | | 204 | | Fixed64 second, |
| | | 205 | | Fixed64 multiplier) => |
| | 81951 | 206 | | Fixed64.TryMultiplyDivide( |
| | 81951 | 207 | | first, |
| | 81951 | 208 | | second, |
| | 81951 | 209 | | multiplier, |
| | 81951 | 210 | | Fixed64.One, |
| | 81951 | 211 | | out Fixed64 product) |
| | 81951 | 212 | | ? product |
| | 81951 | 213 | | : Fixed64.MaxValue; |
| | | 214 | | |
| | | 215 | | /// <inheritdoc/> |
| | | 216 | | public override bool ColliderOverlapsRay(RaycastSegmentWorker worker, ref SwiftList<Vector3d> outputIntersectionPoin |
| | | 217 | | { |
| | 452 | 218 | | if (Shape == ColliderType.AABox) |
| | 439 | 219 | | return worker.CheckAABBoxOverlaps(this, ref outputIntersectionPoints); |
| | | 220 | | |
| | 13 | 221 | | return worker.CheckOBBoxOverlaps(this, ref outputIntersectionPoints); |
| | | 222 | | } |
| | | 223 | | } |