| | | 1 | | //======================================================================= |
| | | 2 | | // CollisionPairMixed.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 Gravitas.Colliders; |
| | | 9 | | using SwiftCollections; |
| | | 10 | | using System; |
| | | 11 | | using System.Runtime.CompilerServices; |
| | | 12 | | |
| | | 13 | | namespace Gravitas.CollisionHandling; |
| | | 14 | | |
| | | 15 | | /// <summary> |
| | | 16 | | /// Owns one stable mixed 3D/2D collision pair identity and contact lifecycle. |
| | | 17 | | /// </summary> |
| | | 18 | | internal sealed partial class CollisionPairMixed |
| | | 19 | | { |
| | | 20 | | private bool _isColliding; |
| | | 21 | | private bool _isTriggerPair; |
| | | 22 | | private bool _notificationInProgress; |
| | | 23 | | private bool _separationPending; |
| | | 24 | | private bool _collider3DNotified; |
| | | 25 | | private bool _collider2DNotified; |
| | | 26 | | private long _lifetimeVersion; |
| | | 27 | | |
| | 212 | 28 | | public CollisionPairMixed(LSCollider collider3D, LSCollider2D collider2D) |
| | | 29 | | { |
| | 212 | 30 | | Collider3D = collider3D; |
| | 212 | 31 | | Collider2D = collider2D; |
| | 212 | 32 | | Initialize(collider3D, collider2D); |
| | 212 | 33 | | } |
| | | 34 | | |
| | | 35 | | public LSCollider Collider3D { get; private set; } |
| | | 36 | | |
| | | 37 | | public LSCollider2D Collider2D { get; private set; } |
| | | 38 | | |
| | | 39 | | public int Collider3DId { get; private set; } |
| | | 40 | | |
| | | 41 | | public int Collider2DId { get; private set; } |
| | | 42 | | |
| | | 43 | | public ulong Key { get; private set; } |
| | | 44 | | |
| | 894 | 45 | | public GravitasWorldContext Context => Collider3D.Context; |
| | | 46 | | |
| | | 47 | | public int LastFrame { get; private set; } = -1; |
| | | 48 | | |
| | | 49 | | public MixedContact Contact { get; private set; } |
| | | 50 | | |
| | 420 | 51 | | internal long LifetimeVersion => _lifetimeVersion; |
| | | 52 | | |
| | 32 | 53 | | internal bool IsNotificationInProgress => _notificationInProgress; |
| | | 54 | | |
| | | 55 | | public void Initialize(LSCollider collider3D, LSCollider2D collider2D) |
| | | 56 | | { |
| | 214 | 57 | | SwiftThrowHelper.ThrowIfNull(collider3D, nameof(collider3D)); |
| | 214 | 58 | | SwiftThrowHelper.ThrowIfNull(collider2D, nameof(collider2D)); |
| | | 59 | | |
| | 214 | 60 | | _lifetimeVersion++; |
| | 214 | 61 | | Collider3D = collider3D; |
| | 214 | 62 | | Collider2D = collider2D; |
| | 214 | 63 | | Collider3DId = collider3D.Id; |
| | 214 | 64 | | Collider2DId = collider2D.Id; |
| | 214 | 65 | | Key = MixedColliderKey.CreateKey(Collider3DId, Collider2DId); |
| | 214 | 66 | | LastFrame = -1; |
| | 214 | 67 | | _isColliding = false; |
| | 214 | 68 | | _isTriggerPair = collider3D.IsTrigger || collider2D.IsTrigger; |
| | 214 | 69 | | _notificationInProgress = false; |
| | 214 | 70 | | _separationPending = false; |
| | 214 | 71 | | _collider3DNotified = false; |
| | 214 | 72 | | _collider2DNotified = false; |
| | 214 | 73 | | Contact = default; |
| | 214 | 74 | | } |
| | | 75 | | |
| | | 76 | | public void MarkColliding(int frame, MixedContact contact) |
| | | 77 | | { |
| | 184 | 78 | | bool changed = !_isColliding; |
| | 184 | 79 | | _isColliding = true; |
| | 184 | 80 | | _isTriggerPair = Collider3D.IsTrigger || Collider2D.IsTrigger; |
| | 184 | 81 | | Contact = contact; |
| | 184 | 82 | | LastFrame = frame; |
| | | 83 | | |
| | 184 | 84 | | Context.Diagnostics.EmitMixedContact(this, contact); |
| | | 85 | | |
| | 184 | 86 | | var registration3D = new ColliderLifetimeToken(Collider3D); |
| | 184 | 87 | | var registration2D = new ColliderLifetimeToken2D(Collider2D); |
| | 184 | 88 | | bool shouldRaiseTrigger = ColliderTriggerEventPolicy.ShouldRaise(Collider3D, Collider2D); |
| | 184 | 89 | | _notificationInProgress = true; |
| | 184 | 90 | | SwiftList<Exception>? notificationExceptions = null; |
| | | 91 | | try |
| | | 92 | | { |
| | 184 | 93 | | bool notifyEnter3D = changed || !_collider3DNotified; |
| | 184 | 94 | | _collider3DNotified = true; |
| | 184 | 95 | | Collider3D.NotifyMixedContact( |
| | 184 | 96 | | Collider2D, |
| | 184 | 97 | | true, |
| | 184 | 98 | | notifyEnter3D, |
| | 184 | 99 | | _isTriggerPair, |
| | 184 | 100 | | allowInactive: false, |
| | 184 | 101 | | registration3D, |
| | 184 | 102 | | registration2D, |
| | 184 | 103 | | shouldRaiseTrigger); |
| | 183 | 104 | | if (_isColliding && registration3D.IsActive && registration2D.IsActive && !_separationPending) |
| | | 105 | | { |
| | 179 | 106 | | bool notifyEnter2D = changed || !_collider2DNotified; |
| | 179 | 107 | | _collider2DNotified = true; |
| | 179 | 108 | | Collider2D.NotifyMixedContact( |
| | 179 | 109 | | Collider3D, |
| | 179 | 110 | | true, |
| | 179 | 111 | | notifyEnter2D, |
| | 179 | 112 | | _isTriggerPair, |
| | 179 | 113 | | allowInactive: false, |
| | 179 | 114 | | registration2D, |
| | 179 | 115 | | registration3D, |
| | 179 | 116 | | shouldRaiseTrigger); |
| | | 117 | | } |
| | 183 | 118 | | } |
| | 1 | 119 | | catch (Exception exception) |
| | | 120 | | { |
| | 1 | 121 | | CollisionNotificationExceptions.Capture(ref notificationExceptions, exception); |
| | 1 | 122 | | } |
| | | 123 | | |
| | | 124 | | try |
| | | 125 | | { |
| | 184 | 126 | | EndNotification( |
| | 184 | 127 | | registration3D, |
| | 184 | 128 | | registration2D, |
| | 184 | 129 | | shouldRaiseTrigger); |
| | 183 | 130 | | } |
| | 1 | 131 | | catch (Exception exception) |
| | | 132 | | { |
| | 1 | 133 | | CollisionNotificationExceptions.Capture(ref notificationExceptions, exception); |
| | 1 | 134 | | } |
| | | 135 | | |
| | 184 | 136 | | CollisionNotificationExceptions.ThrowIfAny(notificationExceptions); |
| | 182 | 137 | | } |
| | | 138 | | |
| | | 139 | | public void MarkResting(int frame) |
| | | 140 | | { |
| | 6 | 141 | | LastFrame = frame; |
| | 6 | 142 | | } |
| | | 143 | | |
| | | 144 | | public void MarkResting(int frame, MixedContact contact) |
| | | 145 | | { |
| | 2 | 146 | | Contact = contact; |
| | 2 | 147 | | LastFrame = frame; |
| | 2 | 148 | | } |
| | | 149 | | |
| | | 150 | | public void MarkSeparated() |
| | | 151 | | { |
| | | 152 | | // The 2D side is admitted only after the 3D side, so it cannot be the sole notified side. |
| | 33 | 153 | | if (!_isColliding && !_collider3DNotified) |
| | 2 | 154 | | return; |
| | | 155 | | |
| | 31 | 156 | | _isColliding = false; |
| | 31 | 157 | | Contact = default; |
| | 31 | 158 | | if (_notificationInProgress) |
| | | 159 | | { |
| | 9 | 160 | | _separationPending = true; |
| | 9 | 161 | | return; |
| | | 162 | | } |
| | | 163 | | |
| | 22 | 164 | | var registration3D = new ColliderLifetimeToken(Collider3D); |
| | 22 | 165 | | var registration2D = new ColliderLifetimeToken2D(Collider2D); |
| | 22 | 166 | | _notificationInProgress = true; |
| | | 167 | | try |
| | | 168 | | { |
| | 22 | 169 | | NotifySeparation( |
| | 22 | 170 | | registration3D, |
| | 22 | 171 | | registration2D, |
| | 22 | 172 | | ColliderTriggerEventPolicy.ShouldRaise(Collider3D, Collider2D)); |
| | 22 | 173 | | } |
| | | 174 | | finally |
| | | 175 | | { |
| | 22 | 176 | | _separationPending = false; |
| | 22 | 177 | | _notificationInProgress = false; |
| | 22 | 178 | | } |
| | 22 | 179 | | } |
| | | 180 | | |
| | | 181 | | private void EndNotification( |
| | | 182 | | in ColliderLifetimeToken registration3D, |
| | | 183 | | in ColliderLifetimeToken2D registration2D, |
| | | 184 | | bool shouldRaiseTrigger) |
| | | 185 | | { |
| | | 186 | | try |
| | | 187 | | { |
| | 184 | 188 | | if (!_separationPending) |
| | 175 | 189 | | return; |
| | | 190 | | |
| | 9 | 191 | | _separationPending = false; |
| | 9 | 192 | | NotifySeparation(registration3D, registration2D, shouldRaiseTrigger); |
| | 8 | 193 | | } |
| | | 194 | | finally |
| | | 195 | | { |
| | 184 | 196 | | _separationPending = false; |
| | 184 | 197 | | _notificationInProgress = false; |
| | 184 | 198 | | } |
| | 183 | 199 | | } |
| | | 200 | | |
| | | 201 | | private void NotifySeparation( |
| | | 202 | | in ColliderLifetimeToken registration3D, |
| | | 203 | | in ColliderLifetimeToken2D registration2D, |
| | | 204 | | bool shouldRaiseTrigger) |
| | | 205 | | { |
| | 31 | 206 | | bool notify2D = _collider2DNotified; |
| | 31 | 207 | | _collider3DNotified = false; |
| | 31 | 208 | | _collider2DNotified = false; |
| | 31 | 209 | | SwiftList<Exception>? notificationExceptions = null; |
| | | 210 | | try |
| | | 211 | | { |
| | 31 | 212 | | Collider3D.NotifyMixedContact( |
| | 31 | 213 | | Collider2D, |
| | 31 | 214 | | false, |
| | 31 | 215 | | true, |
| | 31 | 216 | | _isTriggerPair, |
| | 31 | 217 | | allowInactive: true, |
| | 31 | 218 | | registration3D, |
| | 31 | 219 | | registration2D, |
| | 31 | 220 | | shouldRaiseTrigger); |
| | 30 | 221 | | } |
| | 1 | 222 | | catch (Exception exception) |
| | | 223 | | { |
| | 1 | 224 | | CollisionNotificationExceptions.Capture(ref notificationExceptions, exception); |
| | 1 | 225 | | } |
| | | 226 | | |
| | 31 | 227 | | if (notify2D && registration2D.IsCurrentLifetime) |
| | | 228 | | { |
| | | 229 | | try |
| | | 230 | | { |
| | 25 | 231 | | Collider2D.NotifyMixedContact( |
| | 25 | 232 | | Collider3D, |
| | 25 | 233 | | false, |
| | 25 | 234 | | true, |
| | 25 | 235 | | _isTriggerPair, |
| | 25 | 236 | | allowInactive: true, |
| | 25 | 237 | | registration2D, |
| | 25 | 238 | | registration3D, |
| | 25 | 239 | | shouldRaiseTrigger); |
| | 24 | 240 | | } |
| | 1 | 241 | | catch (Exception exception) |
| | | 242 | | { |
| | 1 | 243 | | CollisionNotificationExceptions.Capture(ref notificationExceptions, exception); |
| | 1 | 244 | | } |
| | | 245 | | } |
| | | 246 | | |
| | 31 | 247 | | CollisionNotificationExceptions.ThrowIfAny(notificationExceptions); |
| | 30 | 248 | | } |
| | | 249 | | |
| | | 250 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 251 | | internal void WakeSleepingBodiesForCollision() |
| | | 252 | | { |
| | 41 | 253 | | SolidBody? body3D = Collider3D.Body; |
| | 41 | 254 | | SolidBody2D? body2D = Collider2D.Body; |
| | 41 | 255 | | if (body3D == null || body2D == null) |
| | 3 | 256 | | return; |
| | | 257 | | |
| | 38 | 258 | | bool body3DAwake = body3D.IsAwakeForCollision; |
| | 38 | 259 | | bool body2DAwake = body2D.IsAwakeForCollision; |
| | 38 | 260 | | if (body3D.IsSleeping && body2DAwake) |
| | 1 | 261 | | body3D.Wake(); |
| | 38 | 262 | | if (body2D.IsSleeping && body3DAwake) |
| | 1 | 263 | | body2D.Wake(); |
| | 38 | 264 | | } |
| | | 265 | | } |