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