| | | 1 | | //======================================================================= |
| | | 2 | | // PhysicsPartition2D.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.Colliders; |
| | | 9 | | using GridForge.Grids; |
| | | 10 | | using GridForge.Spatial; |
| | | 11 | | using SwiftCollections; |
| | | 12 | | using System.Runtime.CompilerServices; |
| | | 13 | | |
| | | 14 | | namespace Gravitas; |
| | | 15 | | |
| | | 16 | | /// <summary> |
| | | 17 | | /// Stores the pure 2D collider IDs assigned to one GridForge voxel. |
| | | 18 | | /// </summary> |
| | | 19 | | public sealed class PhysicsPartition2D : IVoxelPartition, IRetainedPhysicsPartition<GravitasCollision2DService> |
| | | 20 | | { |
| | | 21 | | private GravitasCollision2DService? _owner; |
| | 46526 | 22 | | private int _emptySinceFrame = -1; |
| | 46526 | 23 | | private int _retainedIndex = -1; |
| | | 24 | | |
| | | 25 | | /// <summary>Creates an inactive 2D physics partition.</summary> |
| | 46526 | 26 | | public PhysicsPartition2D() |
| | | 27 | | { |
| | 46526 | 28 | | ActivationId = -1; |
| | 46526 | 29 | | } |
| | | 30 | | |
| | | 31 | | /// <summary>Gets or sets the world voxel occupied by this partition.</summary> |
| | | 32 | | public WorldVoxelIndex WorldIndex { get; set; } |
| | | 33 | | |
| | | 34 | | /// <summary>Gets or sets whether this partition is attached to a voxel.</summary> |
| | | 35 | | public bool IsPartitioned { get; set; } |
| | | 36 | | |
| | | 37 | | /// <summary>Stores context-local dynamic collider IDs.</summary> |
| | | 38 | | public SwiftSparseSet? ContainedDynamicObjects; |
| | | 39 | | |
| | | 40 | | /// <summary>Stores the awake subset of the dynamic collider IDs.</summary> |
| | | 41 | | public SwiftSparseSet? ContainedAwakeDynamicObjects; |
| | | 42 | | |
| | | 43 | | /// <summary>Stores context-local kinematic collider IDs.</summary> |
| | | 44 | | public SwiftSparseSet? ContainedKinematicObjects; |
| | | 45 | | |
| | | 46 | | /// <summary>Stores context-local static collider IDs.</summary> |
| | | 47 | | public SwiftSparseSet? ContainedStaticObjects; |
| | | 48 | | |
| | | 49 | | /// <summary>Gets the active-partition slot, or <c>-1</c> when inactive.</summary> |
| | | 50 | | public int ActivationId { get; private set; } |
| | | 51 | | |
| | | 52 | | /// <summary>Gets whether this partition has an active-partition slot.</summary> |
| | 13733 | 53 | | public bool IsAllocated => ActivationId != -1; |
| | | 54 | | |
| | | 55 | | internal bool IsEmpty => |
| | 11336173 | 56 | | (ContainedDynamicObjects?.Count ?? 0) == 0 |
| | 11336173 | 57 | | && (ContainedKinematicObjects?.Count ?? 0) == 0 |
| | 11336173 | 58 | | && (ContainedStaticObjects?.Count ?? 0) == 0; |
| | | 59 | | |
| | 19585 | 60 | | internal int EmptySinceFrame => _emptySinceFrame; |
| | | 61 | | |
| | 100921 | 62 | | internal int RetainedIndex => _retainedIndex; |
| | | 63 | | |
| | | 64 | | /// <summary>Gets the number of awake dynamic IDs in this partition.</summary> |
| | 6 | 65 | | public int AwakeDynamicObjectCount => ContainedAwakeDynamicObjects?.Count ?? 0; |
| | | 66 | | |
| | | 67 | | /// <summary>Gets the collision service that owns this partition.</summary> |
| | | 68 | | public GravitasCollision2DService Owner |
| | | 69 | | { |
| | | 70 | | get |
| | | 71 | | { |
| | 269770 | 72 | | SwiftThrowHelper.ThrowIfTrue( |
| | 269770 | 73 | | _owner == null, |
| | 269770 | 74 | | nameof(PhysicsPartition2D), |
| | 269770 | 75 | | "PhysicsPartition2D is missing its owner collision service."); |
| | 269768 | 76 | | return _owner!; |
| | | 77 | | } |
| | | 78 | | } |
| | | 79 | | |
| | | 80 | | /// <summary>Attaches this partition to a voxel.</summary> |
| | | 81 | | public void OnAddToVoxel(Voxel voxel) |
| | | 82 | | { |
| | 50458 | 83 | | SwiftThrowHelper.ThrowIfTrue( |
| | 50458 | 84 | | _owner == null, |
| | 50458 | 85 | | nameof(PhysicsPartition2D), |
| | 50458 | 86 | | "PhysicsPartition2D is missing its owner collision service."); |
| | 50458 | 87 | | WorldIndex = voxel.WorldIndex; |
| | 50458 | 88 | | IsPartitioned = true; |
| | 50458 | 89 | | } |
| | | 90 | | |
| | | 91 | | internal void Distribute( |
| | | 92 | | SwiftList<int> dynamicIds, |
| | | 93 | | SwiftList<int> staticIds) |
| | | 94 | | { |
| | 15550 | 95 | | int dynamicCount = ContainedDynamicObjects?.Count ?? 0; |
| | 15550 | 96 | | int awakeDynamicCount = ContainedAwakeDynamicObjects?.Count ?? 0; |
| | 15550 | 97 | | if (ContainedDynamicObjects == null || dynamicCount == 0 || ContainedAwakeDynamicObjects == null || awakeDynamic |
| | 408 | 98 | | return; |
| | | 99 | | |
| | 15142 | 100 | | ContainedDynamicObjects.CopySortedKeysTo(dynamicIds); |
| | 15142 | 101 | | CopySortedStaticStyleIds(staticIds); |
| | | 102 | | |
| | 64152 | 103 | | for (int j = 0; j < dynamicIds.Count; j++) |
| | | 104 | | { |
| | 16934 | 105 | | int id1 = dynamicIds[j]; |
| | 38640 | 106 | | for (int k = j + 1; k < dynamicIds.Count; k++) |
| | 2386 | 107 | | Owner.Context.Physics2D.ProcessPartitionCandidate(id1, dynamicIds[k], WorldIndex); |
| | | 108 | | |
| | 44544 | 109 | | for (int k = 0; k < staticIds.Count; k++) |
| | 5338 | 110 | | Owner.Context.Physics2D.ProcessPartitionCandidate(id1, staticIds[k], WorldIndex); |
| | | 111 | | } |
| | 15142 | 112 | | } |
| | | 113 | | |
| | | 114 | | internal void CopyAllColliderIds(SwiftList<int> destination) |
| | | 115 | | { |
| | 120424 | 116 | | destination.FastClear(); |
| | 120424 | 117 | | CopyIds(ContainedDynamicObjects, destination); |
| | 120424 | 118 | | CopyIds(ContainedKinematicObjects, destination); |
| | 120424 | 119 | | CopyIds(ContainedStaticObjects, destination); |
| | 120424 | 120 | | destination.SortInPlace(); |
| | 120424 | 121 | | } |
| | | 122 | | |
| | | 123 | | internal void CopyStaticStyleColliderIds(SwiftList<int> destination) |
| | | 124 | | { |
| | 49904 | 125 | | destination.FastClear(); |
| | 49904 | 126 | | CopyIds(ContainedKinematicObjects, destination); |
| | 49904 | 127 | | CopyIds(ContainedStaticObjects, destination); |
| | 49904 | 128 | | destination.SortInPlace(); |
| | 49904 | 129 | | } |
| | | 130 | | |
| | | 131 | | private void CopySortedStaticStyleIds(SwiftList<int> destination) |
| | | 132 | | { |
| | 15142 | 133 | | destination.FastClear(); |
| | 15142 | 134 | | CopyIds(ContainedKinematicObjects, destination); |
| | 15142 | 135 | | CopyIds(ContainedStaticObjects, destination); |
| | 15142 | 136 | | destination.SortInPlace(); |
| | 15142 | 137 | | } |
| | | 138 | | |
| | | 139 | | private static void CopyIds(SwiftSparseSet? source, SwiftList<int> destination) |
| | | 140 | | { |
| | 491364 | 141 | | if (source == null) |
| | 360707 | 142 | | return; |
| | | 143 | | |
| | 971050 | 144 | | for (int i = 0; i < source.Count; i++) |
| | 354868 | 145 | | destination.Add(source.DenseKeys[i]); |
| | 130657 | 146 | | } |
| | | 147 | | |
| | | 148 | | /// <summary>Adds a dynamic collider ID to this partition.</summary> |
| | | 149 | | public void AddDynamicObject(int item) |
| | | 150 | | { |
| | 40804 | 151 | | ContainedDynamicObjects ??= new(); |
| | 40804 | 152 | | if (!ContainedDynamicObjects.Add(item)) |
| | 2 | 153 | | return; |
| | | 154 | | |
| | 40802 | 155 | | MarkOccupied(); |
| | 40802 | 156 | | SetDynamicObjectAwake(item, IsDynamicObjectAwake(item)); |
| | | 157 | | |
| | 40802 | 158 | | if (ContainedDynamicObjects.Count == 1) |
| | 36445 | 159 | | ActivationId = Owner.ActivatePartition(this); |
| | 40802 | 160 | | } |
| | | 161 | | |
| | | 162 | | /// <summary>Adds a static collider ID to this partition.</summary> |
| | | 163 | | public void AddStaticObject(int item) |
| | | 164 | | { |
| | 22188 | 165 | | ContainedStaticObjects ??= new(); |
| | 22188 | 166 | | if (ContainedStaticObjects.Add(item)) |
| | 22186 | 167 | | MarkOccupied(); |
| | 22188 | 168 | | } |
| | | 169 | | |
| | | 170 | | /// <summary>Adds a kinematic collider ID to this partition.</summary> |
| | | 171 | | public void AddKinematicObject(int item) |
| | | 172 | | { |
| | 3811 | 173 | | ContainedKinematicObjects ??= new(); |
| | 3811 | 174 | | if (ContainedKinematicObjects.Add(item)) |
| | 3809 | 175 | | MarkOccupied(); |
| | 3811 | 176 | | } |
| | | 177 | | |
| | | 178 | | /// <summary>Removes a dynamic collider ID from this partition.</summary> |
| | | 179 | | public void RemoveDynamicObject(int item) |
| | | 180 | | { |
| | 13458 | 181 | | if (ContainedDynamicObjects?.Remove(item) != true) |
| | | 182 | | { |
| | 4 | 183 | | GravitasLogger.DebugChannel.Info($"2D dynamic item not removed - {item}"); |
| | 4 | 184 | | return; |
| | | 185 | | } |
| | | 186 | | |
| | 13454 | 187 | | ContainedAwakeDynamicObjects?.Remove(item); |
| | | 188 | | |
| | 13454 | 189 | | if (ContainedDynamicObjects.Count > 0) |
| | 3288 | 190 | | return; |
| | | 191 | | |
| | 10166 | 192 | | Owner.DeactivatePartition(ActivationId); |
| | 10166 | 193 | | ActivationId = -1; |
| | 10166 | 194 | | MarkEmptyIfUnoccupied(); |
| | 10166 | 195 | | } |
| | | 196 | | |
| | | 197 | | /// <summary>Removes a static collider ID from this partition.</summary> |
| | | 198 | | public void RemoveStaticObject(int item) |
| | | 199 | | { |
| | 1799 | 200 | | if (ContainedStaticObjects?.Remove(item) != true) |
| | | 201 | | { |
| | 5 | 202 | | GravitasLogger.DebugChannel.Info($"2D static item not removed - {item}"); |
| | 5 | 203 | | return; |
| | | 204 | | } |
| | | 205 | | |
| | 1794 | 206 | | MarkEmptyIfUnoccupied(); |
| | 1794 | 207 | | } |
| | | 208 | | |
| | | 209 | | /// <summary>Removes a kinematic collider ID from this partition.</summary> |
| | | 210 | | public void RemoveKinematicObject(int item) |
| | | 211 | | { |
| | 2095 | 212 | | if (ContainedKinematicObjects?.Remove(item) != true) |
| | | 213 | | { |
| | 5 | 214 | | GravitasLogger.DebugChannel.Info($"2D kinematic item not removed - {item}"); |
| | 5 | 215 | | return; |
| | | 216 | | } |
| | | 217 | | |
| | 2090 | 218 | | MarkEmptyIfUnoccupied(); |
| | 2090 | 219 | | } |
| | | 220 | | |
| | | 221 | | /// <summary>Returns whether a dynamic collider ID is marked awake.</summary> |
| | 10 | 222 | | public bool ContainsAwakeDynamicObject(int item) => ContainedAwakeDynamicObjects?.Contains(item) == true; |
| | | 223 | | |
| | | 224 | | /// <summary>Updates the awake state for a dynamic collider in this partition.</summary> |
| | | 225 | | public void SetDynamicObjectAwake(int item, bool awake) |
| | | 226 | | { |
| | 43059 | 227 | | if (ContainedDynamicObjects?.Contains(item) != true) |
| | 2 | 228 | | return; |
| | | 229 | | |
| | 43057 | 230 | | if (awake) |
| | | 231 | | { |
| | 41433 | 232 | | ContainedAwakeDynamicObjects ??= new(); |
| | 41433 | 233 | | ContainedAwakeDynamicObjects.Add(item); |
| | 41433 | 234 | | return; |
| | | 235 | | } |
| | | 236 | | |
| | 1624 | 237 | | ContainedAwakeDynamicObjects?.Remove(item); |
| | 1429 | 238 | | } |
| | | 239 | | |
| | | 240 | | private bool IsDynamicObjectAwake(int item) |
| | | 241 | | { |
| | 40802 | 242 | | if (!Owner.Context.Physics2D.TryGetColliderById(item, out LSCollider2D? collider)) |
| | 89 | 243 | | return true; |
| | | 244 | | |
| | 40713 | 245 | | SolidBody2D? body = collider!.Body; |
| | 40713 | 246 | | return body == null || body.IsAwakeForCollision; |
| | | 247 | | } |
| | | 248 | | |
| | | 249 | | /// <summary>Releases this partition when it is removed from a voxel.</summary> |
| | | 250 | | public void OnRemoveFromVoxel(Voxel voxel) |
| | | 251 | | { |
| | 50458 | 252 | | Owner.ReleasePartition(this); |
| | 50458 | 253 | | } |
| | | 254 | | |
| | | 255 | | internal void SetOwner(GravitasCollision2DService owner) |
| | | 256 | | { |
| | 50473 | 257 | | SwiftThrowHelper.ThrowIfNull(owner, nameof(owner)); |
| | 50473 | 258 | | SwiftThrowHelper.ThrowIfArgument( |
| | 50473 | 259 | | _owner != null && !ReferenceEquals(_owner, owner), |
| | 50473 | 260 | | nameof(owner), |
| | 50473 | 261 | | "PhysicsPartition2D is already owned by a different collision service."); |
| | | 262 | | |
| | 50472 | 263 | | _owner = owner; |
| | 50472 | 264 | | } |
| | | 265 | | |
| | 11151233 | 266 | | internal bool IsOwnedBy(GravitasCollision2DService owner) => ReferenceEquals(_owner, owner); |
| | | 267 | | |
| | | 268 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 269 | | internal void SetRetainedIndex(int index) |
| | | 270 | | { |
| | 77820 | 271 | | SwiftThrowHelper.ThrowIfNegative(index, nameof(index)); |
| | 77820 | 272 | | _retainedIndex = index; |
| | 77820 | 273 | | } |
| | | 274 | | |
| | | 275 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 50463 | 276 | | internal void ClearRetainedIndex() => _retainedIndex = -1; |
| | | 277 | | |
| | | 278 | | internal void ResetRetainedMembership() |
| | | 279 | | { |
| | 3 | 280 | | ContainedDynamicObjects?.Clear(); |
| | 3 | 281 | | ContainedAwakeDynamicObjects?.Clear(); |
| | 3 | 282 | | ContainedKinematicObjects?.Clear(); |
| | 3 | 283 | | ContainedStaticObjects?.Clear(); |
| | 3 | 284 | | ActivationId = -1; |
| | 3 | 285 | | MarkEmpty(_owner?.Context.FrameCount ?? 0); |
| | 3 | 286 | | } |
| | | 287 | | |
| | | 288 | | internal void ResetForPool() |
| | | 289 | | { |
| | 50465 | 290 | | ContainedDynamicObjects?.Clear(); |
| | 50465 | 291 | | ContainedAwakeDynamicObjects?.Clear(); |
| | 50465 | 292 | | ContainedKinematicObjects?.Clear(); |
| | 50465 | 293 | | ContainedStaticObjects?.Clear(); |
| | | 294 | | |
| | 50465 | 295 | | if (ActivationId != -1) |
| | 26277 | 296 | | Owner.DeactivatePartition(ActivationId); |
| | | 297 | | |
| | 50465 | 298 | | _owner = null; |
| | 50465 | 299 | | ActivationId = -1; |
| | 50465 | 300 | | IsPartitioned = false; |
| | 50465 | 301 | | _emptySinceFrame = -1; |
| | 50465 | 302 | | _retainedIndex = -1; |
| | 50465 | 303 | | } |
| | | 304 | | |
| | | 305 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 66797 | 306 | | private void MarkOccupied() => _emptySinceFrame = -1; |
| | | 307 | | |
| | | 308 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 309 | | private void MarkEmptyIfUnoccupied() |
| | | 310 | | { |
| | 14050 | 311 | | if (IsEmpty) |
| | 10987 | 312 | | MarkEmpty(Owner.Context.FrameCount); |
| | 14050 | 313 | | } |
| | | 314 | | |
| | | 315 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 10990 | 316 | | private void MarkEmpty(int frame) => _emptySinceFrame = frame; |
| | | 317 | | |
| | | 318 | | /// <summary>Updates the world voxel index used by this partition.</summary> |
| | 50458 | 319 | | public void SetParentIndex(WorldVoxelIndex parentIndex) => WorldIndex = parentIndex; |
| | | 320 | | |
| | 100921 | 321 | | int IRetainedPhysicsPartition<GravitasCollision2DService>.RetainedIndex => RetainedIndex; |
| | | 322 | | |
| | 11150941 | 323 | | bool IRetainedPhysicsPartition<GravitasCollision2DService>.IsEmpty => IsEmpty; |
| | | 324 | | |
| | 19578 | 325 | | int IRetainedPhysicsPartition<GravitasCollision2DService>.EmptySinceFrame => EmptySinceFrame; |
| | | 326 | | |
| | 11151233 | 327 | | bool IRetainedPhysicsPartition<GravitasCollision2DService>.IsOwnedBy(GravitasCollision2DService owner) => IsOwnedBy( |
| | | 328 | | |
| | 77820 | 329 | | void IRetainedPhysicsPartition<GravitasCollision2DService>.SetRetainedIndex(int index) => SetRetainedIndex(index); |
| | | 330 | | |
| | 50463 | 331 | | void IRetainedPhysicsPartition<GravitasCollision2DService>.ClearRetainedIndex() => ClearRetainedIndex(); |
| | | 332 | | } |