| | | 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 System; |
| | | 9 | | using System.Collections.Generic; |
| | | 10 | | using System.Runtime.CompilerServices; |
| | | 11 | | using FixedMathSharp; |
| | | 12 | | using Gravitas.Colliders; |
| | | 13 | | using Gravitas.CollisionHandling; |
| | | 14 | | using GridForge.Grids; |
| | | 15 | | using GridForge.Spatial; |
| | | 16 | | using GridForge.Utility; |
| | | 17 | | using SwiftCollections; |
| | | 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; |
| | 10191 | 30 | | private readonly SwiftSlotMap<PhysicsPartition> _activePartitions = new(); |
| | 10191 | 31 | | private readonly SwiftStack<PhysicsPartition> _inactivePartitionPool = new(DefaultPartitionPoolCapacity); |
| | 10191 | 32 | | private readonly SwiftList<Voxel> _coveredVoxels = new(); |
| | 10191 | 33 | | private readonly SwiftHashSet<WorldVoxelIndex> _nextCoveredCoordinates = new(); |
| | 10191 | 34 | | private readonly GridTraceScratch _traceScratch = new(); |
| | 10191 | 35 | | private readonly SwiftList<PhysicsPartition> _retainedPartitions = new(); |
| | 10191 | 36 | | private readonly SwiftList<PhysicsPartition> _emptyRetainedPartitions = new(); |
| | 10191 | 37 | | private readonly SwiftList<PhysicsPartition> _distributionPartitions = new(); |
| | 10191 | 38 | | private readonly SwiftList<int> _distributionDynamicIds = new(); |
| | 10191 | 39 | | private readonly SwiftList<int> _distributionStaticIds = new(); |
| | 10191 | 40 | | private readonly SwiftList<int> _planarQueryPartitionColliderIds = new(); |
| | 10191 | 41 | | private readonly SwiftHashSet<int> _planarQueryUniqueColliderIds = new(); |
| | 10191 | 42 | | private readonly DynamicCcdCandidateIndex2D _planarQueryCandidates = |
| | 10191 | 43 | | new(capacity: 0, supportsUpdates: true); |
| | | 44 | | private readonly Action<PhysicsPartition> _releaseRetainedPartition; |
| | 10191 | 45 | | private readonly object _cullDistributorLock = new(); |
| | | 46 | | |
| | | 47 | | private int _cullDistributor; |
| | | 48 | | private int _retainedPartitionRetirementCursor; |
| | | 49 | | private uint _planarQueryWorldVersion; |
| | | 50 | | |
| | | 51 | | /// <summary> |
| | | 52 | | /// Initializes a new collision service for the supplied context. |
| | | 53 | | /// </summary> |
| | | 54 | | /// <param name="context">The owning world context.</param> |
| | 10191 | 55 | | public GravitasCollisionService(GravitasWorldContext context) |
| | | 56 | | { |
| | 10191 | 57 | | ChronicleGuard.ThrowIfNull(context, nameof(context)); |
| | 10191 | 58 | | _context = context; |
| | 10191 | 59 | | _releaseRetainedPartition = ReleasePartition; |
| | 10191 | 60 | | } |
| | | 61 | | |
| | | 62 | | /// <summary> |
| | | 63 | | /// Gets the owning world context. |
| | | 64 | | /// </summary> |
| | 3216199 | 65 | | public GravitasWorldContext Context => _context; |
| | | 66 | | |
| | | 67 | | /// <summary> |
| | | 68 | | /// Gets the collision distribution version for this context. |
| | | 69 | | /// </summary> |
| | | 70 | | public uint Version { get; private set; } = 1; |
| | | 71 | | |
| | | 72 | | /// <summary> |
| | | 73 | | /// Gets the number of active partitions in this context. |
| | | 74 | | /// </summary> |
| | 24 | 75 | | public int ActivePartitionCount => _activePartitions.Count; |
| | | 76 | | |
| | | 77 | | /// <summary> |
| | | 78 | | /// Gets the number of inactive partitions currently available for reuse. |
| | | 79 | | /// </summary> |
| | 18 | 80 | | public int InactivePartitionCount => _inactivePartitionPool.Count; |
| | | 81 | | |
| | 24 | 82 | | internal int RetainedPartitionCount => _retainedPartitions.Count; |
| | | 83 | | |
| | | 84 | | internal int CullDistributor |
| | | 85 | | { |
| | | 86 | | get |
| | | 87 | | { |
| | 1866 | 88 | | lock (_cullDistributorLock) |
| | | 89 | | { |
| | 1866 | 90 | | if (_cullDistributor > 1) |
| | 608 | 91 | | _cullDistributor = -1; |
| | | 92 | | |
| | 1866 | 93 | | return _cullDistributor++; |
| | | 94 | | } |
| | 1866 | 95 | | } |
| | | 96 | | } |
| | | 97 | | |
| | | 98 | | /// <summary> |
| | | 99 | | /// Resets transient collision state owned by this context. |
| | | 100 | | /// </summary> |
| | | 101 | | public void Reset() |
| | | 102 | | { |
| | 113 | 103 | | DetachRetainedPartitions(); |
| | 113 | 104 | | _activePartitions.Clear(); |
| | 113 | 105 | | _coveredVoxels.FastClear(); |
| | 113 | 106 | | _nextCoveredCoordinates.Clear(); |
| | 113 | 107 | | _traceScratch.Clear(); |
| | 113 | 108 | | _distributionPartitions.FastClear(); |
| | 113 | 109 | | _distributionDynamicIds.FastClear(); |
| | 113 | 110 | | _distributionStaticIds.FastClear(); |
| | 113 | 111 | | _planarQueryPartitionColliderIds.FastClear(); |
| | 113 | 112 | | _planarQueryUniqueColliderIds.Clear(); |
| | 113 | 113 | | _planarQueryCandidates.Clear(); |
| | 113 | 114 | | _inactivePartitionPool.Clear(); |
| | 113 | 115 | | Version = 1; |
| | 113 | 116 | | _cullDistributor = 0; |
| | 113 | 117 | | _retainedPartitionRetirementCursor = 0; |
| | 113 | 118 | | _planarQueryWorldVersion = 0; |
| | 113 | 119 | | } |
| | | 120 | | |
| | 137 | 121 | | private void DetachRetainedPartitions() => RetainedPartitionLifecycle.DetachAll( |
| | 137 | 122 | | _retainedPartitions, |
| | 137 | 123 | | _emptyRetainedPartitions, |
| | 137 | 124 | | _context.World, |
| | 137 | 125 | | this, |
| | 137 | 126 | | _releaseRetainedPartition, |
| | 137 | 127 | | nameof(PhysicsPartition), |
| | 137 | 128 | | "Unable to detach retained physics partition from its voxel during reset."); |
| | | 129 | | |
| | 1159174 | 130 | | private void TrackRetainedPartition(PhysicsPartition partition) => RetainedPartitionLifecycle.Track( |
| | 1159174 | 131 | | _retainedPartitions, |
| | 1159174 | 132 | | _emptyRetainedPartitions, |
| | 1159174 | 133 | | this, |
| | 1159174 | 134 | | partition, |
| | 1159174 | 135 | | nameof(PhysicsPartition)); |
| | | 136 | | |
| | 1159179 | 137 | | private void UntrackRetainedPartition(PhysicsPartition partition) => RetainedPartitionLifecycle.Untrack( |
| | 1159179 | 138 | | _retainedPartitions, |
| | 1159179 | 139 | | _emptyRetainedPartitions, |
| | 1159179 | 140 | | this, |
| | 1159179 | 141 | | partition, |
| | 1159179 | 142 | | ref _retainedPartitionRetirementCursor); |
| | | 143 | | |
| | | 144 | | internal bool IsPartitionRefreshRequired(LSCollider collider) => |
| | 306503 | 145 | | !collider.MatchesPartitionGridBounds(collider.BoundsMin, collider.BoundsMax, ResolvePartitionKind(collider)); |
| | | 146 | | |
| | | 147 | | internal void RebuildPartitionsAfterWorldChange() |
| | | 148 | | { |
| | 24 | 149 | | DetachRetainedPartitions(); |
| | 24 | 150 | | _planarQueryCandidates.Clear(); |
| | 80 | 151 | | for (int i = 0; i < _context.Physics.ColliderCount; i++) |
| | | 152 | | { |
| | 16 | 153 | | LSCollider collider = _context.Physics.GetColliderByServiceIndex(i); |
| | 16 | 154 | | collider.MarkUnpartitioned(); |
| | 16 | 155 | | collider.ClearPartitionCoordinates(); |
| | 16 | 156 | | collider.RepartitionCommittedShape(); |
| | | 157 | | } |
| | 24 | 158 | | _planarQueryWorldVersion = _context.World.Version; |
| | 24 | 159 | | } |
| | | 160 | | |
| | 449039 | 161 | | internal int ResolvePartitionKind(LSCollider collider) => (int)GetMobilityKind(collider); |
| | | 162 | | |
| | | 163 | | internal bool PartitionObject( |
| | | 164 | | LSCollider collider, |
| | | 165 | | ref SwiftList<WorldVoxelIndex> partitionedCoordinates) |
| | | 166 | | { |
| | 143353 | 167 | | ChronicleGuard.ThrowIfNull(collider, nameof(collider)); |
| | 143353 | 168 | | ChronicleGuard.ThrowIfArgument( |
| | 143353 | 169 | | !ReferenceEquals(collider.Context, _context), |
| | 143353 | 170 | | nameof(collider), |
| | 143353 | 171 | | "Collider must belong to this collision service context."); |
| | | 172 | | |
| | 143353 | 173 | | PartitionCoveredVoxels(collider, partitionedCoordinates, GetMobilityKind(collider)); |
| | 143353 | 174 | | if (partitionedCoordinates.Count == 0) |
| | | 175 | | { |
| | 3098 | 176 | | collider.MarkUnpartitioned(); |
| | 3098 | 177 | | _planarQueryCandidates.Remove(collider.Id); |
| | 3098 | 178 | | return false; |
| | | 179 | | } |
| | | 180 | | |
| | 140255 | 181 | | _planarQueryCandidates.AddOrUpdate( |
| | 140255 | 182 | | collider.Id, |
| | 140255 | 183 | | new DynamicCcdPlanarBounds( |
| | 140255 | 184 | | collider.BoundsMin.X, |
| | 140255 | 185 | | collider.BoundsMin.Z, |
| | 140255 | 186 | | collider.BoundsMax.X, |
| | 140255 | 187 | | collider.BoundsMax.Z)); |
| | 140255 | 188 | | return true; |
| | | 189 | | } |
| | | 190 | | |
| | | 191 | | private void PartitionCoveredVoxels( |
| | | 192 | | LSCollider collider, |
| | | 193 | | SwiftList<WorldVoxelIndex> partitionedCoordinates, |
| | | 194 | | PhysicsMobilityType kind) |
| | | 195 | | { |
| | 143353 | 196 | | GridWorld world = _context.World; |
| | 143353 | 197 | | GridTracer.GetCoveredVoxelsInto( |
| | 143353 | 198 | | world, |
| | 143353 | 199 | | collider.BoundsMin, |
| | 143353 | 200 | | collider.BoundsMax, |
| | 143353 | 201 | | _coveredVoxels, |
| | 143353 | 202 | | _traceScratch, |
| | 143353 | 203 | | Fixed64.Half); |
| | | 204 | | |
| | 143353 | 205 | | var traversal = new GridTraversalState(world, GridTraversalPaddingMode.MaxCellEdge); |
| | 143353 | 206 | | _nextCoveredCoordinates.Clear(); |
| | 143353 | 207 | | int coveredCount = 0; |
| | 16695778 | 208 | | for (int i = 0; i < _coveredVoxels.Count; i++) |
| | | 209 | | { |
| | 8204536 | 210 | | Voxel voxel = _coveredVoxels[i]; |
| | 8204536 | 211 | | if (collider.IsPositionInBounds(traversal.GetCellEdge(voxel), voxel.WorldPosition)) |
| | | 212 | | { |
| | 5516466 | 213 | | _nextCoveredCoordinates.Add(voxel.WorldIndex); |
| | 5516466 | 214 | | _coveredVoxels[coveredCount++] = voxel; |
| | | 215 | | } |
| | | 216 | | } |
| | | 217 | | |
| | | 218 | | // Preserve surviving memberships without changing tracer order or raw |
| | | 219 | | // bounds. Idempotent additions below also repair externally removed ones. |
| | 143353 | 220 | | PhysicsMobilityType previousKind = GetStoredMobilityKind(collider.PartitionKind); |
| | 143353 | 221 | | RemovePartitionMemberships(collider, partitionedCoordinates, previousKind, previousKind == kind); |
| | 143353 | 222 | | partitionedCoordinates.FastClear(); |
| | 11319638 | 223 | | for (int i = 0; i < coveredCount; i++) |
| | 5516466 | 224 | | PartitionVoxel(collider, partitionedCoordinates, _coveredVoxels[i], kind); |
| | 143353 | 225 | | } |
| | | 226 | | |
| | | 227 | | private void PartitionVoxel( |
| | | 228 | | LSCollider collider, |
| | | 229 | | SwiftList<WorldVoxelIndex> partitionedCoordinates, |
| | | 230 | | Voxel voxel, |
| | | 231 | | PhysicsMobilityType kind) |
| | | 232 | | { |
| | 5516466 | 233 | | if (!voxel.TryGetPartition(out PhysicsPartition? partition)) |
| | | 234 | | { |
| | 1159173 | 235 | | partition = RentPartition(); |
| | 1159173 | 236 | | ChronicleGuard.ThrowIfTrue( |
| | 1159173 | 237 | | !voxel.TryAddPartition(partition), |
| | 1159173 | 238 | | nameof(GravitasCollisionService), |
| | 1159173 | 239 | | "Unable to attach 3D physics partition to voxel."); |
| | | 240 | | |
| | 1159173 | 241 | | TrackRetainedPartition(partition); |
| | | 242 | | } |
| | | 243 | | |
| | 5516466 | 244 | | partitionedCoordinates.Add(voxel.WorldIndex); |
| | 5516466 | 245 | | AddObject(partition!, collider.Id, kind); |
| | 5516466 | 246 | | if (kind == PhysicsMobilityType.Dynamic) |
| | 4345403 | 247 | | partition!.SetDynamicObjectAwake(collider.Id, collider.Body!.IsAwakeForCollision); |
| | 5516466 | 248 | | } |
| | | 249 | | |
| | | 250 | | internal bool ClearPartitionedObject(LSCollider collider, bool force = false) |
| | | 251 | | { |
| | 474 | 252 | | ChronicleGuard.ThrowIfNull(collider, nameof(collider)); |
| | 474 | 253 | | ChronicleGuard.ThrowIfArgument( |
| | 474 | 254 | | !ReferenceEquals(collider.Context, _context), |
| | 474 | 255 | | nameof(collider), |
| | 474 | 256 | | "Collider must belong to this collision service context."); |
| | | 257 | | |
| | 474 | 258 | | if (!collider.IsPartitioned) |
| | 1 | 259 | | return false; |
| | | 260 | | |
| | 473 | 261 | | PhysicsMobilityType currentKind = GetMobilityKind(collider); |
| | 473 | 262 | | if (!force && collider.MatchesPartitionGridBounds(collider.BoundsMin, collider.BoundsMax, (int)currentKind)) |
| | 1 | 263 | | return false; |
| | | 264 | | |
| | 472 | 265 | | PhysicsMobilityType partitionKind = GetStoredMobilityKind(collider.PartitionKind); |
| | 472 | 266 | | _planarQueryCandidates.Remove(collider.Id); |
| | 472 | 267 | | RemovePartitionMemberships(collider, collider.PartitionCoordinates!, partitionKind, preserveCovered: false); |
| | | 268 | | |
| | 472 | 269 | | collider.MarkUnpartitioned(); |
| | 472 | 270 | | collider.ClearPartitionCoordinates(); |
| | | 271 | | |
| | 472 | 272 | | return true; |
| | | 273 | | } |
| | | 274 | | |
| | | 275 | | private void RemovePartitionMemberships( |
| | | 276 | | LSCollider collider, |
| | | 277 | | SwiftList<WorldVoxelIndex> coordinates, |
| | | 278 | | PhysicsMobilityType partitionKind, |
| | | 279 | | bool preserveCovered) |
| | | 280 | | { |
| | 143825 | 281 | | GridWorld world = _context.World; |
| | 8875766 | 282 | | for (int i = 0; i < coordinates.Count; i++) |
| | | 283 | | { |
| | 4294058 | 284 | | WorldVoxelIndex coordinate = coordinates[i]; |
| | 4294058 | 285 | | if (preserveCovered && _nextCoveredCoordinates.Contains(coordinate)) |
| | | 286 | | continue; |
| | | 287 | | |
| | 372532 | 288 | | if (!world.ActiveGrids.IsAllocated(coordinate.GridIndex) |
| | 372532 | 289 | | || !world.TryGetVoxel(coordinate, out Voxel? voxel) |
| | 372532 | 290 | | || !voxel!.TryGetPartition(out PhysicsPartition? partition)) |
| | | 291 | | { |
| | | 292 | | continue; |
| | | 293 | | } |
| | | 294 | | |
| | 372450 | 295 | | RemoveObject(partition!, collider.Id, partitionKind); |
| | | 296 | | |
| | | 297 | | // Keep the voxel partition attached after it becomes empty. Re-adding |
| | | 298 | | // the same partition type through GridForge carries metadata overhead, |
| | | 299 | | // while an empty PhysicsPartition is inactive and query-invisible. |
| | | 300 | | } |
| | 143825 | 301 | | } |
| | | 302 | | |
| | | 303 | | internal void QueryPlanarColliderCandidates( |
| | | 304 | | DynamicCcdPlanarBounds bounds, |
| | | 305 | | SwiftList<int> results) |
| | | 306 | | { |
| | 52715 | 307 | | _context.RefreshQueryPartitions(); |
| | 52715 | 308 | | if (_planarQueryWorldVersion != _context.World.Version) |
| | 463 | 309 | | RebuildPlanarQueryCandidates(); |
| | | 310 | | |
| | 52715 | 311 | | _planarQueryCandidates.Query(bounds, results); |
| | 52715 | 312 | | } |
| | | 313 | | |
| | | 314 | | private void RebuildPlanarQueryCandidates() |
| | | 315 | | { |
| | 463 | 316 | | _planarQueryCandidates.Clear(); |
| | 463 | 317 | | _planarQueryUniqueColliderIds.Clear(); |
| | 359146 | 318 | | for (int partitionIndex = 0; partitionIndex < _retainedPartitions.Count; partitionIndex++) |
| | | 319 | | { |
| | 179110 | 320 | | _retainedPartitions[partitionIndex].CopyAllColliderIds(_planarQueryPartitionColliderIds); |
| | 821288 | 321 | | for (int colliderIndex = 0; colliderIndex < _planarQueryPartitionColliderIds.Count; colliderIndex++) |
| | | 322 | | { |
| | 231534 | 323 | | int colliderId = _planarQueryPartitionColliderIds[colliderIndex]; |
| | 231534 | 324 | | if (!_planarQueryUniqueColliderIds.Add(colliderId) |
| | 231534 | 325 | | || !_context.Physics.TryGetColliderById(colliderId, out LSCollider? collider)) |
| | | 326 | | { |
| | | 327 | | continue; |
| | | 328 | | } |
| | | 329 | | |
| | 1345 | 330 | | _planarQueryCandidates.AddOrUpdate( |
| | 1345 | 331 | | colliderId, |
| | 1345 | 332 | | new DynamicCcdPlanarBounds( |
| | 1345 | 333 | | collider!.BoundsMin.X, |
| | 1345 | 334 | | collider.BoundsMin.Z, |
| | 1345 | 335 | | collider.BoundsMax.X, |
| | 1345 | 336 | | collider.BoundsMax.Z)); |
| | | 337 | | } |
| | | 338 | | } |
| | | 339 | | |
| | 463 | 340 | | _planarQueryWorldVersion = _context.World.Version; |
| | 463 | 341 | | } |
| | | 342 | | |
| | | 343 | | internal void RefreshPartitionAwakeState(LSCollider collider) |
| | | 344 | | { |
| | 1776 | 345 | | ChronicleGuard.ThrowIfNull(collider, nameof(collider)); |
| | 1776 | 346 | | ChronicleGuard.ThrowIfArgument( |
| | 1776 | 347 | | !ReferenceEquals(collider.Context, _context), |
| | 1776 | 348 | | nameof(collider), |
| | 1776 | 349 | | "Collider must belong to this collision service context."); |
| | | 350 | | |
| | 1776 | 351 | | SwiftList<WorldVoxelIndex> coordinates = collider.PartitionCoordinates!; |
| | 1776 | 352 | | SolidBody? body = collider.Body; |
| | 1776 | 353 | | if (collider.IsStatic || body!.IsKinematic) |
| | 29 | 354 | | return; |
| | | 355 | | |
| | 1747 | 356 | | bool awake = body.IsAwakeForCollision; |
| | 1747 | 357 | | GridWorld world = _context.World; |
| | | 358 | | |
| | 410386 | 359 | | for (int i = 0; i < coordinates.Count; i++) |
| | | 360 | | { |
| | 203446 | 361 | | WorldVoxelIndex coordinate = coordinates[i]; |
| | 203446 | 362 | | if (!world.ActiveGrids.IsAllocated(coordinate.GridIndex) |
| | 203446 | 363 | | || !world.TryGetVoxel(coordinate, out Voxel? voxel) |
| | 203446 | 364 | | || !voxel!.TryGetPartition(out PhysicsPartition? partition)) |
| | | 365 | | { |
| | | 366 | | continue; |
| | | 367 | | } |
| | | 368 | | |
| | 203391 | 369 | | partition!.SetDynamicObjectAwake(collider.Id, awake); |
| | | 370 | | } |
| | 1747 | 371 | | } |
| | | 372 | | |
| | | 373 | | internal void CheckAndDistributeCollisions() |
| | | 374 | | { |
| | 63063 | 375 | | Version++; |
| | | 376 | | |
| | 63063 | 377 | | _distributionPartitions.FastClear(); |
| | 4149180 | 378 | | foreach (PhysicsPartition partition in _activePartitions) |
| | 2011527 | 379 | | _distributionPartitions.Add(partition); |
| | | 380 | | |
| | 63063 | 381 | | _distributionPartitions.SortInPlace(PartitionOrderComparer); |
| | | 382 | | |
| | 4149180 | 383 | | for (int i = 0; i < _distributionPartitions.Count; i++) |
| | | 384 | | { |
| | 2011527 | 385 | | _distributionPartitions[i].Distribute( |
| | 2011527 | 386 | | _distributionDynamicIds, |
| | 2011527 | 387 | | _distributionStaticIds); |
| | | 388 | | } |
| | 63063 | 389 | | } |
| | | 390 | | |
| | 1811824 | 391 | | internal void RefreshRetainedPartitionEligibility(PhysicsPartition partition) => RetainedPartitionLifecycle.RefreshE |
| | 1811824 | 392 | | _emptyRetainedPartitions, this, partition); |
| | | 393 | | |
| | 63057 | 394 | | internal void RetireExpiredRetainedPartitions() => RetainedPartitionLifecycle.RetireExpired( |
| | 63057 | 395 | | _retainedPartitions, |
| | 63057 | 396 | | _context.World, |
| | 63057 | 397 | | this, |
| | 63057 | 398 | | _context.Settings.RetainedPartitionRetirementSweepBudget, |
| | 63057 | 399 | | _context.FrameCount, |
| | 63057 | 400 | | _context.Settings.RetainedPartitionTimeToKillFrames, |
| | 63057 | 401 | | _releaseRetainedPartition, |
| | 63057 | 402 | | ref _retainedPartitionRetirementCursor); |
| | | 403 | | |
| | | 404 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 405 | | private static PhysicsMobilityType GetMobilityKind(LSCollider collider) |
| | | 406 | | { |
| | 592865 | 407 | | if (collider.IsStatic) |
| | 165377 | 408 | | return PhysicsMobilityType.Static; |
| | | 409 | | |
| | 427488 | 410 | | SolidBody? body = collider.Body; |
| | 427488 | 411 | | return body!.IsKinematic ? PhysicsMobilityType.Kinematic : PhysicsMobilityType.Dynamic; |
| | | 412 | | } |
| | | 413 | | |
| | | 414 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 143825 | 415 | | private static PhysicsMobilityType GetStoredMobilityKind(int partitionKind) => partitionKind == (int)PhysicsMobility |
| | 143825 | 416 | | ? PhysicsMobilityType.Kinematic |
| | 143825 | 417 | | : partitionKind == (int)PhysicsMobilityType.Static |
| | 143825 | 418 | | ? PhysicsMobilityType.Static |
| | 143825 | 419 | | : PhysicsMobilityType.Dynamic; |
| | | 420 | | |
| | | 421 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 422 | | private static void AddObject(PhysicsPartition partition, int id, PhysicsMobilityType kind) |
| | | 423 | | { |
| | 5516466 | 424 | | if (kind == PhysicsMobilityType.Static) |
| | | 425 | | { |
| | 790257 | 426 | | partition.AddStaticObject(id); |
| | 790257 | 427 | | return; |
| | | 428 | | } |
| | | 429 | | |
| | 4726209 | 430 | | if (kind == PhysicsMobilityType.Kinematic) |
| | | 431 | | { |
| | 380806 | 432 | | partition.AddKinematicObject(id); |
| | 380806 | 433 | | return; |
| | | 434 | | } |
| | | 435 | | |
| | 4345403 | 436 | | partition.AddDynamicObject(id); |
| | 4345403 | 437 | | } |
| | | 438 | | |
| | | 439 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 440 | | private static void RemoveObject(PhysicsPartition partition, int id, PhysicsMobilityType kind) |
| | | 441 | | { |
| | 372450 | 442 | | if (kind == PhysicsMobilityType.Static) |
| | | 443 | | { |
| | 44979 | 444 | | partition.RemoveStaticObject(id); |
| | 44979 | 445 | | return; |
| | | 446 | | } |
| | | 447 | | |
| | 327471 | 448 | | if (kind == PhysicsMobilityType.Kinematic) |
| | | 449 | | { |
| | 232282 | 450 | | partition.RemoveKinematicObject(id); |
| | 232282 | 451 | | return; |
| | | 452 | | } |
| | | 453 | | |
| | 95189 | 454 | | partition.RemoveDynamicObject(id); |
| | 95189 | 455 | | } |
| | | 456 | | |
| | | 457 | | private sealed class PhysicsPartitionOrderComparer : IComparer<PhysicsPartition> |
| | | 458 | | { |
| | | 459 | | public int Compare(PhysicsPartition? left, PhysicsPartition? right) => |
| | 8297402 | 460 | | WorldVoxelIndexOrdering.Compare3D(left!.WorldIndex, right!.WorldIndex); |
| | | 461 | | } |
| | | 462 | | |
| | | 463 | | internal int ActivatePartition(PhysicsPartition partition) |
| | | 464 | | { |
| | 537548 | 465 | | ChronicleGuard.ThrowIfNull(partition, nameof(partition)); |
| | 537548 | 466 | | ChronicleGuard.ThrowIfArgument( |
| | 537548 | 467 | | !ReferenceEquals(partition.Owner, this), |
| | 537548 | 468 | | nameof(partition), |
| | 537548 | 469 | | "Partition must belong to this collision service."); |
| | | 470 | | |
| | 537548 | 471 | | return _activePartitions.Add(partition); |
| | | 472 | | } |
| | | 473 | | |
| | 537545 | 474 | | internal void DeactivatePartition(int activationId) => _activePartitions.TryRemoveAt(activationId); |
| | | 475 | | |
| | | 476 | | internal PhysicsPartition RentPartition() |
| | | 477 | | { |
| | 1159183 | 478 | | if (_inactivePartitionPool.Count == 0) |
| | 1158969 | 479 | | TryRetireEmptyRetainedPartitionForReuse(); |
| | | 480 | | |
| | 1159183 | 481 | | PhysicsPartition partition = _inactivePartitionPool.Count > 0 |
| | 1159183 | 482 | | ? _inactivePartitionPool.Pop() |
| | 1159183 | 483 | | : new PhysicsPartition(); |
| | 1159183 | 484 | | partition.SetOwner(this); |
| | 1159183 | 485 | | return partition; |
| | | 486 | | } |
| | | 487 | | |
| | 1158969 | 488 | | private bool TryRetireEmptyRetainedPartitionForReuse() => RetainedPartitionLifecycle.TryRetireEmptyForReuse( |
| | 1158969 | 489 | | _emptyRetainedPartitions, |
| | 1158969 | 490 | | _inactivePartitionPool, |
| | 1158969 | 491 | | _context.World, |
| | 1158969 | 492 | | this, |
| | 1158969 | 493 | | _releaseRetainedPartition); |
| | | 494 | | |
| | | 495 | | internal void ReleasePartition(PhysicsPartition partition) |
| | | 496 | | { |
| | 1159179 | 497 | | ChronicleGuard.ThrowIfNull(partition, nameof(partition)); |
| | 1159179 | 498 | | ChronicleGuard.ThrowIfArgument( |
| | 1159179 | 499 | | !ReferenceEquals(partition.Owner, this), |
| | 1159179 | 500 | | nameof(partition), |
| | 1159179 | 501 | | "Partition must be released through its owning collision service."); |
| | | 502 | | |
| | 1159179 | 503 | | UntrackRetainedPartition(partition); |
| | 1159179 | 504 | | partition.ResetForPool(); |
| | 1159179 | 505 | | _inactivePartitionPool.Push(partition); |
| | 1159179 | 506 | | } |
| | | 507 | | } |