| | | 1 | | //======================================================================= |
| | | 2 | | // ColliderCanonicalBounds2D.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 | | |
| | | 11 | | namespace Gravitas.Colliders; |
| | | 12 | | |
| | | 13 | | /// <summary> |
| | | 14 | | /// Contains methods for computing canonical bounds of 2D colliders, |
| | | 15 | | /// including proxy radii and relative bounds, based on collider type and properties. |
| | | 16 | | /// </summary> |
| | | 17 | | internal static class ColliderCanonicalBounds2D |
| | | 18 | | { |
| | | 19 | | // The caller supplies a compound leaf. Retain its authored local box until |
| | | 20 | | // the final mesh-relative transform; clipped world bounds lose boundary geometry. |
| | | 21 | | internal static void GetMixedRelativeBounds(LSCollider2D collider, |
| | | 22 | | Vector3d referenceOrigin, FixedQuaternion referenceRotation, out FixedBoundBox relativeBounds, |
| | | 23 | | out FixedOrientedBox orientedBounds) |
| | | 24 | | { |
| | 509 | 25 | | orientedBounds = default; |
| | | 26 | | Vector2d minimum, maximum; |
| | 509 | 27 | | Fixed64 rotation = collider.Rotation; |
| | 509 | 28 | | if (collider is LSCircleCollider2D circle) |
| | | 29 | | { |
| | 4 | 30 | | minimum = -Vector2d.One * circle.ScaledRadius; |
| | 4 | 31 | | maximum = -minimum; |
| | | 32 | | } |
| | 505 | 33 | | else if (collider is LSCapsuleCollider2D capsule) |
| | | 34 | | { |
| | | 35 | | // Admitted capsule reach bounds every local extent before world clipping. |
| | 418 | 36 | | FixedBoundArea bounds = FixedBoundArea.FromCenteredRotatedCapsuleClippedToDomain( |
| | 418 | 37 | | Vector2d.Zero, Fixed64.Zero, capsule.AxisLength, capsule.ScaledRadius); |
| | 418 | 38 | | minimum = bounds.Min; |
| | 418 | 39 | | maximum = bounds.Max; |
| | | 40 | | } |
| | | 41 | | else |
| | | 42 | | { |
| | 87 | 43 | | rotation = collider.ConvexRotation; |
| | 87 | 44 | | minimum = maximum = collider.GetScaledLocalVertexUnchecked(0); |
| | 624 | 45 | | for (int i = 1; i < collider.VertexCount; i++) |
| | | 46 | | { |
| | 225 | 47 | | Vector2d vertex = collider.GetScaledLocalVertexUnchecked(i); |
| | 225 | 48 | | minimum = new Vector2d(FixedMath.Min(minimum.X, vertex.X), FixedMath.Min(minimum.Y, vertex.Y)); |
| | 225 | 49 | | maximum = new Vector2d(FixedMath.Max(maximum.X, vertex.X), FixedMath.Max(maximum.Y, vertex.Y)); |
| | | 50 | | } |
| | | 51 | | } |
| | | 52 | | // Keep the enclosure centered on the canonical origin, avoiding another |
| | | 53 | | // rounded world-center transform for asymmetric authored polygons. |
| | 509 | 54 | | Vector2d negativeMinimum = -minimum; |
| | 509 | 55 | | Vector3d origin = new(collider.Center.X, collider.MixedSlabCenterY, collider.Center.Y); |
| | 509 | 56 | | FixedQuaternion orientation = FixedQuaternion.FromAxisAngle(Vector3d.Up, -rotation); |
| | 509 | 57 | | orientedBounds = new FixedOrientedBox(origin, orientation, new Vector3d( |
| | 509 | 58 | | FixedMath.Max(negativeMinimum.X, maximum.X), collider.MixedHalfThickness, |
| | 509 | 59 | | FixedMath.Max(negativeMinimum.Y, maximum.Y))); |
| | 509 | 60 | | relativeBounds = FixedBoundBox.FromRelativeRotatedBoundsClippedToDomain( |
| | 509 | 61 | | origin, orientation, |
| | 509 | 62 | | new Vector3d(minimum.X, -collider.MixedHalfThickness, minimum.Y), |
| | 509 | 63 | | new Vector3d(maximum.X, collider.MixedHalfThickness, maximum.Y), referenceOrigin, referenceRotation); |
| | 509 | 64 | | } |
| | | 65 | | |
| | | 66 | | private static Fixed64 GetShapeCenteredProxyRadius( |
| | | 67 | | LSCollider2D collider) |
| | | 68 | | { |
| | 14 | 69 | | if (collider is LSCircleCollider2D circle) |
| | 2 | 70 | | return circle.ScaledRadius; |
| | 12 | 71 | | if (collider is LSCapsuleCollider2D capsule) |
| | | 72 | | { |
| | 1 | 73 | | FixedBoundArea localBounds = |
| | 1 | 74 | | FixedBoundArea.FromCenteredCapsuleClippedToDomain( |
| | 1 | 75 | | Vector2d.Zero, |
| | 1 | 76 | | Vector2d.Forward, |
| | 1 | 77 | | capsule.AxisLength, |
| | 1 | 78 | | capsule.ScaledRadius); |
| | 1 | 79 | | return localBounds.Max.Y; |
| | | 80 | | } |
| | 11 | 81 | | if (collider is LSPolygonCollider2D polygon) |
| | 9 | 82 | | return polygon.GetCurrentGeometryReach(); |
| | 2 | 83 | | Fixed64 radius = Fixed64.Zero; |
| | 12 | 84 | | for (int index = 0; index < collider.VertexCount; index++) |
| | | 85 | | { |
| | 5 | 86 | | if (!collider.GetScaledLocalVertexUnchecked(index) |
| | 5 | 87 | | .TryGetMagnitudeCeiling(out Fixed64 vertexRadius)) |
| | | 88 | | { |
| | 1 | 89 | | return Fixed64.MaxValue; |
| | | 90 | | } |
| | | 91 | | |
| | 4 | 92 | | radius = FixedMath.Max(radius, vertexRadius); |
| | | 93 | | } |
| | | 94 | | |
| | 1 | 95 | | return radius; |
| | | 96 | | } |
| | | 97 | | |
| | | 98 | | internal static Fixed64 GetCompoundCenteredProxyRadius(LSCompoundCollider2D compound, out Fixed64 rotationalRadius) |
| | | 99 | | { |
| | 1425 | 100 | | Fixed64 radius = Fixed64.Zero; |
| | 1425 | 101 | | rotationalRadius = Fixed64.Zero; |
| | 6406 | 102 | | for (int index = 0; index < compound.PartCount; index++) |
| | | 103 | | { |
| | 1778 | 104 | | var part = compound.GetPartCollider(index); |
| | 1778 | 105 | | Vector2d offset = part.CanonicalCenter - compound.CanonicalCenter; |
| | 1778 | 106 | | radius = FixedMath.Max(radius, ColliderGeometryPolicy.Add( |
| | 1778 | 107 | | ColliderGeometryPolicy.Magnitude(offset), part.CanonicalCenteredProxyRadius)); |
| | | 108 | | // Retain a separate all-angle enclosure for the frame's CCD proxy. |
| | 1778 | 109 | | rotationalRadius = FixedMath.Max(rotationalRadius, part.CanonicalPivotProxyRadius); |
| | | 110 | | } |
| | 1425 | 111 | | return radius; |
| | | 112 | | } |
| | | 113 | | |
| | | 114 | | internal static Fixed64 GetCurrentCompoundCenteredProxyRadius( |
| | | 115 | | LSCompoundCollider2D compound) |
| | | 116 | | { |
| | 14 | 117 | | Fixed64 radius = Fixed64.Zero; |
| | 54 | 118 | | for (int index = 0; index < compound.PartCount; index++) |
| | | 119 | | { |
| | 14 | 120 | | LSCollider2D part = compound.GetPartCollider(index); |
| | 14 | 121 | | bool distanceResolved = part.TryGetCurrentScaledOffset( |
| | 14 | 122 | | out Vector2d centerOffset) |
| | 14 | 123 | | & centerOffset.TryGetMagnitudeCeiling( |
| | 14 | 124 | | out Fixed64 centerDistance); |
| | 14 | 125 | | bool radiusResolved = Fixed64.TryAdd( |
| | 14 | 126 | | centerDistance, |
| | 14 | 127 | | GetShapeCenteredProxyRadius(part), |
| | 14 | 128 | | out Fixed64 partRadius); |
| | 14 | 129 | | if (!(distanceResolved & radiusResolved)) |
| | 1 | 130 | | return Fixed64.MaxValue; |
| | | 131 | | |
| | 13 | 132 | | radius = FixedMath.Max(radius, partRadius); |
| | | 133 | | } |
| | | 134 | | |
| | 13 | 135 | | return radius; |
| | | 136 | | } |
| | | 137 | | |
| | | 138 | | internal static Fixed64 GetGroundProbeRadius( |
| | | 139 | | LSCollider2D collider) |
| | | 140 | | { |
| | 39573 | 141 | | if (collider is LSCircleCollider2D circle) |
| | 12962 | 142 | | return circle.ScaledRadius; |
| | 26611 | 143 | | if (collider is LSCapsuleCollider2D capsule) |
| | 5655 | 144 | | return capsule.ScaledRadius; |
| | 20956 | 145 | | if (collider is LSCompoundCollider2D compound) |
| | | 146 | | { |
| | 1425 | 147 | | GetCompoundRelativeBounds(compound, compound.CanonicalCenter, |
| | 1425 | 148 | | out Vector2d minimum, out Vector2d maximum); |
| | 1425 | 149 | | return GetMinimumAbsoluteExtent(minimum, maximum); |
| | | 150 | | } |
| | 19531 | 151 | | if (collider.VertexCount == 0) |
| | 16 | 152 | | return Fixed64.Zero; |
| | | 153 | | |
| | 19515 | 154 | | GetConvexBoundsFromRelativeCenter(collider, Vector2d.Zero, |
| | 19515 | 155 | | out Vector2d convexMinimum, out Vector2d convexMaximum); |
| | 19515 | 156 | | return GetMinimumAbsoluteExtent(convexMinimum, convexMaximum); |
| | | 157 | | } |
| | | 158 | | |
| | | 159 | | private static void GetCompoundRelativeBounds(LSCompoundCollider2D compound, |
| | | 160 | | Vector2d referenceCenter, out Vector2d minimum, out Vector2d maximum) |
| | | 161 | | { |
| | 1425 | 162 | | minimum = maximum = default; |
| | 6406 | 163 | | for (int index = 0; index < compound.PartCount; index++) |
| | | 164 | | { |
| | 1778 | 165 | | GetRelativeBounds(compound.GetPartCollider(index), referenceCenter, |
| | 1778 | 166 | | out Vector2d partMinimum, out Vector2d partMaximum); |
| | 1778 | 167 | | if (index == 0) |
| | | 168 | | { |
| | 1425 | 169 | | minimum = partMinimum; |
| | 1425 | 170 | | maximum = partMaximum; |
| | | 171 | | } |
| | | 172 | | else |
| | | 173 | | { |
| | 353 | 174 | | minimum = new Vector2d(FixedMath.Min(minimum.X, partMinimum.X), FixedMath.Min(minimum.Y, partMinimum.Y)) |
| | 353 | 175 | | maximum = new Vector2d(FixedMath.Max(maximum.X, partMaximum.X), FixedMath.Max(maximum.Y, partMaximum.Y)) |
| | | 176 | | } |
| | | 177 | | } |
| | 1425 | 178 | | } |
| | | 179 | | |
| | | 180 | | private static void GetRelativeBounds(LSCollider2D collider, Vector2d referenceCenter, |
| | | 181 | | out Vector2d minimum, out Vector2d maximum) |
| | | 182 | | { |
| | | 183 | | // Called only during publication. Admitted part spacing and local reach |
| | | 184 | | // bound these relative coordinates, even at an absolute scalar face. |
| | 1778 | 185 | | Vector2d relativeCenter = collider.CanonicalCenter - referenceCenter; |
| | 1778 | 186 | | if (collider is LSCircleCollider2D circle) |
| | | 187 | | { |
| | 425 | 188 | | GetRoundBounds(relativeCenter, circle.ScaledRadius, circle.ScaledRadius, out minimum, out maximum); |
| | 425 | 189 | | return; |
| | | 190 | | } |
| | 1353 | 191 | | if (collider is LSCapsuleCollider2D capsule) |
| | | 192 | | { |
| | 182 | 193 | | FixedBoundArea centeredBounds = FixedBoundArea.FromCenteredRotatedCapsuleClippedToDomain( |
| | 182 | 194 | | Vector2d.Zero, capsule.Rotation, capsule.AxisLength, capsule.ScaledRadius); |
| | 182 | 195 | | GetRoundBounds(relativeCenter, centeredBounds.Max.X, centeredBounds.Max.Y, out minimum, out maximum); |
| | 182 | 196 | | return; |
| | | 197 | | } |
| | 1171 | 198 | | GetConvexBoundsFromRelativeCenter(collider, relativeCenter, out minimum, out maximum); |
| | 1171 | 199 | | } |
| | | 200 | | |
| | | 201 | | private static void GetRoundBounds(Vector2d center, Fixed64 extentX, Fixed64 extentY, |
| | | 202 | | out Vector2d minimum, out Vector2d maximum) |
| | | 203 | | { |
| | 607 | 204 | | Vector2d extents = new(extentX, extentY); |
| | 607 | 205 | | minimum = center - extents; |
| | 607 | 206 | | maximum = center + extents; |
| | 607 | 207 | | } |
| | | 208 | | |
| | | 209 | | private static void GetConvexBoundsFromRelativeCenter(LSCollider2D collider, Vector2d relativeCenter, |
| | | 210 | | out Vector2d minimum, out Vector2d maximum) |
| | | 211 | | { |
| | | 212 | | // Admission bounds the local rotated sums; retain the existing fused |
| | | 213 | | // transform rounding rather than project products independently. |
| | 20686 | 214 | | _ = Vector2d.TryTransformPoint(relativeCenter, collider.GetScaledLocalVertexUnchecked(0), |
| | 20686 | 215 | | collider.ConvexRotation, out minimum); |
| | 20686 | 216 | | maximum = minimum; |
| | 166272 | 217 | | for (int index = 1; index < collider.VertexCount; index++) |
| | | 218 | | { |
| | 62450 | 219 | | _ = Vector2d.TryTransformPoint(relativeCenter, collider.GetScaledLocalVertexUnchecked(index), |
| | 62450 | 220 | | collider.ConvexRotation, out Vector2d point); |
| | 62450 | 221 | | minimum = new Vector2d(FixedMath.Min(minimum.X, point.X), FixedMath.Min(minimum.Y, point.Y)); |
| | 62450 | 222 | | maximum = new Vector2d(FixedMath.Max(maximum.X, point.X), FixedMath.Max(maximum.Y, point.Y)); |
| | | 223 | | } |
| | 20686 | 224 | | } |
| | | 225 | | |
| | | 226 | | private static Fixed64 GetMinimumAbsoluteExtent( |
| | | 227 | | Vector2d minimum, |
| | | 228 | | Vector2d maximum) => |
| | 20940 | 229 | | FixedMath.Min( |
| | 20940 | 230 | | FixedMath.Max(minimum.X.Abs(), maximum.X.Abs()), |
| | 20940 | 231 | | FixedMath.Max(minimum.Y.Abs(), maximum.Y.Abs())); |
| | | 232 | | } |