| | | 1 | | //======================================================================= |
| | | 2 | | // GravitasQueryMixedService.SphereAgainst2DReducers.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.Colliders; |
| | | 12 | | using Gravitas.CollisionHandling; |
| | | 13 | | |
| | | 14 | | namespace Gravitas.Queries; |
| | | 15 | | |
| | | 16 | | /// <content> |
| | | 17 | | /// Owns mixed swept-sphere reducers against embedded 2D collider targets. |
| | | 18 | | /// </content> |
| | | 19 | | public sealed partial class GravitasQueryMixedService |
| | | 20 | | { |
| | | 21 | | internal static bool TryResolveExactSphereSourceFor2D(LSCollider source, LSCollider2D target, |
| | | 22 | | Vector3d displacement, out Vector3d start, out Vector3d end, out Fixed64 radius) |
| | | 23 | | { |
| | 1370 | 24 | | start = source.Center; radius = source.ScaledRadius; |
| | 686 | 25 | | if (!Vector3d.TryAdd(start, displacement, out end)) return false; |
| | | 26 | | // Tiny angular arcs can deliberately fall back to the linear arbiter. |
| | | 27 | | // Refine only unchanged geometry, including a kinematic host's goal. |
| | 684 | 28 | | if (source.Body is { HasRotationInvariantCollider: false } body |
| | 684 | 29 | | && (body.AngularVelocity != Vector3d.Zero |
| | 684 | 30 | | || body.SampleContinuousCollisionAngularVelocity(Fixed64.Zero) != Vector3d.Zero |
| | 687 | 31 | | || body.IsKinematic && body.Agent.Transform.WorldRotation != body.Rotation)) return false; |
| | 1046 | 32 | | if (source is LSSphereCollider) return true; |
| | 363 | 33 | | if (source is not LSCapsuleCollider capsule) return false; |
| | 270 | 34 | | if (capsule.AxisLength == Fixed64.Zero) return true; |
| | 268 | 35 | | var basis = new WideRationalBasis3d(capsule.Rotation); |
| | | 36 | | // A vertical core and a center inside the target's finite Y band |
| | | 37 | | // supply exactly the same X/Z disk as this true-radius sphere. |
| | | 38 | | // Check the complete chord; arbitrary/inflated proxies remain unknown. |
| | 268 | 39 | | return basis.Yx.IsZero && basis.Yz.IsZero |
| | 268 | 40 | | && MixedEmbedded2DGeometry.ContainsPointInSlab(target, start.Y) |
| | 268 | 41 | | && MixedEmbedded2DGeometry.ContainsPointInSlab(target, end.Y); |
| | | 42 | | } |
| | | 43 | | |
| | | 44 | | private static bool TrySweepSphereAgainst2D( |
| | | 45 | | Vector3d start, Vector3d end, Vector3d direction, Fixed64 length, |
| | | 46 | | Fixed64 radius, LSCollider2D collider, out PhysicsMixedHit hit, |
| | | 47 | | bool requireClosingContact = false) |
| | | 48 | | { |
| | 1294 | 49 | | return TrySweepSphereAgainst2DCore(start, end, direction, length, radius, collider, |
| | 1294 | 50 | | requireClosingContact, out hit) |
| | 1294 | 51 | | && (!requireClosingContact |
| | 1294 | 52 | | || MixedContact.IsClosingProjection(end - start, hit.NormalFor3DSource)); |
| | | 53 | | } |
| | | 54 | | |
| | | 55 | | private static bool TrySweepSphereAgainst2DCore( |
| | | 56 | | Vector3d start, |
| | | 57 | | Vector3d end, |
| | | 58 | | Vector3d direction, |
| | | 59 | | Fixed64 length, |
| | | 60 | | Fixed64 radius, |
| | | 61 | | LSCollider2D collider, |
| | | 62 | | bool requireClosingContact, |
| | | 63 | | out PhysicsMixedHit hit) |
| | | 64 | | { |
| | 1294 | 65 | | if (collider is LSCompoundCollider2D compound) |
| | 41 | 66 | | return TrySweepSphereAgainstCompound2D(start, end, direction, length, radius, compound, requireClosingContac |
| | | 67 | | |
| | 1253 | 68 | | if (collider is LSCircleCollider2D circle) |
| | 77 | 69 | | return TrySweepSphereAgainstCircleSlab(start, end, direction, length, radius, circle, out hit); |
| | | 70 | | |
| | 1176 | 71 | | if (collider is LSCapsuleCollider2D capsule) |
| | 18 | 72 | | return TrySweepSphereAgainstCapsuleSlab(start, end, direction, length, radius, capsule, out hit); |
| | | 73 | | |
| | 1158 | 74 | | if (collider is LSAABBoxCollider2D || collider is LSPolygonCollider2D) |
| | 1157 | 75 | | return TrySweepSphereAgainstConvexSlab(start, end, direction, length, radius, collider, out hit); |
| | | 76 | | |
| | 1 | 77 | | throw new InvalidOperationException( |
| | 1 | 78 | | "The embedded 2D collider type is not supported by mixed queries."); |
| | | 79 | | } |
| | | 80 | | |
| | | 81 | | private static bool TrySweepSphereAgainstCompound2D( |
| | | 82 | | Vector3d start, |
| | | 83 | | Vector3d end, |
| | | 84 | | Vector3d direction, |
| | | 85 | | Fixed64 length, |
| | | 86 | | Fixed64 radius, |
| | | 87 | | LSCompoundCollider2D compound, |
| | | 88 | | bool requireClosingContact, |
| | | 89 | | out PhysicsMixedHit hit) |
| | | 90 | | { |
| | 41 | 91 | | bool found = false; |
| | 41 | 92 | | Fixed64 bestDistance = Fixed64.MaxValue; |
| | 41 | 93 | | PhysicsMixedHit best = default; |
| | | 94 | | |
| | 244 | 95 | | for (int i = 0; i < compound.PartCount; i++) |
| | | 96 | | { |
| | 81 | 97 | | LSCollider2D part = compound.GetPartCollider(i); |
| | 81 | 98 | | if (!TrySweepSphereAgainst2D(start, end, direction, length, radius, part, out PhysicsMixedHit candidate, req |
| | | 99 | | continue; |
| | | 100 | | |
| | 79 | 101 | | if (!PhysicsHitSelectionPolicy.ShouldReplaceDistance(candidate.Distance, found, bestDistance)) |
| | | 102 | | continue; |
| | | 103 | | |
| | 41 | 104 | | best = candidate; |
| | 41 | 105 | | bestDistance = candidate.Distance; |
| | 41 | 106 | | found = true; |
| | | 107 | | } |
| | | 108 | | |
| | 41 | 109 | | if (!found) |
| | | 110 | | { |
| | 1 | 111 | | hit = default; |
| | 1 | 112 | | return false; |
| | | 113 | | } |
| | | 114 | | |
| | 40 | 115 | | hit = new PhysicsMixedHit( |
| | 40 | 116 | | null, |
| | 40 | 117 | | compound, |
| | 40 | 118 | | best.Anchor3D, |
| | 40 | 119 | | best.Anchor2D, |
| | 40 | 120 | | best.Normal3DTo2D, |
| | 40 | 121 | | best.ReducerKind, |
| | 40 | 122 | | best.Distance, |
| | 40 | 123 | | best.Direction3D); |
| | 40 | 124 | | return true; |
| | | 125 | | } |
| | | 126 | | |
| | | 127 | | internal static bool TrySweepSphereAgainstCapsuleSlab( |
| | | 128 | | Vector3d start, |
| | | 129 | | Vector3d end, |
| | | 130 | | Vector3d direction, |
| | | 131 | | Fixed64 length, |
| | | 132 | | Fixed64 radius, |
| | | 133 | | LSCapsuleCollider2D capsule, |
| | | 134 | | out PhysicsMixedHit hit) |
| | | 135 | | { |
| | 25 | 136 | | if (IsSphereOverlappingCapsuleSlabCapCore(start, radius, capsule)) |
| | | 137 | | { |
| | 1 | 138 | | hit = BuildSphereAgainst2DHit( |
| | 1 | 139 | | capsule, |
| | 1 | 140 | | start, |
| | 1 | 141 | | radius, |
| | 1 | 142 | | PhysicsQueryReducerKind.Exact, |
| | 1 | 143 | | Fixed64.Zero, |
| | 1 | 144 | | direction); |
| | 1 | 145 | | return true; |
| | | 146 | | } |
| | | 147 | | |
| | 24 | 148 | | Fixed64 slabMinY = capsule.MixedBounds3D.Min.Y; |
| | 24 | 149 | | Fixed64 slabMaxY = capsule.MixedBounds3D.Max.Y; |
| | 24 | 150 | | SweepCandidate best = default; |
| | | 151 | | |
| | 24 | 152 | | if (Fixed64.TryAdd(slabMaxY, radius, out Fixed64 expandedMaxY)) |
| | | 153 | | { |
| | 23 | 154 | | TrySweepCapsuleSlabCapFace( |
| | 23 | 155 | | start, |
| | 23 | 156 | | end, |
| | 23 | 157 | | length, |
| | 23 | 158 | | capsule, |
| | 23 | 159 | | expandedMaxY, |
| | 23 | 160 | | requireStartBeyondPlane: true, |
| | 23 | 161 | | -Vector3d.Up, |
| | 23 | 162 | | ref best); |
| | | 163 | | } |
| | | 164 | | |
| | 24 | 165 | | if (Fixed64.TrySubtract(slabMinY, radius, out Fixed64 expandedMinY)) |
| | | 166 | | { |
| | 23 | 167 | | TrySweepCapsuleSlabCapFace( |
| | 23 | 168 | | start, |
| | 23 | 169 | | end, |
| | 23 | 170 | | length, |
| | 23 | 171 | | capsule, |
| | 23 | 172 | | expandedMinY, |
| | 23 | 173 | | requireStartBeyondPlane: false, |
| | 23 | 174 | | Vector3d.Up, |
| | 23 | 175 | | ref best); |
| | | 176 | | } |
| | | 177 | | |
| | 24 | 178 | | TrySweepCapsuleSlabSide( |
| | 24 | 179 | | start, |
| | 24 | 180 | | end, |
| | 24 | 181 | | length, |
| | 24 | 182 | | capsule, |
| | 24 | 183 | | capsule.ScaledRadius, |
| | 24 | 184 | | radius, |
| | 24 | 185 | | slabMinY, |
| | 24 | 186 | | slabMaxY, |
| | 24 | 187 | | ref best); |
| | 24 | 188 | | TrySweepCapsuleSlabBoundaryEdges( |
| | 24 | 189 | | start, |
| | 24 | 190 | | end, |
| | 24 | 191 | | length, |
| | 24 | 192 | | capsule, |
| | 24 | 193 | | capsule.ScaledRadius, |
| | 24 | 194 | | radius, |
| | 24 | 195 | | slabMinY, |
| | 24 | 196 | | slabMaxY, |
| | 24 | 197 | | ref best); |
| | | 198 | | |
| | 24 | 199 | | if (!best.Found) |
| | | 200 | | { |
| | 10 | 201 | | hit = default; |
| | 10 | 202 | | return false; |
| | | 203 | | } |
| | | 204 | | |
| | 14 | 205 | | Vector3d sweepCenter = new FixedSegment(start, end).GetPointAtDistance(best.Distance, length); |
| | 14 | 206 | | hit = BuildSphereAgainst2DHit( |
| | 14 | 207 | | capsule, |
| | 14 | 208 | | sweepCenter, |
| | 14 | 209 | | radius, |
| | 14 | 210 | | PhysicsQueryReducerKind.Exact, |
| | 14 | 211 | | best.Distance, |
| | 14 | 212 | | direction, |
| | 14 | 213 | | best.FeatureNormal); |
| | 14 | 214 | | return true; |
| | | 215 | | } |
| | | 216 | | |
| | | 217 | | private static bool IsSphereOverlappingCapsuleSlabCapCore( |
| | | 218 | | Vector3d center, |
| | | 219 | | Fixed64 radius, |
| | | 220 | | LSCapsuleCollider2D capsule) |
| | | 221 | | { |
| | 25 | 222 | | Fixed64 slabMinY = capsule.MixedBounds3D.Min.Y; |
| | 25 | 223 | | Fixed64 slabMaxY = capsule.MixedBounds3D.Max.Y; |
| | | 224 | | Fixed64 verticalExcess; |
| | 25 | 225 | | if (center.Y > slabMaxY) |
| | | 226 | | { |
| | 10 | 227 | | if (!Fixed64.TrySubtract(center.Y, slabMaxY, out verticalExcess)) |
| | 1 | 228 | | return false; |
| | | 229 | | } |
| | 15 | 230 | | else if (center.Y < slabMinY) |
| | | 231 | | { |
| | 3 | 232 | | if (!Fixed64.TrySubtract(slabMinY, center.Y, out verticalExcess)) |
| | 1 | 233 | | return false; |
| | | 234 | | } |
| | | 235 | | else |
| | 12 | 236 | | return false; |
| | | 237 | | |
| | 11 | 238 | | return verticalExcess <= radius |
| | 11 | 239 | | && capsule.ContainsPoint(new Vector2d(center.X, center.Z)); |
| | | 240 | | } |
| | | 241 | | |
| | | 242 | | internal static bool TrySweepSphereAgainstConvexSlab( |
| | | 243 | | Vector3d start, |
| | | 244 | | Vector3d end, |
| | | 245 | | Vector3d direction, |
| | | 246 | | Fixed64 length, |
| | | 247 | | Fixed64 radius, |
| | | 248 | | LSCollider2D collider, |
| | | 249 | | out PhysicsMixedHit hit) |
| | | 250 | | { |
| | 1163 | 251 | | if (DoesSphereOverlapConvexSlab(start, radius, collider)) |
| | | 252 | | { |
| | 388 | 253 | | hit = BuildSphereAgainst2DHit( |
| | 388 | 254 | | collider, |
| | 388 | 255 | | start, |
| | 388 | 256 | | radius, |
| | 388 | 257 | | PhysicsQueryReducerKind.Exact, |
| | 388 | 258 | | Fixed64.Zero, |
| | 388 | 259 | | direction); |
| | 388 | 260 | | return true; |
| | | 261 | | } |
| | | 262 | | |
| | 775 | 263 | | Fixed64 slabMinY = collider.MixedBounds3D.Min.Y; |
| | 775 | 264 | | Fixed64 slabMaxY = collider.MixedBounds3D.Max.Y; |
| | 775 | 265 | | SweepCandidate best = default; |
| | | 266 | | |
| | 775 | 267 | | if (Fixed64.TryAdd(slabMaxY, radius, out Fixed64 expandedMaxY)) |
| | | 268 | | { |
| | 774 | 269 | | TrySweepConvexSlabCapFace( |
| | 774 | 270 | | start, |
| | 774 | 271 | | end, |
| | 774 | 272 | | length, |
| | 774 | 273 | | collider, |
| | 774 | 274 | | expandedMaxY, |
| | 774 | 275 | | requireStartBeyondPlane: true, |
| | 774 | 276 | | -Vector3d.Up, |
| | 774 | 277 | | ref best); |
| | | 278 | | } |
| | | 279 | | |
| | 775 | 280 | | if (Fixed64.TrySubtract(slabMinY, radius, out Fixed64 expandedMinY)) |
| | | 281 | | { |
| | 774 | 282 | | TrySweepConvexSlabCapFace( |
| | 774 | 283 | | start, |
| | 774 | 284 | | end, |
| | 774 | 285 | | length, |
| | 774 | 286 | | collider, |
| | 774 | 287 | | expandedMinY, |
| | 774 | 288 | | requireStartBeyondPlane: false, |
| | 774 | 289 | | Vector3d.Up, |
| | 774 | 290 | | ref best); |
| | | 291 | | } |
| | | 292 | | |
| | 775 | 293 | | if (TryGetConvexSlabLocalSweep( |
| | 775 | 294 | | start, |
| | 775 | 295 | | end, |
| | 775 | 296 | | collider, |
| | 775 | 297 | | out Vector3d localStart, |
| | 775 | 298 | | out Vector3d localEnd)) |
| | | 299 | | { |
| | 774 | 300 | | Fixed64 localSlabMinY = -collider.MixedHalfThickness; |
| | 774 | 301 | | Fixed64 localSlabMaxY = collider.MixedHalfThickness; |
| | 774 | 302 | | TrySweepConvexSlabSideFaces( |
| | 774 | 303 | | localStart, |
| | 774 | 304 | | localEnd, |
| | 774 | 305 | | length, |
| | 774 | 306 | | radius, |
| | 774 | 307 | | collider, |
| | 774 | 308 | | localSlabMinY, |
| | 774 | 309 | | localSlabMaxY, |
| | 774 | 310 | | ref best); |
| | 774 | 311 | | TrySweepConvexSlabEdges( |
| | 774 | 312 | | localStart, |
| | 774 | 313 | | localEnd, |
| | 774 | 314 | | length, |
| | 774 | 315 | | radius, |
| | 774 | 316 | | collider, |
| | 774 | 317 | | localSlabMinY, |
| | 774 | 318 | | localSlabMaxY, |
| | 774 | 319 | | ref best); |
| | | 320 | | } |
| | | 321 | | |
| | 775 | 322 | | if (!best.Found) |
| | | 323 | | { |
| | 131 | 324 | | hit = default; |
| | 131 | 325 | | return false; |
| | | 326 | | } |
| | | 327 | | |
| | 644 | 328 | | Vector3d sweepCenter = new FixedSegment(start, end).GetPointAtDistance(best.Distance, length); |
| | 644 | 329 | | hit = BuildSphereAgainst2DHit( |
| | 644 | 330 | | collider, |
| | 644 | 331 | | sweepCenter, |
| | 644 | 332 | | radius, |
| | 644 | 333 | | PhysicsQueryReducerKind.Exact, |
| | 644 | 334 | | best.Distance, |
| | 644 | 335 | | direction, |
| | 644 | 336 | | best.FeatureNormal); |
| | 644 | 337 | | return true; |
| | | 338 | | } |
| | | 339 | | |
| | | 340 | | private static void TrySweepConvexSlabCapFace( |
| | | 341 | | Vector3d start, |
| | | 342 | | Vector3d end, |
| | | 343 | | Fixed64 length, |
| | | 344 | | LSCollider2D collider, |
| | | 345 | | Fixed64 planeY, |
| | | 346 | | bool requireStartBeyondPlane, |
| | | 347 | | Vector3d featureNormal, |
| | | 348 | | ref SweepCandidate best) |
| | | 349 | | { |
| | 1548 | 350 | | bool canReach = requireStartBeyondPlane |
| | 1548 | 351 | | ? start.Y > planeY && end.Y <= planeY |
| | 1548 | 352 | | : start.Y < planeY && end.Y >= planeY; |
| | 1548 | 353 | | if (!canReach) |
| | 1121 | 354 | | return; |
| | | 355 | | |
| | 427 | 356 | | Fixed64 distance = GetCapDistance(start.Y, end.Y, planeY, length); |
| | 427 | 357 | | Vector3d point = new FixedSegment(start, end).GetPointAtDistance(distance, length); |
| | 427 | 358 | | if (!collider.ContainsPoint(new Vector2d(point.X, point.Z))) |
| | 2 | 359 | | return; |
| | | 360 | | |
| | 425 | 361 | | TryKeepEarlierSweep(true, distance, featureNormal, ref best); |
| | 425 | 362 | | } |
| | | 363 | | |
| | | 364 | | private static void TrySweepConvexSlabSideFaces( |
| | | 365 | | Vector3d start, |
| | | 366 | | Vector3d end, |
| | | 367 | | Fixed64 length, |
| | | 368 | | Fixed64 radius, |
| | | 369 | | LSCollider2D collider, |
| | | 370 | | Fixed64 slabMinY, |
| | | 371 | | Fixed64 slabMaxY, |
| | | 372 | | ref SweepCandidate best) |
| | | 373 | | { |
| | 774 | 374 | | Vector2d planarStart = new(start.X, start.Z); |
| | 774 | 375 | | Vector2d planarDisplacement = new(end.X - start.X, end.Z - start.Z); |
| | 774 | 376 | | Vector2d polygonCenter = Vector2d.Zero; |
| | 774 | 377 | | int vertexCount = collider.VertexCount; |
| | | 378 | | |
| | 7740 | 379 | | for (int i = 0; i < vertexCount; i++) |
| | | 380 | | { |
| | 3096 | 381 | | Vector2d first = collider.GetScaledLocalVertexUnchecked(i); |
| | 3096 | 382 | | Vector2d second = collider.GetScaledLocalVertexUnchecked((i + 1) % vertexCount); |
| | 3096 | 383 | | Vector2d edge = second - first; |
| | 3096 | 384 | | Fixed64 edgeLength = edge.Magnitude; |
| | 3096 | 385 | | Vector2d edgeDirection = edge / edgeLength; |
| | 3096 | 386 | | Vector2d outward = new(edge.Y, -edge.X); |
| | 3096 | 387 | | if (Vector2d.Dot(polygonCenter - first, outward) > Fixed64.Zero) |
| | 304 | 388 | | outward = -outward; |
| | 3096 | 389 | | outward /= outward.Magnitude; |
| | 3096 | 390 | | Fixed64 signedStart = Vector2d.Dot(planarStart - first, outward); |
| | 3096 | 391 | | Fixed64 signedDirection = Vector2d.Dot(planarDisplacement, outward); |
| | 3096 | 392 | | if (signedStart <= radius || signedDirection >= Fixed64.Zero) |
| | | 393 | | continue; |
| | | 394 | | |
| | | 395 | | // One fused ratio retains physical-distance precision without |
| | | 396 | | // changing the authored segment through direction normalization. |
| | 312 | 397 | | if (!Fixed64.TryMultiplyDivide(radius - signedStart, length, |
| | 312 | 398 | | signedDirection, out Fixed64 distance) |
| | 312 | 399 | | || distance > length) |
| | | 400 | | continue; |
| | | 401 | | |
| | 294 | 402 | | Vector3d sweepCenter = new FixedSegment(start, end).GetPointAtDistance(distance, length); |
| | 294 | 403 | | if (sweepCenter.Y < slabMinY || sweepCenter.Y > slabMaxY) |
| | | 404 | | continue; |
| | | 405 | | |
| | 290 | 406 | | Vector2d planarPoint = new(sweepCenter.X, sweepCenter.Z); |
| | 290 | 407 | | Fixed64 projection = Vector2d.Dot(planarPoint - first, edgeDirection); |
| | 290 | 408 | | if (projection < Fixed64.Zero || projection > edgeLength) |
| | | 409 | | continue; |
| | | 410 | | |
| | 125 | 411 | | TryKeepEarlierSweep(true, distance, default, ref best); |
| | | 412 | | } |
| | 774 | 413 | | } |
| | | 414 | | |
| | | 415 | | private static void TrySweepConvexSlabEdges( |
| | | 416 | | Vector3d start, |
| | | 417 | | Vector3d end, |
| | | 418 | | Fixed64 length, |
| | | 419 | | Fixed64 radius, |
| | | 420 | | LSCollider2D collider, |
| | | 421 | | Fixed64 slabMinY, |
| | | 422 | | Fixed64 slabMaxY, |
| | | 423 | | ref SweepCandidate best) |
| | | 424 | | { |
| | 774 | 425 | | int vertexCount = collider.VertexCount; |
| | 7740 | 426 | | for (int i = 0; i < vertexCount; i++) |
| | | 427 | | { |
| | 3096 | 428 | | Vector2d first = collider.GetScaledLocalVertexUnchecked(i); |
| | 3096 | 429 | | Vector2d second = collider.GetScaledLocalVertexUnchecked((i + 1) % vertexCount); |
| | 3096 | 430 | | Vector3d bottomFirst = new(first.X, slabMinY, first.Y); |
| | 3096 | 431 | | Vector3d topFirst = new(first.X, slabMaxY, first.Y); |
| | 3096 | 432 | | Vector3d bottomSecond = new(second.X, slabMinY, second.Y); |
| | 3096 | 433 | | Vector3d topSecond = new(second.X, slabMaxY, second.Y); |
| | | 434 | | |
| | 3096 | 435 | | TryKeepEarlierSweep( |
| | 3096 | 436 | | TrySweepPointAgainstSegmentCapsule3D( |
| | 3096 | 437 | | start, |
| | 3096 | 438 | | end, |
| | 3096 | 439 | | length, |
| | 3096 | 440 | | bottomFirst, |
| | 3096 | 441 | | topFirst, |
| | 3096 | 442 | | Fixed64.Zero, |
| | 3096 | 443 | | radius, |
| | 3096 | 444 | | out Fixed64 verticalDistance), |
| | 3096 | 445 | | verticalDistance, |
| | 3096 | 446 | | default, |
| | 3096 | 447 | | ref best); |
| | 3096 | 448 | | TryKeepEarlierSweep( |
| | 3096 | 449 | | TrySweepPointAgainstSegmentCapsule3D( |
| | 3096 | 450 | | start, |
| | 3096 | 451 | | end, |
| | 3096 | 452 | | length, |
| | 3096 | 453 | | bottomFirst, |
| | 3096 | 454 | | bottomSecond, |
| | 3096 | 455 | | Fixed64.Zero, |
| | 3096 | 456 | | radius, |
| | 3096 | 457 | | out Fixed64 bottomDistance), |
| | 3096 | 458 | | bottomDistance, |
| | 3096 | 459 | | default, |
| | 3096 | 460 | | ref best); |
| | 3096 | 461 | | TryKeepEarlierSweep( |
| | 3096 | 462 | | TrySweepPointAgainstSegmentCapsule3D( |
| | 3096 | 463 | | start, |
| | 3096 | 464 | | end, |
| | 3096 | 465 | | length, |
| | 3096 | 466 | | topFirst, |
| | 3096 | 467 | | topSecond, |
| | 3096 | 468 | | Fixed64.Zero, |
| | 3096 | 469 | | radius, |
| | 3096 | 470 | | out Fixed64 topDistance), |
| | 3096 | 471 | | topDistance, |
| | 3096 | 472 | | default, |
| | 3096 | 473 | | ref best); |
| | | 474 | | } |
| | 774 | 475 | | } |
| | | 476 | | |
| | | 477 | | private static void TrySweepCapsuleSlabCapFace( |
| | | 478 | | Vector3d start, |
| | | 479 | | Vector3d end, |
| | | 480 | | Fixed64 length, |
| | | 481 | | LSCapsuleCollider2D capsule, |
| | | 482 | | Fixed64 planeY, |
| | | 483 | | bool requireStartBeyondPlane, |
| | | 484 | | Vector3d featureNormal, |
| | | 485 | | ref SweepCandidate best) |
| | | 486 | | { |
| | 46 | 487 | | bool canReach = requireStartBeyondPlane |
| | 46 | 488 | | ? start.Y > planeY && end.Y <= planeY |
| | 46 | 489 | | : start.Y < planeY && end.Y >= planeY; |
| | 46 | 490 | | if (!canReach) |
| | 40 | 491 | | return; |
| | | 492 | | |
| | 6 | 493 | | Fixed64 distance = GetCapDistance(start.Y, end.Y, planeY, length); |
| | 6 | 494 | | Vector3d point = new FixedSegment(start, end).GetPointAtDistance(distance, length); |
| | 6 | 495 | | if (!capsule.ContainsPoint(new Vector2d(point.X, point.Z))) |
| | 2 | 496 | | return; |
| | | 497 | | |
| | 4 | 498 | | TryKeepEarlierSweep(true, distance, featureNormal, ref best); |
| | 4 | 499 | | } |
| | | 500 | | |
| | | 501 | | private static void TrySweepCapsuleSlabSide( |
| | | 502 | | Vector3d start, |
| | | 503 | | Vector3d end, |
| | | 504 | | Fixed64 length, |
| | | 505 | | LSCapsuleCollider2D capsule, |
| | | 506 | | Fixed64 targetRadius, |
| | | 507 | | Fixed64 radiusExpansion, |
| | | 508 | | Fixed64 slabMinY, |
| | | 509 | | Fixed64 slabMaxY, |
| | | 510 | | ref SweepCandidate best) |
| | | 511 | | { |
| | 24 | 512 | | var planarSegment = new FixedSegment2d( |
| | 24 | 513 | | new Vector2d(start.X, start.Z), |
| | 24 | 514 | | new Vector2d(end.X, end.Z)); |
| | 24 | 515 | | if (!planarSegment.TryGetCapsuleIntersectionDistanceInterval( |
| | 24 | 516 | | capsule.Center, |
| | 24 | 517 | | capsule.Rotation, |
| | 24 | 518 | | capsule.AxisLength, |
| | 24 | 519 | | targetRadius, |
| | 24 | 520 | | radiusExpansion, |
| | 24 | 521 | | length, |
| | 24 | 522 | | out Fixed64 distance, |
| | 24 | 523 | | out _, |
| | 24 | 524 | | out _, |
| | 24 | 525 | | out _)) |
| | | 526 | | { |
| | 5 | 527 | | return; |
| | | 528 | | } |
| | | 529 | | |
| | 19 | 530 | | Fixed64 y = new FixedSegment(start, end).GetPointAtDistance(distance, length).Y; |
| | 19 | 531 | | if (y < slabMinY || y > slabMaxY) |
| | 10 | 532 | | return; |
| | | 533 | | |
| | 9 | 534 | | TryKeepEarlierSweep(true, distance, default, ref best); |
| | 9 | 535 | | } |
| | | 536 | | |
| | | 537 | | private static void TrySweepCapsuleSlabBoundaryEdges( |
| | | 538 | | Vector3d start, |
| | | 539 | | Vector3d end, |
| | | 540 | | Fixed64 length, |
| | | 541 | | LSCapsuleCollider2D capsule, |
| | | 542 | | Fixed64 targetRadius, |
| | | 543 | | Fixed64 radiusExpansion, |
| | | 544 | | Fixed64 slabMinY, |
| | | 545 | | Fixed64 slabMaxY, |
| | | 546 | | ref SweepCandidate best) |
| | | 547 | | { |
| | 24 | 548 | | Vector2d capsuleAxis = new( |
| | 24 | 549 | | -FixedMath.Sin(capsule.Rotation), |
| | 24 | 550 | | FixedMath.Cos(capsule.Rotation)); |
| | 24 | 551 | | if (!FixedSegment2d.TryGetCenteredAxisEndpoint( |
| | 24 | 552 | | capsule.Center, |
| | 24 | 553 | | capsuleAxis, |
| | 24 | 554 | | capsule.AxisLength, |
| | 24 | 555 | | positive: false, |
| | 24 | 556 | | out Vector2d firstEndpoint) |
| | 24 | 557 | | || !FixedSegment2d.TryGetCenteredAxisEndpoint( |
| | 24 | 558 | | capsule.Center, |
| | 24 | 559 | | capsuleAxis, |
| | 24 | 560 | | capsule.AxisLength, |
| | 24 | 561 | | positive: true, |
| | 24 | 562 | | out Vector2d secondEndpoint)) |
| | | 563 | | { |
| | 2 | 564 | | return; |
| | | 565 | | } |
| | | 566 | | |
| | 22 | 567 | | Vector2d outward = |
| | 22 | 568 | | new Vector2d(capsuleAxis.Y, -capsuleAxis.X) |
| | 22 | 569 | | * targetRadius; |
| | 22 | 570 | | Vector2d firstSideStart = firstEndpoint + outward; |
| | 22 | 571 | | Vector2d firstSideEnd = secondEndpoint + outward; |
| | 22 | 572 | | Vector2d secondSideStart = firstEndpoint - outward; |
| | 22 | 573 | | Vector2d secondSideEnd = secondEndpoint - outward; |
| | | 574 | | |
| | 22 | 575 | | TryKeepCapsuleSlabStraightRimSweep( |
| | 22 | 576 | | start, end, length, firstSideStart, firstSideEnd, slabMinY, radiusExpansion, ref best); |
| | 22 | 577 | | TryKeepCapsuleSlabStraightRimSweep( |
| | 22 | 578 | | start, end, length, firstSideStart, firstSideEnd, slabMaxY, radiusExpansion, ref best); |
| | 22 | 579 | | TryKeepCapsuleSlabStraightRimSweep( |
| | 22 | 580 | | start, end, length, secondSideStart, secondSideEnd, slabMinY, radiusExpansion, ref best); |
| | 22 | 581 | | TryKeepCapsuleSlabStraightRimSweep( |
| | 22 | 582 | | start, end, length, secondSideStart, secondSideEnd, slabMaxY, radiusExpansion, ref best); |
| | | 583 | | |
| | 22 | 584 | | Fixed64 slabCenterY = capsule.MixedSlabCenterY; |
| | 22 | 585 | | Fixed64 slabHalfThickness = capsule.MixedHalfThickness; |
| | 22 | 586 | | TryKeepEarlierSweep( |
| | 22 | 587 | | new FixedSegment(start, end).TryGetSweptSphereFiniteCylinderIntersectionDistance( |
| | 22 | 588 | | new Vector3d(firstEndpoint.X, slabCenterY, firstEndpoint.Y), |
| | 22 | 589 | | Vector3d.Up, |
| | 22 | 590 | | slabHalfThickness, |
| | 22 | 591 | | targetRadius, |
| | 22 | 592 | | radiusExpansion, |
| | 22 | 593 | | length, |
| | 22 | 594 | | out Fixed64 firstVerticalDistance), |
| | 22 | 595 | | firstVerticalDistance, |
| | 22 | 596 | | default, |
| | 22 | 597 | | ref best); |
| | 22 | 598 | | TryKeepEarlierSweep( |
| | 22 | 599 | | new FixedSegment(start, end).TryGetSweptSphereFiniteCylinderIntersectionDistance( |
| | 22 | 600 | | new Vector3d(secondEndpoint.X, slabCenterY, secondEndpoint.Y), |
| | 22 | 601 | | Vector3d.Up, |
| | 22 | 602 | | slabHalfThickness, |
| | 22 | 603 | | targetRadius, |
| | 22 | 604 | | radiusExpansion, |
| | 22 | 605 | | length, |
| | 22 | 606 | | out Fixed64 secondVerticalDistance), |
| | 22 | 607 | | secondVerticalDistance, |
| | 22 | 608 | | default, |
| | 22 | 609 | | ref best); |
| | 22 | 610 | | } |
| | | 611 | | |
| | | 612 | | private static void TryKeepCapsuleSlabStraightRimSweep( |
| | | 613 | | Vector3d start, |
| | | 614 | | Vector3d end, |
| | | 615 | | Fixed64 length, |
| | | 616 | | Vector2d planarStart, |
| | | 617 | | Vector2d planarEnd, |
| | | 618 | | Fixed64 y, |
| | | 619 | | Fixed64 radiusExpansion, |
| | | 620 | | ref SweepCandidate best) |
| | | 621 | | { |
| | 88 | 622 | | TryKeepEarlierSweep( |
| | 88 | 623 | | TrySweepPointAgainstSegmentCapsule3D( |
| | 88 | 624 | | start, |
| | 88 | 625 | | end, |
| | 88 | 626 | | length, |
| | 88 | 627 | | new Vector3d(planarStart.X, y, planarStart.Y), |
| | 88 | 628 | | new Vector3d(planarEnd.X, y, planarEnd.Y), |
| | 88 | 629 | | Fixed64.Zero, |
| | 88 | 630 | | radiusExpansion, |
| | 88 | 631 | | out Fixed64 distance), |
| | 88 | 632 | | distance, |
| | 88 | 633 | | default, |
| | 88 | 634 | | ref best); |
| | 88 | 635 | | } |
| | | 636 | | |
| | | 637 | | private static bool DoesSphereOverlapConvexSlab( |
| | | 638 | | Vector3d center, |
| | | 639 | | Fixed64 radius, |
| | | 640 | | LSCollider2D collider) |
| | | 641 | | { |
| | 1163 | 642 | | if (!FixedMath.TryGetSphereSlabCrossSectionRadius( |
| | 1163 | 643 | | center.Y, |
| | 1163 | 644 | | radius, |
| | 1163 | 645 | | collider.MixedSlabCenterY, |
| | 1163 | 646 | | collider.MixedHalfThickness, |
| | 1163 | 647 | | out Fixed64 planarRadius)) |
| | | 648 | | { |
| | 428 | 649 | | return false; |
| | | 650 | | } |
| | | 651 | | |
| | 735 | 652 | | Vector2d planarPoint = new(center.X, center.Z); |
| | 735 | 653 | | if (collider.ContainsPoint(planarPoint)) |
| | 7 | 654 | | return true; |
| | | 655 | | |
| | | 656 | | // The convex reducer admits boxes and polygons, which always provide a conceptual boundary. |
| | 728 | 657 | | bool hasBoundary = collider.TryGetClosestBoundaryAnchor(planarPoint, out FixedPointAnchor2d boundary); |
| | | 658 | | System.Diagnostics.Debug.Assert(hasBoundary); |
| | | 659 | | |
| | | 660 | | // Admission is a geometric comparison; vector magnitude's unit-vector |
| | | 661 | | // shortcut must not turn a positive separation into an initial overlap. |
| | 728 | 662 | | var queryCenter = new FixedPointAnchor2d(planarPoint, Fixed64.Zero, Vector2d.Zero); |
| | 728 | 663 | | var radiusPoint = new FixedPointAnchor2d(planarPoint, Fixed64.Zero, |
| | 728 | 664 | | new Vector2d(planarRadius, Fixed64.Zero)); |
| | 728 | 665 | | return WidePointAnchor2d.CompareSquaredDistances(queryCenter, boundary, radiusPoint) <= 0; |
| | | 666 | | } |
| | | 667 | | |
| | | 668 | | private static bool TryGetConvexSlabLocalSweep( |
| | | 669 | | Vector3d start, |
| | | 670 | | Vector3d end, |
| | | 671 | | LSCollider2D collider, |
| | | 672 | | out Vector3d localStart, |
| | | 673 | | out Vector3d localEnd) |
| | | 674 | | { |
| | 775 | 675 | | var frameOrigin = new Vector3d( |
| | 775 | 676 | | collider.Center.X, |
| | 775 | 677 | | collider.MixedSlabCenterY, |
| | 775 | 678 | | collider.Center.Y); |
| | 775 | 679 | | FixedQuaternion frameRotation = |
| | 775 | 680 | | FixedQuaternion.FromAxisAngle( |
| | 775 | 681 | | Vector3d.Up, |
| | 775 | 682 | | -collider.ConvexRotation); |
| | 775 | 683 | | var startAnchor = new FixedPointAnchor( |
| | 775 | 684 | | start, |
| | 775 | 685 | | FixedQuaternion.Identity, |
| | 775 | 686 | | Vector3d.Zero); |
| | 775 | 687 | | var endAnchor = new FixedPointAnchor( |
| | 775 | 688 | | end, |
| | 775 | 689 | | FixedQuaternion.Identity, |
| | 775 | 690 | | Vector3d.Zero); |
| | 775 | 691 | | bool representable = startAnchor.TryGetLocalPointIn( |
| | 775 | 692 | | frameOrigin, |
| | 775 | 693 | | frameRotation, |
| | 775 | 694 | | out localStart) |
| | 775 | 695 | | & endAnchor.TryGetLocalPointIn( |
| | 775 | 696 | | frameOrigin, |
| | 775 | 697 | | frameRotation, |
| | 775 | 698 | | out localEnd); |
| | 775 | 699 | | return representable; |
| | | 700 | | } |
| | | 701 | | |
| | | 702 | | private static bool TrySweepPointAgainstSegmentCapsule3D( |
| | | 703 | | Vector3d start, |
| | | 704 | | Vector3d end, |
| | | 705 | | Fixed64 length, |
| | | 706 | | Vector3d segmentStart, |
| | | 707 | | Vector3d segmentEnd, |
| | | 708 | | Fixed64 radius, |
| | | 709 | | Fixed64 radiusExpansion, |
| | | 710 | | out Fixed64 distance) |
| | | 711 | | { |
| | 9376 | 712 | | return new FixedSegment(start, end).TryGetCapsuleIntersectionDistanceInterval( |
| | 9376 | 713 | | new FixedSegment(segmentStart, segmentEnd), |
| | 9376 | 714 | | radius, |
| | 9376 | 715 | | radiusExpansion, |
| | 9376 | 716 | | length, |
| | 9376 | 717 | | out distance, |
| | 9376 | 718 | | out _, |
| | 9376 | 719 | | out _, |
| | 9376 | 720 | | out _); |
| | | 721 | | } |
| | | 722 | | |
| | | 723 | | private static Fixed64 GetCapDistance( |
| | | 724 | | Fixed64 start, |
| | | 725 | | Fixed64 end, |
| | | 726 | | Fixed64 plane, |
| | | 727 | | Fixed64 length) |
| | | 728 | | { |
| | | 729 | | // Cap admission proves that plane lies between start and end. The public |
| | | 730 | | // sweep contract has already proved the segment displacement and length |
| | | 731 | | // representable, so the fused result is bounded by length and cannot fail. |
| | 433 | 732 | | Fixed64 verticalSpan = end - start; |
| | 433 | 733 | | Fixed64 capOffset = plane - start; |
| | 433 | 734 | | _ = Fixed64.TryMultiplyDivide(length, capOffset, verticalSpan, out Fixed64 distance); |
| | 433 | 735 | | return distance; |
| | | 736 | | } |
| | | 737 | | |
| | | 738 | | private static void TryKeepEarlierSweep( |
| | | 739 | | bool candidateFound, |
| | | 740 | | Fixed64 candidateDistance, |
| | | 741 | | Vector3d candidateFeatureNormal, |
| | | 742 | | ref SweepCandidate best) |
| | | 743 | | { |
| | 9983 | 744 | | if (!candidateFound) |
| | 8131 | 745 | | return; |
| | | 746 | | |
| | 1852 | 747 | | if (best.Found && candidateDistance >= best.Distance) |
| | 967 | 748 | | return; |
| | | 749 | | |
| | 885 | 750 | | best.Found = true; |
| | 885 | 751 | | best.Distance = candidateDistance; |
| | 885 | 752 | | best.FeatureNormal = candidateFeatureNormal; |
| | 885 | 753 | | } |
| | | 754 | | |
| | | 755 | | private struct SweepCandidate |
| | | 756 | | { |
| | | 757 | | public bool Found; |
| | | 758 | | public Fixed64 Distance; |
| | | 759 | | public Vector3d FeatureNormal; |
| | | 760 | | } |
| | | 761 | | |
| | | 762 | | } |