| | | 1 | | //======================================================================= |
| | | 2 | | // CollisionDetection2D.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 System; |
| | | 12 | | using System.Runtime.CompilerServices; |
| | | 13 | | |
| | | 14 | | namespace Gravitas.CollisionHandling; |
| | | 15 | | |
| | | 16 | | /// <summary> |
| | | 17 | | /// Deterministic pure 2D narrow-phase collision checks. |
| | | 18 | | /// </summary> |
| | | 19 | | internal static class CollisionDetection2D |
| | | 20 | | { |
| | | 21 | | internal static bool TryCollide(LSCollider2D colliderA, LSCollider2D colliderB, out Contact2D contact) |
| | | 22 | | { |
| | 2373 | 23 | | SwiftThrowHelper.ThrowIfNull(colliderA, nameof(colliderA)); |
| | 2373 | 24 | | SwiftThrowHelper.ThrowIfNull(colliderB, nameof(colliderB)); |
| | | 25 | | |
| | 2373 | 26 | | CollisionType2D collisionType = ColliderSettings2D.GetCollisionType(colliderA.Shape, colliderB.Shape); |
| | 2373 | 27 | | return TryCollide(new CollisionWorkItem2D(colliderA, colliderB, collisionType), out contact); |
| | | 28 | | } |
| | | 29 | | |
| | | 30 | | internal static bool TryCollide(CollisionPair2D pair, ContactManifold2D manifold, int frame) |
| | | 31 | | { |
| | 546 | 32 | | SwiftThrowHelper.ThrowIfNull(pair, nameof(pair)); |
| | 546 | 33 | | return TryCollide(CollisionWorkItem2D.Create(pair), manifold, frame); |
| | | 34 | | } |
| | | 35 | | |
| | | 36 | | internal static bool TryCollide(CollisionWorkItem2D item, ContactManifold2D manifold, int frame) |
| | | 37 | | { |
| | 621 | 38 | | SwiftThrowHelper.ThrowIfNull(manifold, nameof(manifold)); |
| | 621 | 39 | | manifold.BeginUpdate(frame); |
| | 621 | 40 | | return TryCollide(item, manifold); |
| | | 41 | | } |
| | | 42 | | |
| | | 43 | | internal static bool TryCollide(CollisionWorkItem2D item, out Contact2D contact) |
| | | 44 | | { |
| | 2375 | 45 | | LSCollider2D colliderA = item.ColliderA; |
| | 2375 | 46 | | LSCollider2D colliderB = item.ColliderB; |
| | 2375 | 47 | | if (!BoundsOverlap(colliderA, colliderB)) |
| | | 48 | | { |
| | 1399 | 49 | | contact = default; |
| | 1399 | 50 | | return false; |
| | | 51 | | } |
| | | 52 | | |
| | 976 | 53 | | switch (item.CollisionType) |
| | | 54 | | { |
| | | 55 | | case CollisionType2D.Circle_Circle: |
| | 58 | 56 | | return TryCircleCircle((LSCircleCollider2D)colliderA, (LSCircleCollider2D)colliderB, out contact); |
| | | 57 | | case CollisionType2D.Circle_Convex: |
| | 38 | 58 | | return TryCircleConvex((LSCircleCollider2D)colliderA, colliderB, out contact); |
| | | 59 | | case CollisionType2D.Convex_Circle: |
| | 785 | 60 | | bool result = TryCircleConvex((LSCircleCollider2D)colliderB, colliderA, out Contact2D reversed); |
| | 785 | 61 | | contact = result |
| | 785 | 62 | | ? new Contact2D( |
| | 785 | 63 | | reversed.AnchorB, |
| | 785 | 64 | | reversed.AnchorA, |
| | 785 | 65 | | -reversed.Normal, |
| | 785 | 66 | | reversed.Depth, |
| | 785 | 67 | | reversed.DepthIsClamped) |
| | 785 | 68 | | : default; |
| | 785 | 69 | | return result; |
| | | 70 | | case CollisionType2D.Convex_Convex: |
| | 53 | 71 | | return TryConvexConvex(colliderA, colliderB, out contact); |
| | | 72 | | case CollisionType2D.Capsule_Circle: |
| | 7 | 73 | | return TryCapsuleCircle((LSCapsuleCollider2D)colliderA, (LSCircleCollider2D)colliderB, out contact); |
| | | 74 | | case CollisionType2D.Circle_Capsule: |
| | 4 | 75 | | bool circleCapsule = TryCapsuleCircle((LSCapsuleCollider2D)colliderB, (LSCircleCollider2D)colliderA, out |
| | 4 | 76 | | contact = circleCapsule |
| | 4 | 77 | | ? new Contact2D( |
| | 4 | 78 | | circleCapsuleReversed.AnchorB, |
| | 4 | 79 | | circleCapsuleReversed.AnchorA, |
| | 4 | 80 | | -circleCapsuleReversed.Normal, |
| | 4 | 81 | | circleCapsuleReversed.Depth, |
| | 4 | 82 | | circleCapsuleReversed.DepthIsClamped) |
| | 4 | 83 | | : default; |
| | 4 | 84 | | return circleCapsule; |
| | | 85 | | case CollisionType2D.Capsule_Convex: |
| | 6 | 86 | | return TryCapsuleConvex((LSCapsuleCollider2D)colliderA, colliderB, out contact); |
| | | 87 | | case CollisionType2D.Convex_Capsule: |
| | 4 | 88 | | bool convexCapsule = TryCapsuleConvex((LSCapsuleCollider2D)colliderB, colliderA, out Contact2D convexCap |
| | 4 | 89 | | contact = convexCapsule |
| | 4 | 90 | | ? new Contact2D( |
| | 4 | 91 | | convexCapsuleReversed.AnchorB, |
| | 4 | 92 | | convexCapsuleReversed.AnchorA, |
| | 4 | 93 | | -convexCapsuleReversed.Normal, |
| | 4 | 94 | | convexCapsuleReversed.Depth, |
| | 4 | 95 | | convexCapsuleReversed.DepthIsClamped) |
| | 4 | 96 | | : default; |
| | 4 | 97 | | return convexCapsule; |
| | | 98 | | case CollisionType2D.Capsule_Capsule: |
| | 10 | 99 | | return TryCapsuleCapsule((LSCapsuleCollider2D)colliderA, (LSCapsuleCollider2D)colliderB, out contact); |
| | | 100 | | case CollisionType2D.Compound: |
| | 10 | 101 | | return TryCompound(colliderA, colliderB, out contact); |
| | | 102 | | default: |
| | 1 | 103 | | contact = default; |
| | 1 | 104 | | return false; |
| | | 105 | | } |
| | | 106 | | } |
| | | 107 | | |
| | | 108 | | private static bool TryCollide(CollisionWorkItem2D item, ContactManifold2D manifold) |
| | | 109 | | { |
| | 649 | 110 | | LSCollider2D colliderA = item.ColliderA; |
| | 649 | 111 | | LSCollider2D colliderB = item.ColliderB; |
| | 649 | 112 | | if (!BoundsOverlap(colliderA, colliderB)) |
| | 17 | 113 | | return false; |
| | | 114 | | |
| | 632 | 115 | | return item.CollisionType switch |
| | 632 | 116 | | { |
| | 455 | 117 | | CollisionType2D.Circle_Circle => TryCircleCircle((LSCircleCollider2D)colliderA, (LSCircleCollider2D)collider |
| | 4 | 118 | | CollisionType2D.Circle_Convex => TryCircleConvex((LSCircleCollider2D)colliderA, colliderB, manifold), |
| | 89 | 119 | | CollisionType2D.Convex_Circle => TryCircleConvexReversed((LSCircleCollider2D)colliderB, colliderA, manifold) |
| | 25 | 120 | | CollisionType2D.Convex_Convex => TryConvexConvex(colliderA, colliderB, manifold), |
| | 11 | 121 | | CollisionType2D.Capsule_Circle => TryCapsuleCircle((LSCapsuleCollider2D)colliderA, (LSCircleCollider2D)colli |
| | 4 | 122 | | CollisionType2D.Circle_Capsule => TryCapsuleCircleReversed((LSCapsuleCollider2D)colliderB, (LSCircleCollider |
| | 10 | 123 | | CollisionType2D.Capsule_Convex => TryCapsuleConvex((LSCapsuleCollider2D)colliderA, colliderB, manifold), |
| | 9 | 124 | | CollisionType2D.Convex_Capsule => TryCapsuleConvexReversed((LSCapsuleCollider2D)colliderB, colliderA, manifo |
| | 7 | 125 | | CollisionType2D.Capsule_Capsule => TryCapsuleCapsule((LSCapsuleCollider2D)colliderA, (LSCapsuleCollider2D)co |
| | 17 | 126 | | CollisionType2D.Compound => TryCompound(colliderA, colliderB, manifold), |
| | 1 | 127 | | _ => false |
| | 632 | 128 | | }; |
| | | 129 | | } |
| | | 130 | | |
| | | 131 | | private static bool TryCircleCircle(LSCircleCollider2D colliderA, LSCircleCollider2D colliderB, out Contact2D contac |
| | | 132 | | { |
| | 513 | 133 | | Vector2d delta = colliderB.Center - colliderA.Center; |
| | 513 | 134 | | Fixed64 radius = colliderA.ScaledRadius + colliderB.ScaledRadius; |
| | 513 | 135 | | Fixed64 distanceSquared = delta.MagnitudeSquared; |
| | 513 | 136 | | if (distanceSquared > radius * radius) |
| | | 137 | | { |
| | 34 | 138 | | contact = default; |
| | 34 | 139 | | return false; |
| | | 140 | | } |
| | | 141 | | |
| | 479 | 142 | | Fixed64 distance = distanceSquared > Fixed64.Zero ? FixedMath.Sqrt(distanceSquared) : Fixed64.Zero; |
| | 479 | 143 | | Vector2d normal = distance > Fixed64.Zero ? delta / distance : Vector2d.Right; |
| | 479 | 144 | | Fixed64 depth = radius - distance; |
| | 479 | 145 | | contact = new Contact2D( |
| | 479 | 146 | | new ContactAnchor2D( |
| | 479 | 147 | | colliderA.Center, |
| | 479 | 148 | | normal * colliderA.ScaledRadius), |
| | 479 | 149 | | new ContactAnchor2D( |
| | 479 | 150 | | colliderB.Center, |
| | 479 | 151 | | -normal * colliderB.ScaledRadius), |
| | 479 | 152 | | normal, |
| | 479 | 153 | | depth); |
| | 479 | 154 | | return true; |
| | | 155 | | } |
| | | 156 | | |
| | | 157 | | private static bool TryCircleCircle( |
| | | 158 | | LSCircleCollider2D colliderA, |
| | | 159 | | LSCircleCollider2D colliderB, |
| | | 160 | | ContactManifold2D manifold) |
| | | 161 | | { |
| | 455 | 162 | | if (!TryCircleCircle(colliderA, colliderB, out Contact2D contact)) |
| | 10 | 163 | | return false; |
| | | 164 | | |
| | 445 | 165 | | AddContact(manifold, contact, colliderA, colliderB); |
| | 445 | 166 | | return true; |
| | | 167 | | } |
| | | 168 | | |
| | | 169 | | private static bool TryCircleConvex(LSCircleCollider2D circle, LSCollider2D convex, out Contact2D contact) |
| | | 170 | | { |
| | 916 | 171 | | Span<Vector2d> convexOffsets = stackalloc Vector2d[4]; |
| | 916 | 172 | | ReadOnlySpan<Vector2d> convexVertexOffsets = |
| | 916 | 173 | | GetConvexVertexOffsets(convex, convexOffsets); |
| | 916 | 174 | | if (!FixedConvex2dRelations.TryGetCircleContact( |
| | 916 | 175 | | circle.Center, |
| | 916 | 176 | | circle.Rotation, |
| | 916 | 177 | | circle.ScaledRadius, |
| | 916 | 178 | | convex.Center, |
| | 916 | 179 | | convex.ConvexRotation, |
| | 916 | 180 | | convexVertexOffsets, |
| | 916 | 181 | | out FixedPointAnchor2d circleAnchor, |
| | 916 | 182 | | out FixedPointAnchor2d convexAnchor, |
| | 916 | 183 | | out Vector2d normal, |
| | 916 | 184 | | out Fixed64 depth, |
| | 916 | 185 | | out bool depthIsClamped)) |
| | | 186 | | { |
| | 387 | 187 | | contact = default; |
| | 387 | 188 | | return false; |
| | | 189 | | } |
| | | 190 | | |
| | 529 | 191 | | contact = new Contact2D( |
| | 529 | 192 | | new ContactAnchor2D(circleAnchor), |
| | 529 | 193 | | new ContactAnchor2D(convexAnchor), |
| | 529 | 194 | | normal, |
| | 529 | 195 | | depth, |
| | 529 | 196 | | depthIsClamped); |
| | 529 | 197 | | return true; |
| | | 198 | | } |
| | | 199 | | |
| | | 200 | | private static bool TryCircleConvex( |
| | | 201 | | LSCircleCollider2D circle, |
| | | 202 | | LSCollider2D convex, |
| | | 203 | | ContactManifold2D manifold) |
| | | 204 | | { |
| | 4 | 205 | | if (!TryCircleConvex(circle, convex, out Contact2D contact)) |
| | 1 | 206 | | return false; |
| | | 207 | | |
| | 3 | 208 | | AddContact(manifold, contact, circle, convex); |
| | 3 | 209 | | return true; |
| | | 210 | | } |
| | | 211 | | |
| | | 212 | | private static bool TryCircleConvexReversed( |
| | | 213 | | LSCircleCollider2D circle, |
| | | 214 | | LSCollider2D convex, |
| | | 215 | | ContactManifold2D manifold) |
| | | 216 | | { |
| | 89 | 217 | | if (!TryCircleConvex(circle, convex, out Contact2D contact)) |
| | 1 | 218 | | return false; |
| | | 219 | | |
| | 88 | 220 | | manifold.AddContact( |
| | 88 | 221 | | contact.AnchorB, |
| | 88 | 222 | | contact.AnchorA, |
| | 88 | 223 | | contact.Depth, |
| | 88 | 224 | | -contact.Normal, |
| | 88 | 225 | | convex.Material, |
| | 88 | 226 | | circle.Material, |
| | 88 | 227 | | contact.DepthIsClamped); |
| | 88 | 228 | | return true; |
| | | 229 | | } |
| | | 230 | | |
| | | 231 | | private static bool TryCapsuleCircle(LSCapsuleCollider2D capsule, LSCircleCollider2D circle, out Contact2D contact) |
| | | 232 | | { |
| | 26 | 233 | | Vector2d fallbackNormal = capsule.GetNormalFromCenteredAxis(circle.Center); |
| | 26 | 234 | | if (!FixedSegment2d.TryGetCenteredCapsulesContact( |
| | 26 | 235 | | capsule.Center, |
| | 26 | 236 | | capsule.Rotation, |
| | 26 | 237 | | capsule.AxisLength, |
| | 26 | 238 | | capsule.ScaledRadius, |
| | 26 | 239 | | circle.Center, |
| | 26 | 240 | | circle.Rotation, |
| | 26 | 241 | | Fixed64.Zero, |
| | 26 | 242 | | circle.ScaledRadius, |
| | 26 | 243 | | fallbackNormal, |
| | 26 | 244 | | out FixedContactAnchors2d fixedContact)) |
| | | 245 | | { |
| | 4 | 246 | | contact = default; |
| | 4 | 247 | | return false; |
| | | 248 | | } |
| | | 249 | | |
| | 22 | 250 | | contact = new Contact2D( |
| | 22 | 251 | | new ContactAnchor2D(fixedContact.FirstAnchor), |
| | 22 | 252 | | new ContactAnchor2D(fixedContact.SecondAnchor), |
| | 22 | 253 | | fixedContact.Normal, |
| | 22 | 254 | | fixedContact.Depth, |
| | 22 | 255 | | fixedContact.DepthIsClamped); |
| | 22 | 256 | | return true; |
| | | 257 | | } |
| | | 258 | | |
| | | 259 | | private static bool TryCapsuleCircle( |
| | | 260 | | LSCapsuleCollider2D capsule, |
| | | 261 | | LSCircleCollider2D circle, |
| | | 262 | | ContactManifold2D manifold) |
| | | 263 | | { |
| | 11 | 264 | | if (!TryCapsuleCircle(capsule, circle, out Contact2D contact)) |
| | 1 | 265 | | return false; |
| | | 266 | | |
| | 10 | 267 | | AddContact(manifold, contact, capsule, circle); |
| | 10 | 268 | | return true; |
| | | 269 | | } |
| | | 270 | | |
| | | 271 | | private static bool TryCapsuleCircleReversed( |
| | | 272 | | LSCapsuleCollider2D capsule, |
| | | 273 | | LSCircleCollider2D circle, |
| | | 274 | | ContactManifold2D manifold) |
| | | 275 | | { |
| | 4 | 276 | | if (!TryCapsuleCircle(capsule, circle, out Contact2D contact)) |
| | 1 | 277 | | return false; |
| | | 278 | | |
| | 3 | 279 | | manifold.AddContact( |
| | 3 | 280 | | contact.AnchorB, |
| | 3 | 281 | | contact.AnchorA, |
| | 3 | 282 | | contact.Depth, |
| | 3 | 283 | | -contact.Normal, |
| | 3 | 284 | | circle.Material, |
| | 3 | 285 | | capsule.Material, |
| | 3 | 286 | | contact.DepthIsClamped); |
| | 3 | 287 | | return true; |
| | | 288 | | } |
| | | 289 | | |
| | | 290 | | private static bool TryCapsuleCapsule(LSCapsuleCollider2D colliderA, LSCapsuleCollider2D colliderB, out Contact2D co |
| | | 291 | | { |
| | 17 | 292 | | Vector2d fallbackNormal = colliderB.AxisLength <= Fixed64.Epsilon |
| | 17 | 293 | | ? colliderA.GetNormalFromCenteredAxis(colliderB.Center) |
| | 17 | 294 | | : colliderA.AxisLength <= Fixed64.Epsilon |
| | 17 | 295 | | ? -colliderB.GetNormalFromCenteredAxis(colliderA.Center) |
| | 17 | 296 | | : OrientCoincidentCapsuleNormal(colliderB.Center - colliderA.Center); |
| | 17 | 297 | | if (!FixedSegment2d.TryGetCenteredCapsulesContact( |
| | 17 | 298 | | colliderA.Center, |
| | 17 | 299 | | colliderA.Rotation, |
| | 17 | 300 | | colliderA.AxisLength, |
| | 17 | 301 | | colliderA.ScaledRadius, |
| | 17 | 302 | | colliderB.Center, |
| | 17 | 303 | | colliderB.Rotation, |
| | 17 | 304 | | colliderB.AxisLength, |
| | 17 | 305 | | colliderB.ScaledRadius, |
| | 17 | 306 | | fallbackNormal, |
| | 17 | 307 | | out FixedContactAnchors2d fixedContact)) |
| | | 308 | | { |
| | 2 | 309 | | contact = default; |
| | 2 | 310 | | return false; |
| | | 311 | | } |
| | | 312 | | |
| | 15 | 313 | | contact = new Contact2D( |
| | 15 | 314 | | new ContactAnchor2D(fixedContact.FirstAnchor), |
| | 15 | 315 | | new ContactAnchor2D(fixedContact.SecondAnchor), |
| | 15 | 316 | | fixedContact.Normal, |
| | 15 | 317 | | fixedContact.Depth, |
| | 15 | 318 | | fixedContact.DepthIsClamped); |
| | 15 | 319 | | return true; |
| | | 320 | | } |
| | | 321 | | |
| | | 322 | | private static bool TryCapsuleCapsule( |
| | | 323 | | LSCapsuleCollider2D colliderA, |
| | | 324 | | LSCapsuleCollider2D colliderB, |
| | | 325 | | ContactManifold2D manifold) |
| | | 326 | | { |
| | 7 | 327 | | if (!TryCapsuleCapsule(colliderA, colliderB, out Contact2D contact)) |
| | 1 | 328 | | return false; |
| | | 329 | | |
| | 6 | 330 | | AddContact(manifold, contact, colliderA, colliderB); |
| | 6 | 331 | | return true; |
| | | 332 | | } |
| | | 333 | | |
| | | 334 | | private static bool TryCapsuleConvex(LSCapsuleCollider2D capsule, LSCollider2D convex, out Contact2D contact) |
| | | 335 | | { |
| | 10 | 336 | | Span<FixedPointAnchor2d> capsuleAnchors = |
| | 10 | 337 | | stackalloc FixedPointAnchor2d[2]; |
| | 10 | 338 | | Span<FixedPointAnchor2d> convexAnchors = |
| | 10 | 339 | | stackalloc FixedPointAnchor2d[2]; |
| | 10 | 340 | | if (!TryGetCapsuleConvexContacts( |
| | 10 | 341 | | capsule, |
| | 10 | 342 | | convex, |
| | 10 | 343 | | capsuleAnchors, |
| | 10 | 344 | | convexAnchors, |
| | 10 | 345 | | out _, |
| | 10 | 346 | | out Vector2d normal, |
| | 10 | 347 | | out Fixed64 depth, |
| | 10 | 348 | | out bool depthIsClamped)) |
| | | 349 | | { |
| | 3 | 350 | | contact = default; |
| | 3 | 351 | | return false; |
| | | 352 | | } |
| | | 353 | | |
| | 7 | 354 | | contact = new Contact2D( |
| | 7 | 355 | | new ContactAnchor2D(capsuleAnchors[0]), |
| | 7 | 356 | | new ContactAnchor2D(convexAnchors[0]), |
| | 7 | 357 | | normal, |
| | 7 | 358 | | depth, |
| | 7 | 359 | | depthIsClamped); |
| | 7 | 360 | | return true; |
| | | 361 | | } |
| | | 362 | | |
| | | 363 | | private static bool TryGetCapsuleConvexContacts( |
| | | 364 | | LSCapsuleCollider2D capsule, |
| | | 365 | | LSCollider2D convex, |
| | | 366 | | Span<FixedPointAnchor2d> capsuleAnchors, |
| | | 367 | | Span<FixedPointAnchor2d> convexAnchors, |
| | | 368 | | out int contactCount, |
| | | 369 | | out Vector2d normal, |
| | | 370 | | out Fixed64 depth, |
| | | 371 | | out bool depthIsClamped) |
| | | 372 | | { |
| | 29 | 373 | | Span<Vector2d> vertexOffsets = stackalloc Vector2d[4]; |
| | 29 | 374 | | ReadOnlySpan<Vector2d> convexVertexOffsets = |
| | 29 | 375 | | GetConvexVertexOffsets(convex, vertexOffsets); |
| | 29 | 376 | | return FixedSegment2d.TryGetCenteredCapsuleConvexContacts( |
| | 29 | 377 | | capsule.Center, |
| | 29 | 378 | | capsule.Rotation, |
| | 29 | 379 | | Vector2d.Forward, |
| | 29 | 380 | | capsule.AxisLength, |
| | 29 | 381 | | capsule.ScaledRadius, |
| | 29 | 382 | | convex.Center, |
| | 29 | 383 | | convex.ConvexRotation, |
| | 29 | 384 | | convexVertexOffsets, |
| | 29 | 385 | | capsuleAnchors, |
| | 29 | 386 | | convexAnchors, |
| | 29 | 387 | | out contactCount, |
| | 29 | 388 | | out normal, |
| | 29 | 389 | | out depth, |
| | 29 | 390 | | out depthIsClamped); |
| | | 391 | | } |
| | | 392 | | |
| | | 393 | | private static ReadOnlySpan<Vector2d> GetConvexVertexOffsets( |
| | | 394 | | LSCollider2D convex, |
| | | 395 | | Span<Vector2d> scratch) |
| | | 396 | | { |
| | 1101 | 397 | | if (convex is LSPolygonCollider2D polygon) |
| | 899 | 398 | | return polygon.ScaledLocalVertices; |
| | | 399 | | |
| | 202 | 400 | | int vertexCount = convex.VertexCount; |
| | 2020 | 401 | | for (int i = 0; i < vertexCount; i++) |
| | 808 | 402 | | scratch[i] = convex.GetScaledLocalVertexUnchecked(i); |
| | 202 | 403 | | return scratch.Slice(0, vertexCount); |
| | | 404 | | } |
| | | 405 | | |
| | | 406 | | private static bool TryCapsuleConvex( |
| | | 407 | | LSCapsuleCollider2D capsule, |
| | | 408 | | LSCollider2D convex, |
| | | 409 | | ContactManifold2D manifold) |
| | | 410 | | { |
| | 10 | 411 | | Span<FixedPointAnchor2d> capsuleAnchors = |
| | 10 | 412 | | stackalloc FixedPointAnchor2d[2]; |
| | 10 | 413 | | Span<FixedPointAnchor2d> convexAnchors = |
| | 10 | 414 | | stackalloc FixedPointAnchor2d[2]; |
| | 10 | 415 | | if (!TryGetCapsuleConvexContacts( |
| | 10 | 416 | | capsule, |
| | 10 | 417 | | convex, |
| | 10 | 418 | | capsuleAnchors, |
| | 10 | 419 | | convexAnchors, |
| | 10 | 420 | | out int contactCount, |
| | 10 | 421 | | out Vector2d normal, |
| | 10 | 422 | | out Fixed64 depth, |
| | 10 | 423 | | out bool depthIsClamped)) |
| | | 424 | | { |
| | 1 | 425 | | return false; |
| | | 426 | | } |
| | | 427 | | |
| | 48 | 428 | | for (int i = 0; i < contactCount; i++) |
| | | 429 | | { |
| | 15 | 430 | | manifold.AddContact( |
| | 15 | 431 | | new ContactAnchor2D(capsuleAnchors[i]), |
| | 15 | 432 | | new ContactAnchor2D(convexAnchors[i]), |
| | 15 | 433 | | depth, |
| | 15 | 434 | | normal, |
| | 15 | 435 | | capsule.Material, |
| | 15 | 436 | | convex.Material, |
| | 15 | 437 | | depthIsClamped); |
| | | 438 | | } |
| | 9 | 439 | | return true; |
| | | 440 | | } |
| | | 441 | | |
| | | 442 | | private static bool TryCapsuleConvexReversed( |
| | | 443 | | LSCapsuleCollider2D capsule, |
| | | 444 | | LSCollider2D convex, |
| | | 445 | | ContactManifold2D manifold) |
| | | 446 | | { |
| | 9 | 447 | | Span<FixedPointAnchor2d> capsuleAnchors = |
| | 9 | 448 | | stackalloc FixedPointAnchor2d[2]; |
| | 9 | 449 | | Span<FixedPointAnchor2d> convexAnchors = |
| | 9 | 450 | | stackalloc FixedPointAnchor2d[2]; |
| | 9 | 451 | | if (!TryGetCapsuleConvexContacts( |
| | 9 | 452 | | capsule, |
| | 9 | 453 | | convex, |
| | 9 | 454 | | capsuleAnchors, |
| | 9 | 455 | | convexAnchors, |
| | 9 | 456 | | out int contactCount, |
| | 9 | 457 | | out Vector2d normal, |
| | 9 | 458 | | out Fixed64 depth, |
| | 9 | 459 | | out bool depthIsClamped)) |
| | | 460 | | { |
| | 1 | 461 | | return false; |
| | | 462 | | } |
| | | 463 | | |
| | 42 | 464 | | for (int i = 0; i < contactCount; i++) |
| | | 465 | | { |
| | 13 | 466 | | manifold.AddContact( |
| | 13 | 467 | | new ContactAnchor2D(convexAnchors[i]), |
| | 13 | 468 | | new ContactAnchor2D(capsuleAnchors[i]), |
| | 13 | 469 | | depth, |
| | 13 | 470 | | -normal, |
| | 13 | 471 | | convex.Material, |
| | 13 | 472 | | capsule.Material, |
| | 13 | 473 | | depthIsClamped); |
| | | 474 | | } |
| | 8 | 475 | | return true; |
| | | 476 | | } |
| | | 477 | | |
| | | 478 | | private static bool TryConvexConvex(LSCollider2D colliderA, LSCollider2D colliderB, out Contact2D contact) |
| | | 479 | | { |
| | 53 | 480 | | Span<Vector2d> firstVertexScratch = stackalloc Vector2d[4]; |
| | 53 | 481 | | Span<Vector2d> secondVertexScratch = stackalloc Vector2d[4]; |
| | 53 | 482 | | ReadOnlySpan<Vector2d> firstVertexOffsets = |
| | 53 | 483 | | GetConvexVertexOffsets(colliderA, firstVertexScratch); |
| | 53 | 484 | | ReadOnlySpan<Vector2d> secondVertexOffsets = |
| | 53 | 485 | | GetConvexVertexOffsets(colliderB, secondVertexScratch); |
| | 53 | 486 | | Span<FixedPointAnchor2d> firstContactAnchors = |
| | 53 | 487 | | stackalloc FixedPointAnchor2d[2]; |
| | 53 | 488 | | Span<FixedPointAnchor2d> secondContactAnchors = |
| | 53 | 489 | | stackalloc FixedPointAnchor2d[2]; |
| | 53 | 490 | | if (!FixedConvex2dRelations.TryGetConvexContacts( |
| | 53 | 491 | | colliderA.Center, |
| | 53 | 492 | | colliderA.ConvexRotation, |
| | 53 | 493 | | firstVertexOffsets, |
| | 53 | 494 | | colliderB.Center, |
| | 53 | 495 | | colliderB.ConvexRotation, |
| | 53 | 496 | | secondVertexOffsets, |
| | 53 | 497 | | firstContactAnchors, |
| | 53 | 498 | | secondContactAnchors, |
| | 53 | 499 | | out _, |
| | 53 | 500 | | out Vector2d normal, |
| | 53 | 501 | | out Fixed64 depth, |
| | 53 | 502 | | out bool depthIsClamped)) |
| | | 503 | | { |
| | 3 | 504 | | contact = default; |
| | 3 | 505 | | return false; |
| | | 506 | | } |
| | | 507 | | |
| | 50 | 508 | | contact = new Contact2D( |
| | 50 | 509 | | new ContactAnchor2D(colliderA.GetConvexSupportAnchor(normal)), |
| | 50 | 510 | | new ContactAnchor2D(colliderB.GetConvexSupportAnchor(-normal)), |
| | 50 | 511 | | normal, |
| | 50 | 512 | | depth, |
| | 50 | 513 | | depthIsClamped); |
| | 50 | 514 | | return true; |
| | | 515 | | } |
| | | 516 | | |
| | | 517 | | private static bool TryConvexConvex( |
| | | 518 | | LSCollider2D colliderA, |
| | | 519 | | LSCollider2D colliderB, |
| | | 520 | | ContactManifold2D manifold) |
| | | 521 | | { |
| | 25 | 522 | | Span<Vector2d> firstVertexScratch = stackalloc Vector2d[4]; |
| | 25 | 523 | | Span<Vector2d> secondVertexScratch = stackalloc Vector2d[4]; |
| | 25 | 524 | | ReadOnlySpan<Vector2d> firstVertexOffsets = |
| | 25 | 525 | | GetConvexVertexOffsets(colliderA, firstVertexScratch); |
| | 25 | 526 | | ReadOnlySpan<Vector2d> secondVertexOffsets = |
| | 25 | 527 | | GetConvexVertexOffsets(colliderB, secondVertexScratch); |
| | 25 | 528 | | Span<FixedPointAnchor2d> firstContactAnchors = |
| | 25 | 529 | | stackalloc FixedPointAnchor2d[2]; |
| | 25 | 530 | | Span<FixedPointAnchor2d> secondContactAnchors = |
| | 25 | 531 | | stackalloc FixedPointAnchor2d[2]; |
| | 25 | 532 | | if (!FixedConvex2dRelations.TryGetConvexContacts( |
| | 25 | 533 | | colliderA.Center, |
| | 25 | 534 | | colliderA.ConvexRotation, |
| | 25 | 535 | | firstVertexOffsets, |
| | 25 | 536 | | colliderB.Center, |
| | 25 | 537 | | colliderB.ConvexRotation, |
| | 25 | 538 | | secondVertexOffsets, |
| | 25 | 539 | | firstContactAnchors, |
| | 25 | 540 | | secondContactAnchors, |
| | 25 | 541 | | out int contactCount, |
| | 25 | 542 | | out Vector2d normal, |
| | 25 | 543 | | out Fixed64 depth, |
| | 25 | 544 | | out bool depthIsClamped)) |
| | | 545 | | { |
| | 1 | 546 | | return false; |
| | | 547 | | } |
| | | 548 | | |
| | 136 | 549 | | for (int i = 0; i < contactCount; i++) |
| | | 550 | | { |
| | 44 | 551 | | manifold.AddContact( |
| | 44 | 552 | | new ContactAnchor2D(firstContactAnchors[i]), |
| | 44 | 553 | | new ContactAnchor2D(secondContactAnchors[i]), |
| | 44 | 554 | | depth, |
| | 44 | 555 | | normal, |
| | 44 | 556 | | colliderA.Material, |
| | 44 | 557 | | colliderB.Material, |
| | 44 | 558 | | depthIsClamped); |
| | | 559 | | } |
| | | 560 | | |
| | 24 | 561 | | return true; |
| | | 562 | | } |
| | | 563 | | |
| | | 564 | | private static bool TryCompound(LSCollider2D colliderA, LSCollider2D colliderB, out Contact2D contact) |
| | | 565 | | { |
| | 10 | 566 | | if (colliderA is LSCompoundCollider2D compoundA) |
| | | 567 | | { |
| | 6 | 568 | | if (colliderB is LSCompoundCollider2D compoundB) |
| | 3 | 569 | | return TryCompoundCompound(compoundA, compoundB, out contact); |
| | | 570 | | |
| | 3 | 571 | | return TryCompoundOther(compoundA, colliderB, compoundIsA: true, out contact); |
| | | 572 | | } |
| | | 573 | | |
| | 4 | 574 | | if (colliderB is LSCompoundCollider2D compound) |
| | 3 | 575 | | return TryCompoundOther(compound, colliderA, compoundIsA: false, out contact); |
| | | 576 | | |
| | 1 | 577 | | contact = default; |
| | 1 | 578 | | return false; |
| | | 579 | | } |
| | | 580 | | |
| | | 581 | | private static bool TryCompoundCompound( |
| | | 582 | | LSCompoundCollider2D compoundA, |
| | | 583 | | LSCompoundCollider2D compoundB, |
| | | 584 | | out Contact2D contact) |
| | | 585 | | { |
| | 3 | 586 | | bool found = false; |
| | 3 | 587 | | Contact2D best = default; |
| | | 588 | | |
| | 16 | 589 | | for (int i = 0; i < compoundA.PartCount; i++) |
| | | 590 | | { |
| | 5 | 591 | | LSCollider2D partA = compoundA.GetPartCollider(i); |
| | 26 | 592 | | for (int j = 0; j < compoundB.PartCount; j++) |
| | | 593 | | { |
| | 8 | 594 | | LSCollider2D partB = compoundB.GetPartCollider(j); |
| | 8 | 595 | | if (!TryCollide(partA, partB, out Contact2D candidate)) |
| | | 596 | | continue; |
| | | 597 | | |
| | 3 | 598 | | if (ContactSelectionPolicy.ShouldReplaceWithDeeper(candidate, found, best)) |
| | | 599 | | { |
| | 2 | 600 | | best = candidate; |
| | 2 | 601 | | found = true; |
| | | 602 | | } |
| | | 603 | | } |
| | | 604 | | } |
| | | 605 | | |
| | 3 | 606 | | if (!found) |
| | | 607 | | { |
| | 1 | 608 | | contact = default; |
| | 1 | 609 | | return false; |
| | | 610 | | } |
| | | 611 | | |
| | 2 | 612 | | contact = best; |
| | 2 | 613 | | return true; |
| | | 614 | | } |
| | | 615 | | |
| | | 616 | | private static bool TryCompoundOther( |
| | | 617 | | LSCompoundCollider2D compound, |
| | | 618 | | LSCollider2D other, |
| | | 619 | | bool compoundIsA, |
| | | 620 | | out Contact2D contact) |
| | | 621 | | { |
| | 6 | 622 | | bool found = false; |
| | 6 | 623 | | Contact2D best = default; |
| | | 624 | | |
| | 32 | 625 | | for (int i = 0; i < compound.PartCount; i++) |
| | | 626 | | { |
| | 10 | 627 | | LSCollider2D part = compound.GetPartCollider(i); |
| | | 628 | | Contact2D candidate; |
| | 10 | 629 | | bool collided = compoundIsA |
| | 10 | 630 | | ? TryCollide(part, other, out candidate) |
| | 10 | 631 | | : TryCollide(other, part, out candidate); |
| | | 632 | | |
| | 10 | 633 | | if (!collided) |
| | | 634 | | continue; |
| | | 635 | | |
| | 5 | 636 | | if (ContactSelectionPolicy.ShouldReplaceWithDeeper(candidate, found, best)) |
| | | 637 | | { |
| | 4 | 638 | | best = candidate; |
| | 4 | 639 | | found = true; |
| | | 640 | | } |
| | | 641 | | } |
| | | 642 | | |
| | 6 | 643 | | if (!found) |
| | | 644 | | { |
| | 2 | 645 | | contact = default; |
| | 2 | 646 | | return false; |
| | | 647 | | } |
| | | 648 | | |
| | 4 | 649 | | contact = best; |
| | 4 | 650 | | return true; |
| | | 651 | | } |
| | | 652 | | |
| | | 653 | | private static bool TryCompound( |
| | | 654 | | LSCollider2D colliderA, |
| | | 655 | | LSCollider2D colliderB, |
| | | 656 | | ContactManifold2D manifold) |
| | | 657 | | { |
| | 17 | 658 | | if (colliderA is LSCompoundCollider2D compoundA) |
| | | 659 | | { |
| | 14 | 660 | | if (colliderB is LSCompoundCollider2D compoundB) |
| | 4 | 661 | | return TryCompoundCompound(compoundA, compoundB, manifold); |
| | | 662 | | |
| | 10 | 663 | | return TryCompoundOther(compoundA, colliderB, compoundIsA: true, manifold); |
| | | 664 | | } |
| | | 665 | | |
| | 3 | 666 | | if (colliderB is LSCompoundCollider2D compound) |
| | 2 | 667 | | return TryCompoundOther(compound, colliderA, compoundIsA: false, manifold); |
| | | 668 | | |
| | 1 | 669 | | return false; |
| | | 670 | | } |
| | | 671 | | |
| | | 672 | | private static bool TryCompoundCompound( |
| | | 673 | | LSCompoundCollider2D compoundA, |
| | | 674 | | LSCompoundCollider2D compoundB, |
| | | 675 | | ContactManifold2D manifold) |
| | | 676 | | { |
| | 4 | 677 | | bool found = false; |
| | 4 | 678 | | ContactManifold2D scratch = compoundA.PartManifoldScratch; |
| | 22 | 679 | | for (int i = 0; i < compoundA.PartCount; i++) |
| | | 680 | | { |
| | 7 | 681 | | LSCollider2D partA = compoundA.GetPartCollider(i); |
| | 32 | 682 | | for (int j = 0; j < compoundB.PartCount; j++) |
| | | 683 | | { |
| | 9 | 684 | | LSCollider2D partB = compoundB.GetPartCollider(j); |
| | 9 | 685 | | CollisionType2D collisionType = ColliderSettings2D.GetCollisionType(partA.Shape, partB.Shape); |
| | 9 | 686 | | scratch.BeginUpdate(manifold.LastUpdatedFrame); |
| | 9 | 687 | | if (!TryCollide(new CollisionWorkItem2D(partA, partB, collisionType), scratch)) |
| | | 688 | | continue; |
| | | 689 | | |
| | 2 | 690 | | found = true; |
| | 2 | 691 | | AddCompoundPartContacts( |
| | 2 | 692 | | manifold, |
| | 2 | 693 | | scratch, |
| | 2 | 694 | | featureNamespaceA: i + 1, |
| | 2 | 695 | | featureNamespaceB: -(j + 1)); |
| | | 696 | | } |
| | | 697 | | } |
| | | 698 | | |
| | 4 | 699 | | return found; |
| | | 700 | | } |
| | | 701 | | |
| | | 702 | | private static bool TryCompoundOther( |
| | | 703 | | LSCompoundCollider2D compound, |
| | | 704 | | LSCollider2D other, |
| | | 705 | | bool compoundIsA, |
| | | 706 | | ContactManifold2D manifold) |
| | | 707 | | { |
| | 12 | 708 | | bool found = false; |
| | 12 | 709 | | ContactManifold2D scratch = compound.PartManifoldScratch; |
| | 62 | 710 | | for (int i = 0; i < compound.PartCount; i++) |
| | | 711 | | { |
| | 19 | 712 | | LSCollider2D part = compound.GetPartCollider(i); |
| | 19 | 713 | | LSCollider2D colliderA = compoundIsA ? part : other; |
| | 19 | 714 | | LSCollider2D colliderB = compoundIsA ? other : part; |
| | 19 | 715 | | CollisionType2D collisionType = ColliderSettings2D.GetCollisionType(colliderA.Shape, colliderB.Shape); |
| | 19 | 716 | | scratch.BeginUpdate(manifold.LastUpdatedFrame); |
| | 19 | 717 | | if (!TryCollide(new CollisionWorkItem2D(colliderA, colliderB, collisionType), scratch)) |
| | | 718 | | continue; |
| | | 719 | | |
| | 16 | 720 | | found = true; |
| | 16 | 721 | | AddCompoundPartContacts( |
| | 16 | 722 | | manifold, |
| | 16 | 723 | | scratch, |
| | 16 | 724 | | featureNamespaceA: compoundIsA ? i + 1 : 0, |
| | 16 | 725 | | featureNamespaceB: compoundIsA ? 0 : -(i + 1)); |
| | | 726 | | } |
| | | 727 | | |
| | 12 | 728 | | return found; |
| | | 729 | | } |
| | | 730 | | |
| | | 731 | | private static void AddCompoundPartContacts( |
| | | 732 | | ContactManifold2D destination, |
| | | 733 | | ContactManifold2D source, |
| | | 734 | | int featureNamespaceA, |
| | | 735 | | int featureNamespaceB) |
| | | 736 | | { |
| | 76 | 737 | | for (int i = 0; i < source.Count; i++) |
| | | 738 | | { |
| | 20 | 739 | | ManifoldContact2D contact = source[i]; |
| | 20 | 740 | | destination.AddContact( |
| | 20 | 741 | | contact.AnchorA, |
| | 20 | 742 | | contact.AnchorB, |
| | 20 | 743 | | contact.Depth, |
| | 20 | 744 | | contact.Normal, |
| | 20 | 745 | | contact.MaterialA, |
| | 20 | 746 | | contact.MaterialB, |
| | 20 | 747 | | contact.DepthIsClamped, |
| | 20 | 748 | | featureNamespaceA, |
| | 20 | 749 | | featureNamespaceB); |
| | | 750 | | } |
| | 18 | 751 | | } |
| | | 752 | | |
| | | 753 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 754 | | private static void AddContact( |
| | | 755 | | ContactManifold2D manifold, |
| | | 756 | | Contact2D contact, |
| | | 757 | | LSCollider2D colliderA, |
| | | 758 | | LSCollider2D colliderB) => |
| | 464 | 759 | | manifold.AddContact( |
| | 464 | 760 | | contact.AnchorA, |
| | 464 | 761 | | contact.AnchorB, |
| | 464 | 762 | | contact.Depth, |
| | 464 | 763 | | contact.Normal, |
| | 464 | 764 | | colliderA.Material, |
| | 464 | 765 | | colliderB.Material, |
| | 464 | 766 | | contact.DepthIsClamped); |
| | | 767 | | |
| | | 768 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 769 | | internal static bool BoundsOverlap(LSCollider2D colliderA, LSCollider2D colliderB) => |
| | 10356 | 770 | | colliderA.MinX <= colliderB.MaxX |
| | 10356 | 771 | | && colliderA.MaxX >= colliderB.MinX |
| | 10356 | 772 | | && colliderA.MinY <= colliderB.MaxY |
| | 10356 | 773 | | && colliderA.MaxY >= colliderB.MinY; |
| | | 774 | | |
| | | 775 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 776 | | private static Vector2d OrientCoincidentCapsuleNormal(Vector2d direction) => |
| | 9 | 777 | | direction.MagnitudeSquared > Fixed64.Epsilon && direction.X < Fixed64.Zero |
| | 9 | 778 | | ? -Vector2d.Right |
| | 9 | 779 | | : Vector2d.Right; |
| | | 780 | | |
| | | 781 | | } |