| | | 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 Gravitas.CollisionHandling; |
| | | 9 | | using GridForge.Grids; |
| | | 10 | | using GridForge.Spatial; |
| | | 11 | | using SwiftCollections; |
| | | 12 | | using System.Runtime.CompilerServices; |
| | | 13 | | |
| | | 14 | | namespace Gravitas; |
| | | 15 | | |
| | | 16 | | internal enum PhysicsPartitionMobilityKind |
| | | 17 | | { |
| | | 18 | | Dynamic = 0, |
| | | 19 | | Kinematic = 1, |
| | | 20 | | Static = 2 |
| | | 21 | | } |
| | | 22 | | |
| | | 23 | | /// <summary> |
| | | 24 | | /// Stores the 3D collider IDs assigned to one GridForge voxel. |
| | | 25 | | /// </summary> |
| | | 26 | | public class PhysicsPartition : IVoxelPartition, IRetainedPhysicsPartition<GravitasCollisionService> |
| | | 27 | | { |
| | | 28 | | private GravitasCollisionService? _owner; |
| | 372994 | 29 | | private int _emptySinceFrame = -1; |
| | 372994 | 30 | | private int _retainedIndex = -1; |
| | | 31 | | |
| | | 32 | | /// <summary>Gets or sets the world voxel occupied by this partition.</summary> |
| | | 33 | | public WorldVoxelIndex WorldIndex { get; set; } |
| | | 34 | | |
| | | 35 | | /// <summary>Gets or sets whether this partition is attached to a voxel.</summary> |
| | | 36 | | public bool IsPartitioned { get; set; } |
| | | 37 | | |
| | | 38 | | /// <summary> |
| | | 39 | | /// Stores context-local dynamic body IDs. |
| | | 40 | | /// </summary> |
| | | 41 | | public SwiftSparseSet? ContainedDynamicObjects; |
| | | 42 | | |
| | | 43 | | /// <summary> |
| | | 44 | | /// Stores the subset of dynamic collider IDs whose bodies can currently drive collision work. |
| | | 45 | | /// </summary> |
| | | 46 | | public SwiftSparseSet? ContainedAwakeDynamicObjects; |
| | | 47 | | |
| | | 48 | | /// <summary>Stores context-local kinematic collider IDs.</summary> |
| | | 49 | | public SwiftSparseSet? ContainedKinematicObjects; |
| | | 50 | | |
| | | 51 | | /// <summary>Stores context-local static collider IDs.</summary> |
| | | 52 | | public SwiftSparseSet? ContainedStaticObjects; |
| | | 53 | | |
| | | 54 | | /// <summary>Gets the active-partition slot, or <c>-1</c> when inactive.</summary> |
| | | 55 | | public int ActivationId { get; private set; } |
| | | 56 | | |
| | | 57 | | /// <summary>Gets whether this partition has an active-partition slot.</summary> |
| | 36849 | 58 | | public bool IsAllocated => ActivationId != -1; |
| | | 59 | | |
| | | 60 | | internal bool IsEmpty => |
| | 2147483647 | 61 | | (ContainedDynamicObjects?.Count ?? 0) == 0 |
| | 2147483647 | 62 | | && (ContainedKinematicObjects?.Count ?? 0) == 0 |
| | 2147483647 | 63 | | && (ContainedStaticObjects?.Count ?? 0) == 0; |
| | | 64 | | |
| | 9187 | 65 | | internal int EmptySinceFrame => _emptySinceFrame; |
| | | 66 | | |
| | 810499 | 67 | | internal int RetainedIndex => _retainedIndex; |
| | | 68 | | |
| | | 69 | | /// <summary> |
| | | 70 | | /// Gets the number of awake dynamic IDs currently in this partition. |
| | | 71 | | /// </summary> |
| | 8 | 72 | | public int AwakeDynamicObjectCount => ContainedAwakeDynamicObjects?.Count ?? 0; |
| | | 73 | | |
| | | 74 | | /// <summary>Gets the collision service that owns this partition.</summary> |
| | | 75 | | public GravitasCollisionService Owner |
| | | 76 | | { |
| | | 77 | | get |
| | | 78 | | { |
| | 2228563 | 79 | | SwiftThrowHelper.ThrowIfTrue( |
| | 2228563 | 80 | | _owner == null, |
| | 2228563 | 81 | | nameof(PhysicsPartition), |
| | 2228563 | 82 | | "PhysicsPartition is missing its owner collision service."); |
| | 2228560 | 83 | | return _owner!; |
| | | 84 | | } |
| | | 85 | | } |
| | | 86 | | |
| | | 87 | | /// <summary>Creates an inactive 3D physics partition.</summary> |
| | 372994 | 88 | | public PhysicsPartition() |
| | | 89 | | { |
| | 372994 | 90 | | ActivationId = -1; |
| | 372994 | 91 | | } |
| | | 92 | | |
| | | 93 | | /// <summary>Attaches this partition to a voxel.</summary> |
| | | 94 | | public void OnAddToVoxel(Voxel voxel) |
| | | 95 | | { |
| | 405247 | 96 | | SwiftThrowHelper.ThrowIfTrue(_owner == null, nameof(PhysicsPartition), "PhysicsPartition is missing its owner co |
| | 405247 | 97 | | WorldIndex = voxel.WorldIndex; |
| | 405247 | 98 | | IsPartitioned = true; |
| | 405247 | 99 | | } |
| | | 100 | | |
| | | 101 | | internal void Distribute( |
| | | 102 | | SwiftList<int> dynamicIds, |
| | | 103 | | SwiftList<int> staticIds) |
| | | 104 | | { |
| | 126223 | 105 | | int dynamicCount = ContainedDynamicObjects?.Count ?? 0; |
| | 126223 | 106 | | int awakeDynamicCount = ContainedAwakeDynamicObjects?.Count ?? 0; |
| | 126223 | 107 | | if (ContainedDynamicObjects == null || dynamicCount == 0 || ContainedAwakeDynamicObjects == null || awakeDynamic |
| | 13873 | 108 | | return; |
| | | 109 | | |
| | 112350 | 110 | | ContainedDynamicObjects.CopySortedKeysTo(dynamicIds); |
| | 112350 | 111 | | CopySortedStaticStyleIds(staticIds); |
| | | 112 | | |
| | | 113 | | // Sleeping bodies stay query-visible in dynamic membership, while awake membership gates partition work. |
| | | 114 | | // Once an active partition is selected, all local dynamic links are emitted so the discrete |
| | | 115 | | // island builder can wake and solve connected bodies in a stable graph instead of a flat pair pass. |
| | 561042 | 116 | | for (int j = 0; j < dynamicIds.Count; j++) |
| | | 117 | | { |
| | 168171 | 118 | | int id1 = dynamicIds[j]; |
| | 510000 | 119 | | for (int k = j + 1; k < dynamicIds.Count; k++) |
| | 86829 | 120 | | ProcessPair(id1, dynamicIds[k]); |
| | | 121 | | |
| | 435354 | 122 | | for (int k = 0; k < staticIds.Count; k++) |
| | | 123 | | { |
| | 49506 | 124 | | int id2 = staticIds[k]; |
| | 49506 | 125 | | ProcessPair(id1, id2); |
| | | 126 | | } |
| | | 127 | | } |
| | 112350 | 128 | | } |
| | | 129 | | |
| | | 130 | | private void CopySortedStaticStyleIds(SwiftList<int> destination) |
| | | 131 | | { |
| | 112350 | 132 | | destination.FastClear(); |
| | 112350 | 133 | | CopyIds(ContainedKinematicObjects, destination); |
| | 112350 | 134 | | CopyIds(ContainedStaticObjects, destination); |
| | 112350 | 135 | | destination.SortInPlace(); |
| | 112350 | 136 | | } |
| | | 137 | | |
| | | 138 | | internal void CopyAllColliderIds(SwiftList<int> destination) |
| | | 139 | | { |
| | 2021 | 140 | | destination.FastClear(); |
| | 2021 | 141 | | CopyIds(ContainedDynamicObjects, destination); |
| | 2021 | 142 | | CopyIds(ContainedKinematicObjects, destination); |
| | 2021 | 143 | | CopyIds(ContainedStaticObjects, destination); |
| | 2021 | 144 | | destination.SortInPlace(); |
| | 2021 | 145 | | } |
| | | 146 | | |
| | | 147 | | internal void CopyStaticStyleColliderIds(SwiftList<int> destination) |
| | | 148 | | { |
| | 2 | 149 | | destination.FastClear(); |
| | 2 | 150 | | CopyIds(ContainedKinematicObjects, destination); |
| | 2 | 151 | | CopyIds(ContainedStaticObjects, destination); |
| | 2 | 152 | | destination.SortInPlace(); |
| | 2 | 153 | | } |
| | | 154 | | |
| | | 155 | | private static void CopyIds(SwiftSparseSet? source, SwiftList<int> destination) |
| | | 156 | | { |
| | 230767 | 157 | | if (source == null) |
| | 202864 | 158 | | return; |
| | | 159 | | |
| | 110156 | 160 | | for (int i = 0; i < source.Count; i++) |
| | 27175 | 161 | | destination.Add(source.DenseKeys[i]); |
| | 27903 | 162 | | } |
| | | 163 | | |
| | | 164 | | private void ProcessPair(int id1, int id2) |
| | | 165 | | { |
| | 136335 | 166 | | GravitasCollisionService owner = Owner; |
| | 136335 | 167 | | CollisionPair? pair = owner.Context.Physics.GetCollisionPair(id1, id2); |
| | | 168 | | |
| | | 169 | | //Ensures collision pairs are not run twice |
| | 136335 | 170 | | if (pair == null || pair.PartitionVersion == owner.Version) |
| | 132043 | 171 | | return; |
| | | 172 | | |
| | 4292 | 173 | | pair.PartitionVersion = owner.Version; |
| | 4292 | 174 | | pair.UpdateCollisionDeferred(); |
| | 4292 | 175 | | } |
| | | 176 | | |
| | | 177 | | /// <summary>Adds a dynamic collider ID to this partition.</summary> |
| | | 178 | | public void AddDynamicObject(int item) |
| | | 179 | | { |
| | 306991 | 180 | | ContainedDynamicObjects ??= new(); |
| | 306991 | 181 | | if (!ContainedDynamicObjects.Add(item)) |
| | 2 | 182 | | return; |
| | | 183 | | |
| | 306989 | 184 | | MarkOccupied(); |
| | 306989 | 185 | | SetDynamicObjectAwake(item, IsDynamicObjectAwake(item)); |
| | | 186 | | |
| | 306989 | 187 | | if (ContainedDynamicObjects.Count == 1) |
| | 220726 | 188 | | ActivationId = Owner.ActivatePartition(this); |
| | 306989 | 189 | | } |
| | | 190 | | |
| | | 191 | | /// <summary>Adds a static collider ID to this partition.</summary> |
| | | 192 | | public void AddStaticObject(int item) |
| | | 193 | | { |
| | 308360 | 194 | | ContainedStaticObjects ??= new(); |
| | 308360 | 195 | | if (ContainedStaticObjects.Add(item)) |
| | 308358 | 196 | | MarkOccupied(); |
| | 308360 | 197 | | } |
| | | 198 | | |
| | | 199 | | /// <summary>Adds a kinematic collider ID to this partition.</summary> |
| | | 200 | | public void AddKinematicObject(int item) |
| | | 201 | | { |
| | 358940 | 202 | | ContainedKinematicObjects ??= new(); |
| | 358940 | 203 | | if (ContainedKinematicObjects.Add(item)) |
| | 358938 | 204 | | MarkOccupied(); |
| | 358940 | 205 | | } |
| | | 206 | | |
| | | 207 | | /// <summary>Removes a dynamic collider ID from this partition.</summary> |
| | | 208 | | public void RemoveDynamicObject(int item) |
| | | 209 | | { |
| | 158850 | 210 | | if (ContainedDynamicObjects?.Remove(item) != true) |
| | | 211 | | { |
| | 4 | 212 | | GravitasLogger.DebugChannel.Info($"Dynamic item not removed - {item}"); |
| | 4 | 213 | | return; |
| | | 214 | | } |
| | | 215 | | |
| | 158846 | 216 | | ContainedAwakeDynamicObjects?.Remove(item); |
| | | 217 | | |
| | 158846 | 218 | | if (ContainedDynamicObjects.Count > 0) |
| | 75138 | 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 |
| | 83708 | 222 | | Owner.DeactivatePartition(ActivationId); |
| | 83708 | 223 | | ActivationId = -1; |
| | 83708 | 224 | | MarkEmptyIfUnoccupied(); |
| | 83708 | 225 | | } |
| | | 226 | | |
| | | 227 | | /// <summary>Removes a static collider ID from this partition.</summary> |
| | | 228 | | public void RemoveStaticObject(int item) |
| | | 229 | | { |
| | 6158 | 230 | | if (ContainedStaticObjects?.Remove(item) != true) |
| | | 231 | | { |
| | 5 | 232 | | GravitasLogger.DebugChannel.Info($"Static item not removed - {item}"); |
| | 5 | 233 | | return; |
| | | 234 | | } |
| | | 235 | | |
| | 6153 | 236 | | MarkEmptyIfUnoccupied(); |
| | 6153 | 237 | | } |
| | | 238 | | |
| | | 239 | | /// <summary>Removes a kinematic collider ID from this partition.</summary> |
| | | 240 | | public void RemoveKinematicObject(int item) |
| | | 241 | | { |
| | 342285 | 242 | | if (ContainedKinematicObjects?.Remove(item) != true) |
| | | 243 | | { |
| | 5 | 244 | | GravitasLogger.DebugChannel.Info($"Kinematic item not removed - {item}"); |
| | 5 | 245 | | return; |
| | | 246 | | } |
| | | 247 | | |
| | 342280 | 248 | | MarkEmptyIfUnoccupied(); |
| | 342280 | 249 | | } |
| | | 250 | | |
| | | 251 | | /// <summary> |
| | | 252 | | /// Returns true when the supplied dynamic collider ID is marked awake in this partition. |
| | | 253 | | /// </summary> |
| | 9 | 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 | | { |
| | 315644 | 261 | | if (ContainedDynamicObjects?.Contains(item) != true) |
| | 2 | 262 | | return; |
| | | 263 | | |
| | 315642 | 264 | | if (awake) |
| | | 265 | | { |
| | 309850 | 266 | | ContainedAwakeDynamicObjects ??= new(); |
| | 309850 | 267 | | ContainedAwakeDynamicObjects.Add(item); |
| | 309850 | 268 | | return; |
| | | 269 | | } |
| | | 270 | | |
| | 5792 | 271 | | ContainedAwakeDynamicObjects?.Remove(item); |
| | 5575 | 272 | | } |
| | | 273 | | |
| | | 274 | | private bool IsDynamicObjectAwake(int item) |
| | | 275 | | { |
| | 306989 | 276 | | if (!Owner.Context.Physics.TryGetColliderById(item, out Gravitas.Colliders.LSCollider? collider)) |
| | 12 | 277 | | return true; |
| | | 278 | | |
| | 306977 | 279 | | SolidBody? body = collider!.Body; |
| | 306977 | 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 | | { |
| | 405248 | 286 | | Owner.ReleasePartition(this); |
| | 405247 | 287 | | } |
| | | 288 | | |
| | | 289 | | internal void SetOwner(GravitasCollisionService owner) |
| | | 290 | | { |
| | 405263 | 291 | | SwiftThrowHelper.ThrowIfNull(owner, nameof(owner)); |
| | 405263 | 292 | | SwiftThrowHelper.ThrowIfArgument( |
| | 405263 | 293 | | _owner != null && !ReferenceEquals(_owner, owner), |
| | 405263 | 294 | | nameof(owner), |
| | 405263 | 295 | | "PhysicsPartition is already owned by a different collision service."); |
| | | 296 | | |
| | 405262 | 297 | | _owner = owner; |
| | 405262 | 298 | | } |
| | | 299 | | |
| | 2147483647 | 300 | | internal bool IsOwnedBy(GravitasCollisionService owner) => ReferenceEquals(_owner, owner); |
| | | 301 | | |
| | | 302 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 303 | | internal void SetRetainedIndex(int index) |
| | | 304 | | { |
| | 661810 | 305 | | SwiftThrowHelper.ThrowIfNegative(index, nameof(index)); |
| | 661810 | 306 | | _retainedIndex = index; |
| | 661810 | 307 | | } |
| | | 308 | | |
| | | 309 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 405252 | 310 | | internal void ClearRetainedIndex() => _retainedIndex = -1; |
| | | 311 | | |
| | | 312 | | internal void ResetRetainedMembership() |
| | | 313 | | { |
| | 3 | 314 | | ContainedDynamicObjects?.Clear(); |
| | 3 | 315 | | ContainedAwakeDynamicObjects?.Clear(); |
| | 3 | 316 | | ContainedKinematicObjects?.Clear(); |
| | 3 | 317 | | ContainedStaticObjects?.Clear(); |
| | 3 | 318 | | ActivationId = -1; |
| | 3 | 319 | | MarkEmpty(_owner?.Context.FrameCount ?? 0); |
| | 3 | 320 | | } |
| | | 321 | | |
| | | 322 | | internal void ResetForPool() |
| | | 323 | | { |
| | 405254 | 324 | | ContainedDynamicObjects?.Clear(); |
| | 405254 | 325 | | ContainedAwakeDynamicObjects?.Clear(); |
| | 405254 | 326 | | ContainedKinematicObjects?.Clear(); |
| | 405254 | 327 | | ContainedStaticObjects?.Clear(); |
| | | 328 | | |
| | 405254 | 329 | | if (ActivationId != -1) |
| | 137016 | 330 | | Owner.DeactivatePartition(ActivationId); |
| | | 331 | | |
| | 405254 | 332 | | _owner = null; |
| | 405254 | 333 | | ActivationId = -1; |
| | 405254 | 334 | | IsPartitioned = false; |
| | 405254 | 335 | | _emptySinceFrame = -1; |
| | 405254 | 336 | | _retainedIndex = -1; |
| | 405254 | 337 | | } |
| | | 338 | | |
| | | 339 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 974285 | 340 | | private void MarkOccupied() => _emptySinceFrame = -1; |
| | | 341 | | |
| | | 342 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 343 | | private void MarkEmptyIfUnoccupied() |
| | | 344 | | { |
| | 432141 | 345 | | if (IsEmpty) |
| | 312558 | 346 | | MarkEmpty(Owner.Context.FrameCount); |
| | 432141 | 347 | | } |
| | | 348 | | |
| | | 349 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 312561 | 350 | | private void MarkEmpty(int frame) => _emptySinceFrame = frame; |
| | | 351 | | |
| | | 352 | | /// <summary> |
| | | 353 | | /// Sets the parent index for the current voxel in the world. |
| | | 354 | | /// </summary> |
| | | 355 | | /// <param name="parentIndex">The index to assign as the parent of the current voxel.</param> |
| | 405247 | 356 | | public void SetParentIndex(WorldVoxelIndex parentIndex) => WorldIndex = parentIndex; |
| | | 357 | | |
| | 810499 | 358 | | int IRetainedPhysicsPartition<GravitasCollisionService>.RetainedIndex => RetainedIndex; |
| | | 359 | | |
| | 2147483647 | 360 | | bool IRetainedPhysicsPartition<GravitasCollisionService>.IsEmpty => IsEmpty; |
| | | 361 | | |
| | 9180 | 362 | | int IRetainedPhysicsPartition<GravitasCollisionService>.EmptySinceFrame => EmptySinceFrame; |
| | | 363 | | |
| | 2147483647 | 364 | | bool IRetainedPhysicsPartition<GravitasCollisionService>.IsOwnedBy(GravitasCollisionService owner) => IsOwnedBy(owne |
| | | 365 | | |
| | 661810 | 366 | | void IRetainedPhysicsPartition<GravitasCollisionService>.SetRetainedIndex(int index) => SetRetainedIndex(index); |
| | | 367 | | |
| | 405252 | 368 | | void IRetainedPhysicsPartition<GravitasCollisionService>.ClearRetainedIndex() => ClearRetainedIndex(); |
| | | 369 | | } |