| | | 1 | | //======================================================================= |
| | | 2 | | // CollisionPair2D.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 Gravitas.Colliders; |
| | | 11 | | using Gravitas.CollisionHandling; |
| | | 12 | | using SwiftCollections; |
| | | 13 | | |
| | | 14 | | namespace Gravitas; |
| | | 15 | | |
| | | 16 | | /// <summary>Owns one deterministic pure 2D contact pair and its lifecycle.</summary> |
| | | 17 | | /// <content>Owns pair identity, contact notifications, warm starts and collision-pass freshness.</content> |
| | | 18 | | internal sealed partial class CollisionPair2D |
| | | 19 | | { |
| | | 20 | | private bool _isColliding; |
| | | 21 | | private ContactWarmStartCache2D _warmStart; |
| | | 22 | | internal GroupedSolveState? ResponseState; |
| | | 23 | | private bool _notificationInProgress; |
| | | 24 | | private bool _separationPending; |
| | | 25 | | private bool _colliderANotified; |
| | | 26 | | private bool _colliderBNotified; |
| | | 27 | | private SolidBody2D? _pendingBodyA; |
| | | 28 | | private SolidBody2D? _pendingBodyB; |
| | | 29 | | private long _lifetimeVersion; |
| | | 30 | | private uint _lastColliderAContactDependencyVersion; |
| | | 31 | | private uint _lastColliderBContactDependencyVersion; |
| | | 32 | | private bool _surfaceContactChanged; |
| | | 33 | | private ulong? _surfaceCollisionLayers; |
| | | 34 | | private bool _wasTriggerPair; |
| | | 35 | | private SolidBody2D? _recoveryBody; |
| | | 36 | | private Vector2d _recoveryPosition; |
| | | 37 | | |
| | 745 | 38 | | public CollisionPair2D(LSCollider2D colliderA, LSCollider2D colliderB) |
| | | 39 | | { |
| | 745 | 40 | | ColliderA = colliderA; |
| | 745 | 41 | | ColliderB = colliderB; |
| | 745 | 42 | | Initialize(colliderA, colliderB); |
| | 745 | 43 | | } |
| | | 44 | | |
| | | 45 | | public LSCollider2D ColliderA { get; private set; } |
| | | 46 | | |
| | | 47 | | public LSCollider2D ColliderB { get; private set; } |
| | | 48 | | |
| | | 49 | | public int Id1 { get; private set; } |
| | | 50 | | |
| | | 51 | | public int Id2 { get; private set; } |
| | | 52 | | |
| | | 53 | | public CollisionType2D CollisionType { get; private set; } |
| | | 54 | | |
| | | 55 | | public long LastFrame { get; private set; } = -1; |
| | | 56 | | |
| | | 57 | | // Collision-pass freshness is independent of hosts advancing the public frame clock. |
| | | 58 | | internal long LastVisitToken { get; private set; } = -1; |
| | | 59 | | |
| | 55 | 60 | | public bool IsColliding => _isColliding; |
| | | 61 | | |
| | 619 | 62 | | internal bool IsNotificationInProgress => _notificationInProgress; |
| | | 63 | | |
| | 11449 | 64 | | internal long LifetimeVersion => _lifetimeVersion; |
| | | 65 | | |
| | | 66 | | internal bool ContactDependenciesChanged => |
| | 5818 | 67 | | _surfaceContactChanged || _lastColliderAContactDependencyVersion != ColliderA.ContactDependencyVersion |
| | 5818 | 68 | | || _lastColliderBContactDependencyVersion != ColliderB.ContactDependencyVersion |
| | 5818 | 69 | | || _surfaceCollisionLayers.HasValue && _surfaceCollisionLayers != CurrentSurfaceCollisionLayers; |
| | | 70 | | |
| | | 71 | | private ulong? CurrentSurfaceCollisionLayers |
| | | 72 | | { |
| | | 73 | | get |
| | | 74 | | { |
| | 9930 | 75 | | if (ColliderA.IsTrigger || ColliderB.IsTrigger) return null; |
| | 9756 | 76 | | LSCollider2D? mover = ColliderA.StaticSurface != null ? ColliderB |
| | 9756 | 77 | | : ColliderB.StaticSurface != null ? ColliderA : null; |
| | 9756 | 78 | | return mover == null ? null : ColliderA.Context.Settings.GetCollisionLayerMask(mover.Layer, default, true, f |
| | | 79 | | } |
| | | 80 | | } |
| | | 81 | | |
| | | 82 | | internal (ulong? Layers, ContactManifold2D? Before, Vector2d? Recovery) BeginCollisionLayerUpdate(ContactManifold2D |
| | | 83 | | { |
| | 7073 | 84 | | ulong? layers = CurrentSurfaceCollisionLayers; |
| | 7073 | 85 | | if (!_isColliding || !layers.HasValue || !_surfaceCollisionLayers.HasValue || layers == _surfaceCollisionLayers) |
| | 7065 | 86 | | return (layers, null, null); |
| | 8 | 87 | | ContactManifold2D before = ColliderA.Context.CollisionScratch.SurfaceContactSnapshot2D; |
| | 8 | 88 | | before.CopyFrom(target, target.LastUpdatedFrame); |
| | 8 | 89 | | return (layers, before, HasRecovery ? _recoveryPosition : null); |
| | | 90 | | } |
| | | 91 | | |
| | | 92 | | internal void CompleteCollisionLayerUpdate(ContactManifold2D target, |
| | | 93 | | (ulong? Layers, ContactManifold2D? Before, Vector2d? Recovery) previous) |
| | | 94 | | { |
| | 7070 | 95 | | if (previous.Before != null) |
| | | 96 | | { |
| | 8 | 97 | | Vector2d? recovery = HasRecovery ? _recoveryPosition : null; |
| | 8 | 98 | | if (!previous.Before.HasSameContacts(target) || previous.Recovery != recovery) |
| | | 99 | | { |
| | 6 | 100 | | ClearWarmStart(); ClearResponseSnapshot(); |
| | 3 | 101 | | WakeBodiesForContactChange(); |
| | | 102 | | } |
| | | 103 | | } |
| | 7070 | 104 | | _surfaceCollisionLayers = previous.Layers; |
| | 7070 | 105 | | } |
| | | 106 | | |
| | | 107 | | public ContactManifold2D Manifold { get; } = new(); |
| | | 108 | | |
| | 888 | 109 | | internal bool HasRecovery => _recoveryBody != null; |
| | | 110 | | |
| | 6286 | 111 | | internal bool HasResponse => Manifold.HasContact || HasRecovery; |
| | | 112 | | |
| | 8454 | 113 | | internal void ClearRecovery() => _recoveryBody = null; |
| | | 114 | | |
| | | 115 | | internal void SetRecovery(LSCollider2D mover, Vector2d center) |
| | | 116 | | { |
| | 19 | 117 | | SolidBody2D body = mover.Body!; |
| | | 118 | | // The immediately preceding certificate proved these bounded operations |
| | | 119 | | // and the actual publishable body pose; no callback intervenes here. |
| | 19 | 120 | | _recoveryBody = body; |
| | 19 | 121 | | _recoveryPosition = body.Position + (center - mover.Center); |
| | 19 | 122 | | } |
| | | 123 | | |
| | | 124 | | internal void ApplyRecovery() |
| | | 125 | | { |
| | 11316 | 126 | | if (_recoveryBody is not { } body) return; |
| | 14 | 127 | | if (!Vector2d.TrySubtract(_recoveryPosition, body.Position, out Vector2d delta) |
| | 14 | 128 | | || body.PrepareCollisionPositionCorrection(delta) != _recoveryPosition) |
| | 2 | 129 | | throw new InvalidOperationException("Pure 2D static-surface recovery is outside the movable body's supported |
| | 12 | 130 | | StaticSurface2D? surface = ColliderA.StaticSurface ?? ColliderB.StaticSurface; |
| | | 131 | | // An earlier island response may have moved a final-pose blocker. |
| | 12 | 132 | | if (surface == null || !Vector2d.TryAdd(body.Collider.Center, delta, out Vector2d center) |
| | 12 | 133 | | || !surface.IsRecoveryClear(body.Collider, center)) |
| | 6 | 134 | | throw new InvalidOperationException("Pure 2D static-surface recovery no longer has a certified clear final p |
| | 6 | 135 | | ClearRecovery(); |
| | 6 | 136 | | body.ApplyCollisionPositionState(_recoveryPosition); |
| | 6 | 137 | | } |
| | | 138 | | |
| | | 139 | | public void Initialize(LSCollider2D colliderA, LSCollider2D colliderB) |
| | | 140 | | { |
| | 1192 | 141 | | _lifetimeVersion++; |
| | 1192 | 142 | | AssignPriority(colliderA, colliderB); |
| | 1192 | 143 | | Id1 = ColliderA.Id; |
| | 1192 | 144 | | Id2 = ColliderB.Id; |
| | 1192 | 145 | | CollisionType = ColliderSettings2D.GetCollisionType(ColliderA.Shape, ColliderB.Shape); |
| | 1192 | 146 | | ResetPairState(); |
| | 1192 | 147 | | } |
| | | 148 | | |
| | | 149 | | private void AssignPriority(LSCollider2D colliderA, LSCollider2D colliderB) |
| | | 150 | | { |
| | 1192 | 151 | | if (ShouldFirstColliderLead(colliderA, colliderB)) |
| | | 152 | | { |
| | 484 | 153 | | ColliderA = colliderA; |
| | 484 | 154 | | ColliderB = colliderB; |
| | 484 | 155 | | return; |
| | | 156 | | } |
| | | 157 | | |
| | 708 | 158 | | ColliderA = colliderB; |
| | 708 | 159 | | ColliderB = colliderA; |
| | 708 | 160 | | } |
| | | 161 | | |
| | | 162 | | internal static bool ShouldFirstColliderLead(LSCollider2D colliderA, LSCollider2D colliderB) |
| | | 163 | | { |
| | 1192 | 164 | | if (colliderA.Priority != colliderB.Priority) |
| | 622 | 165 | | return colliderA.Priority > colliderB.Priority; |
| | | 166 | | |
| | 570 | 167 | | SolidBody2D? bodyA = colliderA.Body; |
| | 570 | 168 | | SolidBody2D? bodyB = colliderB.Body; |
| | 570 | 169 | | if (bodyA == null || bodyB == null) |
| | 165 | 170 | | return colliderA.Id <= colliderB.Id; |
| | | 171 | | |
| | 405 | 172 | | if (bodyA.LinearSpeed != bodyB.LinearSpeed) |
| | 64 | 173 | | return bodyA.LinearSpeed > bodyB.LinearSpeed; |
| | | 174 | | |
| | 341 | 175 | | return colliderA.Id <= colliderB.Id; |
| | | 176 | | } |
| | | 177 | | |
| | | 178 | | public void MarkColliding(long frame) |
| | | 179 | | { |
| | 113 | 180 | | if (!Manifold.HasContact && ColliderA.StaticSurface == null && ColliderB.StaticSurface == null) |
| | 1 | 181 | | return; |
| | | 182 | | |
| | 112 | 183 | | bool changed = MarkCollidingState(frame); |
| | | 184 | | |
| | 112 | 185 | | if (!ColliderA.IsTrigger && !ColliderB.IsTrigger) |
| | | 186 | | { |
| | 30 | 187 | | CollisionResponse2D.Resolve(this); |
| | 30 | 188 | | WakeSleepingBodiesForCollision(); |
| | | 189 | | } |
| | | 190 | | |
| | 112 | 191 | | NotifyColliders(isColliding: true, changed); |
| | 112 | 192 | | } |
| | | 193 | | |
| | | 194 | | internal void MarkCollidingDeferred(long frame) |
| | | 195 | | { |
| | 5816 | 196 | | if (!Manifold.HasContact && ColliderA.StaticSurface == null && ColliderB.StaticSurface == null) |
| | 1 | 197 | | return; |
| | | 198 | | |
| | 5815 | 199 | | bool changed = MarkCollidingState(frame); |
| | 5815 | 200 | | NotifyColliders(isColliding: true, changed); |
| | 5813 | 201 | | } |
| | | 202 | | |
| | | 203 | | public void MarkResting(long frame) |
| | | 204 | | { |
| | 659 | 205 | | RefreshContactDependencies(); |
| | 662 | 206 | | if (!Manifold.HasContact) ClearWarmStart(); |
| | 659 | 207 | | LastFrame = frame; |
| | 659 | 208 | | LastVisitToken = ColliderA.Context.LateSimulateToken; |
| | 659 | 209 | | } |
| | | 210 | | |
| | | 211 | | public void MarkSeparated() |
| | | 212 | | { |
| | 185 | 213 | | bool wasColliding = _isColliding; |
| | 185 | 214 | | if (wasColliding && !_wasTriggerPair) |
| | 164 | 215 | | WakeBodiesForContactChange(); |
| | 185 | 216 | | ResetCollisionState(); |
| | 185 | 217 | | if (!wasColliding) |
| | 2 | 218 | | return; |
| | | 219 | | |
| | 183 | 220 | | if (_notificationInProgress) |
| | | 221 | | { |
| | 13 | 222 | | _separationPending = true; |
| | 13 | 223 | | _pendingBodyA = ColliderA.Body; |
| | 13 | 224 | | _pendingBodyB = ColliderB.Body; |
| | 13 | 225 | | return; |
| | | 226 | | } |
| | | 227 | | |
| | 170 | 228 | | NotifyColliders(isColliding: false, isChanged: true); |
| | 167 | 229 | | } |
| | | 230 | | |
| | | 231 | | internal void StoreWarmStartImpulse(in ContactGroupKey group, ulong contactId, |
| | | 232 | | Fixed64 normalImpulse, Fixed64 tangentImpulse) => |
| | 16784 | 233 | | _warmStart.Set(group, contactId, normalImpulse, tangentImpulse); |
| | | 234 | | |
| | | 235 | | internal bool TryGetWarmStartImpulse(in ContactGroupKey group, ulong contactId, |
| | 17816 | 236 | | out ContactWarmStartImpulse impulse) => _warmStart.TryGet(group, contactId, out impulse); |
| | | 237 | | |
| | | 238 | | internal void RemoveWarmStartImpulse(in ContactGroupKey group, ulong contactId) => |
| | 11 | 239 | | _warmStart.Remove(group, contactId); |
| | | 240 | | |
| | 9813 | 241 | | internal void RetainWarmStart() => _warmStart.Retain(Manifold); |
| | | 242 | | |
| | | 243 | | internal void ClearResponseSnapshot() |
| | | 244 | | { |
| | 2062 | 245 | | if (ResponseState != null) ResponseState.Impact = default; |
| | 1666 | 246 | | } |
| | | 247 | | |
| | 180 | 248 | | internal void ClearWarmStart() => _warmStart.Clear(); |
| | | 249 | | |
| | | 250 | | internal void InvalidateSurfaceContact(LSCollider2D member, SwiftHashSet<int> suppliers) |
| | | 251 | | { |
| | 88 | 252 | | if (!Manifold.ReferencesSuppliers(suppliers, ColliderA == member, member.Id + 1)) return; |
| | 60 | 253 | | _surfaceContactChanged = true; |
| | 60 | 254 | | ClearWarmStart(); |
| | 60 | 255 | | ClearResponseSnapshot(); |
| | 118 | 256 | | if (_isColliding && !_wasTriggerPair) WakeBodiesForContactChange(); |
| | 60 | 257 | | } |
| | | 258 | | |
| | | 259 | | private void ResetPairState() |
| | | 260 | | { |
| | 1192 | 261 | | ResetCollisionState(); |
| | 1192 | 262 | | _notificationInProgress = false; |
| | 1192 | 263 | | _colliderANotified = false; |
| | 1192 | 264 | | _colliderBNotified = false; |
| | 1192 | 265 | | ClearPendingNotificationState(); |
| | 1192 | 266 | | } |
| | | 267 | | |
| | | 268 | | private void ResetCollisionState() |
| | | 269 | | { |
| | 1377 | 270 | | ClearRecovery(); |
| | 1377 | 271 | | ClearResponseSnapshot(); |
| | 1377 | 272 | | _isColliding = false; |
| | 1377 | 273 | | _lastColliderAContactDependencyVersion = 0; |
| | 1377 | 274 | | _lastColliderBContactDependencyVersion = 0; |
| | 1377 | 275 | | _surfaceContactChanged = false; |
| | 1377 | 276 | | _surfaceCollisionLayers = null; |
| | 1377 | 277 | | _wasTriggerPair = false; |
| | 1377 | 278 | | LastFrame = -1; |
| | 1377 | 279 | | LastVisitToken = -1; |
| | 1377 | 280 | | Manifold.Reset(); |
| | 1377 | 281 | | _warmStart.Clear(); |
| | 1377 | 282 | | } |
| | | 283 | | |
| | | 284 | | private void NotifyColliders(bool isColliding, bool isChanged) |
| | | 285 | | { |
| | 6097 | 286 | | var registrationA = new ColliderLifetimeToken2D(ColliderA); |
| | 6097 | 287 | | var registrationB = new ColliderLifetimeToken2D(ColliderB); |
| | 6097 | 288 | | LSCollider2D colliderA = registrationA.Collider; |
| | 6097 | 289 | | LSCollider2D colliderB = registrationB.Collider; |
| | 6097 | 290 | | SolidBody2D? bodyA = colliderA.Body; |
| | 6097 | 291 | | SolidBody2D? bodyB = colliderB.Body; |
| | 6097 | 292 | | bool isTriggerPair = colliderA.IsTrigger || colliderB.IsTrigger; |
| | 6097 | 293 | | bool shouldRaiseTriggerA = isTriggerPair && ColliderTriggerEventPolicy.ShouldRaise(colliderA, colliderB); |
| | 6097 | 294 | | bool shouldRaiseTriggerB = isTriggerPair && ColliderTriggerEventPolicy.ShouldRaise(colliderB, colliderA); |
| | 6097 | 295 | | _notificationInProgress = true; |
| | 6097 | 296 | | SwiftList<Exception>? notificationExceptions = null; |
| | | 297 | | try |
| | | 298 | | { |
| | 6097 | 299 | | if (isColliding) |
| | | 300 | | { |
| | 5927 | 301 | | bool notifyEnterA = isChanged || !_colliderANotified; |
| | 5927 | 302 | | _colliderANotified = true; |
| | 5927 | 303 | | colliderA.NotifyContact( |
| | 5927 | 304 | | colliderB, |
| | 5927 | 305 | | bodyB, |
| | 5927 | 306 | | isColliding: true, |
| | 5927 | 307 | | notifyEnterA, |
| | 5927 | 308 | | allowInactive: false, |
| | 5927 | 309 | | registrationA, |
| | 5927 | 310 | | registrationB, |
| | 5927 | 311 | | isTriggerPair, |
| | 5927 | 312 | | shouldRaiseTriggerA); |
| | 5926 | 313 | | if (registrationA.IsActive && registrationB.IsActive && !_separationPending) |
| | | 314 | | { |
| | 5916 | 315 | | bool notifyEnterB = isChanged || !_colliderBNotified; |
| | 5916 | 316 | | _colliderBNotified = true; |
| | 5916 | 317 | | colliderB.NotifyContact( |
| | 5916 | 318 | | colliderA, |
| | 5916 | 319 | | bodyA, |
| | 5916 | 320 | | isColliding: true, |
| | 5916 | 321 | | notifyEnterB, |
| | 5916 | 322 | | allowInactive: false, |
| | 5916 | 323 | | registrationB, |
| | 5916 | 324 | | registrationA, |
| | 5916 | 325 | | isTriggerPair, |
| | 5916 | 326 | | shouldRaiseTriggerB); |
| | | 327 | | } |
| | | 328 | | } |
| | | 329 | | else |
| | | 330 | | { |
| | 170 | 331 | | NotifySeparation( |
| | 170 | 332 | | registrationA, |
| | 170 | 333 | | registrationB, |
| | 170 | 334 | | bodyA, |
| | 170 | 335 | | bodyB, |
| | 170 | 336 | | isChanged, |
| | 170 | 337 | | allowInactive: false, |
| | 170 | 338 | | isTriggerPair, |
| | 170 | 339 | | shouldRaiseTriggerA, |
| | 170 | 340 | | shouldRaiseTriggerB); |
| | | 341 | | } |
| | 6093 | 342 | | } |
| | 4 | 343 | | catch (Exception exception) |
| | | 344 | | { |
| | 4 | 345 | | CollisionNotificationExceptions.Capture(ref notificationExceptions, exception); |
| | 4 | 346 | | } |
| | | 347 | | |
| | | 348 | | try |
| | | 349 | | { |
| | 6097 | 350 | | EndNotification( |
| | 6097 | 351 | | registrationA, |
| | 6097 | 352 | | registrationB, |
| | 6097 | 353 | | isTriggerPair, |
| | 6097 | 354 | | shouldRaiseTriggerA, |
| | 6097 | 355 | | shouldRaiseTriggerB); |
| | 6096 | 356 | | } |
| | 1 | 357 | | catch (Exception exception) |
| | | 358 | | { |
| | 1 | 359 | | CollisionNotificationExceptions.Capture(ref notificationExceptions, exception); |
| | 1 | 360 | | } |
| | | 361 | | |
| | 6097 | 362 | | CollisionNotificationExceptions.ThrowIfAny(notificationExceptions); |
| | 6092 | 363 | | } |
| | | 364 | | |
| | | 365 | | private void EndNotification( |
| | | 366 | | in ColliderLifetimeToken2D registrationA, |
| | | 367 | | in ColliderLifetimeToken2D registrationB, |
| | | 368 | | bool isTriggerPair, |
| | | 369 | | bool shouldRaiseTriggerA, |
| | | 370 | | bool shouldRaiseTriggerB) |
| | | 371 | | { |
| | | 372 | | try |
| | | 373 | | { |
| | 6097 | 374 | | if (!_separationPending) |
| | 6084 | 375 | | return; |
| | | 376 | | |
| | 13 | 377 | | SolidBody2D? bodyA = _pendingBodyA; |
| | 13 | 378 | | SolidBody2D? bodyB = _pendingBodyB; |
| | 13 | 379 | | ClearPendingNotificationState(); |
| | 13 | 380 | | NotifySeparation( |
| | 13 | 381 | | registrationA, |
| | 13 | 382 | | registrationB, |
| | 13 | 383 | | bodyA, |
| | 13 | 384 | | bodyB, |
| | 13 | 385 | | isChanged: true, |
| | 13 | 386 | | allowInactive: true, |
| | 13 | 387 | | isTriggerPair, |
| | 13 | 388 | | shouldRaiseTriggerA, |
| | 13 | 389 | | shouldRaiseTriggerB); |
| | 12 | 390 | | } |
| | | 391 | | finally |
| | | 392 | | { |
| | 6097 | 393 | | ClearPendingNotificationState(); |
| | 6097 | 394 | | _notificationInProgress = false; |
| | 6097 | 395 | | } |
| | 6096 | 396 | | } |
| | | 397 | | |
| | | 398 | | private void NotifySeparation( |
| | | 399 | | in ColliderLifetimeToken2D registrationA, |
| | | 400 | | in ColliderLifetimeToken2D registrationB, |
| | | 401 | | SolidBody2D? bodyA, |
| | | 402 | | SolidBody2D? bodyB, |
| | | 403 | | bool isChanged, |
| | | 404 | | bool allowInactive, |
| | | 405 | | bool isTriggerPair, |
| | | 406 | | bool shouldRaiseTriggerA, |
| | | 407 | | bool shouldRaiseTriggerB) |
| | | 408 | | { |
| | 183 | 409 | | bool notifyA = _colliderANotified; |
| | 183 | 410 | | bool notifyB = _colliderBNotified; |
| | 183 | 411 | | _colliderANotified = false; |
| | 183 | 412 | | _colliderBNotified = false; |
| | 183 | 413 | | SwiftList<Exception>? notificationExceptions = null; |
| | 183 | 414 | | if (notifyA) |
| | | 415 | | { |
| | | 416 | | try |
| | | 417 | | { |
| | 183 | 418 | | registrationA.Collider.NotifyContact( |
| | 183 | 419 | | registrationB.Collider, |
| | 183 | 420 | | bodyB, |
| | 183 | 421 | | isColliding: false, |
| | 183 | 422 | | isChanged, |
| | 183 | 423 | | allowInactive, |
| | 183 | 424 | | registrationA, |
| | 183 | 425 | | registrationB, |
| | 183 | 426 | | isTriggerPair, |
| | 183 | 427 | | shouldRaiseTriggerA); |
| | 179 | 428 | | } |
| | 4 | 429 | | catch (Exception exception) |
| | | 430 | | { |
| | 4 | 431 | | CollisionNotificationExceptions.Capture(ref notificationExceptions, exception); |
| | 4 | 432 | | } |
| | | 433 | | } |
| | | 434 | | |
| | 183 | 435 | | if (notifyB && registrationB.IsCurrentLifetime) |
| | | 436 | | { |
| | | 437 | | try |
| | | 438 | | { |
| | 173 | 439 | | registrationB.Collider.NotifyContact( |
| | 173 | 440 | | registrationA.Collider, |
| | 173 | 441 | | bodyA, |
| | 173 | 442 | | isColliding: false, |
| | 173 | 443 | | isChanged, |
| | 173 | 444 | | allowInactive, |
| | 173 | 445 | | registrationB, |
| | 173 | 446 | | registrationA, |
| | 173 | 447 | | isTriggerPair, |
| | 173 | 448 | | shouldRaiseTriggerB); |
| | 172 | 449 | | } |
| | 1 | 450 | | catch (Exception exception) |
| | | 451 | | { |
| | 1 | 452 | | CollisionNotificationExceptions.Capture(ref notificationExceptions, exception); |
| | 1 | 453 | | } |
| | | 454 | | } |
| | | 455 | | |
| | 183 | 456 | | CollisionNotificationExceptions.ThrowIfAny(notificationExceptions); |
| | 179 | 457 | | } |
| | | 458 | | |
| | | 459 | | private void ClearPendingNotificationState() |
| | | 460 | | { |
| | 7302 | 461 | | _separationPending = false; |
| | 7302 | 462 | | _pendingBodyA = null; |
| | 7302 | 463 | | _pendingBodyB = null; |
| | 7302 | 464 | | } |
| | | 465 | | |
| | | 466 | | private bool MarkCollidingState(long frame) |
| | | 467 | | { |
| | 5927 | 468 | | RefreshContactDependencies(); |
| | 6025 | 469 | | if (!Manifold.HasContact) ClearWarmStart(); |
| | 5927 | 470 | | bool changed = !_isColliding; |
| | 5927 | 471 | | _isColliding = true; |
| | 5927 | 472 | | LastFrame = frame; |
| | 5927 | 473 | | LastVisitToken = ColliderA.Context.LateSimulateToken; |
| | 5927 | 474 | | return changed; |
| | | 475 | | } |
| | | 476 | | |
| | | 477 | | internal void RefreshContactDependencies() |
| | | 478 | | { |
| | 6607 | 479 | | uint versionA = ColliderA.ContactDependencyVersion; |
| | 6607 | 480 | | uint versionB = ColliderB.ContactDependencyVersion; |
| | 6607 | 481 | | bool triggerPair = ColliderA.IsTrigger || ColliderB.IsTrigger; |
| | | 482 | | // Even continued overlap can replace a sleeping body's load-bearing |
| | | 483 | | // normal or friction constraint; unchanged solid contacts retain sleep. |
| | 6607 | 484 | | if (_isColliding) |
| | | 485 | | { |
| | 6026 | 486 | | if (triggerPair != _wasTriggerPair) |
| | | 487 | | { |
| | 3 | 488 | | ClearWarmStart(); |
| | 3 | 489 | | WakeBodiesForContactChange(); |
| | | 490 | | } |
| | 6023 | 491 | | else if (!triggerPair) |
| | | 492 | | { |
| | 6246 | 493 | | if (versionB != _lastColliderBContactDependencyVersion) ColliderA.Body?.WakeForContactChange(); |
| | 6188 | 494 | | if (versionA != _lastColliderAContactDependencyVersion) ColliderB.Body?.WakeForContactChange(); |
| | | 495 | | } |
| | | 496 | | } |
| | | 497 | | |
| | 6607 | 498 | | _lastColliderAContactDependencyVersion = versionA; |
| | 6607 | 499 | | _lastColliderBContactDependencyVersion = versionB; |
| | 6607 | 500 | | _surfaceContactChanged = false; |
| | 6607 | 501 | | _wasTriggerPair = triggerPair; |
| | 6607 | 502 | | } |
| | | 503 | | |
| | | 504 | | private void WakeBodiesForContactChange() |
| | | 505 | | { |
| | 228 | 506 | | ColliderA.Body?.WakeForContactChange(); |
| | 228 | 507 | | ColliderB.Body?.WakeForContactChange(); |
| | 226 | 508 | | } |
| | | 509 | | |
| | | 510 | | internal void WakeSleepingBodiesForCollision() |
| | | 511 | | { |
| | 4627 | 512 | | SolidBody2D? bodyA = ColliderA.Body; |
| | 4627 | 513 | | SolidBody2D? bodyB = ColliderB.Body; |
| | 4627 | 514 | | if (bodyA == null || bodyB == null) |
| | 2532 | 515 | | return; |
| | | 516 | | |
| | 2095 | 517 | | bool bodyAAwake = !bodyA.IsSleeping; |
| | 2095 | 518 | | bool bodyBAwake = !bodyB.IsSleeping; |
| | 2095 | 519 | | if (bodyA.IsSleeping && bodyBAwake) |
| | 2 | 520 | | bodyA.Wake(); |
| | 2095 | 521 | | if (bodyB.IsSleeping && bodyAAwake) |
| | 2 | 522 | | bodyB.Wake(); |
| | 2095 | 523 | | } |
| | | 524 | | } |