| | | 1 | | //======================================================================= |
| | | 2 | | // CollisionPair.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 GridForge.Grids; |
| | | 11 | | using GridForge.Grids.Topology; |
| | | 12 | | using SwiftCollections; |
| | | 13 | | using System; |
| | | 14 | | using System.Runtime.CompilerServices; |
| | | 15 | | |
| | | 16 | | namespace Gravitas.CollisionHandling; |
| | | 17 | | |
| | | 18 | | internal enum CollisionResponseDispatchMode |
| | | 19 | | { |
| | | 20 | | Immediate, |
| | | 21 | | Deferred |
| | | 22 | | } |
| | | 23 | | |
| | | 24 | | /// <summary> |
| | | 25 | | /// Owns deterministic 3D pair lifecycle, culling, contact, and notification state. |
| | | 26 | | /// </summary> |
| | | 27 | | public partial class CollisionPair |
| | | 28 | | { |
| | | 29 | | /// <summary>Stores the host-controlled pair diagnostic flag.</summary> |
| | 639 | 30 | | public bool Debug = true; |
| | | 31 | | |
| | | 32 | | /// <summary>Gets whether this pooled pair currently represents two registered colliders.</summary> |
| | | 33 | | public bool Active { get; private set; } |
| | | 34 | | |
| | | 35 | | private bool _isPooledForDeactivation; |
| | | 36 | | |
| | | 37 | | /// <summary>Gets the world context that owns this pair.</summary> |
| | | 38 | | public GravitasWorldContext Context { get; private set; } = null!; |
| | | 39 | | |
| | | 40 | | /// <summary>Gets the GridForge world owned by <see cref="Context"/>.</summary> |
| | 1682 | 41 | | public GridWorld World => Context.World; |
| | | 42 | | |
| | | 43 | | // stores order in which they come in |
| | | 44 | | /// <summary>Gets the first collider ID used to identify the pair.</summary> |
| | | 45 | | public int Id1 { get; private set; } |
| | | 46 | | /// <summary>Gets the second collider ID used to identify the pair.</summary> |
| | | 47 | | public int Id2 { get; private set; } |
| | | 48 | | |
| | | 49 | | /// <summary>Gets the collider selected to lead narrow-phase dispatch.</summary> |
| | | 50 | | public LSCollider ColliderA { get; private set; } = null!; |
| | | 51 | | /// <summary>Gets the other collider in the pair.</summary> |
| | | 52 | | public LSCollider ColliderB { get; private set; } = null!; |
| | | 53 | | |
| | | 54 | | /// <summary>Tracks the collision-service partition pass that last processed this pair.</summary> |
| | | 55 | | public uint PartitionVersion; |
| | | 56 | | /// <summary>Tracks reuse of this pooled pair instance.</summary> |
| | 639 | 57 | | public ushort PairVersion = 1; |
| | | 58 | | |
| | | 59 | | /// <summary>Gets the last simulation frame in which this pair was updated.</summary> |
| | | 60 | | public int LastFrame { get; private set; } |
| | | 61 | | /// <summary>Gets the last simulation frame in which this pair had contact.</summary> |
| | | 62 | | public int LastCollidedFrame { get; private set; } |
| | | 63 | | |
| | | 64 | | private Fixed64 _fastCollideDistance; |
| | | 65 | | private Fixed64 _fastDistance; |
| | | 66 | | /// <summary>Gets the narrow-phase dispatch type for this collider combination.</summary> |
| | | 67 | | public CollisionType CollisionType { get; private set; } |
| | 639 | 68 | | private bool _doPhysics = true; |
| | | 69 | | |
| | | 70 | | /// <summary>Gets the remaining deferred updates before this pair is checked again.</summary> |
| | | 71 | | public short CullCounter { get; private set; } |
| | | 72 | | private bool _preventDistanceCull; |
| | | 73 | | private Fixed64 _fastDistanceOffset; |
| | | 74 | | private uint _lastColliderABroadPhaseVersion; |
| | | 75 | | private uint _lastColliderBBroadPhaseVersion; |
| | | 76 | | |
| | | 77 | | private bool _isColliding; |
| | | 78 | | private bool _isCollidingChanged; |
| | | 79 | | private bool _notificationInProgress; |
| | | 80 | | private bool _separationPending; |
| | | 81 | | private bool _colliderANotified; |
| | | 82 | | private bool _colliderBNotified; |
| | | 83 | | private SolidBody? _pendingBodyA; |
| | | 84 | | private SolidBody? _pendingBodyB; |
| | | 85 | | private long _lifetimeVersion; |
| | | 86 | | |
| | 36 | 87 | | internal bool IsNotificationInProgress => _notificationInProgress; |
| | | 88 | | |
| | 9798 | 89 | | internal long LifetimeVersion => _lifetimeVersion; |
| | | 90 | | |
| | | 91 | | /// <summary>Gets the deterministic contact manifold owned by this pair.</summary> |
| | | 92 | | public ContactManifold Manifold { get; } = new(); |
| | | 93 | | |
| | | 94 | | private ContactWarmStartCache _warmStart; |
| | | 95 | | |
| | 1278 | 96 | | internal CollisionPair(LSCollider c1, LSCollider c2) => Initialize(c1, c2); |
| | | 97 | | |
| | | 98 | | /// <summary> |
| | | 99 | | /// Initializes the CollisionPair with the given colliders. |
| | | 100 | | /// </summary> |
| | | 101 | | /// <param name="c1">The first collider.</param> |
| | | 102 | | /// <param name="c2">The second collider.</param> |
| | | 103 | | internal void Initialize(LSCollider c1, LSCollider c2) |
| | | 104 | | { |
| | 640 | 105 | | SwiftThrowHelper.ThrowIfNull(c1, nameof(c1)); |
| | 640 | 106 | | SwiftThrowHelper.ThrowIfNull(c2, nameof(c2)); |
| | 640 | 107 | | SwiftThrowHelper.ThrowIfArgument(c1 == c2, nameof(c2), "Cannot create a CollisionPair with the same collider."); |
| | 640 | 108 | | GravitasWorldContext context = c1.Context; |
| | 640 | 109 | | SwiftThrowHelper.ThrowIfArgument( |
| | 640 | 110 | | !ReferenceEquals(context, c2.Context), |
| | 640 | 111 | | nameof(c2), |
| | 640 | 112 | | "Colliders must be in the same context to create a CollisionPair."); |
| | | 113 | | |
| | 639 | 114 | | Context = context; |
| | 639 | 115 | | _lifetimeVersion++; |
| | | 116 | | |
| | 639 | 117 | | Reset(); |
| | | 118 | | |
| | 639 | 119 | | AssignPriority(c1, c2); |
| | 639 | 120 | | Id1 = ColliderA.Id; |
| | 639 | 121 | | Id2 = ColliderB.Id; |
| | | 122 | | |
| | 639 | 123 | | CollisionType = ColliderSettings.GetCollisionType(ColliderA.Shape, ColliderB.Shape); |
| | | 124 | | |
| | | 125 | | // Calculate the square of the sum of the radii of the bounding spheres |
| | 639 | 126 | | _fastCollideDistance = ColliderA!.Bounds.Scope.Magnitude + ColliderB!.Bounds.Scope.Magnitude; |
| | 639 | 127 | | _fastCollideDistance *= _fastCollideDistance; |
| | | 128 | | |
| | 639 | 129 | | _doPhysics = ColliderA!.Body != null && ColliderB!.Body != null && !ColliderA!.IsTrigger && !ColliderB!.IsTrigge |
| | | 130 | | |
| | | 131 | | // Immediately check collision. If collision distance is too large, do |
| | | 132 | | // not cull based on distance. |
| | 639 | 133 | | CullCounter = 0; |
| | 639 | 134 | | _preventDistanceCull = _fastCollideDistance > Context.Environment.CullFastDistanceMax; |
| | 639 | 135 | | _fastDistanceOffset = Fixed64.FromRaw((int)_fastCollideDistance) + (Fixed64.One * 2) * (Fixed64.One * 2); |
| | | 136 | | |
| | 639 | 137 | | LastCollidedFrame = Context.FrameCount; |
| | 639 | 138 | | RefreshBroadPhaseVersions(); |
| | 639 | 139 | | PairVersion++; |
| | 639 | 140 | | Active = true; |
| | 639 | 141 | | } |
| | | 142 | | |
| | | 143 | | /// <summary>Orders the colliders for stable narrow-phase dispatch.</summary> |
| | | 144 | | public void AssignPriority(LSCollider c1, LSCollider c2) |
| | | 145 | | { |
| | 639 | 146 | | if (ShouldFirstColliderLead(c1, c2)) |
| | | 147 | | { |
| | 558 | 148 | | ColliderA = c1; |
| | 558 | 149 | | ColliderB = c2; |
| | 558 | 150 | | return; |
| | | 151 | | } |
| | | 152 | | |
| | 81 | 153 | | ColliderA = c2; |
| | 81 | 154 | | ColliderB = c1; |
| | 81 | 155 | | } |
| | | 156 | | |
| | | 157 | | private static bool ShouldFirstColliderLead(LSCollider c1, LSCollider c2) |
| | | 158 | | { |
| | 639 | 159 | | if (c1.Priority != c2.Priority) |
| | 268 | 160 | | return c1.Priority > c2.Priority; |
| | | 161 | | |
| | 371 | 162 | | if (c1.Body == null || c2.Body == null) |
| | 26 | 163 | | return true; |
| | | 164 | | |
| | 345 | 165 | | if (c1.Body.LinearSpeed != c2.Body.LinearSpeed) |
| | 84 | 166 | | return c1.Body.LinearSpeed > c2.Body.LinearSpeed; |
| | | 167 | | |
| | 261 | 168 | | return true; |
| | | 169 | | } |
| | | 170 | | |
| | | 171 | | /// <summary> |
| | | 172 | | /// Checks and distributes collisions between colliders. |
| | | 173 | | /// Called by Partition Manager every fixed update if 2 colliders are on the same partion. |
| | | 174 | | /// </summary> |
| | 22 | 175 | | public void UpdateCollision() => UpdateCollision(CollisionResponseDispatchMode.Immediate); |
| | | 176 | | |
| | 4297 | 177 | | internal void UpdateCollisionDeferred() => UpdateCollision(CollisionResponseDispatchMode.Deferred); |
| | | 178 | | |
| | | 179 | | private void UpdateCollision(CollisionResponseDispatchMode responseMode) |
| | | 180 | | { |
| | 4319 | 181 | | if (!Active) |
| | 1 | 182 | | return; |
| | | 183 | | |
| | 4318 | 184 | | UpdateLastFrame(); |
| | 4318 | 185 | | DeactivateAndPoolIfRequired(); |
| | | 186 | | |
| | 4318 | 187 | | if (IsCullStateInvalidated()) |
| | 4223 | 188 | | CullCounter = 0; |
| | | 189 | | |
| | 4318 | 190 | | if (CullCounter <= 0) |
| | | 191 | | { |
| | 4317 | 192 | | ProcessCollision(responseMode); |
| | 4317 | 193 | | RefreshBroadPhaseVersions(); |
| | 4317 | 194 | | if (_isCollidingChanged && !_isColliding) |
| | 41 | 195 | | Manifold.Reset(); |
| | | 196 | | |
| | 4317 | 197 | | HandleCullingIfNotColliding(); |
| | 4317 | 198 | | return; |
| | | 199 | | } |
| | | 200 | | |
| | 1 | 201 | | CullCounter--; // Culled and one step closer to checking again. |
| | 1 | 202 | | } |
| | | 203 | | |
| | 4318 | 204 | | private void UpdateLastFrame() => LastFrame = Context.FrameCount; |
| | | 205 | | |
| | | 206 | | private void DeactivateAndPoolIfRequired() |
| | | 207 | | { |
| | 4318 | 208 | | if (_isPooledForDeactivation) |
| | 4045 | 209 | | return; |
| | | 210 | | |
| | 273 | 211 | | Context.Physics.PoolForDeactivation(this); |
| | 273 | 212 | | _isPooledForDeactivation = true; |
| | 273 | 213 | | } |
| | | 214 | | |
| | | 215 | | private void ProcessCollision(CollisionResponseDispatchMode responseMode) |
| | | 216 | | { |
| | 4317 | 217 | | if (!ShouldPerformCollisionCheck()) |
| | | 218 | | { |
| | 1813 | 219 | | _isCollidingChanged = _isColliding; |
| | 1813 | 220 | | _isColliding = false; |
| | 1813 | 221 | | Manifold.Reset(); |
| | 1813 | 222 | | _warmStart.Clear(); |
| | 1813 | 223 | | return; |
| | | 224 | | } |
| | | 225 | | |
| | 2504 | 226 | | bool result = CheckCollision(); |
| | 2504 | 227 | | if (result) |
| | 2488 | 228 | | Context.Diagnostics.EmitContact(this, result); |
| | | 229 | | |
| | 2504 | 230 | | if (result ^ _isColliding) |
| | | 231 | | { |
| | 191 | 232 | | _isColliding = result; |
| | 191 | 233 | | _isCollidingChanged = true; |
| | | 234 | | } |
| | | 235 | | |
| | 2504 | 236 | | if (!result || !Manifold.HasContact) |
| | | 237 | | { |
| | 16 | 238 | | _warmStart.Clear(); |
| | 16 | 239 | | return; |
| | | 240 | | } |
| | | 241 | | |
| | 2488 | 242 | | if (!_doPhysics) |
| | 1652 | 243 | | return; |
| | | 244 | | |
| | 836 | 245 | | if (responseMode == CollisionResponseDispatchMode.Deferred) |
| | | 246 | | { |
| | 824 | 247 | | Context.Physics.QueueDiscreteResponsePair(this); |
| | 824 | 248 | | return; |
| | | 249 | | } |
| | | 250 | | |
| | 12 | 251 | | WakeSleepingBodiesForCollision(); |
| | 12 | 252 | | CollisionResponse.CalculateImpulse(this); |
| | 12 | 253 | | } |
| | | 254 | | |
| | | 255 | | internal void WakeSleepingBodiesForCollision() |
| | | 256 | | { |
| | 79 | 257 | | SolidBody? bodyA = ColliderA.Body; |
| | 79 | 258 | | SolidBody? bodyB = ColliderB.Body; |
| | 79 | 259 | | if (bodyA == null || bodyB == null) |
| | 2 | 260 | | return; |
| | | 261 | | |
| | 77 | 262 | | bool bodyAAwake = bodyA.IsAwakeForCollision; |
| | 77 | 263 | | bool bodyBAwake = bodyB.IsAwakeForCollision; |
| | | 264 | | |
| | 77 | 265 | | if (bodyA.IsSleeping && bodyBAwake) |
| | 1 | 266 | | bodyA.Wake(); |
| | 77 | 267 | | if (bodyB.IsSleeping && bodyAAwake) |
| | 2 | 268 | | bodyB.Wake(); |
| | 77 | 269 | | } |
| | | 270 | | |
| | | 271 | | /// <summary>Raises contact or separation notifications for the current pair state.</summary> |
| | | 272 | | public void NotifyCollidersOfContact() |
| | | 273 | | { |
| | 3855 | 274 | | bool isColliding = _isColliding; |
| | 3855 | 275 | | bool isChanged = _isCollidingChanged; |
| | | 276 | | |
| | 3855 | 277 | | var registrationA = new ColliderLifetimeToken(ColliderA); |
| | 3855 | 278 | | var registrationB = new ColliderLifetimeToken(ColliderB); |
| | 3855 | 279 | | LSCollider colliderA = registrationA.Collider; |
| | 3855 | 280 | | LSCollider colliderB = registrationB.Collider; |
| | 3855 | 281 | | SolidBody? bodyA = colliderA.Body; |
| | 3855 | 282 | | SolidBody? bodyB = colliderB.Body; |
| | 3855 | 283 | | bool isTriggerPair = colliderA.IsTrigger || colliderB.IsTrigger; |
| | 3855 | 284 | | bool shouldRaiseTriggerA = isTriggerPair && ColliderTriggerEventPolicy.ShouldRaise(colliderA, colliderB); |
| | 3855 | 285 | | bool shouldRaiseTriggerB = isTriggerPair && ColliderTriggerEventPolicy.ShouldRaise(colliderB, colliderA); |
| | 3855 | 286 | | _notificationInProgress = true; |
| | 3855 | 287 | | SwiftList<Exception>? notificationExceptions = null; |
| | | 288 | | try |
| | | 289 | | { |
| | 3855 | 290 | | if (isColliding) |
| | | 291 | | { |
| | 2848 | 292 | | bool notifyEnterA = isChanged && !_colliderANotified; |
| | 2848 | 293 | | _colliderANotified = true; |
| | 2848 | 294 | | colliderA.NotifyContact( |
| | 2848 | 295 | | colliderB, |
| | 2848 | 296 | | bodyB, |
| | 2848 | 297 | | isColliding: true, |
| | 2848 | 298 | | notifyEnterA, |
| | 2848 | 299 | | allowInactive: false, |
| | 2848 | 300 | | registrationA, |
| | 2848 | 301 | | registrationB, |
| | 2848 | 302 | | isTriggerPair, |
| | 2848 | 303 | | shouldRaiseTriggerA); |
| | 2845 | 304 | | if (registrationA.IsActive && registrationB.IsActive && !_separationPending) |
| | | 305 | | { |
| | 2837 | 306 | | bool notifyEnterB = isChanged && !_colliderBNotified; |
| | 2837 | 307 | | _colliderBNotified = true; |
| | 2837 | 308 | | colliderB.NotifyContact( |
| | 2837 | 309 | | colliderA, |
| | 2837 | 310 | | bodyA, |
| | 2837 | 311 | | isColliding: true, |
| | 2837 | 312 | | notifyEnterB, |
| | 2837 | 313 | | allowInactive: false, |
| | 2837 | 314 | | registrationB, |
| | 2837 | 315 | | registrationA, |
| | 2837 | 316 | | isTriggerPair, |
| | 2837 | 317 | | shouldRaiseTriggerB); |
| | | 318 | | } |
| | | 319 | | } |
| | | 320 | | else |
| | | 321 | | { |
| | 1007 | 322 | | NotifySeparation( |
| | 1007 | 323 | | registrationA, |
| | 1007 | 324 | | registrationB, |
| | 1007 | 325 | | bodyA, |
| | 1007 | 326 | | bodyB, |
| | 1007 | 327 | | isChanged, |
| | 1007 | 328 | | isTriggerPair, |
| | 1007 | 329 | | shouldRaiseTriggerA, |
| | 1007 | 330 | | shouldRaiseTriggerB); |
| | | 331 | | } |
| | 3849 | 332 | | } |
| | 6 | 333 | | catch (Exception exception) |
| | | 334 | | { |
| | 6 | 335 | | CollisionNotificationExceptions.Capture(ref notificationExceptions, exception); |
| | 6 | 336 | | } |
| | | 337 | | |
| | | 338 | | try |
| | | 339 | | { |
| | 3855 | 340 | | EndNotification( |
| | 3855 | 341 | | registrationA, |
| | 3855 | 342 | | registrationB, |
| | 3855 | 343 | | isTriggerPair, |
| | 3855 | 344 | | shouldRaiseTriggerA, |
| | 3855 | 345 | | shouldRaiseTriggerB); |
| | 3854 | 346 | | } |
| | 1 | 347 | | catch (Exception exception) |
| | | 348 | | { |
| | 1 | 349 | | CollisionNotificationExceptions.Capture(ref notificationExceptions, exception); |
| | 1 | 350 | | } |
| | | 351 | | |
| | 3855 | 352 | | _isCollidingChanged &= _isColliding |
| | 3855 | 353 | | & !(_colliderANotified & _colliderBNotified); |
| | | 354 | | |
| | 3855 | 355 | | CollisionNotificationExceptions.ThrowIfAny(notificationExceptions); |
| | 3848 | 356 | | } |
| | | 357 | | |
| | | 358 | | private void EndNotification( |
| | | 359 | | in ColliderLifetimeToken registrationA, |
| | | 360 | | in ColliderLifetimeToken registrationB, |
| | | 361 | | bool isTriggerPair, |
| | | 362 | | bool shouldRaiseTriggerA, |
| | | 363 | | bool shouldRaiseTriggerB) |
| | | 364 | | { |
| | | 365 | | try |
| | | 366 | | { |
| | 3855 | 367 | | if (!_separationPending) |
| | 3846 | 368 | | return; |
| | | 369 | | |
| | 9 | 370 | | SolidBody? bodyA = _pendingBodyA; |
| | 9 | 371 | | SolidBody? bodyB = _pendingBodyB; |
| | 9 | 372 | | ClearPendingNotificationState(); |
| | 9 | 373 | | NotifySeparation( |
| | 9 | 374 | | registrationA, |
| | 9 | 375 | | registrationB, |
| | 9 | 376 | | bodyA, |
| | 9 | 377 | | bodyB, |
| | 9 | 378 | | isChanged: true, |
| | 9 | 379 | | isTriggerPair, |
| | 9 | 380 | | shouldRaiseTriggerA, |
| | 9 | 381 | | shouldRaiseTriggerB); |
| | 8 | 382 | | } |
| | | 383 | | finally |
| | | 384 | | { |
| | 3855 | 385 | | ClearPendingNotificationState(); |
| | 3855 | 386 | | _notificationInProgress = false; |
| | 3855 | 387 | | } |
| | 3854 | 388 | | } |
| | | 389 | | |
| | | 390 | | private void NotifySeparation( |
| | | 391 | | in ColliderLifetimeToken registrationA, |
| | | 392 | | in ColliderLifetimeToken registrationB, |
| | | 393 | | SolidBody? bodyA, |
| | | 394 | | SolidBody? bodyB, |
| | | 395 | | bool isChanged, |
| | | 396 | | bool isTriggerPair, |
| | | 397 | | bool shouldRaiseTriggerA, |
| | | 398 | | bool shouldRaiseTriggerB) |
| | | 399 | | { |
| | 1016 | 400 | | bool notifyA = _colliderANotified; |
| | 1016 | 401 | | bool notifyB = _colliderBNotified; |
| | 1016 | 402 | | _colliderANotified = false; |
| | 1016 | 403 | | _colliderBNotified = false; |
| | | 404 | | |
| | 1016 | 405 | | SwiftList<Exception>? notificationExceptions = null; |
| | 1016 | 406 | | if (notifyA) |
| | | 407 | | { |
| | | 408 | | try |
| | | 409 | | { |
| | 67 | 410 | | registrationA.Collider.NotifyContact( |
| | 67 | 411 | | registrationB.Collider, |
| | 67 | 412 | | bodyB, |
| | 67 | 413 | | isColliding: false, |
| | 67 | 414 | | isChanged, |
| | 67 | 415 | | allowInactive: true, |
| | 67 | 416 | | registrationA, |
| | 67 | 417 | | registrationB, |
| | 67 | 418 | | isTriggerPair, |
| | 67 | 419 | | shouldRaiseTriggerA); |
| | 63 | 420 | | } |
| | 4 | 421 | | catch (Exception exception) |
| | | 422 | | { |
| | 4 | 423 | | CollisionNotificationExceptions.Capture(ref notificationExceptions, exception); |
| | 4 | 424 | | } |
| | | 425 | | } |
| | | 426 | | |
| | 1016 | 427 | | if (notifyB && registrationB.IsCurrentLifetime) |
| | | 428 | | { |
| | | 429 | | try |
| | | 430 | | { |
| | 56 | 431 | | registrationB.Collider.NotifyContact( |
| | 56 | 432 | | registrationA.Collider, |
| | 56 | 433 | | bodyA, |
| | 56 | 434 | | isColliding: false, |
| | 56 | 435 | | isChanged, |
| | 56 | 436 | | allowInactive: true, |
| | 56 | 437 | | registrationB, |
| | 56 | 438 | | registrationA, |
| | 56 | 439 | | isTriggerPair, |
| | 56 | 440 | | shouldRaiseTriggerB); |
| | 55 | 441 | | } |
| | 1 | 442 | | catch (Exception exception) |
| | | 443 | | { |
| | 1 | 444 | | CollisionNotificationExceptions.Capture(ref notificationExceptions, exception); |
| | 1 | 445 | | } |
| | | 446 | | } |
| | | 447 | | |
| | 1016 | 448 | | CollisionNotificationExceptions.ThrowIfAny(notificationExceptions); |
| | 1012 | 449 | | } |
| | | 450 | | |
| | | 451 | | private void ClearPendingNotificationState() |
| | | 452 | | { |
| | 4504 | 453 | | _separationPending = false; |
| | 4504 | 454 | | _pendingBodyA = null; |
| | 4504 | 455 | | _pendingBodyB = null; |
| | 4504 | 456 | | } |
| | | 457 | | |
| | | 458 | | private void HandleCullingIfNotColliding() |
| | | 459 | | { |
| | 4317 | 460 | | if (_isColliding) |
| | | 461 | | { |
| | 2488 | 462 | | LastCollidedFrame = Context.FrameCount; |
| | 2488 | 463 | | return; |
| | | 464 | | } |
| | | 465 | | |
| | 1829 | 466 | | CalculateCullScore(); |
| | 1829 | 467 | | } |
| | | 468 | | |
| | | 469 | | internal bool TryPreserveSleepingRestingContact() |
| | | 470 | | { |
| | 4728 | 471 | | if (!_isColliding || !Manifold.HasContact) |
| | 1884 | 472 | | return false; |
| | | 473 | | |
| | 2844 | 474 | | if (ColliderA.Body?.IsSleeping != true && ColliderB.Body?.IsSleeping != true) |
| | 2454 | 475 | | return false; |
| | | 476 | | |
| | 390 | 477 | | LastCollidedFrame = Context.FrameCount; |
| | 390 | 478 | | return true; |
| | | 479 | | } |
| | | 480 | | |
| | | 481 | | private bool CheckCollision() |
| | | 482 | | { |
| | 2504 | 483 | | if (!BroadPhaseVersionChanged() && _isColliding) |
| | 19 | 484 | | return _isColliding; |
| | | 485 | | |
| | 2485 | 486 | | return CollisionDetection.DoCollisionCheck(this); |
| | | 487 | | } |
| | | 488 | | |
| | | 489 | | private bool IsCullStateInvalidated() |
| | | 490 | | { |
| | 4318 | 491 | | return ColliderA.PartitionChanged |
| | 4318 | 492 | | || ColliderB.PartitionChanged |
| | 4318 | 493 | | || BroadPhaseVersionChanged(); |
| | | 494 | | } |
| | | 495 | | |
| | | 496 | | private bool BroadPhaseVersionChanged() |
| | | 497 | | { |
| | 2601 | 498 | | return ColliderA.BroadPhaseVersion != _lastColliderABroadPhaseVersion |
| | 2601 | 499 | | || ColliderB.BroadPhaseVersion != _lastColliderBBroadPhaseVersion; |
| | | 500 | | } |
| | | 501 | | |
| | | 502 | | private void RefreshBroadPhaseVersions() |
| | | 503 | | { |
| | 4956 | 504 | | _lastColliderABroadPhaseVersion = ColliderA.BroadPhaseVersion; |
| | 4956 | 505 | | _lastColliderBBroadPhaseVersion = ColliderB.BroadPhaseVersion; |
| | 4956 | 506 | | } |
| | | 507 | | |
| | | 508 | | private bool ShouldPerformCollisionCheck() |
| | | 509 | | { |
| | | 510 | | // Center distance remains a scheduling signal only. Canonical geometry |
| | | 511 | | // may extend beyond a scalar face while its conservative broad-phase |
| | | 512 | | // bounds are clipped to the representable domain, which can shorten |
| | | 513 | | // Bounds.Scope without shortening the physical shape. |
| | 4317 | 514 | | _fastDistance = Vector3d.DistanceSquared(ColliderA.Center, ColliderB.Center); |
| | | 515 | | |
| | | 516 | | // Inclusive bounds overlap preserves zero-depth touching contacts for the manifold pass. |
| | 4317 | 517 | | return BoundsOverlapInclusive(ColliderA, ColliderB); |
| | | 518 | | } |
| | | 519 | | |
| | | 520 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 521 | | private static bool BoundsOverlapInclusive(LSCollider colliderA, LSCollider colliderB) |
| | | 522 | | { |
| | 4317 | 523 | | return colliderA.BoundsMin.X <= colliderB.BoundsMax.X |
| | 4317 | 524 | | && colliderA.BoundsMax.X >= colliderB.BoundsMin.X |
| | 4317 | 525 | | && colliderA.BoundsMin.Y <= colliderB.BoundsMax.Y |
| | 4317 | 526 | | && colliderA.BoundsMax.Y >= colliderB.BoundsMin.Y |
| | 4317 | 527 | | && colliderA.BoundsMin.Z <= colliderB.BoundsMax.Z |
| | 4317 | 528 | | && colliderA.BoundsMax.Z >= colliderB.BoundsMin.Z; |
| | | 529 | | } |
| | | 530 | | |
| | | 531 | | private void CalculateCullScore() |
| | | 532 | | { |
| | 1829 | 533 | | int distanceScore = 0; |
| | 1829 | 534 | | int velocityScore = 0; |
| | 1829 | 535 | | if (!_preventDistanceCull) |
| | | 536 | | { |
| | 1683 | 537 | | int distanceMax = Context.Environment.CullDistanceMax; |
| | 1683 | 538 | | if (distanceMax > 0) |
| | | 539 | | { |
| | 1682 | 540 | | int step = GetCullDistanceStep(World!); |
| | 1682 | 541 | | distanceScore = Math.Clamp((int)(_fastDistance - _fastDistanceOffset) / step + Context.Collisions.CullDi |
| | | 542 | | } |
| | | 543 | | |
| | 1683 | 544 | | int cullVelocityStep = Context.Environment.CullVelocityStep; |
| | 1683 | 545 | | if (cullVelocityStep > 0) |
| | 1682 | 546 | | velocityScore = Math.Clamp((int)(ColliderA.Velocity - ColliderB.Velocity).Magnitude / cullVelocityStep, |
| | | 547 | | } |
| | | 548 | | |
| | 1829 | 549 | | int timeScore = 0; |
| | 1829 | 550 | | int cullTimeStep = Context.Environment.CullTimeStep; |
| | 1829 | 551 | | if (cullTimeStep > 0) |
| | 1828 | 552 | | timeScore = Math.Clamp((Context.FrameCount - LastCollidedFrame) / cullTimeStep, 0, Context.Environment.CullT |
| | | 553 | | |
| | 1829 | 554 | | CullCounter = (short)Math.Clamp(distanceScore + timeScore - velocityScore, 0, short.MaxValue); |
| | 1829 | 555 | | } |
| | | 556 | | |
| | | 557 | | /// <summary> |
| | | 558 | | /// Defines the step value for distance-based culling. The score is increased |
| | | 559 | | /// when the distance between objects increases. Higher values make the culling more aggressive for distant objects. |
| | | 560 | | /// </summary> |
| | | 561 | | private int GetCullDistanceStep(GridWorld world) |
| | | 562 | | { |
| | 1682 | 563 | | int distanceMax = Context.Environment.CullDistanceMax; |
| | 1682 | 564 | | Fixed64 cellEdge = GridTopologyMetricUtility.GetRepresentativeCellEdge(world); |
| | 1682 | 565 | | int step = ((cellEdge + Fixed64.One * 2) * (cellEdge + Fixed64.One * 2) / distanceMax).CeilToInt(); |
| | 1682 | 566 | | return Math.Max(1, step); |
| | | 567 | | } |
| | | 568 | | |
| | | 569 | | /// <summary>Clears transient contact and notification state before reuse.</summary> |
| | | 570 | | public void Reset() |
| | | 571 | | { |
| | 640 | 572 | | Manifold.Reset(); |
| | 640 | 573 | | _warmStart.Clear(); |
| | 640 | 574 | | _isColliding = false; |
| | 640 | 575 | | _isCollidingChanged = false; |
| | 640 | 576 | | _isPooledForDeactivation = false; |
| | 640 | 577 | | _notificationInProgress = false; |
| | 640 | 578 | | _colliderANotified = false; |
| | 640 | 579 | | _colliderBNotified = false; |
| | 640 | 580 | | ClearPendingNotificationState(); |
| | 640 | 581 | | } |
| | | 582 | | |
| | | 583 | | internal void StoreWarmStartImpulse( |
| | | 584 | | ulong contactId, |
| | | 585 | | Vector3d normal, |
| | | 586 | | Fixed64 normalImpulse, |
| | | 587 | | Fixed64 tangentImpulse, |
| | | 588 | | Fixed64 secondaryTangentImpulse = default) => |
| | 9118 | 589 | | _warmStart.Set(contactId, normal, normalImpulse, tangentImpulse, secondaryTangentImpulse); |
| | | 590 | | |
| | | 591 | | internal bool TryGetWarmStartImpulse(ulong contactId, out ContactWarmStartImpulse impulse) => |
| | 9132 | 592 | | _warmStart.TryGet(contactId, out impulse); |
| | | 593 | | |
| | | 594 | | internal void RemoveWarmStartImpulse(ulong contactId) => |
| | 11 | 595 | | _warmStart.Remove(contactId); |
| | | 596 | | |
| | 2 | 597 | | internal void ClearWarmStart() => _warmStart.Clear(); |
| | | 598 | | |
| | | 599 | | /// <summary> |
| | | 600 | | /// Deactivates the CollisionPair. |
| | | 601 | | /// </summary> |
| | | 602 | | public void Deactivate() |
| | | 603 | | { |
| | 39 | 604 | | bool notifySeparation = _isColliding || _colliderANotified || _colliderBNotified; |
| | 39 | 605 | | if (notifySeparation) |
| | | 606 | | { |
| | 30 | 607 | | _isColliding = false; |
| | 30 | 608 | | _isCollidingChanged = true; |
| | 30 | 609 | | if (_notificationInProgress) |
| | | 610 | | { |
| | 9 | 611 | | _separationPending = true; |
| | 9 | 612 | | _pendingBodyA = ColliderA.Body; |
| | 9 | 613 | | _pendingBodyB = ColliderB.Body; |
| | | 614 | | } |
| | | 615 | | } |
| | | 616 | | |
| | 39 | 617 | | Manifold.Reset(); |
| | 39 | 618 | | _warmStart.Clear(); |
| | 39 | 619 | | _isPooledForDeactivation = false; |
| | 39 | 620 | | Active = false; |
| | | 621 | | |
| | 39 | 622 | | if (notifySeparation && !_notificationInProgress) |
| | 21 | 623 | | NotifyCollidersOfContact(); |
| | 38 | 624 | | } |
| | | 625 | | } |