| | | 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 | | internal static Fixed64 GetCurrentCenteredProxyRadius( |
| | | 20 | | LSCollider2D collider) |
| | | 21 | | { |
| | 5 | 22 | | if (collider is LSCompoundCollider2D compound) |
| | 2 | 23 | | return GetCurrentCompoundCenteredProxyRadius(compound); |
| | | 24 | | |
| | 3 | 25 | | return GetShapeCenteredProxyRadius(collider); |
| | | 26 | | } |
| | | 27 | | |
| | | 28 | | internal static Fixed64 GetCenteredProxyRadius( |
| | | 29 | | LSCollider2D collider) |
| | | 30 | | { |
| | 13367 | 31 | | if (collider is LSCompoundCollider2D compound) |
| | 112 | 32 | | return GetCompoundCenteredProxyRadius(compound); |
| | | 33 | | |
| | 13255 | 34 | | return GetShapeCenteredProxyRadius(collider); |
| | | 35 | | } |
| | | 36 | | |
| | | 37 | | private static Fixed64 GetShapeCenteredProxyRadius( |
| | | 38 | | LSCollider2D collider) |
| | | 39 | | { |
| | 13258 | 40 | | if (collider is LSCircleCollider2D circle) |
| | 6177 | 41 | | return circle.ScaledRadius; |
| | 7081 | 42 | | if (collider is LSCapsuleCollider2D capsule) |
| | | 43 | | { |
| | 749 | 44 | | FixedBoundArea localBounds = |
| | 749 | 45 | | FixedBoundArea.FromCenteredCapsuleClippedToDomain( |
| | 749 | 46 | | Vector2d.Zero, |
| | 749 | 47 | | Vector2d.Forward, |
| | 749 | 48 | | capsule.AxisLength, |
| | 749 | 49 | | capsule.ScaledRadius); |
| | 749 | 50 | | return localBounds.Max.Y; |
| | | 51 | | } |
| | 6332 | 52 | | if (collider.VertexCount == 0) |
| | 12 | 53 | | return Fixed64.MaxValue; |
| | | 54 | | |
| | 6320 | 55 | | Fixed64 radius = Fixed64.Zero; |
| | 63144 | 56 | | for (int index = 0; index < collider.VertexCount; index++) |
| | | 57 | | { |
| | 25254 | 58 | | if (!collider.GetScaledLocalVertexUnchecked(index) |
| | 25254 | 59 | | .TryGetMagnitudeCeiling(out Fixed64 vertexRadius)) |
| | | 60 | | { |
| | 2 | 61 | | return Fixed64.MaxValue; |
| | | 62 | | } |
| | | 63 | | |
| | 25252 | 64 | | radius = FixedMath.Max(radius, vertexRadius); |
| | | 65 | | } |
| | | 66 | | |
| | 6318 | 67 | | return radius; |
| | | 68 | | } |
| | | 69 | | |
| | | 70 | | private static Fixed64 GetCurrentCompoundCenteredProxyRadius( |
| | | 71 | | LSCompoundCollider2D compound) |
| | | 72 | | { |
| | 2 | 73 | | Fixed64 radius = Fixed64.Zero; |
| | 6 | 74 | | for (int index = 0; index < compound.PartCount; index++) |
| | | 75 | | { |
| | 2 | 76 | | LSCollider2D part = compound.GetPartCollider(index); |
| | 2 | 77 | | bool distanceResolved = part.TryGetCurrentScaledOffset( |
| | 2 | 78 | | out Vector2d centerOffset) |
| | 2 | 79 | | & centerOffset.TryGetMagnitudeCeiling( |
| | 2 | 80 | | out Fixed64 centerDistance); |
| | 2 | 81 | | bool radiusResolved = Fixed64.TryAdd( |
| | 2 | 82 | | centerDistance, |
| | 2 | 83 | | GetCurrentCenteredProxyRadius(part), |
| | 2 | 84 | | out Fixed64 partRadius); |
| | 2 | 85 | | if (!(distanceResolved & radiusResolved)) |
| | 1 | 86 | | return Fixed64.MaxValue; |
| | | 87 | | |
| | 1 | 88 | | radius = FixedMath.Max(radius, partRadius); |
| | | 89 | | } |
| | | 90 | | |
| | 1 | 91 | | return radius; |
| | | 92 | | } |
| | | 93 | | |
| | | 94 | | internal static Fixed64 GetGroundProbeRadius( |
| | | 95 | | LSCollider2D collider) |
| | | 96 | | { |
| | 13368 | 97 | | if (collider is LSCircleCollider2D circle) |
| | 6175 | 98 | | return circle.ScaledRadius; |
| | 7193 | 99 | | if (collider is LSCapsuleCollider2D capsule) |
| | 749 | 100 | | return capsule.ScaledRadius; |
| | 6444 | 101 | | if (collider is LSCompoundCollider2D compound) |
| | | 102 | | { |
| | 113 | 103 | | return TryGetCompoundRelativeBounds( |
| | 113 | 104 | | compound, |
| | 113 | 105 | | compound.CanonicalCenter, |
| | 113 | 106 | | out Vector2d minimum, |
| | 113 | 107 | | out Vector2d maximum) |
| | 113 | 108 | | ? GetMinimumAbsoluteExtent(minimum, maximum) |
| | 113 | 109 | | : Fixed64.MaxValue; |
| | | 110 | | } |
| | 6331 | 111 | | if (collider.VertexCount == 0) |
| | 11 | 112 | | return Fixed64.Zero; |
| | | 113 | | |
| | 6320 | 114 | | return TryGetConvexBoundsFromRelativeCenter( |
| | 6320 | 115 | | collider, |
| | 6320 | 116 | | Vector2d.Zero, |
| | 6320 | 117 | | out Vector2d convexMinimum, |
| | 6320 | 118 | | out Vector2d convexMaximum) |
| | 6320 | 119 | | ? GetMinimumAbsoluteExtent(convexMinimum, convexMaximum) |
| | 6320 | 120 | | : Fixed64.MaxValue; |
| | | 121 | | } |
| | | 122 | | |
| | | 123 | | private static Fixed64 GetCompoundCenteredProxyRadius( |
| | | 124 | | LSCompoundCollider2D compound) |
| | | 125 | | { |
| | 112 | 126 | | Fixed64 radius = Fixed64.Zero; |
| | 658 | 127 | | for (int index = 0; index < compound.PartCount; index++) |
| | | 128 | | { |
| | 219 | 129 | | LSCollider2D part = compound.GetPartCollider(index); |
| | 219 | 130 | | bool offsetResolved = Vector2d.TrySubtract( |
| | 219 | 131 | | part.CanonicalCenter, |
| | 219 | 132 | | compound.CanonicalCenter, |
| | 219 | 133 | | out Vector2d centerOffset); |
| | 219 | 134 | | bool distanceResolved = |
| | 219 | 135 | | centerOffset.TryGetMagnitudeCeiling( |
| | 219 | 136 | | out Fixed64 centerDistance); |
| | 219 | 137 | | bool radiusResolved = Fixed64.TryAdd( |
| | 219 | 138 | | centerDistance, |
| | 219 | 139 | | part.CanonicalCenteredProxyRadius, |
| | 219 | 140 | | out Fixed64 partRadius); |
| | 219 | 141 | | if (!(offsetResolved & distanceResolved & radiusResolved)) |
| | 2 | 142 | | return Fixed64.MaxValue; |
| | | 143 | | |
| | 217 | 144 | | radius = FixedMath.Max(radius, partRadius); |
| | | 145 | | } |
| | | 146 | | |
| | 110 | 147 | | return radius; |
| | | 148 | | } |
| | | 149 | | |
| | | 150 | | private static bool TryGetCompoundRelativeBounds( |
| | | 151 | | LSCompoundCollider2D compound, |
| | | 152 | | Vector2d referenceCenter, |
| | | 153 | | out Vector2d minimum, |
| | | 154 | | out Vector2d maximum) |
| | | 155 | | { |
| | 113 | 156 | | minimum = default; |
| | 113 | 157 | | maximum = default; |
| | 113 | 158 | | bool hasBounds = false; |
| | 660 | 159 | | for (int index = 0; index < compound.PartCount; index++) |
| | | 160 | | { |
| | 220 | 161 | | LSCollider2D part = compound.GetPartCollider(index); |
| | 220 | 162 | | if (!TryGetRelativeBounds( |
| | 220 | 163 | | part, |
| | 220 | 164 | | referenceCenter, |
| | 220 | 165 | | out Vector2d partMinimum, |
| | 220 | 166 | | out Vector2d partMaximum)) |
| | | 167 | | { |
| | 3 | 168 | | minimum = default; |
| | 3 | 169 | | maximum = default; |
| | 3 | 170 | | return false; |
| | | 171 | | } |
| | | 172 | | |
| | 217 | 173 | | if (!hasBounds) |
| | | 174 | | { |
| | 110 | 175 | | minimum = partMinimum; |
| | 110 | 176 | | maximum = partMaximum; |
| | 110 | 177 | | hasBounds = true; |
| | 110 | 178 | | continue; |
| | | 179 | | } |
| | | 180 | | |
| | 107 | 181 | | minimum = new Vector2d( |
| | 107 | 182 | | FixedMath.Min(minimum.X, partMinimum.X), |
| | 107 | 183 | | FixedMath.Min(minimum.Y, partMinimum.Y)); |
| | 107 | 184 | | maximum = new Vector2d( |
| | 107 | 185 | | FixedMath.Max(maximum.X, partMaximum.X), |
| | 107 | 186 | | FixedMath.Max(maximum.Y, partMaximum.Y)); |
| | | 187 | | } |
| | | 188 | | |
| | 110 | 189 | | return hasBounds; |
| | | 190 | | } |
| | | 191 | | |
| | | 192 | | private static bool TryGetRelativeBounds( |
| | | 193 | | LSCollider2D collider, |
| | | 194 | | Vector2d referenceCenter, |
| | | 195 | | out Vector2d minimum, |
| | | 196 | | out Vector2d maximum) |
| | | 197 | | { |
| | 220 | 198 | | if (!Vector2d.TrySubtract( |
| | 220 | 199 | | collider.CanonicalCenter, |
| | 220 | 200 | | referenceCenter, |
| | 220 | 201 | | out Vector2d relativeCenter)) |
| | | 202 | | { |
| | 1 | 203 | | minimum = default; |
| | 1 | 204 | | maximum = default; |
| | 1 | 205 | | return false; |
| | | 206 | | } |
| | | 207 | | |
| | 219 | 208 | | if (collider is LSCircleCollider2D circle) |
| | | 209 | | { |
| | 146 | 210 | | return TryGetRoundBounds( |
| | 146 | 211 | | relativeCenter, |
| | 146 | 212 | | circle.ScaledRadius, |
| | 146 | 213 | | circle.ScaledRadius, |
| | 146 | 214 | | out minimum, |
| | 146 | 215 | | out maximum); |
| | | 216 | | } |
| | | 217 | | |
| | 73 | 218 | | if (collider is LSCapsuleCollider2D capsule) |
| | | 219 | | { |
| | 19 | 220 | | FixedBoundArea centeredBounds = |
| | 19 | 221 | | FixedBoundArea.FromCenteredRotatedCapsuleClippedToDomain( |
| | 19 | 222 | | Vector2d.Zero, |
| | 19 | 223 | | capsule.Rotation, |
| | 19 | 224 | | capsule.AxisLength, |
| | 19 | 225 | | capsule.ScaledRadius); |
| | 19 | 226 | | return TryGetRoundBounds( |
| | 19 | 227 | | relativeCenter, |
| | 19 | 228 | | centeredBounds.Max.X, |
| | 19 | 229 | | centeredBounds.Max.Y, |
| | 19 | 230 | | out minimum, |
| | 19 | 231 | | out maximum); |
| | | 232 | | } |
| | | 233 | | |
| | 54 | 234 | | return TryGetConvexBoundsFromRelativeCenter( |
| | 54 | 235 | | collider, |
| | 54 | 236 | | relativeCenter, |
| | 54 | 237 | | out minimum, |
| | 54 | 238 | | out maximum); |
| | | 239 | | } |
| | | 240 | | |
| | | 241 | | private static bool TryGetRoundBounds( |
| | | 242 | | Vector2d center, |
| | | 243 | | Fixed64 extentX, |
| | | 244 | | Fixed64 extentY, |
| | | 245 | | out Vector2d minimum, |
| | | 246 | | out Vector2d maximum) |
| | | 247 | | { |
| | 165 | 248 | | bool representable = |
| | 165 | 249 | | Fixed64.TrySubtract(center.X, extentX, out Fixed64 minimumX) |
| | 165 | 250 | | & Fixed64.TrySubtract(center.Y, extentY, out Fixed64 minimumY) |
| | 165 | 251 | | & Fixed64.TryAdd(center.X, extentX, out Fixed64 maximumX) |
| | 165 | 252 | | & Fixed64.TryAdd(center.Y, extentY, out Fixed64 maximumY); |
| | 165 | 253 | | minimum = representable |
| | 165 | 254 | | ? new Vector2d(minimumX, minimumY) |
| | 165 | 255 | | : default; |
| | 165 | 256 | | maximum = representable |
| | 165 | 257 | | ? new Vector2d(maximumX, maximumY) |
| | 165 | 258 | | : default; |
| | 165 | 259 | | return representable; |
| | | 260 | | } |
| | | 261 | | |
| | | 262 | | private static bool TryGetConvexBoundsFromRelativeCenter( |
| | | 263 | | LSCollider2D collider, |
| | | 264 | | Vector2d relativeCenter, |
| | | 265 | | out Vector2d minimum, |
| | | 266 | | out Vector2d maximum) |
| | | 267 | | { |
| | 6374 | 268 | | if (!Vector2d.TryTransformPoint( |
| | 6374 | 269 | | relativeCenter, |
| | 6374 | 270 | | collider.GetScaledLocalVertexUnchecked(0), |
| | 6374 | 271 | | collider.ConvexRotation, |
| | 6374 | 272 | | out minimum)) |
| | | 273 | | { |
| | 1 | 274 | | minimum = default; |
| | 1 | 275 | | maximum = default; |
| | 1 | 276 | | return false; |
| | | 277 | | } |
| | | 278 | | |
| | 6373 | 279 | | maximum = minimum; |
| | 50910 | 280 | | for (int index = 1; index < collider.VertexCount; index++) |
| | | 281 | | { |
| | 19083 | 282 | | if (!Vector2d.TryTransformPoint( |
| | 19083 | 283 | | relativeCenter, |
| | 19083 | 284 | | collider.GetScaledLocalVertexUnchecked(index), |
| | 19083 | 285 | | collider.ConvexRotation, |
| | 19083 | 286 | | out Vector2d point)) |
| | | 287 | | { |
| | 1 | 288 | | minimum = default; |
| | 1 | 289 | | maximum = default; |
| | 1 | 290 | | return false; |
| | | 291 | | } |
| | | 292 | | |
| | 19082 | 293 | | minimum = new Vector2d( |
| | 19082 | 294 | | FixedMath.Min(minimum.X, point.X), |
| | 19082 | 295 | | FixedMath.Min(minimum.Y, point.Y)); |
| | 19082 | 296 | | maximum = new Vector2d( |
| | 19082 | 297 | | FixedMath.Max(maximum.X, point.X), |
| | 19082 | 298 | | FixedMath.Max(maximum.Y, point.Y)); |
| | | 299 | | } |
| | | 300 | | |
| | 6372 | 301 | | return true; |
| | | 302 | | } |
| | | 303 | | |
| | | 304 | | private static Fixed64 GetMinimumAbsoluteExtent( |
| | | 305 | | Vector2d minimum, |
| | | 306 | | Vector2d maximum) => |
| | 6428 | 307 | | FixedMath.Min( |
| | 6428 | 308 | | FixedMath.Max(minimum.X.Abs(), maximum.X.Abs()), |
| | 6428 | 309 | | FixedMath.Max(minimum.Y.Abs(), maximum.Y.Abs())); |
| | | 310 | | } |