| | | 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 FixedMathSharp; |
| | | 9 | | using Gravitas.Colliders; |
| | | 10 | | using Gravitas.CollisionHandling; |
| | | 11 | | using SwiftCollections; |
| | | 12 | | using System; |
| | | 13 | | |
| | | 14 | | namespace Gravitas; |
| | | 15 | | |
| | | 16 | | internal sealed partial class CollisionPair2D |
| | | 17 | | { |
| | | 18 | | private bool _isColliding; |
| | | 19 | | private ContactWarmStartCache2D _warmStart; |
| | | 20 | | private bool _notificationInProgress; |
| | | 21 | | private bool _separationPending; |
| | | 22 | | private bool _colliderANotified; |
| | | 23 | | private bool _colliderBNotified; |
| | | 24 | | private SolidBody2D? _pendingBodyA; |
| | | 25 | | private SolidBody2D? _pendingBodyB; |
| | | 26 | | private long _lifetimeVersion; |
| | | 27 | | |
| | 342 | 28 | | public CollisionPair2D(LSCollider2D colliderA, LSCollider2D colliderB) |
| | | 29 | | { |
| | 342 | 30 | | ColliderA = colliderA; |
| | 342 | 31 | | ColliderB = colliderB; |
| | 342 | 32 | | Initialize(colliderA, colliderB); |
| | 342 | 33 | | } |
| | | 34 | | |
| | | 35 | | public LSCollider2D ColliderA { get; private set; } |
| | | 36 | | |
| | | 37 | | public LSCollider2D ColliderB { get; private set; } |
| | | 38 | | |
| | | 39 | | public int Id1 { get; private set; } |
| | | 40 | | |
| | | 41 | | public int Id2 { get; private set; } |
| | | 42 | | |
| | | 43 | | public CollisionType2D CollisionType { get; private set; } |
| | | 44 | | |
| | | 45 | | public int LastFrame { get; private set; } = -1; |
| | | 46 | | |
| | 30 | 47 | | public bool IsColliding => _isColliding; |
| | | 48 | | |
| | 85 | 49 | | internal bool IsNotificationInProgress => _notificationInProgress; |
| | | 50 | | |
| | 988 | 51 | | internal long LifetimeVersion => _lifetimeVersion; |
| | | 52 | | |
| | | 53 | | public ContactManifold2D Manifold { get; } = new(); |
| | | 54 | | |
| | | 55 | | public void Initialize(LSCollider2D colliderA, LSCollider2D colliderB) |
| | | 56 | | { |
| | 351 | 57 | | _lifetimeVersion++; |
| | 351 | 58 | | AssignPriority(colliderA, colliderB); |
| | 351 | 59 | | Id1 = ColliderA.Id; |
| | 351 | 60 | | Id2 = ColliderB.Id; |
| | 351 | 61 | | CollisionType = ColliderSettings2D.GetCollisionType(ColliderA.Shape, ColliderB.Shape); |
| | 351 | 62 | | ResetPairState(); |
| | 351 | 63 | | } |
| | | 64 | | |
| | | 65 | | private void AssignPriority(LSCollider2D colliderA, LSCollider2D colliderB) |
| | | 66 | | { |
| | 351 | 67 | | if (ShouldFirstColliderLead(colliderA, colliderB)) |
| | | 68 | | { |
| | 300 | 69 | | ColliderA = colliderA; |
| | 300 | 70 | | ColliderB = colliderB; |
| | 300 | 71 | | return; |
| | | 72 | | } |
| | | 73 | | |
| | 51 | 74 | | ColliderA = colliderB; |
| | 51 | 75 | | ColliderB = colliderA; |
| | 51 | 76 | | } |
| | | 77 | | |
| | | 78 | | internal static bool ShouldFirstColliderLead(LSCollider2D colliderA, LSCollider2D colliderB) |
| | | 79 | | { |
| | 351 | 80 | | if (colliderA.Priority != colliderB.Priority) |
| | 40 | 81 | | return colliderA.Priority > colliderB.Priority; |
| | | 82 | | |
| | 311 | 83 | | SolidBody2D? bodyA = colliderA.Body; |
| | 311 | 84 | | SolidBody2D? bodyB = colliderB.Body; |
| | 311 | 85 | | if (bodyA == null || bodyB == null) |
| | 26 | 86 | | return colliderA.Id <= colliderB.Id; |
| | | 87 | | |
| | 285 | 88 | | if (bodyA.LinearSpeed != bodyB.LinearSpeed) |
| | 30 | 89 | | return bodyA.LinearSpeed > bodyB.LinearSpeed; |
| | | 90 | | |
| | 255 | 91 | | return colliderA.Id <= colliderB.Id; |
| | | 92 | | } |
| | | 93 | | |
| | | 94 | | public void MarkColliding(int frame) |
| | | 95 | | { |
| | 53 | 96 | | if (!Manifold.HasContact) |
| | 1 | 97 | | return; |
| | | 98 | | |
| | 52 | 99 | | bool changed = MarkCollidingState(frame); |
| | | 100 | | |
| | 52 | 101 | | if (!ColliderA.IsTrigger && !ColliderB.IsTrigger) |
| | | 102 | | { |
| | 29 | 103 | | CollisionResponse2D.Resolve(this); |
| | 29 | 104 | | WakeSleepingBodiesForCollision(); |
| | | 105 | | } |
| | | 106 | | |
| | 52 | 107 | | NotifyColliders(isColliding: true, changed); |
| | 52 | 108 | | } |
| | | 109 | | |
| | | 110 | | internal void MarkCollidingDeferred(int frame) |
| | | 111 | | { |
| | 504 | 112 | | if (!Manifold.HasContact) |
| | 1 | 113 | | return; |
| | | 114 | | |
| | 503 | 115 | | bool changed = MarkCollidingState(frame); |
| | 503 | 116 | | NotifyColliders(isColliding: true, changed); |
| | 501 | 117 | | } |
| | | 118 | | |
| | | 119 | | public void MarkResting(int frame) |
| | | 120 | | { |
| | 8 | 121 | | LastFrame = frame; |
| | 8 | 122 | | } |
| | | 123 | | |
| | | 124 | | public void MarkSeparated() |
| | | 125 | | { |
| | 80 | 126 | | bool wasColliding = _isColliding; |
| | 80 | 127 | | ResetCollisionState(); |
| | 80 | 128 | | if (!wasColliding) |
| | 2 | 129 | | return; |
| | | 130 | | |
| | 78 | 131 | | if (_notificationInProgress) |
| | | 132 | | { |
| | 13 | 133 | | _separationPending = true; |
| | 13 | 134 | | _pendingBodyA = ColliderA.Body; |
| | 13 | 135 | | _pendingBodyB = ColliderB.Body; |
| | 13 | 136 | | return; |
| | | 137 | | } |
| | | 138 | | |
| | 65 | 139 | | NotifyColliders(isColliding: false, isChanged: true); |
| | 65 | 140 | | } |
| | | 141 | | |
| | | 142 | | internal void StoreWarmStartImpulse(ulong contactId, Fixed64 normalImpulse, Fixed64 tangentImpulse) => |
| | 1035 | 143 | | _warmStart.Set(contactId, normalImpulse, tangentImpulse); |
| | | 144 | | |
| | | 145 | | internal bool TryGetWarmStartImpulse(ulong contactId, out ContactWarmStartImpulse impulse) => |
| | 1040 | 146 | | _warmStart.TryGet(contactId, out impulse); |
| | | 147 | | |
| | | 148 | | internal void RemoveWarmStartImpulse(ulong contactId) => |
| | 7 | 149 | | _warmStart.Remove(contactId); |
| | | 150 | | |
| | 2 | 151 | | internal void ClearWarmStart() => _warmStart.Clear(); |
| | | 152 | | |
| | | 153 | | private void ResetPairState() |
| | | 154 | | { |
| | 351 | 155 | | ResetCollisionState(); |
| | 351 | 156 | | _notificationInProgress = false; |
| | 351 | 157 | | _colliderANotified = false; |
| | 351 | 158 | | _colliderBNotified = false; |
| | 351 | 159 | | ClearPendingNotificationState(); |
| | 351 | 160 | | } |
| | | 161 | | |
| | | 162 | | private void ResetCollisionState() |
| | | 163 | | { |
| | 431 | 164 | | _isColliding = false; |
| | 431 | 165 | | LastFrame = -1; |
| | 431 | 166 | | Manifold.Reset(); |
| | 431 | 167 | | _warmStart.Clear(); |
| | 431 | 168 | | } |
| | | 169 | | |
| | | 170 | | private void NotifyColliders(bool isColliding, bool isChanged) |
| | | 171 | | { |
| | 620 | 172 | | var registrationA = new ColliderLifetimeToken2D(ColliderA); |
| | 620 | 173 | | var registrationB = new ColliderLifetimeToken2D(ColliderB); |
| | 620 | 174 | | LSCollider2D colliderA = registrationA.Collider; |
| | 620 | 175 | | LSCollider2D colliderB = registrationB.Collider; |
| | 620 | 176 | | SolidBody2D? bodyA = colliderA.Body; |
| | 620 | 177 | | SolidBody2D? bodyB = colliderB.Body; |
| | 620 | 178 | | bool isTriggerPair = colliderA.IsTrigger || colliderB.IsTrigger; |
| | 620 | 179 | | bool shouldRaiseTriggerA = isTriggerPair && ColliderTriggerEventPolicy.ShouldRaise(colliderA, colliderB); |
| | 620 | 180 | | bool shouldRaiseTriggerB = isTriggerPair && ColliderTriggerEventPolicy.ShouldRaise(colliderB, colliderA); |
| | 620 | 181 | | _notificationInProgress = true; |
| | 620 | 182 | | SwiftList<Exception>? notificationExceptions = null; |
| | | 183 | | try |
| | | 184 | | { |
| | 620 | 185 | | if (isColliding) |
| | | 186 | | { |
| | 555 | 187 | | bool notifyEnterA = isChanged || !_colliderANotified; |
| | 555 | 188 | | _colliderANotified = true; |
| | 555 | 189 | | colliderA.NotifyContact( |
| | 555 | 190 | | colliderB, |
| | 555 | 191 | | bodyB, |
| | 555 | 192 | | isColliding: true, |
| | 555 | 193 | | notifyEnterA, |
| | 555 | 194 | | allowInactive: false, |
| | 555 | 195 | | registrationA, |
| | 555 | 196 | | registrationB, |
| | 555 | 197 | | isTriggerPair, |
| | 555 | 198 | | shouldRaiseTriggerA); |
| | 554 | 199 | | if (registrationA.IsActive && registrationB.IsActive && !_separationPending) |
| | | 200 | | { |
| | 544 | 201 | | bool notifyEnterB = isChanged || !_colliderBNotified; |
| | 544 | 202 | | _colliderBNotified = true; |
| | 544 | 203 | | colliderB.NotifyContact( |
| | 544 | 204 | | colliderA, |
| | 544 | 205 | | bodyA, |
| | 544 | 206 | | isColliding: true, |
| | 544 | 207 | | notifyEnterB, |
| | 544 | 208 | | allowInactive: false, |
| | 544 | 209 | | registrationB, |
| | 544 | 210 | | registrationA, |
| | 544 | 211 | | isTriggerPair, |
| | 544 | 212 | | shouldRaiseTriggerB); |
| | | 213 | | } |
| | | 214 | | } |
| | | 215 | | else |
| | | 216 | | { |
| | 65 | 217 | | NotifySeparation( |
| | 65 | 218 | | registrationA, |
| | 65 | 219 | | registrationB, |
| | 65 | 220 | | bodyA, |
| | 65 | 221 | | bodyB, |
| | 65 | 222 | | isChanged, |
| | 65 | 223 | | allowInactive: false, |
| | 65 | 224 | | isTriggerPair, |
| | 65 | 225 | | shouldRaiseTriggerA, |
| | 65 | 226 | | shouldRaiseTriggerB); |
| | | 227 | | } |
| | 619 | 228 | | } |
| | 1 | 229 | | catch (Exception exception) |
| | | 230 | | { |
| | 1 | 231 | | CollisionNotificationExceptions.Capture(ref notificationExceptions, exception); |
| | 1 | 232 | | } |
| | | 233 | | |
| | | 234 | | try |
| | | 235 | | { |
| | 620 | 236 | | EndNotification( |
| | 620 | 237 | | registrationA, |
| | 620 | 238 | | registrationB, |
| | 620 | 239 | | isTriggerPair, |
| | 620 | 240 | | shouldRaiseTriggerA, |
| | 620 | 241 | | shouldRaiseTriggerB); |
| | 619 | 242 | | } |
| | 1 | 243 | | catch (Exception exception) |
| | | 244 | | { |
| | 1 | 245 | | CollisionNotificationExceptions.Capture(ref notificationExceptions, exception); |
| | 1 | 246 | | } |
| | | 247 | | |
| | 620 | 248 | | CollisionNotificationExceptions.ThrowIfAny(notificationExceptions); |
| | 618 | 249 | | } |
| | | 250 | | |
| | | 251 | | private void EndNotification( |
| | | 252 | | in ColliderLifetimeToken2D registrationA, |
| | | 253 | | in ColliderLifetimeToken2D registrationB, |
| | | 254 | | bool isTriggerPair, |
| | | 255 | | bool shouldRaiseTriggerA, |
| | | 256 | | bool shouldRaiseTriggerB) |
| | | 257 | | { |
| | | 258 | | try |
| | | 259 | | { |
| | 620 | 260 | | if (!_separationPending) |
| | 607 | 261 | | return; |
| | | 262 | | |
| | 13 | 263 | | SolidBody2D? bodyA = _pendingBodyA; |
| | 13 | 264 | | SolidBody2D? bodyB = _pendingBodyB; |
| | 13 | 265 | | ClearPendingNotificationState(); |
| | 13 | 266 | | NotifySeparation( |
| | 13 | 267 | | registrationA, |
| | 13 | 268 | | registrationB, |
| | 13 | 269 | | bodyA, |
| | 13 | 270 | | bodyB, |
| | 13 | 271 | | isChanged: true, |
| | 13 | 272 | | allowInactive: true, |
| | 13 | 273 | | isTriggerPair, |
| | 13 | 274 | | shouldRaiseTriggerA, |
| | 13 | 275 | | shouldRaiseTriggerB); |
| | 12 | 276 | | } |
| | | 277 | | finally |
| | | 278 | | { |
| | 620 | 279 | | ClearPendingNotificationState(); |
| | 620 | 280 | | _notificationInProgress = false; |
| | 620 | 281 | | } |
| | 619 | 282 | | } |
| | | 283 | | |
| | | 284 | | private void NotifySeparation( |
| | | 285 | | in ColliderLifetimeToken2D registrationA, |
| | | 286 | | in ColliderLifetimeToken2D registrationB, |
| | | 287 | | SolidBody2D? bodyA, |
| | | 288 | | SolidBody2D? bodyB, |
| | | 289 | | bool isChanged, |
| | | 290 | | bool allowInactive, |
| | | 291 | | bool isTriggerPair, |
| | | 292 | | bool shouldRaiseTriggerA, |
| | | 293 | | bool shouldRaiseTriggerB) |
| | | 294 | | { |
| | 78 | 295 | | bool notifyA = _colliderANotified; |
| | 78 | 296 | | bool notifyB = _colliderBNotified; |
| | 78 | 297 | | _colliderANotified = false; |
| | 78 | 298 | | _colliderBNotified = false; |
| | 78 | 299 | | SwiftList<Exception>? notificationExceptions = null; |
| | 78 | 300 | | if (notifyA) |
| | | 301 | | { |
| | | 302 | | try |
| | | 303 | | { |
| | 78 | 304 | | registrationA.Collider.NotifyContact( |
| | 78 | 305 | | registrationB.Collider, |
| | 78 | 306 | | bodyB, |
| | 78 | 307 | | isColliding: false, |
| | 78 | 308 | | isChanged, |
| | 78 | 309 | | allowInactive, |
| | 78 | 310 | | registrationA, |
| | 78 | 311 | | registrationB, |
| | 78 | 312 | | isTriggerPair, |
| | 78 | 313 | | shouldRaiseTriggerA); |
| | 77 | 314 | | } |
| | 1 | 315 | | catch (Exception exception) |
| | | 316 | | { |
| | 1 | 317 | | CollisionNotificationExceptions.Capture(ref notificationExceptions, exception); |
| | 1 | 318 | | } |
| | | 319 | | } |
| | | 320 | | |
| | 78 | 321 | | if (notifyB && registrationB.IsCurrentLifetime) |
| | | 322 | | { |
| | | 323 | | try |
| | | 324 | | { |
| | 68 | 325 | | registrationB.Collider.NotifyContact( |
| | 68 | 326 | | registrationA.Collider, |
| | 68 | 327 | | bodyA, |
| | 68 | 328 | | isColliding: false, |
| | 68 | 329 | | isChanged, |
| | 68 | 330 | | allowInactive, |
| | 68 | 331 | | registrationB, |
| | 68 | 332 | | registrationA, |
| | 68 | 333 | | isTriggerPair, |
| | 68 | 334 | | shouldRaiseTriggerB); |
| | 67 | 335 | | } |
| | 1 | 336 | | catch (Exception exception) |
| | | 337 | | { |
| | 1 | 338 | | CollisionNotificationExceptions.Capture(ref notificationExceptions, exception); |
| | 1 | 339 | | } |
| | | 340 | | } |
| | | 341 | | |
| | 78 | 342 | | CollisionNotificationExceptions.ThrowIfAny(notificationExceptions); |
| | 77 | 343 | | } |
| | | 344 | | |
| | | 345 | | private void ClearPendingNotificationState() |
| | | 346 | | { |
| | 984 | 347 | | _separationPending = false; |
| | 984 | 348 | | _pendingBodyA = null; |
| | 984 | 349 | | _pendingBodyB = null; |
| | 984 | 350 | | } |
| | | 351 | | |
| | | 352 | | private bool MarkCollidingState(int frame) |
| | | 353 | | { |
| | 555 | 354 | | bool changed = !_isColliding; |
| | 555 | 355 | | _isColliding = true; |
| | 555 | 356 | | LastFrame = frame; |
| | 555 | 357 | | return changed; |
| | | 358 | | } |
| | | 359 | | |
| | | 360 | | internal void WakeSleepingBodiesForCollision() |
| | | 361 | | { |
| | 107 | 362 | | SolidBody2D? bodyA = ColliderA.Body; |
| | 107 | 363 | | SolidBody2D? bodyB = ColliderB.Body; |
| | 107 | 364 | | if (bodyA == null || bodyB == null) |
| | 15 | 365 | | return; |
| | | 366 | | |
| | 92 | 367 | | bool bodyAAwake = !bodyA.IsSleeping; |
| | 92 | 368 | | bool bodyBAwake = !bodyB.IsSleeping; |
| | 92 | 369 | | if (bodyA.IsSleeping && bodyBAwake) |
| | 2 | 370 | | bodyA.Wake(); |
| | 92 | 371 | | if (bodyB.IsSleeping && bodyAAwake) |
| | 1 | 372 | | bodyB.Wake(); |
| | 92 | 373 | | } |
| | | 374 | | } |