| | | 1 | | //======================================================================= |
| | | 2 | | // GravitasPhysics2DService.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.Runtime.CompilerServices; |
| | | 9 | | using Gravitas.Colliders; |
| | | 10 | | using Gravitas.CollisionHandling; |
| | | 11 | | using SwiftCollections; |
| | | 12 | | |
| | | 13 | | namespace Gravitas; |
| | | 14 | | |
| | | 15 | | /// <summary> |
| | | 16 | | /// Context-owned pure 2D body, broad-phase, narrow-phase, response, and query service. |
| | | 17 | | /// </summary> |
| | | 18 | | /// <content>Owns registration, static continuity, collision passes and dimensional solver lifecycle.</content> |
| | | 19 | | public sealed partial class GravitasPhysics2DService |
| | | 20 | | { |
| | 1 | 21 | | private static readonly CollisionPair2DStableKeyComparer ResponsePairComparer = new(); |
| | 1 | 22 | | private static readonly IslandNodeKeyComparer<DiscreteIslandNode2D> IslandNodeComparer = new(); |
| | 1 | 23 | | private static readonly DiscreteIslandConstraint2DComparer IslandConstraintComparer = new(); |
| | | 24 | | |
| | | 25 | | private readonly GravitasWorldContext _context; |
| | 10191 | 26 | | private readonly SwiftSlotMap<SolidBody2D> _dynamicBodies = new(); |
| | 10191 | 27 | | private readonly ColliderRegistry<LSCollider2D> _colliders = new(); |
| | 10191 | 28 | | private readonly SwiftList<LSCollider2D> _serviceRefreshColliders = new(); |
| | 10191 | 29 | | private readonly SwiftList<StaticSurface2D> _staticSurfaces = new(); |
| | 10191 | 30 | | private readonly SwiftHashSet<ulong> _processedPairKeys = new(); |
| | 10191 | 31 | | private readonly SwiftDictionary<ulong, CollisionPair2D> _pairs = new(); |
| | 10191 | 32 | | private readonly SwiftList<ulong> _pairsToRemove = new(); |
| | 10191 | 33 | | private readonly SwiftList<CollisionPair2D> _pairsPendingSeparation = new(); |
| | 10191 | 34 | | private readonly SwiftStack<CollisionPair2D> _cachedPairs = new(); |
| | 10191 | 35 | | private readonly SwiftList<CollisionPair2D> _discreteResponsePairs = new(); |
| | 10191 | 36 | | private readonly SwiftList<CollisionPair2D> _existingResponsePairCandidates = new(); |
| | 10191 | 37 | | private readonly SwiftHashSet<int> _discreteResponseBodyKeys = new(); |
| | 10191 | 38 | | private readonly SwiftList<int> _discreteResponseBodyQueue = new(); |
| | 10191 | 39 | | private readonly SwiftList<DiscreteIslandNode2D> _discreteIslandNodes = new(); |
| | 10191 | 40 | | private readonly SwiftList<DiscreteIslandConstraint2D> _discreteIslandConstraints = new(); |
| | 10191 | 41 | | private readonly DynamicCcdCandidateIndex2D _planarContinuousCollisionCandidates = new(); |
| | 10191 | 42 | | private readonly DynamicCcdCandidateIndex2D _dirtyPlanarContinuousCollisionCandidates = |
| | 10191 | 43 | | new(supportsUpdates: true); |
| | 10191 | 44 | | private readonly SwiftList<SolidBody2D> _dirtyPlanarContinuousCollisionBodies = new(); |
| | 10191 | 45 | | private readonly SwiftHashSet<SolidBody2D> _dirtyPlanarContinuousCollisionBodySet = new(); |
| | 10191 | 46 | | private readonly DynamicCcdCandidateIndex _mixedContinuousCollisionCandidates = new(); |
| | 10191 | 47 | | private readonly DynamicCcdCandidateIndex _dirtyMixedContinuousCollisionCandidates = |
| | 10191 | 48 | | new(supportsUpdates: true); |
| | 10191 | 49 | | private readonly SwiftList<ColliderLifetimeToken2D> _continuousCollisionCandidateLifetimes = new(); |
| | 10191 | 50 | | private readonly SwiftList<int> _continuousCollisionCandidateIds = new(); |
| | 10191 | 51 | | private readonly SwiftList<int> _dirtyContinuousCollisionCandidateIds = new(); |
| | 10191 | 52 | | private readonly SwiftHashSet<SolidBody2D> _processedContinuousCollisionBodies = new(); |
| | 10191 | 53 | | private readonly SwiftHashSet<SolidBody2D> _queuedContinuousCollisionHandoffBodies = new(); |
| | 10191 | 54 | | private readonly SwiftList<SolidBody2D> _continuousCollisionHandoffQueue = new(); |
| | 10191 | 55 | | private long _continuousCollisionPreparedToken = -1; |
| | | 56 | | private bool _continuousCollisionPreparedMixedIndex; |
| | | 57 | | |
| | | 58 | | /// <summary>Creates the pure 2D physics service for a world context.</summary> |
| | 10191 | 59 | | public GravitasPhysics2DService(GravitasWorldContext context) |
| | | 60 | | { |
| | 10191 | 61 | | ChronicleGuard.ThrowIfNull(context, nameof(context)); |
| | 10191 | 62 | | _context = context; |
| | 10191 | 63 | | } |
| | | 64 | | |
| | | 65 | | /// <summary>Gets the number of registered dynamic bodies.</summary> |
| | | 66 | | public int BodyCount { get; private set; } |
| | | 67 | | |
| | | 68 | | /// <summary>Gets the number of registered 2D colliders.</summary> |
| | 67154 | 69 | | public int ColliderCount => _colliders.Count; |
| | | 70 | | |
| | | 71 | | /// <summary>Gets or sets whether this service advances pure 2D physics.</summary> |
| | | 72 | | public bool SimulatePhysics { get; set; } = true; |
| | | 73 | | |
| | | 74 | | internal int LastBroadPhaseCandidateCount { get; private set; } |
| | | 75 | | |
| | | 76 | | /// <summary> |
| | | 77 | | /// Gets how many service-owned continuous-collision handoff batches were consumed during the last late step. |
| | | 78 | | /// </summary> |
| | | 79 | | public int LastContinuousCollisionIslandCount { get; private set; } |
| | | 80 | | |
| | | 81 | | /// <summary> |
| | | 82 | | /// Gets how many service-owned continuous-collision handoff iterations were consumed during the last late step. |
| | | 83 | | /// </summary> |
| | | 84 | | public int LastContinuousCollisionIslandIterationCount { get; private set; } |
| | | 85 | | |
| | | 86 | | /// <summary> |
| | | 87 | | /// Gets whether the service-owned continuous-collision handoff queue hit its deterministic iteration cap. |
| | | 88 | | /// </summary> |
| | | 89 | | public bool LastContinuousCollisionIslandLimitReached { get; private set; } |
| | | 90 | | |
| | | 91 | | internal void AssimilateBody(SolidBody2D body, PhysicsMobilityType motionType) |
| | | 92 | | { |
| | 6459 | 93 | | ChronicleGuard.ThrowIfNull(body, nameof(body)); |
| | 6459 | 94 | | ChronicleGuard.ThrowIfArgument( |
| | 6459 | 95 | | !ReferenceEquals(body.Context, _context), |
| | 6459 | 96 | | nameof(body), |
| | 6459 | 97 | | "2D body must belong to this physics service context."); |
| | | 98 | | |
| | 6459 | 99 | | if (motionType != PhysicsMobilityType.Static) |
| | | 100 | | { |
| | 4789 | 101 | | body.DynamicId = _dynamicBodies.Add(body); |
| | 4789 | 102 | | _context.ReserveSleepIslands(); |
| | 9537 | 103 | | while (_continuousCollisionCandidateLifetimes.Count <= body.DynamicId) |
| | 4748 | 104 | | _continuousCollisionCandidateLifetimes.Add(default); |
| | 4789 | 105 | | BodyCount++; |
| | | 106 | | } |
| | | 107 | | |
| | 6459 | 108 | | AssimilateCollider(body.Collider); |
| | 6459 | 109 | | } |
| | | 110 | | |
| | | 111 | | internal void RefreshBodyMotionTypeRegistration( |
| | | 112 | | SolidBody2D body, |
| | | 113 | | PhysicsMobilityType previousMotionType) |
| | | 114 | | { |
| | 152 | 115 | | if (!body.IsStatic) body.Collider.StaticSurface?.Remove(body.Collider); |
| | 84 | 116 | | if (previousMotionType == PhysicsMobilityType.Static) |
| | | 117 | | { |
| | 15 | 118 | | body.DynamicId = _dynamicBodies.Add(body); |
| | 15 | 119 | | _context.ReserveSleepIslands(); |
| | 24 | 120 | | while (_continuousCollisionCandidateLifetimes.Count <= body.DynamicId) |
| | 9 | 121 | | _continuousCollisionCandidateLifetimes.Add(default); |
| | 15 | 122 | | BodyCount++; |
| | 15 | 123 | | return; |
| | | 124 | | } |
| | | 125 | | |
| | 69 | 126 | | if (body.MotionType == PhysicsMobilityType.Static) |
| | | 127 | | { |
| | 16 | 128 | | int previousDynamicId = body.DynamicId; |
| | 16 | 129 | | _dynamicBodies.RemoveAt(previousDynamicId); |
| | 16 | 130 | | _continuousCollisionCandidateLifetimes[previousDynamicId] = default; |
| | 16 | 131 | | ReleaseContinuousCollisionCandidateRefresh(body); |
| | 16 | 132 | | body.DynamicId = -1; |
| | 16 | 133 | | BodyCount--; |
| | | 134 | | } |
| | 69 | 135 | | } |
| | | 136 | | |
| | | 137 | | internal void AssimilateCollider(LSCollider2D collider) |
| | | 138 | | { |
| | 9455 | 139 | | ChronicleGuard.ThrowIfNull(collider, nameof(collider)); |
| | 9455 | 140 | | ChronicleGuard.ThrowIfArgument( |
| | 9455 | 141 | | !ReferenceEquals(collider.Context, _context), |
| | 9455 | 142 | | nameof(collider), |
| | 9455 | 143 | | "2D collider must belong to this physics service context."); |
| | | 144 | | |
| | 9455 | 145 | | _colliders.Register(collider); |
| | 9455 | 146 | | RefreshColliderServiceRefreshRegistration(collider); |
| | 9455 | 147 | | _context.Collisions2D.PartitionCollider(collider); |
| | 9455 | 148 | | } |
| | | 149 | | |
| | | 150 | | internal void DessimilateBody(SolidBody2D body) |
| | | 151 | | { |
| | 208 | 152 | | ChronicleGuard.ThrowIfNull(body, nameof(body)); |
| | 208 | 153 | | if (body.DynamicId >= 0 && _dynamicBodies.TryRemoveAt(body.DynamicId)) |
| | | 154 | | { |
| | 161 | 155 | | _continuousCollisionCandidateLifetimes[body.DynamicId] = default; |
| | 161 | 156 | | ReleaseContinuousCollisionCandidateRefresh(body); |
| | 161 | 157 | | BodyCount--; |
| | | 158 | | } |
| | | 159 | | |
| | 208 | 160 | | LSCollider2D collider = body.Collider; |
| | 208 | 161 | | DessimilateCollider(collider); |
| | 208 | 162 | | } |
| | | 163 | | |
| | | 164 | | internal void DessimilateCollider(LSCollider2D collider) |
| | | 165 | | { |
| | 504 | 166 | | ChronicleGuard.ThrowIfNull(collider, nameof(collider)); |
| | 504 | 167 | | collider.IsDeactivationInProgress = true; |
| | | 168 | | try |
| | | 169 | | { |
| | 504 | 170 | | collider.StaticSurface?.Remove(collider); |
| | 504 | 171 | | if (collider.Body != null) |
| | 208 | 172 | | _context.Constraints2D.RemoveJointsForBody(collider.Body); |
| | 504 | 173 | | RemovePairsForCollider(collider); |
| | 504 | 174 | | _context.MixedCollisions.RemovePairsFor2DCollider(collider); |
| | 504 | 175 | | if (collider.IsMixedPartitioned) |
| | 97 | 176 | | _context.MixedCollisions.ClearPartitioned2DCollider(collider, force: true); |
| | 504 | 177 | | if (collider.IsPartitioned) |
| | 466 | 178 | | _context.Collisions2D.ClearPartitionedCollider(collider, force: true); |
| | | 179 | | |
| | 504 | 180 | | RemoveServiceRefreshCollider(collider); |
| | 504 | 181 | | _colliders.Remove(collider); |
| | 504 | 182 | | } |
| | | 183 | | finally |
| | | 184 | | { |
| | 504 | 185 | | collider.IsDeactivationInProgress = false; |
| | 504 | 186 | | } |
| | 504 | 187 | | } |
| | | 188 | | |
| | | 189 | | /// <summary>Creates a context-owned continuity declaration for static filled planar pieces.</summary> |
| | | 190 | | /// <param name="members">Distinct active bodyless or explicit-static AABB or convex polygon colliders registered in |
| | | 191 | | /// <returns>A declaration that retains each member's collider identity and joins only exact shared boundary spans.< |
| | | 192 | | /// <remarks>Call between complete fixed steps. Compound roots/leaves are ineligible. Context reset or disposal expi |
| | | 193 | | /// <exception cref="System.ArgumentException">A member is ineligible or already belongs to a declaration.</exceptio |
| | | 194 | | /// <exception cref="System.ObjectDisposedException">The context has ended.</exception> |
| | | 195 | | /// <exception cref="System.InvalidOperationException">A fixed step is in progress.</exception> |
| | | 196 | | public StaticSurface2D CreateStaticSurface(System.ReadOnlySpan<LSCollider2D> members) |
| | | 197 | | { |
| | 600 | 198 | | var surface = new StaticSurface2D(_context, members); |
| | 597 | 199 | | _staticSurfaces.Add(surface); |
| | 597 | 200 | | return surface; |
| | | 201 | | } |
| | | 202 | | |
| | 584 | 203 | | internal void RemoveStaticSurface(StaticSurface2D surface) => _staticSurfaces.Remove(surface); |
| | | 204 | | |
| | | 205 | | internal void ReleaseStaticSurfaces() |
| | | 206 | | { |
| | 20655 | 207 | | foreach (StaticSurface2D surface in _staticSurfaces) surface.Release(); |
| | 10308 | 208 | | _staticSurfaces.Clear(); |
| | 10308 | 209 | | } |
| | | 210 | | |
| | | 211 | | /// <summary>Runs the authoritative pure 2D simulation phase.</summary> |
| | | 212 | | public void Simulate() |
| | | 213 | | { |
| | 23870 | 214 | | _context.EnterSimulationPhase(); |
| | | 215 | | try |
| | | 216 | | { |
| | 23870 | 217 | | if (!SimulatePhysics) |
| | 16 | 218 | | return; |
| | | 219 | | |
| | 23854 | 220 | | LastBroadPhaseCandidateCount = 0; |
| | 23854 | 221 | | } |
| | | 222 | | finally |
| | | 223 | | { |
| | 23870 | 224 | | _context.ExitSimulationPhase(); |
| | 23870 | 225 | | } |
| | 23870 | 226 | | } |
| | | 227 | | |
| | | 228 | | /// <summary>Completes deferred pure 2D collision, response, and bookkeeping work.</summary> |
| | | 229 | | public void LateSimulate() |
| | | 230 | | { |
| | 5 | 231 | | _context.EnterSimulationPhase(); |
| | | 232 | | try |
| | | 233 | | { |
| | 5 | 234 | | if (!BeginLateSimulateBodies(continuousCollisionFramePrepared: false)) |
| | 1 | 235 | | return; |
| | | 236 | | |
| | 4 | 237 | | ProcessQueuedContinuousCollisionHandoffs(); |
| | 4 | 238 | | CompleteLateSimulatePhysicsStep(); |
| | 4 | 239 | | PublishCollisionPositionStates(); |
| | 4 | 240 | | } |
| | | 241 | | finally |
| | | 242 | | { |
| | 5 | 243 | | _context.ExitSimulationPhase(); |
| | 5 | 244 | | } |
| | 5 | 245 | | } |
| | | 246 | | |
| | | 247 | | internal bool BeginLateSimulateBodies(bool continuousCollisionFramePrepared) |
| | | 248 | | { |
| | 24322 | 249 | | if (!SimulatePhysics) |
| | 1 | 250 | | return false; |
| | | 251 | | |
| | 24321 | 252 | | if (!continuousCollisionFramePrepared) |
| | 18 | 253 | | _context.AdvanceLateSimulateToken(); |
| | | 254 | | |
| | 24321 | 255 | | PrepareContinuousCollisionFrame(); |
| | 24321 | 256 | | BeginContinuousCollisionHandoffFrame(); |
| | | 257 | | |
| | 79335 | 258 | | foreach (SolidBody2D body in _dynamicBodies) |
| | | 259 | | { |
| | 15347 | 260 | | body.LateSimulate(updateSleepState: false, updateColliderState: false); |
| | 15346 | 261 | | _processedContinuousCollisionBodies.Add(body); |
| | | 262 | | } |
| | | 263 | | |
| | 24320 | 264 | | return true; |
| | | 265 | | } |
| | | 266 | | |
| | | 267 | | internal void CompleteLateSimulatePhysicsStep(bool deferSleep = false) |
| | | 268 | | { |
| | 24303 | 269 | | PrepareCollisionPartitions(); |
| | 24303 | 270 | | RunDiscreteCollisionStep(); |
| | 24302 | 271 | | if (!deferSleep) |
| | 8318 | 272 | | _context.UpdateAutomaticSleep(ran3D: false, ran2D: true); |
| | 24302 | 273 | | } |
| | | 274 | | |
| | | 275 | | private bool _collisionPositionPublicationPending; |
| | | 276 | | |
| | 3847 | 277 | | internal void MarkCollisionPositionPublicationPending() => _collisionPositionPublicationPending = true; |
| | | 278 | | |
| | | 279 | | internal void PublishCollisionPositionStates() |
| | | 280 | | { |
| | 113451 | 281 | | if (!_collisionPositionPublicationPending) |
| | 110313 | 282 | | return; |
| | 3138 | 283 | | _collisionPositionPublicationPending = false; |
| | 13762 | 284 | | foreach (SolidBody2D body in _dynamicBodies) |
| | 3743 | 285 | | body.PublishCollisionPositionState(); |
| | 3138 | 286 | | } |
| | | 287 | | |
| | | 288 | | private void PrepareCollisionPartitions() |
| | | 289 | | { |
| | 79238 | 290 | | foreach (SolidBody2D body in _dynamicBodies) |
| | 15316 | 291 | | body.Collider.Simulate(); |
| | | 292 | | |
| | 100740 | 293 | | for (int i = 0; i < _serviceRefreshColliders.Count; i++) |
| | 26067 | 294 | | _serviceRefreshColliders[i].Simulate(); |
| | 24303 | 295 | | } |
| | | 296 | | |
| | | 297 | | private void RunDiscreteCollisionStep() |
| | | 298 | | { |
| | 24303 | 299 | | LastBroadPhaseCandidateCount = 0; |
| | 24303 | 300 | | EnsureFrameCapacity(); |
| | 24303 | 301 | | _processedPairKeys.Clear(); |
| | 24303 | 302 | | _discreteResponsePairs.FastClear(); |
| | 24303 | 303 | | long frame = _context.FrameCount; |
| | 24303 | 304 | | _context.Collisions2D.CheckAndDistributeCollisions(); |
| | 24303 | 305 | | ExpandDiscreteResponsePairs(frame); |
| | 24303 | 306 | | SolveDiscreteResponsePairs(); |
| | 24302 | 307 | | RefreshGroundingFromDiscreteResponse(frame); |
| | 24302 | 308 | | CleanupUntouchedPairs(frame); |
| | 24302 | 309 | | _context.Collisions2D.RetireExpiredRetainedPartitions(); |
| | 24302 | 310 | | } |
| | | 311 | | |
| | | 312 | | /// <summary>Publishes interpolated presentation state for registered 2D bodies.</summary> |
| | | 313 | | public void Visualize() |
| | | 314 | | { |
| | 86 | 315 | | foreach (SolidBody2D body in _dynamicBodies) |
| | 21 | 316 | | body.OnVisualize(); |
| | 22 | 317 | | } |
| | | 318 | | |
| | | 319 | | /// <summary>Clears all context-local pure 2D registration and solver state.</summary> |
| | | 320 | | public void Reset() |
| | | 321 | | { |
| | 117 | 322 | | _collisionPositionPublicationPending = false; |
| | 117 | 323 | | ReleaseStaticSurfaces(); |
| | 117 | 324 | | DiscardContinuousCollisionHandoffQueue(); |
| | | 325 | | // FastClear during simulation can leave references beyond the current count. |
| | 117 | 326 | | _continuousCollisionHandoffQueue.Clear(clearRetainedReferences: true); |
| | 117 | 327 | | _dynamicBodies.Clear(); |
| | 117 | 328 | | _colliders.Clear(); |
| | 117 | 329 | | _serviceRefreshColliders.Clear(); |
| | 117 | 330 | | _groundingBodySnapshot.Clear(clearRetainedReferences: true); |
| | 117 | 331 | | _groundingPairSnapshot.Clear(clearRetainedReferences: true); |
| | 117 | 332 | | _processedPairKeys.Clear(); |
| | 117 | 333 | | _pairs.Clear(); |
| | 117 | 334 | | _pairsToRemove.FastClear(); |
| | 117 | 335 | | _pairsPendingSeparation.Clear(clearRetainedReferences: true); |
| | 117 | 336 | | _cachedPairs.Clear(); |
| | 117 | 337 | | _discreteResponsePairs.Clear(clearRetainedReferences: true); |
| | 117 | 338 | | _existingResponsePairCandidates.Clear(clearRetainedReferences: true); |
| | 117 | 339 | | _discreteResponseBodyKeys.Clear(); |
| | 117 | 340 | | _discreteResponseBodyQueue.FastClear(); |
| | 117 | 341 | | _discreteIslandNodes.Clear(clearRetainedReferences: true); |
| | 117 | 342 | | _discreteIslandConstraints.Clear(clearRetainedReferences: true); |
| | 117 | 343 | | _planarContinuousCollisionCandidates.Clear(); |
| | 117 | 344 | | _dirtyPlanarContinuousCollisionCandidates.Clear(); |
| | 117 | 345 | | _dirtyPlanarContinuousCollisionBodies.Clear(clearRetainedReferences: true); |
| | 117 | 346 | | _dirtyPlanarContinuousCollisionBodySet.Clear(); |
| | 117 | 347 | | _mixedContinuousCollisionCandidates.Clear(); |
| | 117 | 348 | | _dirtyMixedContinuousCollisionCandidates.Clear(); |
| | 117 | 349 | | _continuousCollisionCandidateLifetimes.Clear(); |
| | 117 | 350 | | _continuousCollisionCandidateIds.FastClear(); |
| | 117 | 351 | | _dirtyContinuousCollisionCandidateIds.FastClear(); |
| | 117 | 352 | | _processedContinuousCollisionBodies.Clear(); |
| | 117 | 353 | | _continuousCollisionPreparedToken = -1; |
| | 117 | 354 | | _continuousCollisionPreparedMixedIndex = false; |
| | 117 | 355 | | BodyCount = 0; |
| | 117 | 356 | | LastBroadPhaseCandidateCount = 0; |
| | 117 | 357 | | LastContinuousCollisionIslandCount = 0; |
| | 117 | 358 | | LastContinuousCollisionIslandIterationCount = 0; |
| | 117 | 359 | | LastContinuousCollisionIslandLimitReached = false; |
| | 117 | 360 | | } |
| | | 361 | | |
| | | 362 | | internal bool TryGetColliderById(int colliderId, out LSCollider2D? collider) => |
| | 888957 | 363 | | _colliders.TryGetById(colliderId, out collider); |
| | | 364 | | |
| | | 365 | | internal bool TryGetColliderByServiceIndex(int serviceIndex, out LSCollider2D? collider) => |
| | 2 | 366 | | _colliders.TryGetByServiceIndex(serviceIndex, out collider); |
| | | 367 | | |
| | 33381 | 368 | | internal LSCollider2D GetColliderByServiceIndex(int serviceIndex) => _colliders[serviceIndex]; |
| | | 369 | | |
| | 4578 | 370 | | internal SwiftList<LSCollider2D> PrepareReplayColliders() => _colliders.PrepareReplayColliders(); |
| | | 371 | | |
| | | 372 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 373 | | internal bool TryGetDynamicBody(int dynamicId, out SolidBody2D body) => |
| | 6480 | 374 | | _dynamicBodies.TryGetValue(dynamicId, out body); |
| | | 375 | | |
| | | 376 | | internal void RefreshColliderServiceRefreshRegistration(LSCollider2D collider) |
| | | 377 | | { |
| | 9539 | 378 | | ChronicleGuard.ThrowIfNull(collider, nameof(collider)); |
| | | 379 | | |
| | 9539 | 380 | | if (collider.RequiresServiceSideRefresh) |
| | 4682 | 381 | | AddServiceRefreshCollider(collider); |
| | | 382 | | else |
| | 4857 | 383 | | RemoveServiceRefreshCollider(collider); |
| | 4857 | 384 | | } |
| | | 385 | | |
| | | 386 | | private void AddServiceRefreshCollider(LSCollider2D collider) |
| | | 387 | | { |
| | 4682 | 388 | | collider.SetServiceRefreshIndex(_serviceRefreshColliders.Count); |
| | 4682 | 389 | | _serviceRefreshColliders.Add(collider); |
| | 4682 | 390 | | } |
| | | 391 | | |
| | | 392 | | private void RemoveServiceRefreshCollider(LSCollider2D collider) |
| | | 393 | | { |
| | 5361 | 394 | | int index = collider.ServiceRefreshIndex; |
| | 5361 | 395 | | if (index < 0 || !ReferenceEquals(_serviceRefreshColliders[index], collider)) |
| | | 396 | | { |
| | 5004 | 397 | | collider.ClearServiceRefreshIndex(); |
| | 5004 | 398 | | return; |
| | | 399 | | } |
| | | 400 | | |
| | 357 | 401 | | int lastIndex = _serviceRefreshColliders.Count - 1; |
| | 357 | 402 | | if (index != lastIndex) |
| | | 403 | | { |
| | 69 | 404 | | LSCollider2D moved = _serviceRefreshColliders[lastIndex]; |
| | 69 | 405 | | _serviceRefreshColliders[index] = moved; |
| | 69 | 406 | | moved.SetServiceRefreshIndex(index); |
| | | 407 | | } |
| | | 408 | | |
| | 357 | 409 | | _serviceRefreshColliders.RemoveAt(lastIndex); |
| | 357 | 410 | | collider.ClearServiceRefreshIndex(); |
| | 357 | 411 | | } |
| | | 412 | | |
| | | 413 | | private void EnsureFrameCapacity() |
| | | 414 | | { |
| | 24303 | 415 | | int colliderCount = _colliders.Count; |
| | 24303 | 416 | | if (colliderCount <= 0) |
| | 2054 | 417 | | return; |
| | | 418 | | |
| | 22249 | 419 | | int expectedPairKeyCapacity = System.Math.Max(colliderCount * 4, _pairs.Count); |
| | 22249 | 420 | | _processedPairKeys.EnsureCapacity(expectedPairKeyCapacity); |
| | 22249 | 421 | | _pairsToRemove.EnsureCapacity(expectedPairKeyCapacity); |
| | 22249 | 422 | | _pairsPendingSeparation.EnsureCapacity(expectedPairKeyCapacity); |
| | 22249 | 423 | | _discreteResponsePairs.EnsureCapacity(expectedPairKeyCapacity); |
| | 22249 | 424 | | _existingResponsePairCandidates.EnsureCapacity(expectedPairKeyCapacity); |
| | 22249 | 425 | | _discreteResponseBodyKeys.EnsureCapacity(colliderCount); |
| | 22249 | 426 | | _discreteResponseBodyQueue.EnsureCapacity(colliderCount); |
| | 22249 | 427 | | _discreteIslandNodes.EnsureCapacity(colliderCount); |
| | 22249 | 428 | | _discreteIslandConstraints.EnsureCapacity(expectedPairKeyCapacity); |
| | 22249 | 429 | | if (_context.Settings.PoolingEnabled) |
| | 22241 | 430 | | _cachedPairs.EnsureCapacity(colliderCount); |
| | 22249 | 431 | | } |
| | | 432 | | |
| | | 433 | | } |