| | | 1 | | //======================================================================= |
| | | 2 | | // PhysicsMixedPartition.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 GridForge.Grids; |
| | | 10 | | using GridForge.Spatial; |
| | | 11 | | using SwiftCollections; |
| | | 12 | | |
| | | 13 | | namespace Gravitas; |
| | | 14 | | |
| | | 15 | | |
| | | 16 | | |
| | | 17 | | /// <summary> |
| | | 18 | | /// GridForge voxel partition that stores cross-dimensional broad-phase memberships. |
| | | 19 | | /// </summary> |
| | | 20 | | internal sealed class PhysicsMixedPartition : IVoxelPartition, IRetainedPhysicsPartition<GravitasMixedCollisionService> |
| | | 21 | | { |
| | | 22 | | private GravitasMixedCollisionService? _owner; |
| | 413935 | 23 | | private long _emptySinceFrame = -1; |
| | 413935 | 24 | | private int _retainedIndex = -1; |
| | | 25 | | |
| | 413935 | 26 | | public PhysicsMixedPartition() |
| | | 27 | | { |
| | 413935 | 28 | | ActivationId = -1; |
| | 413935 | 29 | | } |
| | | 30 | | |
| | | 31 | | public WorldVoxelIndex WorldIndex { get; set; } |
| | | 32 | | |
| | | 33 | | public bool IsPartitioned { get; set; } |
| | | 34 | | |
| | | 35 | | internal SwiftHashSet<int>? ContainedDynamic3DObjects; |
| | | 36 | | |
| | | 37 | | internal SwiftHashSet<int>? ContainedAwakeDynamic3DObjects; |
| | | 38 | | |
| | | 39 | | internal SwiftHashSet<int>? ContainedKinematic3DObjects; |
| | | 40 | | |
| | | 41 | | internal SwiftHashSet<int>? ContainedStatic3DObjects; |
| | | 42 | | |
| | | 43 | | internal SwiftHashSet<int>? ContainedDynamic2DObjects; |
| | | 44 | | |
| | | 45 | | internal SwiftHashSet<int>? ContainedAwakeDynamic2DObjects; |
| | | 46 | | |
| | | 47 | | internal SwiftHashSet<int>? ContainedKinematic2DObjects; |
| | | 48 | | |
| | | 49 | | internal SwiftHashSet<int>? ContainedStatic2DObjects; |
| | | 50 | | |
| | | 51 | | public int ActivationId { get; private set; } |
| | | 52 | | |
| | 505721 | 53 | | public bool IsAllocated => ActivationId != -1; |
| | | 54 | | |
| | | 55 | | /// <summary>Gets the number of dynamic 3D collider IDs.</summary> |
| | 3191217 | 56 | | public int Dynamic3DObjectCount => ContainedDynamic3DObjects?.Count ?? 0; |
| | | 57 | | |
| | | 58 | | /// <summary>Gets the number of kinematic 3D collider IDs.</summary> |
| | 2585003 | 59 | | public int Kinematic3DObjectCount => ContainedKinematic3DObjects?.Count ?? 0; |
| | | 60 | | |
| | | 61 | | /// <summary>Gets the number of static 3D collider IDs.</summary> |
| | 2560975 | 62 | | public int Static3DObjectCount => ContainedStatic3DObjects?.Count ?? 0; |
| | | 63 | | |
| | | 64 | | /// <summary>Gets the number of dynamic 2D collider IDs.</summary> |
| | 2050156 | 65 | | public int Dynamic2DObjectCount => ContainedDynamic2DObjects?.Count ?? 0; |
| | | 66 | | |
| | | 67 | | /// <summary>Gets the number of kinematic 2D collider IDs.</summary> |
| | 1993795 | 68 | | public int Kinematic2DObjectCount => ContainedKinematic2DObjects?.Count ?? 0; |
| | | 69 | | |
| | | 70 | | /// <summary>Gets the number of static 2D collider IDs.</summary> |
| | 1985718 | 71 | | public int Static2DObjectCount => ContainedStatic2DObjects?.Count ?? 0; |
| | | 72 | | |
| | 3191108 | 73 | | internal bool IsEmpty => Dynamic3DObjectCount == 0 && Kinematic3DObjectCount == 0 && Static3DObjectCount == 0 |
| | 3191108 | 74 | | && Dynamic2DObjectCount == 0 && Kinematic2DObjectCount == 0 && Static2DObjectCount == 0; |
| | | 75 | | |
| | 31059 | 76 | | internal long EmptySinceFrame => _emptySinceFrame; |
| | | 77 | | |
| | 1872405 | 78 | | internal int RetainedIndex => _retainedIndex; |
| | | 79 | | |
| | | 80 | | internal int EmptyIndex { get; set; } = -1; |
| | | 81 | | |
| | | 82 | | /// <summary>Gets the number of awake dynamic 3D collider IDs.</summary> |
| | 261240 | 83 | | public int AwakeDynamic3DObjectCount => ContainedAwakeDynamic3DObjects?.Count ?? 0; |
| | | 84 | | |
| | | 85 | | /// <summary>Gets the number of awake dynamic 2D collider IDs.</summary> |
| | 261240 | 86 | | public int AwakeDynamic2DObjectCount => ContainedAwakeDynamic2DObjects?.Count ?? 0; |
| | | 87 | | |
| | | 88 | | /// <summary>Gets the number of awake dynamic collider IDs across both dimensions.</summary> |
| | 261239 | 89 | | public int AwakeDynamicObjectCount => AwakeDynamic3DObjectCount + AwakeDynamic2DObjectCount; |
| | | 90 | | |
| | | 91 | | private int MovableDynamicObjectCount => |
| | 905908 | 92 | | (ContainedDynamic3DObjects?.Count ?? 0) + (ContainedDynamic2DObjects?.Count ?? 0); |
| | | 93 | | |
| | | 94 | | public GravitasMixedCollisionService Owner |
| | | 95 | | { |
| | | 96 | | get |
| | | 97 | | { |
| | 3179888 | 98 | | ChronicleGuard.ThrowIfTrue( |
| | 3179888 | 99 | | _owner == null, |
| | 3179888 | 100 | | nameof(PhysicsMixedPartition), |
| | 3179888 | 101 | | "PhysicsMixedPartition is missing its owner collision service."); |
| | 3179886 | 102 | | return _owner!; |
| | | 103 | | } |
| | | 104 | | } |
| | | 105 | | |
| | | 106 | | public void OnAddToVoxel(Voxel voxel) |
| | | 107 | | { |
| | 434905 | 108 | | ChronicleGuard.ThrowIfTrue( |
| | 434905 | 109 | | _owner == null, |
| | 434905 | 110 | | nameof(PhysicsMixedPartition), |
| | 434905 | 111 | | "PhysicsMixedPartition is missing its owner collision service."); |
| | 434905 | 112 | | WorldIndex = voxel.WorldIndex; |
| | 434905 | 113 | | IsPartitioned = true; |
| | 434905 | 114 | | } |
| | | 115 | | |
| | | 116 | | internal void Distribute( |
| | | 117 | | SwiftList<int> dynamic3DIds, |
| | | 118 | | SwiftList<int> kinematic3DIds, |
| | | 119 | | SwiftList<int> static3DIds, |
| | | 120 | | SwiftList<int> dynamic2DIds, |
| | | 121 | | SwiftList<int> kinematic2DIds, |
| | | 122 | | SwiftList<int> static2DIds) |
| | | 123 | | { |
| | 389126 | 124 | | int total3DCount = (ContainedDynamic3DObjects?.Count ?? 0) |
| | 389126 | 125 | | + (ContainedKinematic3DObjects?.Count ?? 0) |
| | 389126 | 126 | | + (ContainedStatic3DObjects?.Count ?? 0); |
| | 389126 | 127 | | int total2DCount = (ContainedDynamic2DObjects?.Count ?? 0) |
| | 389126 | 128 | | + (ContainedKinematic2DObjects?.Count ?? 0) |
| | 389126 | 129 | | + (ContainedStatic2DObjects?.Count ?? 0); |
| | 389126 | 130 | | if (total3DCount == 0 || total2DCount == 0 || AwakeDynamicObjectCount == 0) |
| | 131281 | 131 | | return; |
| | | 132 | | |
| | 257845 | 133 | | CopySortedIds(ContainedDynamic3DObjects, dynamic3DIds); |
| | 257845 | 134 | | CopySortedIds(ContainedKinematic3DObjects, kinematic3DIds); |
| | 257845 | 135 | | CopySortedIds(ContainedStatic3DObjects, static3DIds); |
| | 257845 | 136 | | CopySortedIds(ContainedDynamic2DObjects, dynamic2DIds); |
| | 257845 | 137 | | CopySortedIds(ContainedKinematic2DObjects, kinematic2DIds); |
| | 257845 | 138 | | CopySortedIds(ContainedStatic2DObjects, static2DIds); |
| | | 139 | | |
| | 257845 | 140 | | GravitasMixedCollisionService owner = Owner; |
| | 921064 | 141 | | for (int i = 0; i < dynamic3DIds.Count; i++) |
| | | 142 | | { |
| | 202687 | 143 | | int id3D = dynamic3DIds[i]; |
| | 413120 | 144 | | for (int j = 0; j < dynamic2DIds.Count; j++) |
| | 3873 | 145 | | owner.ProcessPartitionCandidate(id3D, dynamic2DIds[j]); |
| | | 146 | | |
| | 405710 | 147 | | for (int j = 0; j < kinematic2DIds.Count; j++) |
| | 168 | 148 | | owner.ProcessPartitionCandidate(id3D, kinematic2DIds[j]); |
| | | 149 | | |
| | 855278 | 150 | | for (int j = 0; j < static2DIds.Count; j++) |
| | 224952 | 151 | | owner.ProcessPartitionCandidate(id3D, static2DIds[j]); |
| | | 152 | | } |
| | | 153 | | |
| | 633740 | 154 | | for (int i = 0; i < dynamic2DIds.Count; i++) |
| | | 155 | | { |
| | 59025 | 156 | | int id2D = dynamic2DIds[i]; |
| | 118368 | 157 | | for (int j = 0; j < kinematic3DIds.Count; j++) |
| | 159 | 158 | | owner.ProcessPartitionCandidate(kinematic3DIds[j], id2D); |
| | | 159 | | |
| | 262986 | 160 | | for (int j = 0; j < static3DIds.Count; j++) |
| | 72468 | 161 | | owner.ProcessPartitionCandidate(static3DIds[j], id2D); |
| | | 162 | | } |
| | 257845 | 163 | | } |
| | | 164 | | |
| | | 165 | | public void AddDynamic3DObject(int id) |
| | | 166 | | { |
| | 782481 | 167 | | ChronicleGuard.ThrowIfNegative(id, nameof(id)); |
| | 782480 | 168 | | ContainedDynamic3DObjects ??= Owner.RentPartitionMembership(); |
| | 782480 | 169 | | bool shouldActivate = MovableDynamicObjectCount == 0; |
| | 782480 | 170 | | if (!ContainedDynamic3DObjects.Add(id)) |
| | 700065 | 171 | | return; |
| | | 172 | | |
| | 82415 | 173 | | MarkOccupied(); |
| | 82415 | 174 | | SetDynamic3DObjectAwake(id, IsDynamic3DObjectAwake(id)); |
| | 82415 | 175 | | if (shouldActivate) |
| | 81515 | 176 | | ActivationId = Owner.ActivatePartition(this); |
| | 82415 | 177 | | } |
| | | 178 | | |
| | | 179 | | public void AddStatic3DObject(int id) |
| | | 180 | | { |
| | 126351 | 181 | | ChronicleGuard.ThrowIfNegative(id, nameof(id)); |
| | 126350 | 182 | | ContainedStatic3DObjects ??= Owner.RentPartitionMembership(); |
| | 126350 | 183 | | if (ContainedStatic3DObjects.Add(id)) |
| | 123879 | 184 | | MarkOccupied(); |
| | 126350 | 185 | | } |
| | | 186 | | |
| | | 187 | | public void AddKinematic3DObject(int id) |
| | | 188 | | { |
| | 15847 | 189 | | ChronicleGuard.ThrowIfNegative(id, nameof(id)); |
| | 15846 | 190 | | ContainedKinematic3DObjects ??= Owner.RentPartitionMembership(); |
| | 15846 | 191 | | if (ContainedKinematic3DObjects.Add(id)) |
| | 11729 | 192 | | MarkOccupied(); |
| | 15846 | 193 | | } |
| | | 194 | | |
| | | 195 | | public void AddDynamic2DObject(int id) |
| | | 196 | | { |
| | 96281 | 197 | | ChronicleGuard.ThrowIfNegative(id, nameof(id)); |
| | 96280 | 198 | | ContainedDynamic2DObjects ??= Owner.RentPartitionMembership(); |
| | 96280 | 199 | | bool shouldActivate = MovableDynamicObjectCount == 0; |
| | 96280 | 200 | | if (!ContainedDynamic2DObjects.Add(id)) |
| | 78425 | 201 | | return; |
| | | 202 | | |
| | 17855 | 203 | | MarkOccupied(); |
| | 17855 | 204 | | SetDynamic2DObjectAwake(id, IsDynamic2DObjectAwake(id)); |
| | 17855 | 205 | | if (shouldActivate) |
| | 14905 | 206 | | ActivationId = Owner.ActivatePartition(this); |
| | 17855 | 207 | | } |
| | | 208 | | |
| | | 209 | | public void AddStatic2DObject(int id) |
| | | 210 | | { |
| | 273404 | 211 | | ChronicleGuard.ThrowIfNegative(id, nameof(id)); |
| | 273403 | 212 | | ContainedStatic2DObjects ??= Owner.RentPartitionMembership(); |
| | 273403 | 213 | | if (ContainedStatic2DObjects.Add(id)) |
| | 272757 | 214 | | MarkOccupied(); |
| | 273403 | 215 | | } |
| | | 216 | | |
| | | 217 | | public void AddKinematic2DObject(int id) |
| | | 218 | | { |
| | 4885 | 219 | | ChronicleGuard.ThrowIfNegative(id, nameof(id)); |
| | 4884 | 220 | | ContainedKinematic2DObjects ??= Owner.RentPartitionMembership(); |
| | 4884 | 221 | | if (ContainedKinematic2DObjects.Add(id)) |
| | 3761 | 222 | | MarkOccupied(); |
| | 4884 | 223 | | } |
| | | 224 | | |
| | | 225 | | public void RemoveDynamic3DObject(int id) |
| | | 226 | | { |
| | 22616 | 227 | | if (ContainedDynamic3DObjects?.Remove(id) != true) |
| | 3 | 228 | | return; |
| | | 229 | | |
| | 22613 | 230 | | RemoveAndReleaseIfEmpty(ref ContainedAwakeDynamic3DObjects, id); |
| | 22613 | 231 | | ReleaseIfEmpty(ref ContainedDynamic3DObjects); |
| | 22613 | 232 | | DeactivateIfNoDynamicMembers(); |
| | 22613 | 233 | | MarkEmptyIfUnoccupied(); |
| | 22613 | 234 | | } |
| | | 235 | | |
| | | 236 | | public void RemoveStatic3DObject(int id) |
| | | 237 | | { |
| | 8964 | 238 | | if (ContainedStatic3DObjects?.Remove(id) == true) |
| | | 239 | | { |
| | 8961 | 240 | | ReleaseIfEmpty(ref ContainedStatic3DObjects); |
| | 8961 | 241 | | MarkEmptyIfUnoccupied(); |
| | | 242 | | } |
| | 8964 | 243 | | } |
| | | 244 | | |
| | | 245 | | public void RemoveKinematic3DObject(int id) |
| | | 246 | | { |
| | 5997 | 247 | | if (ContainedKinematic3DObjects?.Remove(id) == true) |
| | | 248 | | { |
| | 5994 | 249 | | ReleaseIfEmpty(ref ContainedKinematic3DObjects); |
| | 5994 | 250 | | MarkEmptyIfUnoccupied(); |
| | | 251 | | } |
| | 5997 | 252 | | } |
| | | 253 | | |
| | | 254 | | public void RemoveDynamic2DObject(int id) |
| | | 255 | | { |
| | 4538 | 256 | | if (ContainedDynamic2DObjects?.Remove(id) != true) |
| | 3 | 257 | | return; |
| | | 258 | | |
| | 4535 | 259 | | RemoveAndReleaseIfEmpty(ref ContainedAwakeDynamic2DObjects, id); |
| | 4535 | 260 | | ReleaseIfEmpty(ref ContainedDynamic2DObjects); |
| | 4535 | 261 | | DeactivateIfNoDynamicMembers(); |
| | 4535 | 262 | | MarkEmptyIfUnoccupied(); |
| | 4535 | 263 | | } |
| | | 264 | | |
| | | 265 | | public void RemoveStatic2DObject(int id) |
| | | 266 | | { |
| | 4466 | 267 | | if (ContainedStatic2DObjects?.Remove(id) == true) |
| | | 268 | | { |
| | 4463 | 269 | | ReleaseIfEmpty(ref ContainedStatic2DObjects); |
| | 4463 | 270 | | MarkEmptyIfUnoccupied(); |
| | | 271 | | } |
| | 4466 | 272 | | } |
| | | 273 | | |
| | | 274 | | public void RemoveKinematic2DObject(int id) |
| | | 275 | | { |
| | 1634 | 276 | | if (ContainedKinematic2DObjects?.Remove(id) == true) |
| | | 277 | | { |
| | 1631 | 278 | | ReleaseIfEmpty(ref ContainedKinematic2DObjects); |
| | 1631 | 279 | | MarkEmptyIfUnoccupied(); |
| | | 280 | | } |
| | 1634 | 281 | | } |
| | | 282 | | |
| | | 283 | | public void SetDynamic3DObjectAwake(int id, bool awake) |
| | | 284 | | { |
| | 955929 | 285 | | if (ContainedDynamic3DObjects?.Contains(id) != true) |
| | 7153 | 286 | | return; |
| | | 287 | | |
| | 948776 | 288 | | if (awake) |
| | | 289 | | { |
| | 930921 | 290 | | ContainedAwakeDynamic3DObjects ??= Owner.RentPartitionMembership(); |
| | 930921 | 291 | | ContainedAwakeDynamic3DObjects.Add(id); |
| | 930921 | 292 | | return; |
| | | 293 | | } |
| | | 294 | | |
| | 17855 | 295 | | RemoveAndReleaseIfEmpty(ref ContainedAwakeDynamic3DObjects, id); |
| | 17855 | 296 | | } |
| | | 297 | | |
| | | 298 | | public void SetDynamic2DObjectAwake(int id, bool awake) |
| | | 299 | | { |
| | 155799 | 300 | | if (ContainedDynamic2DObjects?.Contains(id) != true) |
| | 92 | 301 | | return; |
| | | 302 | | |
| | 155707 | 303 | | if (awake) |
| | | 304 | | { |
| | 139513 | 305 | | ContainedAwakeDynamic2DObjects ??= Owner.RentPartitionMembership(); |
| | 139513 | 306 | | ContainedAwakeDynamic2DObjects.Add(id); |
| | 139513 | 307 | | return; |
| | | 308 | | } |
| | | 309 | | |
| | 16194 | 310 | | RemoveAndReleaseIfEmpty(ref ContainedAwakeDynamic2DObjects, id); |
| | 16194 | 311 | | } |
| | | 312 | | |
| | | 313 | | public void OnRemoveFromVoxel(Voxel voxel) |
| | | 314 | | { |
| | 434905 | 315 | | Owner.ReleasePartition(this); |
| | 434905 | 316 | | } |
| | | 317 | | |
| | | 318 | | internal void SetOwner(GravitasMixedCollisionService owner) |
| | | 319 | | { |
| | 434926 | 320 | | ChronicleGuard.ThrowIfNull(owner, nameof(owner)); |
| | 434926 | 321 | | ChronicleGuard.ThrowIfArgument( |
| | 434926 | 322 | | _owner != null && !ReferenceEquals(_owner, owner), |
| | 434926 | 323 | | nameof(owner), |
| | 434926 | 324 | | "PhysicsMixedPartition is already owned by a different collision service."); |
| | | 325 | | |
| | 434925 | 326 | | _owner = owner; |
| | 434925 | 327 | | } |
| | | 328 | | |
| | 2038438 | 329 | | internal bool IsOwnedBy(GravitasMixedCollisionService owner) => ReferenceEquals(_owner, owner); |
| | | 330 | | |
| | | 331 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 332 | | internal void SetRetainedIndex(int index) |
| | | 333 | | { |
| | 767150 | 334 | | ChronicleGuard.ThrowIfNegative(index, nameof(index)); |
| | 767150 | 335 | | _retainedIndex = index; |
| | 767150 | 336 | | } |
| | | 337 | | |
| | | 338 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 434915 | 339 | | internal void ClearRetainedIndex() => _retainedIndex = -1; |
| | | 340 | | |
| | 1872405 | 341 | | int IRetainedPhysicsPartition<GravitasMixedCollisionService>.RetainedIndex => RetainedIndex; |
| | | 342 | | |
| | | 343 | | int IRetainedPhysicsPartition<GravitasMixedCollisionService>.EmptyIndex |
| | | 344 | | { |
| | 1416556 | 345 | | get => EmptyIndex; |
| | 1445876 | 346 | | set => EmptyIndex = value; |
| | | 347 | | } |
| | | 348 | | |
| | 2032498 | 349 | | bool IRetainedPhysicsPartition<GravitasMixedCollisionService>.IsEmpty => IsEmpty; |
| | | 350 | | |
| | 31054 | 351 | | long IRetainedPhysicsPartition<GravitasMixedCollisionService>.EmptySinceFrame => EmptySinceFrame; |
| | | 352 | | |
| | 2038438 | 353 | | bool IRetainedPhysicsPartition<GravitasMixedCollisionService>.IsOwnedBy(GravitasMixedCollisionService owner) => IsOw |
| | | 354 | | |
| | 767150 | 355 | | void IRetainedPhysicsPartition<GravitasMixedCollisionService>.SetRetainedIndex(int index) => SetRetainedIndex(index) |
| | | 356 | | |
| | 434915 | 357 | | void IRetainedPhysicsPartition<GravitasMixedCollisionService>.ClearRetainedIndex() => ClearRetainedIndex(); |
| | | 358 | | |
| | | 359 | | internal void ResetRetainedMembership() |
| | | 360 | | { |
| | 3 | 361 | | ReleaseAllMembership(); |
| | 3 | 362 | | ActivationId = -1; |
| | 3 | 363 | | MarkEmpty(Owner.Context.FrameCount); |
| | 3 | 364 | | } |
| | | 365 | | |
| | | 366 | | internal void ResetForPool() |
| | | 367 | | { |
| | 434917 | 368 | | if (ActivationId != -1) |
| | 70317 | 369 | | Owner.DeactivatePartition(ActivationId); |
| | | 370 | | |
| | 434917 | 371 | | ReleaseAllMembership(); |
| | 434917 | 372 | | _owner = null; |
| | 434917 | 373 | | ActivationId = -1; |
| | 434917 | 374 | | IsPartitioned = false; |
| | 434917 | 375 | | _emptySinceFrame = -1; |
| | 434917 | 376 | | _retainedIndex = -1; |
| | 434917 | 377 | | EmptyIndex = -1; |
| | 434917 | 378 | | } |
| | | 379 | | |
| | 434905 | 380 | | public void SetParentIndex(WorldVoxelIndex parentIndex) => WorldIndex = parentIndex; |
| | | 381 | | |
| | | 382 | | /// <summary>Clears and fills caller-owned storage with 3D collider IDs in ascending order.</summary> |
| | | 383 | | public void Copy3DColliderIds(SwiftList<int> destination) |
| | | 384 | | { |
| | 47150 | 385 | | ChronicleGuard.ThrowIfNull(destination, nameof(destination)); |
| | 47149 | 386 | | destination.FastClear(); |
| | 47149 | 387 | | AppendIds(ContainedDynamic3DObjects, destination); |
| | 47149 | 388 | | AppendIds(ContainedKinematic3DObjects, destination); |
| | 47149 | 389 | | AppendIds(ContainedStatic3DObjects, destination); |
| | 47149 | 390 | | destination.SortInPlace(); |
| | 47149 | 391 | | } |
| | | 392 | | |
| | | 393 | | /// <summary>Clears and fills caller-owned storage with 2D collider IDs in ascending order.</summary> |
| | | 394 | | public void Copy2DColliderIds(SwiftList<int> destination) |
| | | 395 | | { |
| | 16620 | 396 | | ChronicleGuard.ThrowIfNull(destination, nameof(destination)); |
| | 16619 | 397 | | destination.FastClear(); |
| | 16619 | 398 | | AppendIds(ContainedDynamic2DObjects, destination); |
| | 16619 | 399 | | AppendIds(ContainedKinematic2DObjects, destination); |
| | 16619 | 400 | | AppendIds(ContainedStatic2DObjects, destination); |
| | 16619 | 401 | | destination.SortInPlace(); |
| | 16619 | 402 | | } |
| | | 403 | | |
| | | 404 | | internal void CopyStaticStyle3DColliderIds(SwiftList<int> destination) |
| | | 405 | | { |
| | 16261 | 406 | | destination.FastClear(); |
| | 16261 | 407 | | AppendIds(ContainedKinematic3DObjects, destination); |
| | 16261 | 408 | | AppendIds(ContainedStatic3DObjects, destination); |
| | 16261 | 409 | | destination.SortInPlace(); |
| | 16261 | 410 | | } |
| | | 411 | | |
| | | 412 | | internal void CopyStaticStyle2DColliderIds(SwiftList<int> destination) |
| | | 413 | | { |
| | 1016079 | 414 | | destination.FastClear(); |
| | 1016079 | 415 | | AppendIds(ContainedKinematic2DObjects, destination); |
| | 1016079 | 416 | | AppendIds(ContainedStatic2DObjects, destination); |
| | 1016079 | 417 | | destination.SortInPlace(); |
| | 1016079 | 418 | | } |
| | | 419 | | |
| | | 420 | | private static void AppendIds(SwiftHashSet<int>? source, SwiftList<int> destination) |
| | | 421 | | { |
| | 2772078 | 422 | | if (source == null) |
| | 1311527 | 423 | | return; |
| | | 424 | | |
| | 6213798 | 425 | | foreach (int id in source) |
| | 1646348 | 426 | | destination.Add(id); |
| | 1460551 | 427 | | } |
| | | 428 | | |
| | | 429 | | private static void CopySortedIds(SwiftHashSet<int>? source, SwiftList<int> destination) |
| | | 430 | | { |
| | 1547070 | 431 | | if (source == null) |
| | | 432 | | { |
| | 1030976 | 433 | | destination.FastClear(); |
| | 1030976 | 434 | | return; |
| | | 435 | | } |
| | | 436 | | |
| | 516094 | 437 | | destination.FastClear(); |
| | 516094 | 438 | | AppendIds(source, destination); |
| | 516094 | 439 | | destination.SortInPlace(); |
| | 516094 | 440 | | } |
| | | 441 | | |
| | | 442 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 443 | | private void RemoveAndReleaseIfEmpty(ref SwiftHashSet<int>? membership, int id) |
| | | 444 | | { |
| | 61197 | 445 | | membership?.Remove(id); |
| | 61197 | 446 | | ReleaseIfEmpty(ref membership); |
| | 61197 | 447 | | } |
| | | 448 | | |
| | | 449 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 450 | | private void ReleaseIfEmpty(ref SwiftHashSet<int>? membership) |
| | | 451 | | { |
| | 109394 | 452 | | if (membership?.Count == 0) |
| | 79736 | 453 | | Owner.ReleasePartitionMembership(ref membership); |
| | 109394 | 454 | | } |
| | | 455 | | |
| | | 456 | | private void ReleaseAllMembership() |
| | | 457 | | { |
| | 434920 | 458 | | GravitasMixedCollisionService owner = Owner; |
| | 434920 | 459 | | owner.ReleasePartitionMembership(ref ContainedDynamic3DObjects); |
| | 434920 | 460 | | owner.ReleasePartitionMembership(ref ContainedAwakeDynamic3DObjects); |
| | 434920 | 461 | | owner.ReleasePartitionMembership(ref ContainedKinematic3DObjects); |
| | 434920 | 462 | | owner.ReleasePartitionMembership(ref ContainedStatic3DObjects); |
| | 434920 | 463 | | owner.ReleasePartitionMembership(ref ContainedDynamic2DObjects); |
| | 434920 | 464 | | owner.ReleasePartitionMembership(ref ContainedAwakeDynamic2DObjects); |
| | 434920 | 465 | | owner.ReleasePartitionMembership(ref ContainedKinematic2DObjects); |
| | 434920 | 466 | | owner.ReleasePartitionMembership(ref ContainedStatic2DObjects); |
| | 434920 | 467 | | } |
| | | 468 | | |
| | | 469 | | private bool IsDynamic3DObjectAwake(int id) |
| | | 470 | | { |
| | 82415 | 471 | | if (!Owner.Context.Physics.TryGetColliderById(id, out Gravitas.Colliders.LSCollider? collider)) |
| | 8 | 472 | | return true; |
| | | 473 | | |
| | 82407 | 474 | | SolidBody? body = collider!.Body; |
| | 82407 | 475 | | return body == null || body.IsAwakeForCollision; |
| | | 476 | | } |
| | | 477 | | |
| | | 478 | | private bool IsDynamic2DObjectAwake(int id) |
| | | 479 | | { |
| | 17855 | 480 | | if (!Owner.Context.Physics2D.TryGetColliderById(id, out Gravitas.Colliders.LSCollider2D? collider)) |
| | 5 | 481 | | return true; |
| | | 482 | | |
| | 17850 | 483 | | SolidBody2D? body = collider!.Body; |
| | 17850 | 484 | | return body == null || body.IsAwakeForCollision; |
| | | 485 | | } |
| | | 486 | | |
| | | 487 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 488 | | private void DeactivateIfNoDynamicMembers() |
| | | 489 | | { |
| | 27148 | 490 | | if (MovableDynamicObjectCount > 0) |
| | 1048 | 491 | | return; |
| | | 492 | | |
| | 26100 | 493 | | Owner.DeactivatePartition(ActivationId); |
| | 26100 | 494 | | ActivationId = -1; |
| | 26100 | 495 | | } |
| | | 496 | | |
| | | 497 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 498 | | private void MarkOccupied() |
| | | 499 | | { |
| | 512396 | 500 | | _emptySinceFrame = -1; |
| | 512396 | 501 | | Owner.RefreshRetainedPartitionEligibility(this); |
| | 512396 | 502 | | } |
| | | 503 | | |
| | | 504 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 505 | | private void MarkEmptyIfUnoccupied() |
| | | 506 | | { |
| | 48197 | 507 | | if (IsEmpty) |
| | 34328 | 508 | | MarkEmpty(Owner.Context.FrameCount); |
| | 48197 | 509 | | } |
| | | 510 | | |
| | | 511 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 512 | | private void MarkEmpty(long frame) |
| | | 513 | | { |
| | 34331 | 514 | | _emptySinceFrame = frame; |
| | 34331 | 515 | | Owner.RefreshRetainedPartitionEligibility(this); |
| | 34331 | 516 | | } |
| | | 517 | | } |