| | | 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 FixedMathSharp; |
| | | 9 | | using FixedMathSharp.Geometry; |
| | | 10 | | using Gravitas.Queries; |
| | | 11 | | using SwiftCollections; |
| | | 12 | | using System; |
| | | 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/> |
| | 99881 | 24 | | public override ColliderType Shape => Rotation == FixedQuaternion.Identity ? ColliderType.AABox : ColliderType.OBBox |
| | | 25 | | |
| | | 26 | | /// <inheritdoc/> |
| | 3779 | 27 | | public override int Priority => ColliderSettings.GetPriority(Shape); |
| | | 28 | | |
| | | 29 | | /// <summary> |
| | | 30 | | /// Gets the canonical committed oriented-box geometry. |
| | | 31 | | /// </summary> |
| | 33123 | 32 | | public FixedOrientedBox OrientedBox => _orientedBox; |
| | | 33 | | |
| | | 34 | | /// <inheritdoc/> |
| | | 35 | | public override Fixed64 ScaledRadius => |
| | 2109 | 36 | | HasCommittedShape |
| | 2109 | 37 | | ? CanonicalCenteredProxyRadius |
| | 2109 | 38 | | : ColliderCanonicalBounds |
| | 2109 | 39 | | .GetCurrentCenteredProxyRadius(this); |
| | | 40 | | |
| | | 41 | | internal override ExactMassWeight CalculateMassPropertyWeight() |
| | | 42 | | { |
| | | 43 | | Vector3d halfExtents; |
| | 125 | 44 | | if (HasCommittedShape) |
| | | 45 | | { |
| | 122 | 46 | | halfExtents = _orientedBox.HalfExtents; |
| | | 47 | | } |
| | | 48 | | else |
| | | 49 | | { |
| | 3 | 50 | | GetCurrentShapeScales( |
| | 3 | 51 | | out Vector3d ownerScale, |
| | 3 | 52 | | out Vector3d partScale); |
| | 3 | 53 | | halfExtents = ColliderScalePolicy.ScalePositive( |
| | 3 | 54 | | Size, |
| | 3 | 55 | | ownerScale, |
| | 3 | 56 | | partScale, |
| | 3 | 57 | | Fixed64.Two); |
| | | 58 | | } |
| | 125 | 59 | | return ExactMassWeight.FromProduct( |
| | 125 | 60 | | halfExtents.X, |
| | 125 | 61 | | halfExtents.Y, |
| | 125 | 62 | | halfExtents.Z, |
| | 125 | 63 | | (Fixed64)8); |
| | | 64 | | } |
| | | 65 | | |
| | | 66 | | internal override ExactMassWeight CalculatePreparedMassPropertyWeight() |
| | | 67 | | { |
| | 118 | 68 | | Vector3d halfExtents = _preparedOrientedBox.HalfExtents; |
| | 118 | 69 | | return ExactMassWeight.FromProduct( |
| | 118 | 70 | | halfExtents.X, |
| | 118 | 71 | | halfExtents.Y, |
| | 118 | 72 | | halfExtents.Z, |
| | 118 | 73 | | (Fixed64)8); |
| | | 74 | | } |
| | | 75 | | |
| | | 76 | | /// <summary>Creates a cuboid collider with default dimensions.</summary> |
| | 1902 | 77 | | public LSCuboidCollider() { } |
| | | 78 | | |
| | | 79 | | /// <summary>Creates a runtime cuboid from an authored shape definition.</summary> |
| | | 80 | | public LSCuboidCollider(ColliderShapeDefinition definition) |
| | 36 | 81 | | : this() |
| | | 82 | | { |
| | 36 | 83 | | definition.EnsureKind(ColliderShapeDefinitionKind.Cuboid); |
| | 36 | 84 | | Material = definition.Material; |
| | 36 | 85 | | Size = definition.Size; |
| | 36 | 86 | | } |
| | | 87 | | |
| | | 88 | | private protected override void PrepareShape(in ColliderShapeSnapshot snapshot) |
| | | 89 | | { |
| | 7926 | 90 | | Vector3d halfExtents = ColliderScalePolicy.ScalePositive( |
| | 7926 | 91 | | snapshot.Size, |
| | 7926 | 92 | | snapshot.OwnerScale, |
| | 7926 | 93 | | snapshot.PartScale, |
| | 7926 | 94 | | Fixed64.Two); |
| | 7926 | 95 | | _preparedOrientedBox = new FixedOrientedBox( |
| | 7926 | 96 | | snapshot.Center, |
| | 7926 | 97 | | snapshot.Rotation, |
| | 7926 | 98 | | halfExtents); |
| | 7926 | 99 | | _preparedArea = GetSurfaceArea(halfExtents); |
| | 7926 | 100 | | SetPreparedBounds(_preparedOrientedBox.GetBoundsClippedToDomain()); |
| | 7926 | 101 | | } |
| | | 102 | | |
| | | 103 | | private protected override void PublishShape() |
| | | 104 | | { |
| | 7919 | 105 | | _orientedBox = _preparedOrientedBox; |
| | 7919 | 106 | | Area = _preparedArea; |
| | 7919 | 107 | | } |
| | | 108 | | |
| | | 109 | | internal override Fixed3x3 CalculateCenterOfMassInertiaTensor(Fixed64 mass) |
| | | 110 | | { |
| | 1430 | 111 | | Vector3d halfExtents = _orientedBox.HalfExtents; |
| | 1430 | 112 | | Fixed64 xContribution = GetInertiaContribution(mass, halfExtents.X); |
| | 1430 | 113 | | Fixed64 yContribution = GetInertiaContribution(mass, halfExtents.Y); |
| | 1430 | 114 | | Fixed64 zContribution = GetInertiaContribution(mass, halfExtents.Z); |
| | 1430 | 115 | | Fixed64 xx = yContribution + zContribution; |
| | 1430 | 116 | | Fixed64 yy = xContribution + zContribution; |
| | 1430 | 117 | | Fixed64 zz = xContribution + yContribution; |
| | | 118 | | |
| | 1430 | 119 | | Fixed3x3 tensor = new( |
| | 1430 | 120 | | xx, Fixed64.Zero, Fixed64.Zero, |
| | 1430 | 121 | | Fixed64.Zero, yy, Fixed64.Zero, |
| | 1430 | 122 | | Fixed64.Zero, Fixed64.Zero, zz |
| | 1430 | 123 | | ); |
| | 1430 | 124 | | return tensor; |
| | | 125 | | } |
| | | 126 | | |
| | | 127 | | /// <inheritdoc/> |
| | | 128 | | public override Fixed64 GetFrontalArea(Vector3d direction) |
| | | 129 | | { |
| | 900 | 130 | | if (direction.MagnitudeSquared <= Fixed64.Epsilon) |
| | 1 | 131 | | return Area; |
| | | 132 | | |
| | 899 | 133 | | direction = direction.Normalized; |
| | 899 | 134 | | _orientedBox.GetAxes(out Vector3d axisX, out Vector3d axisY, out Vector3d axisZ); |
| | 899 | 135 | | Fixed64 dotX = Vector3d.Dot(direction, axisX).Abs(); |
| | 899 | 136 | | Fixed64 dotY = Vector3d.Dot(direction, axisY).Abs(); |
| | 899 | 137 | | Fixed64 dotZ = Vector3d.Dot(direction, axisZ).Abs(); |
| | 899 | 138 | | Vector3d halfExtents = _orientedBox.HalfExtents; |
| | | 139 | | |
| | | 140 | | // The orthographic projection of a box is the sum of each face area's |
| | | 141 | | // contribution along the view direction. |
| | 899 | 142 | | return GetProjectedFaceArea(halfExtents.Y, halfExtents.Z, dotX) |
| | 899 | 143 | | + GetProjectedFaceArea(halfExtents.X, halfExtents.Z, dotY) |
| | 899 | 144 | | + GetProjectedFaceArea(halfExtents.X, halfExtents.Y, dotZ); |
| | | 145 | | } |
| | | 146 | | |
| | | 147 | | /// <inheritdoc/> |
| | | 148 | | public override Vector3d ClosestPointOnSurface(Vector3d other) |
| | | 149 | | { |
| | 217 | 150 | | FixedPointAnchor anchor = |
| | 217 | 151 | | GetClosestSurfaceAnchor(other, out _); |
| | 217 | 152 | | if (anchor.TryGetPoint(out Vector3d closestPoint)) |
| | 216 | 153 | | return closestPoint; |
| | | 154 | | |
| | 1 | 155 | | throw new InvalidOperationException( |
| | 1 | 156 | | "The selected cuboid surface point is outside the representable coordinate domain."); |
| | | 157 | | } |
| | | 158 | | |
| | | 159 | | /// <inheritdoc/> |
| | | 160 | | public override Vector3d GetNormalAtPoint(Vector3d point) => |
| | 351 | 161 | | _orientedBox.GetNearestFaceNormal(point); |
| | | 162 | | |
| | | 163 | | internal override FixedPointAnchor GetClosestSurfaceAnchor( |
| | | 164 | | Vector3d point, |
| | | 165 | | out Vector3d normal) |
| | | 166 | | { |
| | 428 | 167 | | normal = _orientedBox.GetNearestFaceNormal(point); |
| | 428 | 168 | | return _orientedBox.GetClosestPointAnchor(point); |
| | | 169 | | } |
| | | 170 | | |
| | | 171 | | internal override bool TryGetPlanarSurfaceNormal(Vector3d point, out Vector3d normal) |
| | | 172 | | { |
| | 15 | 173 | | normal = GetNormalAtPoint(point); |
| | 15 | 174 | | return true; |
| | | 175 | | } |
| | | 176 | | |
| | | 177 | | private static Fixed64 GetSurfaceArea(Vector3d halfExtents) => |
| | 7926 | 178 | | GetScaledProduct(halfExtents.X, halfExtents.Y, (Fixed64)8) |
| | 7926 | 179 | | + GetScaledProduct(halfExtents.X, halfExtents.Z, (Fixed64)8) |
| | 7926 | 180 | | + GetScaledProduct(halfExtents.Y, halfExtents.Z, (Fixed64)8); |
| | | 181 | | |
| | | 182 | | private static Fixed64 GetProjectedFaceArea( |
| | | 183 | | Fixed64 firstHalfExtent, |
| | | 184 | | Fixed64 secondHalfExtent, |
| | | 185 | | Fixed64 alignment) => |
| | 2697 | 186 | | Fixed64.TryMultiplyDivide( |
| | 2697 | 187 | | firstHalfExtent, |
| | 2697 | 188 | | secondHalfExtent, |
| | 2697 | 189 | | alignment, |
| | 2697 | 190 | | Fixed64.One / (Fixed64)4, |
| | 2697 | 191 | | out Fixed64 area) |
| | 2697 | 192 | | ? area |
| | 2697 | 193 | | : Fixed64.MaxValue; |
| | | 194 | | |
| | | 195 | | private static Fixed64 GetInertiaContribution(Fixed64 mass, Fixed64 halfExtent) => |
| | 4290 | 196 | | Fixed64.TryMultiplyDivide( |
| | 4290 | 197 | | mass, |
| | 4290 | 198 | | halfExtent, |
| | 4290 | 199 | | halfExtent, |
| | 4290 | 200 | | (Fixed64)3, |
| | 4290 | 201 | | out Fixed64 contribution) |
| | 4290 | 202 | | ? contribution |
| | 4290 | 203 | | : Fixed64.MaxValue; |
| | | 204 | | |
| | | 205 | | private static Fixed64 GetScaledProduct( |
| | | 206 | | Fixed64 first, |
| | | 207 | | Fixed64 second, |
| | | 208 | | Fixed64 multiplier) => |
| | 23778 | 209 | | Fixed64.TryMultiplyDivide( |
| | 23778 | 210 | | first, |
| | 23778 | 211 | | second, |
| | 23778 | 212 | | multiplier, |
| | 23778 | 213 | | Fixed64.One, |
| | 23778 | 214 | | out Fixed64 product) |
| | 23778 | 215 | | ? product |
| | 23778 | 216 | | : Fixed64.MaxValue; |
| | | 217 | | |
| | | 218 | | /// <inheritdoc/> |
| | | 219 | | public override bool ColliderOverlapsRay(RaycastSegmentWorker worker, ref SwiftList<Vector3d> outputIntersectionPoin |
| | | 220 | | { |
| | 112 | 221 | | if (Shape == ColliderType.AABox) |
| | 108 | 222 | | return worker.CheckAABBoxOverlaps(this, ref outputIntersectionPoints); |
| | | 223 | | |
| | 4 | 224 | | return worker.CheckOBBoxOverlaps(this, ref outputIntersectionPoints); |
| | | 225 | | } |
| | | 226 | | } |