| | | 1 | | //======================================================================= |
| | | 2 | | // GravitasCollisionService.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 GridForge.Grids; |
| | | 12 | | using GridForge.Spatial; |
| | | 13 | | using GridForge.Utility; |
| | | 14 | | using SwiftCollections; |
| | | 15 | | using System; |
| | | 16 | | using System.Collections.Generic; |
| | | 17 | | using System.Runtime.CompilerServices; |
| | | 18 | | |
| | | 19 | | namespace Gravitas; |
| | | 20 | | |
| | | 21 | | /// <summary> |
| | | 22 | | /// Owns collision partitioning state for one <see cref="GravitasWorldContext"/>. |
| | | 23 | | /// </summary> |
| | | 24 | | public sealed class GravitasCollisionService |
| | | 25 | | { |
| | | 26 | | private const int DefaultPartitionPoolCapacity = 1024; |
| | 1 | 27 | | private static readonly PhysicsPartitionOrderComparer PartitionOrderComparer = new(); |
| | | 28 | | |
| | | 29 | | private readonly GravitasWorldContext _context; |
| | 5083 | 30 | | private readonly SwiftBucket<PhysicsPartition> _activePartitions = new(); |
| | 5083 | 31 | | private readonly SwiftStack<PhysicsPartition> _inactivePartitionPool = new(DefaultPartitionPoolCapacity); |
| | 5083 | 32 | | private readonly SwiftList<Voxel> _coveredVoxels = new(); |
| | 5083 | 33 | | private readonly GridTraceScratch _traceScratch = new(); |
| | 5083 | 34 | | private readonly SwiftList<PhysicsPartition> _retainedPartitions = new(); |
| | 5083 | 35 | | private readonly SwiftList<PhysicsPartition> _distributionPartitions = new(); |
| | 5083 | 36 | | private readonly SwiftList<int> _distributionDynamicIds = new(); |
| | 5083 | 37 | | private readonly SwiftList<int> _distributionStaticIds = new(); |
| | 5083 | 38 | | private readonly SwiftList<int> _planarQueryPartitionColliderIds = new(); |
| | 5083 | 39 | | private readonly SwiftHashSet<int> _planarQueryUniqueColliderIds = new(); |
| | 5083 | 40 | | private readonly DynamicCcdCandidateIndex2D _planarQueryCandidates = |
| | 5083 | 41 | | new(capacity: 0, supportsUpdates: true); |
| | | 42 | | private readonly Action<PhysicsPartition> _releaseRetainedPartition; |
| | 5083 | 43 | | private readonly object _cullDistributorLock = new(); |
| | | 44 | | |
| | | 45 | | private int _cullDistributor; |
| | | 46 | | private int _retainedPartitionRetirementCursor; |
| | | 47 | | private uint _planarQueryWorldVersion; |
| | | 48 | | |
| | | 49 | | /// <summary> |
| | | 50 | | /// Initializes a new collision service for the supplied context. |
| | | 51 | | /// </summary> |
| | | 52 | | /// <param name="context">The owning world context.</param> |
| | 5083 | 53 | | public GravitasCollisionService(GravitasWorldContext context) |
| | | 54 | | { |
| | 5083 | 55 | | SwiftThrowHelper.ThrowIfNull(context, nameof(context)); |
| | 5083 | 56 | | _context = context; |
| | 5083 | 57 | | _releaseRetainedPartition = ReleasePartition; |
| | 5083 | 58 | | } |
| | | 59 | | |
| | | 60 | | /// <summary> |
| | | 61 | | /// Gets the owning world context. |
| | | 62 | | /// </summary> |
| | 755883 | 63 | | public GravitasWorldContext Context => _context; |
| | | 64 | | |
| | | 65 | | /// <summary> |
| | | 66 | | /// Gets the collision distribution version for this context. |
| | | 67 | | /// </summary> |
| | | 68 | | public uint Version { get; private set; } = 1; |
| | | 69 | | |
| | | 70 | | /// <summary> |
| | | 71 | | /// Gets the number of active partitions in this context. |
| | | 72 | | /// </summary> |
| | 20 | 73 | | public int ActivePartitionCount => _activePartitions.Count; |
| | | 74 | | |
| | | 75 | | /// <summary> |
| | | 76 | | /// Gets the number of inactive partitions currently available for reuse. |
| | | 77 | | /// </summary> |
| | 15 | 78 | | public int InactivePartitionCount => _inactivePartitionPool.Count; |
| | | 79 | | |
| | 18 | 80 | | internal int RetainedPartitionCount => _retainedPartitions.Count; |
| | | 81 | | |
| | | 82 | | internal int CullDistributor |
| | | 83 | | { |
| | | 84 | | get |
| | | 85 | | { |
| | 1682 | 86 | | lock (_cullDistributorLock) |
| | | 87 | | { |
| | 1682 | 88 | | if (_cullDistributor > 1) |
| | 539 | 89 | | _cullDistributor = -1; |
| | | 90 | | |
| | 1682 | 91 | | return _cullDistributor++; |
| | | 92 | | } |
| | 1682 | 93 | | } |
| | | 94 | | } |
| | | 95 | | |
| | | 96 | | /// <summary> |
| | | 97 | | /// Resets transient collision state owned by this context. |
| | | 98 | | /// </summary> |
| | | 99 | | public void Reset() |
| | | 100 | | { |
| | 31 | 101 | | DetachRetainedPartitions(); |
| | 31 | 102 | | _activePartitions.Clear(); |
| | 31 | 103 | | _coveredVoxels.FastClear(); |
| | 31 | 104 | | _traceScratch.Clear(); |
| | 31 | 105 | | _distributionPartitions.FastClear(); |
| | 31 | 106 | | _distributionDynamicIds.FastClear(); |
| | 31 | 107 | | _distributionStaticIds.FastClear(); |
| | 31 | 108 | | _planarQueryPartitionColliderIds.FastClear(); |
| | 31 | 109 | | _planarQueryUniqueColliderIds.Clear(); |
| | 31 | 110 | | _planarQueryCandidates.Clear(); |
| | 31 | 111 | | _inactivePartitionPool.Clear(); |
| | 31 | 112 | | Version = 1; |
| | 31 | 113 | | _cullDistributor = 0; |
| | 31 | 114 | | _retainedPartitionRetirementCursor = 0; |
| | 31 | 115 | | _planarQueryWorldVersion = 0; |
| | 31 | 116 | | } |
| | | 117 | | |
| | 31 | 118 | | private void DetachRetainedPartitions() => RetainedPartitionLifecycle.DetachAll( |
| | 31 | 119 | | _retainedPartitions, |
| | 31 | 120 | | _context.World, |
| | 31 | 121 | | this, |
| | 31 | 122 | | _releaseRetainedPartition, |
| | 31 | 123 | | nameof(PhysicsPartition), |
| | 31 | 124 | | "Unable to detach retained physics partition from its voxel during reset."); |
| | | 125 | | |
| | 405247 | 126 | | private void TrackRetainedPartition(PhysicsPartition partition) => RetainedPartitionLifecycle.Track( |
| | 405247 | 127 | | _retainedPartitions, |
| | 405247 | 128 | | this, |
| | 405247 | 129 | | partition, |
| | 405247 | 130 | | nameof(PhysicsPartition)); |
| | | 131 | | |
| | 405252 | 132 | | private void UntrackRetainedPartition(PhysicsPartition partition) => RetainedPartitionLifecycle.Untrack( |
| | 405252 | 133 | | _retainedPartitions, |
| | 405252 | 134 | | this, |
| | 405252 | 135 | | partition, |
| | 405252 | 136 | | ref _retainedPartitionRetirementCursor); |
| | | 137 | | |
| | | 138 | | internal bool IsPartitionRefreshRequired(LSCollider collider) => |
| | 19685 | 139 | | !collider.MatchesPartitionGridBounds(collider.BoundsMin, collider.BoundsMax, ResolvePartitionKind(collider)); |
| | | 140 | | |
| | 43107 | 141 | | internal int ResolvePartitionKind(LSCollider collider) => (int)GetMobilityKind(collider); |
| | | 142 | | |
| | | 143 | | internal bool PartitionObject( |
| | | 144 | | LSCollider collider, |
| | | 145 | | ref SwiftList<WorldVoxelIndex> partitionedCoordinates) |
| | | 146 | | { |
| | 23907 | 147 | | SwiftThrowHelper.ThrowIfNull(collider, nameof(collider)); |
| | 23907 | 148 | | SwiftThrowHelper.ThrowIfArgument( |
| | 23907 | 149 | | !ReferenceEquals(collider.Context, _context), |
| | 23907 | 150 | | nameof(collider), |
| | 23907 | 151 | | "Collider must belong to this collision service context."); |
| | | 152 | | |
| | 23907 | 153 | | partitionedCoordinates.FastClear(); |
| | | 154 | | |
| | 23907 | 155 | | PartitionCoveredVoxels(collider, partitionedCoordinates, GetMobilityKind(collider)); |
| | 23907 | 156 | | if (partitionedCoordinates.Count == 0) |
| | | 157 | | { |
| | 1201 | 158 | | _planarQueryCandidates.Remove(collider.Id); |
| | 1201 | 159 | | return false; |
| | | 160 | | } |
| | | 161 | | |
| | 22706 | 162 | | _planarQueryCandidates.AddOrUpdate( |
| | 22706 | 163 | | collider.Id, |
| | 22706 | 164 | | new DynamicCcdPlanarBounds( |
| | 22706 | 165 | | collider.BoundsMin.X, |
| | 22706 | 166 | | collider.BoundsMin.Z, |
| | 22706 | 167 | | collider.BoundsMax.X, |
| | 22706 | 168 | | collider.BoundsMax.Z)); |
| | 22706 | 169 | | return true; |
| | | 170 | | } |
| | | 171 | | |
| | | 172 | | private void PartitionCoveredVoxels( |
| | | 173 | | LSCollider collider, |
| | | 174 | | SwiftList<WorldVoxelIndex> partitionedCoordinates, |
| | | 175 | | PhysicsPartitionMobilityKind kind) |
| | | 176 | | { |
| | 23907 | 177 | | GridWorld world = _context.World; |
| | 23907 | 178 | | GridTracer.GetCoveredVoxelsInto( |
| | 23907 | 179 | | world, |
| | 23907 | 180 | | collider.BoundsMin, |
| | 23907 | 181 | | collider.BoundsMax, |
| | 23907 | 182 | | _coveredVoxels, |
| | 23907 | 183 | | _traceScratch, |
| | 23907 | 184 | | Fixed64.Half); |
| | | 185 | | |
| | 23907 | 186 | | var traversal = new GridTraversalState(world, GridTraversalPaddingMode.MaxCellEdge); |
| | 2451866 | 187 | | for (int i = 0; i < _coveredVoxels.Count; i++) |
| | 1202026 | 188 | | TryPartitionVoxel(collider, partitionedCoordinates, _coveredVoxels[i], ref traversal, kind); |
| | 23907 | 189 | | } |
| | | 190 | | |
| | | 191 | | private void TryPartitionVoxel( |
| | | 192 | | LSCollider collider, |
| | | 193 | | SwiftList<WorldVoxelIndex> partitionedCoordinates, |
| | | 194 | | Voxel voxel, |
| | | 195 | | ref GridTraversalState traversal, |
| | | 196 | | PhysicsPartitionMobilityKind kind) |
| | | 197 | | { |
| | 1202026 | 198 | | Fixed64 cellEdge = traversal.GetCellEdge(voxel); |
| | 1202026 | 199 | | if (!collider.IsPositionInBounds(cellEdge, voxel.WorldPosition)) |
| | 227763 | 200 | | return; |
| | | 201 | | |
| | 974263 | 202 | | if (!voxel.TryGetPartition(out PhysicsPartition? partition)) |
| | | 203 | | { |
| | 405247 | 204 | | partition = RentPartition(); |
| | 405247 | 205 | | SwiftThrowHelper.ThrowIfTrue( |
| | 405247 | 206 | | !voxel.TryAddPartition(partition), |
| | 405247 | 207 | | nameof(GravitasCollisionService), |
| | 405247 | 208 | | "Unable to attach 3D physics partition to voxel."); |
| | | 209 | | |
| | 405247 | 210 | | TrackRetainedPartition(partition); |
| | | 211 | | } |
| | | 212 | | |
| | 974263 | 213 | | partitionedCoordinates.Add(voxel.WorldIndex); |
| | 974263 | 214 | | AddObject(partition!, collider.Id, kind); |
| | 974263 | 215 | | } |
| | | 216 | | |
| | | 217 | | internal bool ClearPartitionedObject(LSCollider collider, bool force = false) |
| | | 218 | | { |
| | 18056 | 219 | | SwiftThrowHelper.ThrowIfNull(collider, nameof(collider)); |
| | 18056 | 220 | | SwiftThrowHelper.ThrowIfArgument( |
| | 18056 | 221 | | !ReferenceEquals(collider.Context, _context), |
| | 18056 | 222 | | nameof(collider), |
| | 18056 | 223 | | "Collider must belong to this collision service context."); |
| | | 224 | | |
| | 18056 | 225 | | GridWorld world = _context.World; |
| | 18056 | 226 | | if (!collider.IsPartitioned) |
| | 1 | 227 | | return false; |
| | | 228 | | |
| | 18055 | 229 | | PhysicsPartitionMobilityKind currentKind = GetMobilityKind(collider); |
| | 18055 | 230 | | if (!force && collider.MatchesPartitionGridBounds(collider.BoundsMin, collider.BoundsMax, (int)currentKind)) |
| | 29 | 231 | | return false; |
| | | 232 | | |
| | 18026 | 233 | | PhysicsPartitionMobilityKind partitionKind = GetStoredMobilityKind(collider.PartitionKind); |
| | 18026 | 234 | | _planarQueryCandidates.Remove(collider.Id); |
| | | 235 | | |
| | 1050798 | 236 | | for (int i = 0; i < collider.PartitionCoordinates!.Count; i++) |
| | | 237 | | { |
| | 507373 | 238 | | WorldVoxelIndex coordinate = collider.PartitionCoordinates[i]; |
| | 507373 | 239 | | if (!world.ActiveGrids.IsAllocated(coordinate.GridIndex) |
| | 507373 | 240 | | || !world.TryGetVoxel(coordinate, out Voxel? voxel) |
| | 507373 | 241 | | || !voxel!.TryGetPartition(out PhysicsPartition? partition)) |
| | | 242 | | { |
| | | 243 | | continue; |
| | | 244 | | } |
| | | 245 | | |
| | 507264 | 246 | | RemoveObject(partition!, collider.Id, partitionKind); |
| | | 247 | | |
| | | 248 | | // Keep the voxel partition attached after it becomes empty. Re-adding |
| | | 249 | | // the same partition type through GridForge carries metadata overhead, |
| | | 250 | | // while an empty PhysicsPartition is inactive and query-invisible. |
| | | 251 | | } |
| | | 252 | | |
| | 18026 | 253 | | collider.MarkUnpartitioned(); |
| | 18026 | 254 | | collider.ClearPartitionCoordinates(); |
| | | 255 | | |
| | 18026 | 256 | | return true; |
| | | 257 | | } |
| | | 258 | | |
| | | 259 | | internal void QueryPlanarColliderCandidates( |
| | | 260 | | DynamicCcdPlanarBounds bounds, |
| | | 261 | | SwiftList<int> results) |
| | | 262 | | { |
| | 40 | 263 | | if (_planarQueryWorldVersion != _context.World.Version) |
| | 30 | 264 | | RebuildPlanarQueryCandidates(); |
| | | 265 | | |
| | 40 | 266 | | _planarQueryCandidates.Query(bounds, results); |
| | 40 | 267 | | } |
| | | 268 | | |
| | | 269 | | private void RebuildPlanarQueryCandidates() |
| | | 270 | | { |
| | 30 | 271 | | _planarQueryCandidates.Clear(); |
| | 30 | 272 | | _planarQueryUniqueColliderIds.Clear(); |
| | 4098 | 273 | | for (int partitionIndex = 0; partitionIndex < _retainedPartitions.Count; partitionIndex++) |
| | | 274 | | { |
| | 2019 | 275 | | _retainedPartitions[partitionIndex].CopyAllColliderIds(_planarQueryPartitionColliderIds); |
| | 9824 | 276 | | for (int colliderIndex = 0; colliderIndex < _planarQueryPartitionColliderIds.Count; colliderIndex++) |
| | | 277 | | { |
| | 2893 | 278 | | int colliderId = _planarQueryPartitionColliderIds[colliderIndex]; |
| | 2893 | 279 | | if (!_planarQueryUniqueColliderIds.Add(colliderId) |
| | 2893 | 280 | | || !_context.Physics.TryGetColliderById(colliderId, out LSCollider? collider)) |
| | | 281 | | { |
| | | 282 | | continue; |
| | | 283 | | } |
| | | 284 | | |
| | 42 | 285 | | _planarQueryCandidates.AddOrUpdate( |
| | 42 | 286 | | colliderId, |
| | 42 | 287 | | new DynamicCcdPlanarBounds( |
| | 42 | 288 | | collider!.BoundsMin.X, |
| | 42 | 289 | | collider.BoundsMin.Z, |
| | 42 | 290 | | collider.BoundsMax.X, |
| | 42 | 291 | | collider.BoundsMax.Z)); |
| | | 292 | | } |
| | | 293 | | } |
| | | 294 | | |
| | 30 | 295 | | _planarQueryWorldVersion = _context.World.Version; |
| | 30 | 296 | | } |
| | | 297 | | |
| | | 298 | | internal void RefreshPartitionAwakeState(LSCollider collider) |
| | | 299 | | { |
| | 392 | 300 | | SwiftThrowHelper.ThrowIfNull(collider, nameof(collider)); |
| | 392 | 301 | | SwiftThrowHelper.ThrowIfArgument( |
| | 392 | 302 | | !ReferenceEquals(collider.Context, _context), |
| | 392 | 303 | | nameof(collider), |
| | 392 | 304 | | "Collider must belong to this collision service context."); |
| | | 305 | | |
| | 392 | 306 | | SwiftList<WorldVoxelIndex> coordinates = collider.PartitionCoordinates!; |
| | 392 | 307 | | SolidBody? body = collider.Body; |
| | 392 | 308 | | if (collider.IsStatic || body!.IsKinematic) |
| | 2 | 309 | | return; |
| | | 310 | | |
| | 390 | 311 | | bool awake = body.IsAwakeForCollision; |
| | 390 | 312 | | GridWorld world = _context.World; |
| | | 313 | | |
| | 18190 | 314 | | for (int i = 0; i < coordinates.Count; i++) |
| | | 315 | | { |
| | 8705 | 316 | | WorldVoxelIndex coordinate = coordinates[i]; |
| | 8705 | 317 | | if (!world.ActiveGrids.IsAllocated(coordinate.GridIndex) |
| | 8705 | 318 | | || !world.TryGetVoxel(coordinate, out Voxel? voxel) |
| | 8705 | 319 | | || !voxel!.TryGetPartition(out PhysicsPartition? partition)) |
| | | 320 | | { |
| | | 321 | | continue; |
| | | 322 | | } |
| | | 323 | | |
| | 8650 | 324 | | partition!.SetDynamicObjectAwake(collider.Id, awake); |
| | | 325 | | } |
| | 390 | 326 | | } |
| | | 327 | | |
| | | 328 | | internal void CheckAndDistributeCollisions() |
| | | 329 | | { |
| | 2188 | 330 | | Version++; |
| | | 331 | | |
| | 2188 | 332 | | _distributionPartitions.FastClear(); |
| | 256814 | 333 | | foreach (PhysicsPartition partition in _activePartitions) |
| | 126219 | 334 | | _distributionPartitions.Add(partition); |
| | | 335 | | |
| | 2188 | 336 | | _distributionPartitions.SortInPlace(PartitionOrderComparer); |
| | | 337 | | |
| | 256814 | 338 | | for (int i = 0; i < _distributionPartitions.Count; i++) |
| | | 339 | | { |
| | 126219 | 340 | | _distributionPartitions[i].Distribute( |
| | 126219 | 341 | | _distributionDynamicIds, |
| | 126219 | 342 | | _distributionStaticIds); |
| | | 343 | | } |
| | 2188 | 344 | | } |
| | | 345 | | |
| | 2182 | 346 | | internal void RetireExpiredRetainedPartitions() => RetainedPartitionLifecycle.RetireExpired( |
| | 2182 | 347 | | _retainedPartitions, |
| | 2182 | 348 | | _context.World, |
| | 2182 | 349 | | this, |
| | 2182 | 350 | | _context.Settings.RetainedPartitionRetirementSweepBudget, |
| | 2182 | 351 | | _context.FrameCount, |
| | 2182 | 352 | | _context.Settings.RetainedPartitionTimeToKillFrames, |
| | 2182 | 353 | | _releaseRetainedPartition, |
| | 2182 | 354 | | ref _retainedPartitionRetirementCursor); |
| | | 355 | | |
| | | 356 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 357 | | private static PhysicsPartitionMobilityKind GetMobilityKind(LSCollider collider) |
| | | 358 | | { |
| | 85069 | 359 | | if (collider.IsStatic) |
| | 9908 | 360 | | return PhysicsPartitionMobilityKind.Static; |
| | | 361 | | |
| | 75161 | 362 | | SolidBody? body = collider.Body; |
| | 75161 | 363 | | return body!.IsKinematic ? PhysicsPartitionMobilityKind.Kinematic : PhysicsPartitionMobilityKind.Dynamic; |
| | | 364 | | } |
| | | 365 | | |
| | | 366 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 18026 | 367 | | private static PhysicsPartitionMobilityKind GetStoredMobilityKind(int partitionKind) => partitionKind == (int)Physic |
| | 18026 | 368 | | ? PhysicsPartitionMobilityKind.Kinematic |
| | 18026 | 369 | | : partitionKind == (int)PhysicsPartitionMobilityKind.Static |
| | 18026 | 370 | | ? PhysicsPartitionMobilityKind.Static |
| | 18026 | 371 | | : PhysicsPartitionMobilityKind.Dynamic; |
| | | 372 | | |
| | | 373 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 374 | | private static void AddObject(PhysicsPartition partition, int id, PhysicsPartitionMobilityKind kind) |
| | | 375 | | { |
| | 974263 | 376 | | if (kind == PhysicsPartitionMobilityKind.Static) |
| | | 377 | | { |
| | 308354 | 378 | | partition.AddStaticObject(id); |
| | 308354 | 379 | | return; |
| | | 380 | | } |
| | | 381 | | |
| | 665909 | 382 | | if (kind == PhysicsPartitionMobilityKind.Kinematic) |
| | | 383 | | { |
| | 358934 | 384 | | partition.AddKinematicObject(id); |
| | 358934 | 385 | | return; |
| | | 386 | | } |
| | | 387 | | |
| | 306975 | 388 | | partition.AddDynamicObject(id); |
| | 306975 | 389 | | } |
| | | 390 | | |
| | | 391 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 392 | | private static void RemoveObject(PhysicsPartition partition, int id, PhysicsPartitionMobilityKind kind) |
| | | 393 | | { |
| | 507264 | 394 | | if (kind == PhysicsPartitionMobilityKind.Static) |
| | | 395 | | { |
| | 6151 | 396 | | partition.RemoveStaticObject(id); |
| | 6151 | 397 | | return; |
| | | 398 | | } |
| | | 399 | | |
| | 501113 | 400 | | if (kind == PhysicsPartitionMobilityKind.Kinematic) |
| | | 401 | | { |
| | 342278 | 402 | | partition.RemoveKinematicObject(id); |
| | 342278 | 403 | | return; |
| | | 404 | | } |
| | | 405 | | |
| | 158835 | 406 | | partition.RemoveDynamicObject(id); |
| | 158835 | 407 | | } |
| | | 408 | | |
| | | 409 | | private sealed class PhysicsPartitionOrderComparer : IComparer<PhysicsPartition> |
| | | 410 | | { |
| | | 411 | | public int Compare(PhysicsPartition? left, PhysicsPartition? right) => |
| | 653330 | 412 | | WorldVoxelIndexOrdering.Compare3D(left!.WorldIndex, right!.WorldIndex); |
| | | 413 | | } |
| | | 414 | | |
| | | 415 | | internal int ActivatePartition(PhysicsPartition partition) |
| | | 416 | | { |
| | 220726 | 417 | | SwiftThrowHelper.ThrowIfNull(partition, nameof(partition)); |
| | 220726 | 418 | | SwiftThrowHelper.ThrowIfArgument( |
| | 220726 | 419 | | !ReferenceEquals(partition.Owner, this), |
| | 220726 | 420 | | nameof(partition), |
| | 220726 | 421 | | "Partition must belong to this collision service."); |
| | | 422 | | |
| | 220726 | 423 | | return _activePartitions.Add(partition); |
| | | 424 | | } |
| | | 425 | | |
| | 220725 | 426 | | internal void DeactivatePartition(int activationId) => _activePartitions.TryRemoveAt(activationId); |
| | | 427 | | |
| | | 428 | | internal PhysicsPartition RentPartition() |
| | | 429 | | { |
| | 405255 | 430 | | if (_inactivePartitionPool.Count == 0) |
| | 405200 | 431 | | TryRetireEmptyRetainedPartitionForReuse(); |
| | | 432 | | |
| | 405255 | 433 | | PhysicsPartition partition = _inactivePartitionPool.Count > 0 |
| | 405255 | 434 | | ? _inactivePartitionPool.Pop() |
| | 405255 | 435 | | : new PhysicsPartition(); |
| | 405255 | 436 | | partition.SetOwner(this); |
| | 405255 | 437 | | return partition; |
| | | 438 | | } |
| | | 439 | | |
| | 405200 | 440 | | private bool TryRetireEmptyRetainedPartitionForReuse() => RetainedPartitionLifecycle.TryRetireEmptyForReuse( |
| | 405200 | 441 | | _retainedPartitions, |
| | 405200 | 442 | | _inactivePartitionPool, |
| | 405200 | 443 | | _context.World, |
| | 405200 | 444 | | this, |
| | 405200 | 445 | | _releaseRetainedPartition, |
| | 405200 | 446 | | ref _retainedPartitionRetirementCursor); |
| | | 447 | | |
| | | 448 | | internal void ReleasePartition(PhysicsPartition partition) |
| | | 449 | | { |
| | 405252 | 450 | | SwiftThrowHelper.ThrowIfNull(partition, nameof(partition)); |
| | 405252 | 451 | | SwiftThrowHelper.ThrowIfArgument( |
| | 405252 | 452 | | !ReferenceEquals(partition.Owner, this), |
| | 405252 | 453 | | nameof(partition), |
| | 405252 | 454 | | "Partition must be released through its owning collision service."); |
| | | 455 | | |
| | 405252 | 456 | | UntrackRetainedPartition(partition); |
| | 405252 | 457 | | partition.ResetForPool(); |
| | 405252 | 458 | | _inactivePartitionPool.Push(partition); |
| | 405252 | 459 | | } |
| | | 460 | | } |