| | | 1 | | //======================================================================= |
| | | 2 | | // PhysicsPartition.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.CollisionHandling; |
| | | 10 | | using GridForge.Grids; |
| | | 11 | | using GridForge.Spatial; |
| | | 12 | | using SwiftCollections; |
| | | 13 | | |
| | | 14 | | namespace Gravitas; |
| | | 15 | | |
| | | 16 | | /// <summary> |
| | | 17 | | /// Stores the 3D collider IDs assigned to one GridForge voxel. |
| | | 18 | | /// </summary> |
| | | 19 | | public class PhysicsPartition : IVoxelPartition, IRetainedPhysicsPartition<GravitasCollisionService> |
| | | 20 | | { |
| | | 21 | | private GravitasCollisionService? _owner; |
| | 1098888 | 22 | | private long _emptySinceFrame = -1; |
| | 1098888 | 23 | | private int _retainedIndex = -1; |
| | | 24 | | |
| | | 25 | | /// <summary>Gets or sets the world voxel occupied by this partition.</summary> |
| | | 26 | | public WorldVoxelIndex WorldIndex { get; set; } |
| | | 27 | | |
| | | 28 | | /// <summary>Gets or sets whether this partition is attached to a voxel.</summary> |
| | | 29 | | public bool IsPartitioned { get; set; } |
| | | 30 | | |
| | | 31 | | /// <summary> |
| | | 32 | | /// Stores context-local dynamic body IDs. |
| | | 33 | | /// </summary> |
| | | 34 | | internal SwiftHashSet<int>? ContainedDynamicObjects; |
| | | 35 | | |
| | | 36 | | /// <summary> |
| | | 37 | | /// Stores the subset of dynamic collider IDs whose bodies can currently drive collision work. |
| | | 38 | | /// </summary> |
| | | 39 | | internal SwiftHashSet<int>? ContainedAwakeDynamicObjects; |
| | | 40 | | |
| | | 41 | | /// <summary>Stores context-local kinematic collider IDs.</summary> |
| | | 42 | | internal SwiftHashSet<int>? ContainedKinematicObjects; |
| | | 43 | | |
| | | 44 | | /// <summary>Stores context-local static collider IDs.</summary> |
| | | 45 | | internal SwiftHashSet<int>? ContainedStaticObjects; |
| | | 46 | | |
| | | 47 | | /// <summary>Gets the active-partition slot, or <c>-1</c> when inactive.</summary> |
| | | 48 | | public int ActivationId { get; private set; } |
| | | 49 | | |
| | | 50 | | /// <summary>Gets whether this partition has an active-partition slot.</summary> |
| | 1600690 | 51 | | public bool IsAllocated => ActivationId != -1; |
| | | 52 | | |
| | | 53 | | /// <summary>Gets the number of dynamic collider IDs, including sleeping bodies.</summary> |
| | 7371494 | 54 | | public int DynamicObjectCount => ContainedDynamicObjects?.Count ?? 0; |
| | | 55 | | |
| | | 56 | | /// <summary>Gets the number of kinematic collider IDs.</summary> |
| | 6027377 | 57 | | public int KinematicObjectCount => ContainedKinematicObjects?.Count ?? 0; |
| | | 58 | | |
| | | 59 | | /// <summary>Gets the number of static collider IDs.</summary> |
| | 5755170 | 60 | | public int StaticObjectCount => ContainedStaticObjects?.Count ?? 0; |
| | | 61 | | |
| | 7371493 | 62 | | internal bool IsEmpty => DynamicObjectCount == 0 && KinematicObjectCount == 0 && StaticObjectCount == 0; |
| | | 63 | | |
| | 329071 | 64 | | internal long EmptySinceFrame => _emptySinceFrame; |
| | | 65 | | |
| | 5349451 | 66 | | internal int RetainedIndex => _retainedIndex; |
| | | 67 | | |
| | | 68 | | internal int EmptyIndex { get; set; } = -1; |
| | | 69 | | |
| | | 70 | | /// <summary> |
| | | 71 | | /// Gets the number of awake dynamic IDs currently in this partition. |
| | | 72 | | /// </summary> |
| | 242 | 73 | | public int AwakeDynamicObjectCount => ContainedAwakeDynamicObjects?.Count ?? 0; |
| | | 74 | | |
| | | 75 | | /// <summary>Gets the collision service that owns this partition.</summary> |
| | | 76 | | public GravitasCollisionService Owner |
| | | 77 | | { |
| | | 78 | | get |
| | | 79 | | { |
| | 7147194 | 80 | | ChronicleGuard.ThrowIfTrue( |
| | 7147194 | 81 | | _owner == null, |
| | 7147194 | 82 | | nameof(PhysicsPartition), |
| | 7147194 | 83 | | "PhysicsPartition is missing its owner collision service."); |
| | 7147191 | 84 | | return _owner!; |
| | | 85 | | } |
| | | 86 | | } |
| | | 87 | | |
| | | 88 | | /// <summary>Creates an inactive 3D physics partition.</summary> |
| | 1098888 | 89 | | public PhysicsPartition() |
| | | 90 | | { |
| | 1098888 | 91 | | ActivationId = -1; |
| | 1098888 | 92 | | } |
| | | 93 | | |
| | | 94 | | /// <summary>Attaches this partition to a voxel.</summary> |
| | | 95 | | public void OnAddToVoxel(Voxel voxel) |
| | | 96 | | { |
| | 1159173 | 97 | | ChronicleGuard.ThrowIfTrue(_owner == null, nameof(PhysicsPartition), "PhysicsPartition is missing its owner coll |
| | 1159173 | 98 | | WorldIndex = voxel.WorldIndex; |
| | 1159173 | 99 | | IsPartitioned = true; |
| | 1159173 | 100 | | } |
| | | 101 | | |
| | | 102 | | internal void Distribute( |
| | | 103 | | SwiftList<int> dynamicIds, |
| | | 104 | | SwiftList<int> staticIds) |
| | | 105 | | { |
| | 2011531 | 106 | | int dynamicCount = ContainedDynamicObjects?.Count ?? 0; |
| | 2011531 | 107 | | int awakeDynamicCount = ContainedAwakeDynamicObjects?.Count ?? 0; |
| | 2011531 | 108 | | if (ContainedDynamicObjects == null || dynamicCount == 0 || ContainedAwakeDynamicObjects == null || awakeDynamic |
| | 78326 | 109 | | return; |
| | | 110 | | |
| | 1933205 | 111 | | dynamicIds.FastClear(); |
| | 1933205 | 112 | | CopyIds(ContainedDynamicObjects, dynamicIds); |
| | 1933205 | 113 | | dynamicIds.SortInPlace(); |
| | 1933205 | 114 | | CopyStaticStyleColliderIds(staticIds); |
| | | 115 | | |
| | | 116 | | // Sleeping bodies stay query-visible in dynamic membership, while awake membership gates partition work. |
| | | 117 | | // Once an active partition is selected, all local dynamic links are emitted so the discrete |
| | | 118 | | // island builder can wake and solve connected bodies in a stable graph instead of a flat pair pass. |
| | 8111316 | 119 | | for (int j = 0; j < dynamicIds.Count; j++) |
| | | 120 | | { |
| | 2122453 | 121 | | int id1 = dynamicIds[j]; |
| | 4790934 | 122 | | for (int k = j + 1; k < dynamicIds.Count; k++) |
| | 273014 | 123 | | ProcessPair(id1, dynamicIds[k]); |
| | | 124 | | |
| | 8585270 | 125 | | for (int k = 0; k < staticIds.Count; k++) |
| | | 126 | | { |
| | 2170182 | 127 | | int id2 = staticIds[k]; |
| | 2170182 | 128 | | ProcessPair(id1, id2); |
| | | 129 | | } |
| | | 130 | | } |
| | 1933205 | 131 | | } |
| | | 132 | | |
| | | 133 | | /// <summary>Clears and fills caller-owned storage with all collider IDs in ascending order.</summary> |
| | | 134 | | public void CopyAllColliderIds(SwiftList<int> destination) |
| | | 135 | | { |
| | 179117 | 136 | | ChronicleGuard.ThrowIfNull(destination, nameof(destination)); |
| | 179116 | 137 | | destination.FastClear(); |
| | 179116 | 138 | | CopyIds(ContainedDynamicObjects, destination); |
| | 179116 | 139 | | CopyIds(ContainedKinematicObjects, destination); |
| | 179116 | 140 | | CopyIds(ContainedStaticObjects, destination); |
| | 179116 | 141 | | destination.SortInPlace(); |
| | 179116 | 142 | | } |
| | | 143 | | |
| | | 144 | | internal void CopyStaticStyleColliderIds(SwiftList<int> destination) |
| | | 145 | | { |
| | 1933207 | 146 | | destination.FastClear(); |
| | 1933207 | 147 | | CopyIds(ContainedKinematicObjects, destination); |
| | 1933207 | 148 | | CopyIds(ContainedStaticObjects, destination); |
| | 1933207 | 149 | | destination.SortInPlace(); |
| | 1933207 | 150 | | } |
| | | 151 | | |
| | | 152 | | private static void CopyIds(SwiftHashSet<int>? source, SwiftList<int> destination) |
| | | 153 | | { |
| | 6336967 | 154 | | if (source == null) |
| | 3011941 | 155 | | return; |
| | | 156 | | |
| | 15528150 | 157 | | foreach (int id in source) |
| | 4439049 | 158 | | destination.Add(id); |
| | 3325026 | 159 | | } |
| | | 160 | | |
| | | 161 | | private void ProcessPair(int id1, int id2) |
| | | 162 | | { |
| | 2443196 | 163 | | GravitasCollisionService owner = Owner; |
| | 2443196 | 164 | | CollisionPair? pair = owner.Context.Physics.GetCollisionPair(id1, id2); |
| | | 165 | | |
| | | 166 | | //Ensures collision pairs are not run twice |
| | 2443196 | 167 | | if (pair == null || pair.PartitionVersion == owner.Version) |
| | 2350492 | 168 | | return; |
| | | 169 | | |
| | 92704 | 170 | | pair.PartitionVersion = owner.Version; |
| | 92704 | 171 | | pair.UpdateCollisionDeferred(); |
| | 92704 | 172 | | } |
| | | 173 | | |
| | | 174 | | /// <summary>Adds a dynamic collider ID to this partition.</summary> |
| | | 175 | | public void AddDynamicObject(int item) |
| | | 176 | | { |
| | 4345422 | 177 | | ChronicleGuard.ThrowIfNegative(item, nameof(item)); |
| | 4345421 | 178 | | ContainedDynamicObjects ??= new(); |
| | 4345421 | 179 | | if (!ContainedDynamicObjects.Add(item)) |
| | 3789273 | 180 | | return; |
| | | 181 | | |
| | 556148 | 182 | | MarkOccupied(); |
| | 556148 | 183 | | SetDynamicObjectAwake(item, IsDynamicObjectAwake(item)); |
| | | 184 | | |
| | 556148 | 185 | | if (ContainedDynamicObjects.Count == 1) |
| | 537548 | 186 | | ActivationId = Owner.ActivatePartition(this); |
| | 556148 | 187 | | } |
| | | 188 | | |
| | | 189 | | /// <summary>Adds a static collider ID to this partition.</summary> |
| | | 190 | | public void AddStaticObject(int item) |
| | | 191 | | { |
| | 790270 | 192 | | ChronicleGuard.ThrowIfNegative(item, nameof(item)); |
| | 790269 | 193 | | ContainedStaticObjects ??= new(); |
| | 790269 | 194 | | if (ContainedStaticObjects.Add(item)) |
| | 779284 | 195 | | MarkOccupied(); |
| | 790269 | 196 | | } |
| | | 197 | | |
| | | 198 | | /// <summary>Adds a kinematic collider ID to this partition.</summary> |
| | | 199 | | public void AddKinematicObject(int item) |
| | | 200 | | { |
| | 380815 | 201 | | ChronicleGuard.ThrowIfNegative(item, nameof(item)); |
| | 380814 | 202 | | ContainedKinematicObjects ??= new(); |
| | 380814 | 203 | | if (ContainedKinematicObjects.Add(item)) |
| | 259540 | 204 | | MarkOccupied(); |
| | 380814 | 205 | | } |
| | | 206 | | |
| | | 207 | | /// <summary>Removes a dynamic collider ID from this partition.</summary> |
| | | 208 | | public void RemoveDynamicObject(int item) |
| | | 209 | | { |
| | 95204 | 210 | | if (ContainedDynamicObjects?.Remove(item) != true) |
| | | 211 | | { |
| | 4 | 212 | | GravitasLogger.DebugChannel.Info($"Dynamic item not removed - {item}"); |
| | 4 | 213 | | return; |
| | | 214 | | } |
| | | 215 | | |
| | 95200 | 216 | | ContainedAwakeDynamicObjects?.Remove(item); |
| | | 217 | | |
| | 95200 | 218 | | if (ContainedDynamicObjects.Count > 0) |
| | 4476 | 219 | | return; |
| | | 220 | | |
| | | 221 | | // If there are no more dynamic objects, we can deactivate the partition to save on future checks until it's nee |
| | 90724 | 222 | | Owner.DeactivatePartition(ActivationId); |
| | 90724 | 223 | | ActivationId = -1; |
| | 90724 | 224 | | MarkEmptyIfUnoccupied(); |
| | 90724 | 225 | | } |
| | | 226 | | |
| | | 227 | | /// <summary>Removes a static collider ID from this partition.</summary> |
| | | 228 | | public void RemoveStaticObject(int item) |
| | | 229 | | { |
| | 44987 | 230 | | if (ContainedStaticObjects?.Remove(item) != true) |
| | | 231 | | { |
| | 5 | 232 | | GravitasLogger.DebugChannel.Info($"Static item not removed - {item}"); |
| | 5 | 233 | | return; |
| | | 234 | | } |
| | | 235 | | |
| | 44982 | 236 | | MarkEmptyIfUnoccupied(); |
| | 44982 | 237 | | } |
| | | 238 | | |
| | | 239 | | /// <summary>Removes a kinematic collider ID from this partition.</summary> |
| | | 240 | | public void RemoveKinematicObject(int item) |
| | | 241 | | { |
| | 232289 | 242 | | if (ContainedKinematicObjects?.Remove(item) != true) |
| | | 243 | | { |
| | 5 | 244 | | GravitasLogger.DebugChannel.Info($"Kinematic item not removed - {item}"); |
| | 5 | 245 | | return; |
| | | 246 | | } |
| | | 247 | | |
| | 232284 | 248 | | MarkEmptyIfUnoccupied(); |
| | 232284 | 249 | | } |
| | | 250 | | |
| | | 251 | | /// <summary> |
| | | 252 | | /// Returns true when the supplied dynamic collider ID is marked awake in this partition. |
| | | 253 | | /// </summary> |
| | 15 | 254 | | public bool ContainsAwakeDynamicObject(int item) => ContainedAwakeDynamicObjects?.Contains(item) == true; |
| | | 255 | | |
| | | 256 | | /// <summary> |
| | | 257 | | /// Updates the awake dynamic subset for a collider already present in dynamic membership. |
| | | 258 | | /// </summary> |
| | | 259 | | public void SetDynamicObjectAwake(int item, bool awake) |
| | | 260 | | { |
| | 5104948 | 261 | | if (ContainedDynamicObjects?.Contains(item) != true) |
| | 2 | 262 | | return; |
| | | 263 | | |
| | 5104946 | 264 | | if (awake) |
| | | 265 | | { |
| | 5082012 | 266 | | ContainedAwakeDynamicObjects ??= new(); |
| | 5082012 | 267 | | ContainedAwakeDynamicObjects.Add(item); |
| | 5082012 | 268 | | return; |
| | | 269 | | } |
| | | 270 | | |
| | 22934 | 271 | | ContainedAwakeDynamicObjects?.Remove(item); |
| | 22181 | 272 | | } |
| | | 273 | | |
| | | 274 | | private bool IsDynamicObjectAwake(int item) |
| | | 275 | | { |
| | 556148 | 276 | | if (!Owner.Context.Physics.TryGetColliderById(item, out Gravitas.Colliders.LSCollider? collider)) |
| | 14 | 277 | | return true; |
| | | 278 | | |
| | 556134 | 279 | | SolidBody? body = collider!.Body; |
| | 556134 | 280 | | return body == null || body.IsAwakeForCollision; |
| | | 281 | | } |
| | | 282 | | |
| | | 283 | | /// <summary>Releases this partition when it is removed from a voxel.</summary> |
| | | 284 | | public void OnRemoveFromVoxel(Voxel voxel) |
| | | 285 | | { |
| | 1159174 | 286 | | Owner.ReleasePartition(this); |
| | 1159173 | 287 | | } |
| | | 288 | | |
| | | 289 | | internal void SetOwner(GravitasCollisionService owner) |
| | | 290 | | { |
| | 1159194 | 291 | | ChronicleGuard.ThrowIfNull(owner, nameof(owner)); |
| | 1159194 | 292 | | ChronicleGuard.ThrowIfArgument( |
| | 1159194 | 293 | | _owner != null && !ReferenceEquals(_owner, owner), |
| | 1159194 | 294 | | nameof(owner), |
| | 1159194 | 295 | | "PhysicsPartition is already owned by a different collision service."); |
| | | 296 | | |
| | 1159193 | 297 | | _owner = owner; |
| | 1159193 | 298 | | } |
| | | 299 | | |
| | 7020941 | 300 | | internal bool IsOwnedBy(GravitasCollisionService owner) => ReferenceEquals(_owner, owner); |
| | | 301 | | |
| | | 302 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 303 | | internal void SetRetainedIndex(int index) |
| | | 304 | | { |
| | 1934709 | 305 | | ChronicleGuard.ThrowIfNegative(index, nameof(index)); |
| | 1934709 | 306 | | _retainedIndex = index; |
| | 1934709 | 307 | | } |
| | | 308 | | |
| | | 309 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 1159179 | 310 | | internal void ClearRetainedIndex() => _retainedIndex = -1; |
| | | 311 | | |
| | | 312 | | internal void ResetRetainedMembership() |
| | | 313 | | { |
| | 6 | 314 | | ContainedDynamicObjects?.Clear(); |
| | 6 | 315 | | ContainedAwakeDynamicObjects?.Clear(); |
| | 6 | 316 | | ContainedKinematicObjects?.Clear(); |
| | 6 | 317 | | ContainedStaticObjects?.Clear(); |
| | 6 | 318 | | ActivationId = -1; |
| | 6 | 319 | | MarkEmpty(_owner?.Context.FrameCount ?? 0); |
| | 6 | 320 | | } |
| | | 321 | | |
| | | 322 | | internal void ResetForPool() |
| | | 323 | | { |
| | 1159182 | 324 | | ContainedDynamicObjects?.Clear(); |
| | 1159182 | 325 | | ContainedAwakeDynamicObjects?.Clear(); |
| | 1159182 | 326 | | ContainedKinematicObjects?.Clear(); |
| | 1159182 | 327 | | ContainedStaticObjects?.Clear(); |
| | | 328 | | |
| | 1159182 | 329 | | if (ActivationId != -1) |
| | 446820 | 330 | | Owner.DeactivatePartition(ActivationId); |
| | | 331 | | |
| | 1159182 | 332 | | _owner = null; |
| | 1159182 | 333 | | ActivationId = -1; |
| | 1159182 | 334 | | IsPartitioned = false; |
| | 1159182 | 335 | | _emptySinceFrame = -1; |
| | 1159182 | 336 | | _retainedIndex = -1; |
| | 1159182 | 337 | | EmptyIndex = -1; |
| | 1159182 | 338 | | } |
| | | 339 | | |
| | | 340 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 341 | | private void MarkOccupied() |
| | | 342 | | { |
| | 1594972 | 343 | | _emptySinceFrame = -1; |
| | 1594972 | 344 | | _owner?.RefreshRetainedPartitionEligibility(this); |
| | 1594969 | 345 | | } |
| | | 346 | | |
| | | 347 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 348 | | private void MarkEmptyIfUnoccupied() |
| | | 349 | | { |
| | 367990 | 350 | | if (IsEmpty) |
| | 216852 | 351 | | MarkEmpty(Owner.Context.FrameCount); |
| | 367990 | 352 | | } |
| | | 353 | | |
| | | 354 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 355 | | private void MarkEmpty(long frame) |
| | | 356 | | { |
| | 216858 | 357 | | _emptySinceFrame = frame; |
| | 216858 | 358 | | _owner?.RefreshRetainedPartitionEligibility(this); |
| | 216855 | 359 | | } |
| | | 360 | | |
| | | 361 | | /// <summary> |
| | | 362 | | /// Sets the parent index for the current voxel in the world. |
| | | 363 | | /// </summary> |
| | | 364 | | /// <param name="parentIndex">The index to assign as the parent of the current voxel.</param> |
| | 1159173 | 365 | | public void SetParentIndex(WorldVoxelIndex parentIndex) => WorldIndex = parentIndex; |
| | | 366 | | |
| | 5349450 | 367 | | int IRetainedPhysicsPartition<GravitasCollisionService>.RetainedIndex => RetainedIndex; |
| | | 368 | | |
| | | 369 | | int IRetainedPhysicsPartition<GravitasCollisionService>.EmptyIndex |
| | | 370 | | { |
| | 4130185 | 371 | | get => EmptyIndex; |
| | 4274832 | 372 | | set => EmptyIndex = value; |
| | | 373 | | } |
| | | 374 | | |
| | 7003493 | 375 | | bool IRetainedPhysicsPartition<GravitasCollisionService>.IsEmpty => IsEmpty; |
| | | 376 | | |
| | 329062 | 377 | | long IRetainedPhysicsPartition<GravitasCollisionService>.EmptySinceFrame => EmptySinceFrame; |
| | | 378 | | |
| | 7020941 | 379 | | bool IRetainedPhysicsPartition<GravitasCollisionService>.IsOwnedBy(GravitasCollisionService owner) => IsOwnedBy(owne |
| | | 380 | | |
| | 1934709 | 381 | | void IRetainedPhysicsPartition<GravitasCollisionService>.SetRetainedIndex(int index) => SetRetainedIndex(index); |
| | | 382 | | |
| | 1159179 | 383 | | void IRetainedPhysicsPartition<GravitasCollisionService>.ClearRetainedIndex() => ClearRetainedIndex(); |
| | | 384 | | } |