| | | 1 | | //======================================================================= |
| | | 2 | | // LSCCollider2D.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 Chronicler; |
| | | 9 | | using FixedMathSharp; |
| | | 10 | | using FixedMathSharp.Geometry; |
| | | 11 | | using Gravitas.Materials; |
| | | 12 | | using Gravitas.Support; |
| | | 13 | | using GridForge.Spatial; |
| | | 14 | | using SwiftCollections; |
| | | 15 | | using System.Runtime.CompilerServices; |
| | | 16 | | |
| | | 17 | | namespace Gravitas.Colliders; |
| | | 18 | | |
| | | 19 | | internal readonly struct ColliderLifetimeToken2D |
| | | 20 | | { |
| | | 21 | | internal ColliderLifetimeToken2D(LSCollider2D collider) |
| | | 22 | | { |
| | | 23 | | Collider = collider; |
| | | 24 | | LifetimeVersion = collider.LifetimeVersion; |
| | | 25 | | } |
| | | 26 | | |
| | | 27 | | internal LSCollider2D Collider { get; } |
| | | 28 | | |
| | | 29 | | internal long LifetimeVersion { get; } |
| | | 30 | | |
| | | 31 | | internal bool IsActive => Collider.IsActive && IsCurrentLifetime; |
| | | 32 | | |
| | | 33 | | internal bool IsCurrentLifetime => Collider.LifetimeVersion == LifetimeVersion; |
| | | 34 | | } |
| | | 35 | | |
| | | 36 | | /// <summary> |
| | | 37 | | /// Common runtime type for the closed set of Gravitas-owned pure 2D collider |
| | | 38 | | /// shapes. Use <see cref="ColliderShapeDefinition2D.CreateCollider"/> when |
| | | 39 | | /// constructing from engine-adapter shape data. |
| | | 40 | | /// </summary> |
| | | 41 | | public abstract partial class LSCollider2D : IRecordable, IColliderHierarchyNode, IPhysicsColliderRegistryItem |
| | | 42 | | { |
| | 11008 | 43 | | internal LSCollider2D() { } |
| | | 44 | | |
| | | 45 | | private SolidBody2D? _body; |
| | | 46 | | private IMatterAgent? _agent; |
| | | 47 | | private GravitasWorldContext? _context; |
| | | 48 | | private LSCompoundCollider2D? _compoundOwner; |
| | | 49 | | private Fixed64 _compoundLocalRotation; |
| | 5504 | 50 | | private Vector2d _compoundLocalScale = Vector2d.One; |
| | 5504 | 51 | | private int _id = -1; |
| | 5504 | 52 | | private int _serviceIndex = -1; |
| | 5504 | 53 | | private int _replayOrder = -1; |
| | 5504 | 54 | | private int _replayOrdinal = -1; |
| | | 55 | | private long _lifetimeVersion; |
| | 5504 | 56 | | private int _serviceRefreshIndex = -1; |
| | 5504 | 57 | | private bool _isActive = true; |
| | | 58 | | private bool _isTrigger; |
| | | 59 | | private PhysicsLayer _layer = new(); |
| | 5504 | 60 | | private PhysicsLayerMask _ignoredCollisionLayers = PhysicsLayerMask.None; |
| | 5504 | 61 | | private PhysicsMaterial _material = PhysicsMaterial.Default; |
| | | 62 | | private Vector2d _localOffset; |
| | | 63 | | private FixedBoundArea _bounds; |
| | | 64 | | private FixedBoundBox _mixedBounds3D; |
| | | 65 | | private Fixed64? _mixedHalfThicknessOverride; |
| | | 66 | | private Fixed64 _mixedHalfThickness; |
| | | 67 | | private Fixed64 _mixedSlabCenterY; |
| | | 68 | | private uint _shapeVersion; |
| | 5504 | 69 | | private readonly ColliderRuntimeShapeState<ColliderShapeSnapshot2D> _runtimeShapeState = new(); |
| | | 70 | | private ColliderPartitionState2D _partitionState; |
| | | 71 | | private ColliderPartitionState _mixedPartitionState; |
| | | 72 | | private ColliderQueryState _queryState; |
| | | 73 | | private ColliderPairState<CollisionPair2D> _pairState; |
| | | 74 | | private ColliderHierarchyState _hierarchyState; |
| | | 75 | | |
| | | 76 | | /// <summary>Handles a pure 2D contact notification for the other body.</summary> |
| | | 77 | | public delegate void Body2DCollisionFunc(SolidBody2D other); |
| | | 78 | | /// <summary>Handles a pure 2D trigger notification for the other collider.</summary> |
| | | 79 | | public delegate void Trigger2DCollisionFunc(LSCollider2D other); |
| | | 80 | | /// <summary>Handles a mixed contact or trigger notification for the other 3D collider.</summary> |
| | | 81 | | public delegate void Mixed2DCollisionFunc(LSCollider other); |
| | | 82 | | |
| | | 83 | | /// <summary> |
| | | 84 | | /// Raised while this collider is touching another collider that owns a 2D body. |
| | | 85 | | /// </summary> |
| | | 86 | | public event Body2DCollisionFunc? OnContact; |
| | | 87 | | |
| | | 88 | | /// <summary> |
| | | 89 | | /// Raised on the first simulation frame this non-trigger collider touches another 2D body. |
| | | 90 | | /// </summary> |
| | | 91 | | public event Body2DCollisionFunc? OnContactEnter; |
| | | 92 | | |
| | | 93 | | /// <summary> |
| | | 94 | | /// Raised when this collider stops touching another 2D body. |
| | | 95 | | /// </summary> |
| | | 96 | | public event Body2DCollisionFunc? OnContactExit; |
| | | 97 | | |
| | | 98 | | /// <summary> |
| | | 99 | | /// Raised on the first simulation frame this collider participates in a valid trigger pair. |
| | | 100 | | /// </summary> |
| | | 101 | | public event Trigger2DCollisionFunc? OnTriggerEnter; |
| | | 102 | | |
| | | 103 | | /// <summary> |
| | | 104 | | /// Raised each simulation frame this collider participates in an overlapped valid trigger pair. |
| | | 105 | | /// </summary> |
| | | 106 | | public event Trigger2DCollisionFunc? OnTriggerStay; |
| | | 107 | | |
| | | 108 | | /// <summary> |
| | | 109 | | /// Raised when this collider stops participating in a valid trigger pair. |
| | | 110 | | /// </summary> |
| | | 111 | | public event Trigger2DCollisionFunc? OnTriggerExit; |
| | | 112 | | |
| | | 113 | | /// <summary>Raised while this collider has a physical mixed contact with a 3D collider.</summary> |
| | | 114 | | public event Mixed2DCollisionFunc? OnMixedContact; |
| | | 115 | | /// <summary>Raised when this collider begins a physical mixed contact with a 3D collider.</summary> |
| | | 116 | | public event Mixed2DCollisionFunc? OnMixedContactEnter; |
| | | 117 | | /// <summary>Raised when this collider ends a physical mixed contact with a 3D collider.</summary> |
| | | 118 | | public event Mixed2DCollisionFunc? OnMixedContactExit; |
| | | 119 | | |
| | | 120 | | /// <summary> |
| | | 121 | | /// Raised on the first mixed 2D/3D simulation frame this collider participates in a valid trigger pair. |
| | | 122 | | /// </summary> |
| | | 123 | | public event Mixed2DCollisionFunc? OnMixedTriggerEnter; |
| | | 124 | | |
| | | 125 | | /// <summary> |
| | | 126 | | /// Raised each mixed 2D/3D simulation frame this collider participates in an overlapped valid trigger pair. |
| | | 127 | | /// </summary> |
| | | 128 | | public event Mixed2DCollisionFunc? OnMixedTriggerStay; |
| | | 129 | | |
| | | 130 | | /// <summary> |
| | | 131 | | /// Raised when this collider stops participating in a valid mixed 2D/3D trigger pair. |
| | | 132 | | /// </summary> |
| | | 133 | | public event Mixed2DCollisionFunc? OnMixedTriggerExit; |
| | | 134 | | |
| | | 135 | | /// <summary>Gets the context-local runtime collider identifier.</summary> |
| | 402003 | 136 | | public int Id => _id; |
| | | 137 | | |
| | 862 | 138 | | internal int ReplayOrdinal => _replayOrdinal; |
| | | 139 | | |
| | 19320 | 140 | | internal long LifetimeVersion => _lifetimeVersion; |
| | | 141 | | |
| | 3620 | 142 | | internal int ServiceRefreshIndex => _serviceRefreshIndex; |
| | | 143 | | |
| | 12156 | 144 | | internal bool IsPartitioned => _partitionState.IsPartitioned; |
| | | 145 | | |
| | 8462 | 146 | | internal SwiftList<WorldVoxelIndex>? PartitionCoordinates => _partitionState.Coordinates; |
| | | 147 | | |
| | 2365 | 148 | | internal int PartitionKind => _partitionState.LastPartitionKind; |
| | | 149 | | |
| | 2075 | 150 | | internal bool IsMixedPartitioned => _mixedPartitionState.IsPartitioned; |
| | | 151 | | |
| | 941 | 152 | | internal SwiftList<WorldVoxelIndex>? MixedPartitionCoordinates => _mixedPartitionState.Coordinates; |
| | | 153 | | |
| | 479 | 154 | | internal int MixedPartitionKind => _mixedPartitionState.LastPartitionKind; |
| | | 155 | | |
| | 307 | 156 | | internal uint BroadPhaseVersion => _partitionState.BroadPhaseVersion; |
| | | 157 | | |
| | 249 | 158 | | internal uint RuntimeShapeVersion => _runtimeShapeState.RuntimeVersion; |
| | | 159 | | |
| | 5452 | 160 | | internal bool HasHostBinding => _agent != null; |
| | | 161 | | |
| | 221 | 162 | | internal int CollisionPairCount => _pairState.CollisionPairCount; |
| | | 163 | | |
| | 233 | 164 | | internal int CollisionPairHolderCount => _pairState.CollisionPairHolderCount; |
| | | 165 | | |
| | 814 | 166 | | internal SwiftDictionary<int, CollisionPair2D>? CollisionPairs => _pairState.CollisionPairs; |
| | | 167 | | |
| | 814 | 168 | | internal SwiftHashSet<int>? CollisionPairHolders => _pairState.CollisionPairHolders; |
| | | 169 | | |
| | | 170 | | /// <summary>Gets the owning 2D body, or <see langword="null"/> for a bodyless collider.</summary> |
| | 129395 | 171 | | public SolidBody2D? Body => _body; |
| | | 172 | | |
| | | 173 | | /// <summary> |
| | | 174 | | /// Gets whether this collider is bodyless or belongs to an explicit static |
| | | 175 | | /// body role. |
| | | 176 | | /// </summary> |
| | 14094 | 177 | | public bool IsStatic => _body == null || _body.IsStatic; |
| | | 178 | | |
| | 5240 | 179 | | internal bool RequiresServiceSideRefresh => _body == null || _body.DynamicId < 0; |
| | | 180 | | |
| | | 181 | | /// <summary>Gets the host agent to which this collider is bound.</summary> |
| | | 182 | | public IMatterAgent Agent |
| | | 183 | | { |
| | | 184 | | get |
| | | 185 | | { |
| | 4 | 186 | | SwiftThrowHelper.ThrowIfTrue( |
| | 4 | 187 | | _agent == null, |
| | 4 | 188 | | nameof(LSCollider2D), |
| | 4 | 189 | | "2D collider is not bound to an IMatterAgent."); |
| | 3 | 190 | | return _agent!; |
| | | 191 | | } |
| | | 192 | | } |
| | | 193 | | |
| | 25328 | 194 | | internal IMatterAgent? AgentOrNull => _agent; |
| | | 195 | | |
| | | 196 | | /// <summary>Gets the world context to which this collider is bound.</summary> |
| | | 197 | | public GravitasWorldContext Context |
| | | 198 | | { |
| | | 199 | | get |
| | | 200 | | { |
| | 19713 | 201 | | SwiftThrowHelper.ThrowIfTrue( |
| | 19713 | 202 | | _context == null, |
| | 19713 | 203 | | nameof(LSCollider2D), |
| | 19713 | 204 | | "2D collider is not bound to a GravitasWorldContext."); |
| | 19712 | 205 | | return _context!; |
| | | 206 | | } |
| | | 207 | | } |
| | | 208 | | |
| | | 209 | | /// <summary>Gets or sets whether this collider participates in runtime physics.</summary> |
| | | 210 | | public bool IsActive |
| | | 211 | | { |
| | | 212 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 403196 | 213 | | get => _isActive; |
| | | 214 | | set |
| | | 215 | | { |
| | 138 | 216 | | ThrowIfCompoundPartLifecycle(nameof(IsActive)); |
| | | 217 | | |
| | 138 | 218 | | if (_isActive == value) |
| | 4 | 219 | | return; |
| | | 220 | | |
| | 134 | 221 | | if (_context == null || _id < 0) |
| | | 222 | | { |
| | 123 | 223 | | _isActive = value; |
| | 123 | 224 | | return; |
| | | 225 | | } |
| | | 226 | | |
| | 11 | 227 | | if (value) |
| | | 228 | | { |
| | 2 | 229 | | RebuildRuntimeShapeState(); |
| | 2 | 230 | | _isActive = true; |
| | 2 | 231 | | _context.Collisions2D.RefreshColliderPartition(this); |
| | 2 | 232 | | if (_context.Settings.RuntimeMode.RunsMixedContacts()) |
| | 1 | 233 | | _context.MixedCollisions.Refresh2DColliderPartition(this); |
| | | 234 | | } |
| | | 235 | | else |
| | | 236 | | { |
| | 9 | 237 | | _isActive = false; |
| | 9 | 238 | | _context.Collisions2D.ClearPartitionedCollider(this, force: true); |
| | 9 | 239 | | if (IsMixedPartitioned) |
| | 2 | 240 | | _context.MixedCollisions.ClearPartitioned2DCollider(this, force: true); |
| | | 241 | | } |
| | 10 | 242 | | } |
| | | 243 | | } |
| | | 244 | | |
| | | 245 | | /// <summary> |
| | | 246 | | /// Gets or sets whether this bodyless collider is a trigger volume. |
| | | 247 | | /// Trigger volumes raise trigger enter/stay/exit callbacks for valid overlap |
| | | 248 | | /// pairs and never apply physical response. |
| | | 249 | | /// </summary> |
| | | 250 | | public bool IsTrigger |
| | | 251 | | { |
| | | 252 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 10846 | 253 | | get => _isTrigger; |
| | 70 | 254 | | set => SetTrigger(value); |
| | | 255 | | } |
| | | 256 | | |
| | | 257 | | /// <summary> |
| | | 258 | | /// Gets or sets the single physics layer used by 2D collision matrix and query-mask filtering. |
| | | 259 | | /// </summary> |
| | | 260 | | public PhysicsLayer Layer |
| | | 261 | | { |
| | | 262 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 111651 | 263 | | get => _layer; |
| | | 264 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 726 | 265 | | set => _layer = value; |
| | | 266 | | } |
| | | 267 | | |
| | | 268 | | /// <summary> |
| | | 269 | | /// Gets or sets physical layers this collider ignores for collider-to-collider |
| | | 270 | | /// interactions. Public queries continue to use the caller's query mask. |
| | | 271 | | /// </summary> |
| | | 272 | | public PhysicsLayerMask IgnoredCollisionLayers |
| | | 273 | | { |
| | | 274 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 9 | 275 | | get => _ignoredCollisionLayers; |
| | | 276 | | set |
| | | 277 | | { |
| | 26 | 278 | | if (_ignoredCollisionLayers == value) |
| | 1 | 279 | | return; |
| | | 280 | | |
| | 25 | 281 | | _ignoredCollisionLayers = value; |
| | 25 | 282 | | _body?.Wake(); |
| | 22 | 283 | | } |
| | | 284 | | } |
| | | 285 | | |
| | | 286 | | /// <summary> |
| | | 287 | | /// Gets or sets the deterministic surface material used by pure 2D and |
| | | 288 | | /// mixed collision response for this collider. |
| | | 289 | | /// </summary> |
| | | 290 | | public PhysicsMaterial Material |
| | | 291 | | { |
| | | 292 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 2356 | 293 | | get => _material; |
| | | 294 | | set |
| | | 295 | | { |
| | 908 | 296 | | if (_material == value) |
| | 498 | 297 | | return; |
| | | 298 | | |
| | 410 | 299 | | _material = value; |
| | 410 | 300 | | OnMaterialChanged(); |
| | 410 | 301 | | _body?.Wake(); |
| | 133 | 302 | | } |
| | | 303 | | } |
| | | 304 | | |
| | | 305 | | /// <summary>Gets the runtime pure 2D shape family.</summary> |
| | | 306 | | public abstract ColliderType2D Shape { get; } |
| | | 307 | | |
| | | 308 | | /// <summary>Gets the deterministic pure 2D narrow-phase ordering priority.</summary> |
| | 1539 | 309 | | public virtual int Priority => ColliderSettings2D.GetPriority(Shape); |
| | | 310 | | |
| | | 311 | | internal uint RaycastVersion |
| | | 312 | | { |
| | | 313 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 153285 | 314 | | get => _queryState.RaycastVersion; |
| | | 315 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 36867 | 316 | | set => _queryState.RaycastVersion = value; |
| | | 317 | | } |
| | | 318 | | |
| | | 319 | | internal uint CircleQueryVersion |
| | | 320 | | { |
| | | 321 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 198839 | 322 | | get => _queryState.CircleQueryVersion; |
| | | 323 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 25199 | 324 | | set => _queryState.CircleQueryVersion = value; |
| | | 325 | | } |
| | | 326 | | |
| | | 327 | | /// <summary>Gets whether this collider has a hierarchy parent.</summary> |
| | 2 | 328 | | public bool IsChild => _hierarchyState.IsChild; |
| | | 329 | | |
| | | 330 | | /// <summary>Gets whether this collider is configured as or currently owns a hierarchy parent.</summary> |
| | 2 | 331 | | public bool IsParent => _hierarchyState.IsParent; |
| | | 332 | | |
| | | 333 | | /// <summary>Gets the context-local identifier of the parent collider, or -1 when unparented.</summary> |
| | 5 | 334 | | public int ParentId => ParentKey.Id; |
| | | 335 | | |
| | | 336 | | /// <summary>Gets the 2D parent collider, when the parent belongs to the 2D runtime.</summary> |
| | 5 | 337 | | public LSCollider2D? Parent2D => _hierarchyState.Parent as LSCollider2D; |
| | | 338 | | |
| | | 339 | | /// <summary>Gets the 3D parent collider, when the parent belongs to the 3D runtime.</summary> |
| | 5 | 340 | | public LSCollider? Parent3D => _hierarchyState.Parent as LSCollider; |
| | | 341 | | |
| | 3683 | 342 | | internal LSCollider2D? TopParent2D => _hierarchyState.TopParent as LSCollider2D; |
| | | 343 | | |
| | 2 | 344 | | internal LSCollider? TopParent3D => _hierarchyState.TopParent as LSCollider; |
| | | 345 | | |
| | 787 | 346 | | internal int HierarchyChildCount => _hierarchyState.ChildCount; |
| | | 347 | | |
| | 24597 | 348 | | internal ColliderHierarchyKey HierarchyKey => Id >= 0 ? ColliderHierarchyKey.Create2D(Id) : ColliderHierarchyKey.Non |
| | | 349 | | |
| | 790 | 350 | | internal ColliderHierarchyKey ParentKey => _hierarchyState.ParentKey; |
| | | 351 | | |
| | 783 | 352 | | internal ColliderHierarchyKey TopParentKey => _hierarchyState.TopParentKey; |
| | | 353 | | |
| | 5224 | 354 | | internal ColliderHierarchyState HierarchyState => _hierarchyState; |
| | | 355 | | |
| | 100 | 356 | | ColliderHierarchyKey IColliderHierarchyNode.HierarchyKey => HierarchyKey; |
| | | 357 | | |
| | 27 | 358 | | IColliderHierarchyNode? IColliderHierarchyNode.HierarchyParent => _hierarchyState.Parent; |
| | | 359 | | |
| | | 360 | | /// <summary>Gets or sets the unscaled local center offset.</summary> |
| | | 361 | | public Vector2d LocalOffset |
| | | 362 | | { |
| | | 363 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 36433 | 364 | | get => _localOffset; |
| | | 365 | | set |
| | | 366 | | { |
| | 317 | 367 | | if (_localOffset == value) |
| | 73 | 368 | | return; |
| | | 369 | | |
| | 244 | 370 | | _localOffset = value; |
| | 244 | 371 | | MarkShapeDirty(); |
| | 244 | 372 | | } |
| | | 373 | | } |
| | | 374 | | |
| | | 375 | | /// <summary>Gets the committed pure 2D world-space bounds.</summary> |
| | 1690 | 376 | | public FixedBoundArea Bounds => _bounds; |
| | | 377 | | |
| | 20341 | 378 | | internal FixedBoundBox MixedBounds3D => _mixedBounds3D; |
| | | 379 | | |
| | 14672 | 380 | | internal Fixed64 MixedHalfThickness => _mixedHalfThickness; |
| | | 381 | | |
| | 16261 | 382 | | internal Fixed64 MixedSlabCenterY => _mixedSlabCenterY; |
| | | 383 | | |
| | | 384 | | /// <summary> |
| | | 385 | | /// Gets or sets the optional half-thickness used when this 2D collider is embedded into mixed 2D/3D contacts. |
| | | 386 | | /// </summary> |
| | | 387 | | public Fixed64? MixedHalfThicknessOverride |
| | | 388 | | { |
| | | 389 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 8 | 390 | | get => _mixedHalfThicknessOverride; |
| | | 391 | | set |
| | | 392 | | { |
| | 54 | 393 | | if (value.HasValue) |
| | | 394 | | { |
| | 51 | 395 | | SwiftThrowHelper.ThrowIfArgument( |
| | 51 | 396 | | value.Value <= Fixed64.Zero, |
| | 51 | 397 | | nameof(value), |
| | 51 | 398 | | "2D mixed half-thickness override must be greater than zero."); |
| | | 399 | | } |
| | | 400 | | |
| | 52 | 401 | | if (_mixedHalfThicknessOverride == value) |
| | 2 | 402 | | return; |
| | | 403 | | |
| | 50 | 404 | | _mixedHalfThicknessOverride = value; |
| | 50 | 405 | | MarkShapeDirty(); |
| | 50 | 406 | | } |
| | | 407 | | } |
| | | 408 | | |
| | | 409 | | /// <summary>Gets the minimum world X coordinate of <see cref="Bounds"/>.</summary> |
| | 193714 | 410 | | public Fixed64 MinX => _bounds.Min.X; |
| | | 411 | | |
| | | 412 | | /// <summary>Gets the maximum world X coordinate of <see cref="Bounds"/>.</summary> |
| | 191920 | 413 | | public Fixed64 MaxX => _bounds.Max.X; |
| | | 414 | | |
| | | 415 | | /// <summary>Gets the minimum planar Y coordinate of <see cref="Bounds"/>.</summary> |
| | 163019 | 416 | | public Fixed64 MinY => _bounds.Min.Y; |
| | | 417 | | |
| | | 418 | | /// <summary>Gets the maximum planar Y coordinate of <see cref="Bounds"/>.</summary> |
| | 172905 | 419 | | public Fixed64 MaxY => _bounds.Max.Y; |
| | | 420 | | |
| | | 421 | | internal void Initialize(SolidBody2D body) |
| | | 422 | | { |
| | 4653 | 423 | | InitCore(body.Agent, body); |
| | 4653 | 424 | | } |
| | | 425 | | |
| | | 426 | | /// <summary>Binds and registers this collider as a bodyless pure 2D collider.</summary> |
| | | 427 | | public void InitializeWithNoBody(IMatterAgent agent) |
| | | 428 | | { |
| | 525 | 429 | | ThrowIfCompoundPartLifecycle(nameof(InitializeWithNoBody)); |
| | 525 | 430 | | SwiftThrowHelper.ThrowIfNull(agent, nameof(agent)); |
| | 525 | 431 | | SwiftThrowHelper.ThrowIfArgument( |
| | 525 | 432 | | Id >= 0 || (HasHostBinding && !ReferenceEquals(_agent, agent)), |
| | 525 | 433 | | nameof(agent), |
| | 525 | 434 | | "2D collider is already registered or bound to another host agent."); |
| | 523 | 435 | | PreflightInitialization(agent); |
| | 510 | 436 | | InitCore(agent, null); |
| | 510 | 437 | | Context.Physics2D.AssimilateCollider(this); |
| | 510 | 438 | | } |
| | | 439 | | |
| | | 440 | | internal void PreflightBodyInitialization( |
| | | 441 | | SolidBody2D body, |
| | | 442 | | Vector2d requestedPosition, |
| | | 443 | | Fixed64 requestedRotation) |
| | | 444 | | { |
| | 4658 | 445 | | ThrowIfCompoundPartLifecycle(nameof(Initialize)); |
| | 4658 | 446 | | SwiftThrowHelper.ThrowIfNull(body, nameof(body)); |
| | 4658 | 447 | | ThrowIfTriggerWouldAttachToBody(nameof(Initialize)); |
| | 4657 | 448 | | PreflightInitialization( |
| | 4657 | 449 | | body.Agent, |
| | 4657 | 450 | | useRequestedPose: true, |
| | 4657 | 451 | | requestedPosition, |
| | 4657 | 452 | | requestedRotation); |
| | 4652 | 453 | | } |
| | | 454 | | |
| | | 455 | | private void PreflightInitialization( |
| | | 456 | | IMatterAgent agent, |
| | | 457 | | bool useRequestedPose = false, |
| | | 458 | | Vector2d requestedPosition = default, |
| | | 459 | | Fixed64 requestedRotation = default) |
| | | 460 | | { |
| | 5180 | 461 | | SwiftThrowHelper.ThrowIfNull(agent, nameof(agent)); |
| | 5180 | 462 | | OnBeforeInitialize(agent); |
| | 5180 | 463 | | PrepareStandaloneInitialization( |
| | 5180 | 464 | | agent, |
| | 5180 | 465 | | useRequestedPose, |
| | 5180 | 466 | | requestedPosition, |
| | 5180 | 467 | | requestedRotation); |
| | 5162 | 468 | | } |
| | | 469 | | |
| | | 470 | | private void InitCore(IMatterAgent agent, SolidBody2D? body) |
| | | 471 | | { |
| | 5163 | 472 | | _lifetimeVersion++; |
| | 5163 | 473 | | _body = body; |
| | 5163 | 474 | | _agent = agent; |
| | 5163 | 475 | | _context = agent.Context; |
| | 5163 | 476 | | _isActive = true; |
| | 5163 | 477 | | _queryState.Reset(); |
| | 5163 | 478 | | _hierarchyState.Initialize(agent.IsParent); |
| | 5163 | 479 | | PublishPreparedShape(); |
| | 5163 | 480 | | } |
| | | 481 | | |
| | | 482 | | /// <summary>Validates or prepares derived shape state before runtime registration.</summary> |
| | 5180 | 483 | | protected virtual void OnBeforeInitialize(IMatterAgent agent) { } |
| | | 484 | | |
| | | 485 | | internal void SetPhysicsState(int id, int serviceIndex, int replayOrder) |
| | | 486 | | { |
| | 5162 | 487 | | SwiftThrowHelper.ThrowIfNegative(id, nameof(id)); |
| | 5162 | 488 | | SwiftThrowHelper.ThrowIfNegative(serviceIndex, nameof(serviceIndex)); |
| | 5162 | 489 | | SwiftThrowHelper.ThrowIfNegative(replayOrder, nameof(replayOrder)); |
| | 5162 | 490 | | _id = id; |
| | 5162 | 491 | | _serviceIndex = serviceIndex; |
| | 5162 | 492 | | _replayOrder = replayOrder; |
| | 5162 | 493 | | _replayOrdinal = -1; |
| | 5162 | 494 | | } |
| | | 495 | | |
| | | 496 | | internal void SetServiceIndex(int serviceIndex) |
| | | 497 | | { |
| | 66 | 498 | | SwiftThrowHelper.ThrowIfNegative(serviceIndex, nameof(serviceIndex)); |
| | 66 | 499 | | _serviceIndex = serviceIndex; |
| | 66 | 500 | | } |
| | | 501 | | |
| | | 502 | | internal void SetReplayOrdinal(int replayOrdinal) |
| | | 503 | | { |
| | 1728 | 504 | | SwiftThrowHelper.ThrowIfNegative(replayOrdinal, nameof(replayOrdinal)); |
| | 1728 | 505 | | _replayOrdinal = replayOrdinal; |
| | 1728 | 506 | | } |
| | | 507 | | |
| | | 508 | | internal void SetServiceRefreshIndex(int serviceRefreshIndex) |
| | | 509 | | { |
| | 1896 | 510 | | SwiftThrowHelper.ThrowIfNegative(serviceRefreshIndex, nameof(serviceRefreshIndex)); |
| | 1896 | 511 | | _serviceRefreshIndex = serviceRefreshIndex; |
| | 1896 | 512 | | } |
| | | 513 | | |
| | | 514 | | internal void ClearServiceRefreshIndex() |
| | | 515 | | { |
| | 3620 | 516 | | _serviceRefreshIndex = -1; |
| | 3620 | 517 | | } |
| | | 518 | | |
| | | 519 | | internal void ClearPhysicsState() |
| | | 520 | | { |
| | 405 | 521 | | _partitionState.MarkUnpartitioned(); |
| | 405 | 522 | | _partitionState.ClearCoordinates(); |
| | 405 | 523 | | _mixedPartitionState.MarkUnpartitioned(); |
| | 405 | 524 | | _mixedPartitionState.ClearCoordinates(); |
| | 405 | 525 | | _pairState.ClearCollisionPairs(); |
| | 405 | 526 | | _pairState.ClearCollisionPairHolders(); |
| | 405 | 527 | | _id = -1; |
| | 405 | 528 | | _serviceIndex = -1; |
| | 405 | 529 | | _replayOrder = -1; |
| | 405 | 530 | | _replayOrdinal = -1; |
| | 405 | 531 | | _serviceRefreshIndex = -1; |
| | 405 | 532 | | } |
| | | 533 | | |
| | | 534 | | void IPhysicsColliderRegistryItem.SetRegistryState(int id, int serviceIndex, int replayOrder) => |
| | 5162 | 535 | | SetPhysicsState(id, serviceIndex, replayOrder); |
| | | 536 | | |
| | 263 | 537 | | int IPhysicsColliderRegistryItem.ServiceIndex => _serviceIndex; |
| | | 538 | | |
| | 1208 | 539 | | int IPhysicsColliderRegistryItem.ReplayOrder => _replayOrder; |
| | | 540 | | |
| | | 541 | | void IPhysicsColliderRegistryItem.SetRegistryServiceIndex(int serviceIndex) => |
| | 66 | 542 | | SetServiceIndex(serviceIndex); |
| | | 543 | | |
| | | 544 | | void IPhysicsColliderRegistryItem.SetRegistryReplayOrdinal(int replayOrdinal) => |
| | 1728 | 545 | | SetReplayOrdinal(replayOrdinal); |
| | | 546 | | |
| | 282 | 547 | | void IPhysicsColliderRegistryItem.ClearRegistryState() => ClearPhysicsState(); |
| | | 548 | | |
| | | 549 | | internal void ClearBindingState() |
| | | 550 | | { |
| | 268 | 551 | | _body = null; |
| | 268 | 552 | | _agent = null; |
| | 268 | 553 | | _context = null; |
| | 268 | 554 | | _preparedContext = null; |
| | 268 | 555 | | } |
| | | 556 | | |
| | | 557 | | /// <summary>Unregisters this collider, or deactivates its owning body.</summary> |
| | | 558 | | public void Deactivate() |
| | | 559 | | { |
| | 189 | 560 | | ThrowIfCompoundPartLifecycle(nameof(Deactivate)); |
| | | 561 | | |
| | 189 | 562 | | if (_body != null) |
| | | 563 | | { |
| | 44 | 564 | | _body.Deactivate(); |
| | 44 | 565 | | return; |
| | | 566 | | } |
| | | 567 | | |
| | 145 | 568 | | if (_id >= 0) |
| | 141 | 569 | | _context!.Physics2D.DessimilateCollider(this); |
| | | 570 | | |
| | 145 | 571 | | _isActive = false; |
| | 145 | 572 | | ClearBindingState(); |
| | 145 | 573 | | } |
| | | 574 | | |
| | | 575 | | /// <summary>Refreshes bodyless collider shape and broad-phase state for one simulation step.</summary> |
| | | 576 | | public void Simulate() |
| | | 577 | | { |
| | 3542 | 578 | | ThrowIfCompoundPartLifecycle(nameof(Simulate)); |
| | | 579 | | |
| | 3541 | 580 | | if (!IsActive) |
| | 1 | 581 | | return; |
| | | 582 | | |
| | 3540 | 583 | | Rebuild(); |
| | 3538 | 584 | | } |
| | | 585 | | |
| | | 586 | | internal bool Rebuild() |
| | | 587 | | { |
| | 4813 | 588 | | if (!RebuildRuntimeShapeState()) |
| | 3478 | 589 | | return false; |
| | | 590 | | |
| | 1333 | 591 | | if (_id >= 0) |
| | 1326 | 592 | | return _context!.Collisions2D.RefreshColliderPartitionAfterShapeChange(this); |
| | | 593 | | |
| | 7 | 594 | | return true; |
| | | 595 | | } |
| | | 596 | | |
| | | 597 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 9843 | 598 | | internal bool RebuildRuntimeShapeOnly() => RebuildRuntimeShapeState(); |
| | | 599 | | |
| | | 600 | | internal void ValidateCurrentRuntimeTransform() => |
| | 129 | 601 | | PrepareRuntimeShape( |
| | 129 | 602 | | CaptureShapeSnapshot(), |
| | 129 | 603 | | requireRepresentableMassPoint: true); |
| | | 604 | | |
| | | 605 | | /// <summary>Assigns a 2D hierarchy parent used for collision exclusion.</summary> |
| | | 606 | | public void SetParent(LSCollider2D parent) |
| | | 607 | | { |
| | 16 | 608 | | ThrowIfCompoundPartLifecycle(nameof(SetParent)); |
| | 16 | 609 | | _hierarchyState.SetParent(this, parent); |
| | 16 | 610 | | } |
| | | 611 | | |
| | | 612 | | /// <summary>Assigns a 3D hierarchy parent used for mixed collision exclusion.</summary> |
| | | 613 | | public void SetParent(LSCollider parent) |
| | | 614 | | { |
| | 6 | 615 | | ThrowIfCompoundPartLifecycle(nameof(SetParent)); |
| | 6 | 616 | | _hierarchyState.SetParent(this, parent); |
| | 6 | 617 | | } |
| | | 618 | | |
| | | 619 | | /// <summary>Removes this collider from its current hierarchy parent.</summary> |
| | | 620 | | public void ClearParent() |
| | | 621 | | { |
| | 267 | 622 | | ThrowIfCompoundPartLifecycle(nameof(ClearParent)); |
| | 267 | 623 | | _hierarchyState.ClearParent(this); |
| | 267 | 624 | | } |
| | | 625 | | |
| | | 626 | | /// <summary>Gets whether hierarchy rules exclude collision with another 2D collider.</summary> |
| | | 627 | | public bool IsSibling(LSCollider2D other) => |
| | 9241 | 628 | | _hierarchyState.ExcludesCollisionWith(other._hierarchyState, HierarchyKey, other.HierarchyKey); |
| | | 629 | | |
| | | 630 | | internal bool ExcludesMixedCollisionWith(LSCollider other) => |
| | 1 | 631 | | _hierarchyState.ExcludesCollisionWith(other.HierarchyState, HierarchyKey, other.HierarchyKey); |
| | | 632 | | |
| | | 633 | | internal SwiftList<WorldVoxelIndex> GetOrCreatePartitionCoordinates() |
| | | 634 | | { |
| | 7045 | 635 | | _partitionState.Coordinates ??= new(); |
| | 7045 | 636 | | return _partitionState.Coordinates; |
| | | 637 | | } |
| | | 638 | | |
| | | 639 | | internal SwiftList<WorldVoxelIndex> GetOrCreateMixedPartitionCoordinates() |
| | | 640 | | { |
| | 628 | 641 | | _mixedPartitionState.Coordinates ??= new(); |
| | 628 | 642 | | return _mixedPartitionState.Coordinates; |
| | | 643 | | } |
| | | 644 | | |
| | | 645 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 646 | | internal bool MatchesPartitionGridBounds(Vector2d min, Vector2d max, int partitionKind) => |
| | 2173 | 647 | | _partitionState.MatchesGridBounds(min, max, partitionKind); |
| | | 648 | | |
| | | 649 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 650 | | internal bool MatchesMixedPartitionGridBounds(Vector3d min, Vector3d max, int partitionKind) => |
| | 1264 | 651 | | _mixedPartitionState.IsPartitioned |
| | 1264 | 652 | | && _mixedPartitionState.LastGridBoundsMin == min |
| | 1264 | 653 | | && _mixedPartitionState.LastGridBoundsMax == max |
| | 1264 | 654 | | && _mixedPartitionState.LastPartitionKind == partitionKind; |
| | | 655 | | |
| | | 656 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 657 | | internal void MarkPartitioned(Vector2d min, Vector2d max, int partitionKind) |
| | | 658 | | { |
| | 6745 | 659 | | _partitionState.SetPreviousGridBounds(min, max, partitionKind); |
| | 6745 | 660 | | _partitionState.MarkPartitioned(); |
| | 6745 | 661 | | } |
| | | 662 | | |
| | | 663 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 664 | | internal void MarkMixedPartitioned(Vector3d min, Vector3d max, int partitionKind) |
| | | 665 | | { |
| | 617 | 666 | | _mixedPartitionState.SetPreviousGridBounds(min, max, partitionKind); |
| | 617 | 667 | | _mixedPartitionState.MarkPartitioned(); |
| | 617 | 668 | | } |
| | | 669 | | |
| | | 670 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 2159 | 671 | | internal void MarkUnpartitioned() => _partitionState.MarkUnpartitioned(); |
| | | 672 | | |
| | | 673 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 276 | 674 | | internal void MarkMixedUnpartitioned() => _mixedPartitionState.MarkUnpartitioned(); |
| | | 675 | | |
| | | 676 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 2159 | 677 | | internal void ClearPartitionCoordinates() => _partitionState.ClearCoordinates(); |
| | | 678 | | |
| | | 679 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 276 | 680 | | internal void ClearMixedPartitionCoordinates() => _mixedPartitionState.ClearCoordinates(); |
| | | 681 | | |
| | | 682 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 683 | | internal bool IsPositionInPlanarBounds(Fixed64 cellEdge, Vector3d worldPosition) |
| | | 684 | | { |
| | 82393 | 685 | | Fixed64 padding = cellEdge * Fixed64.Half; |
| | 82393 | 686 | | return worldPosition.X >= MinX - padding |
| | 82393 | 687 | | && worldPosition.X <= MaxX + padding |
| | 82393 | 688 | | && worldPosition.Z >= MinY - padding |
| | 82393 | 689 | | && worldPosition.Z <= MaxY + padding; |
| | | 690 | | } |
| | | 691 | | |
| | | 692 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 693 | | internal bool IsPositionInMixedBounds(Fixed64 cellEdge, Vector3d worldPosition) |
| | | 694 | | { |
| | 29000 | 695 | | Fixed64 padding = cellEdge * Fixed64.Half; |
| | 29000 | 696 | | return worldPosition.X >= _mixedBounds3D.Min.X - padding |
| | 29000 | 697 | | && worldPosition.X <= _mixedBounds3D.Max.X + padding |
| | 29000 | 698 | | && worldPosition.Y >= _mixedBounds3D.Min.Y - padding |
| | 29000 | 699 | | && worldPosition.Y <= _mixedBounds3D.Max.Y + padding |
| | 29000 | 700 | | && worldPosition.Z >= _mixedBounds3D.Min.Z - padding |
| | 29000 | 701 | | && worldPosition.Z <= _mixedBounds3D.Max.Z + padding; |
| | | 702 | | } |
| | | 703 | | |
| | | 704 | | internal bool TryGetCollisionPair(int otherId, out CollisionPair2D? collisionPair) => |
| | 830 | 705 | | _pairState.TryGetCollisionPair(otherId, out collisionPair); |
| | | 706 | | |
| | | 707 | | internal bool TryAddCollisionPair(int otherId, CollisionPair2D collisionPair) => |
| | 257 | 708 | | _pairState.TryAddCollisionPair(otherId, collisionPair); |
| | | 709 | | |
| | | 710 | | internal bool TryRemoveCollisionPair(int otherId, out CollisionPair2D? collisionPair) => |
| | 75 | 711 | | _pairState.TryRemoveCollisionPair(otherId, out collisionPair); |
| | | 712 | | |
| | 255 | 713 | | internal bool TryAddCollisionPairHolder(int otherId) => _pairState.TryAddCollisionPairHolder(otherId); |
| | | 714 | | |
| | 75 | 715 | | internal bool TryRemoveCollisionPairHolder(int otherId) => _pairState.TryRemoveCollisionPairHolder(otherId); |
| | | 716 | | |
| | | 717 | | internal void ClearCollisionPairState() |
| | | 718 | | { |
| | 264 | 719 | | _pairState.ClearCollisionPairs(); |
| | 264 | 720 | | _pairState.ClearCollisionPairHolders(); |
| | 264 | 721 | | } |
| | | 722 | | |
| | | 723 | | internal void ClearRuntimeRelationships() |
| | | 724 | | { |
| | 264 | 725 | | ClearChildParentReferences(); |
| | 264 | 726 | | ClearParent(); |
| | 264 | 727 | | } |
| | | 728 | | |
| | | 729 | | private Fixed64 ResolveAgentRotation() |
| | | 730 | | { |
| | 127 | 731 | | return _agent == null |
| | 127 | 732 | | ? Fixed64.Zero |
| | 127 | 733 | | : _agent.Transform.WorldRotationXZRadians; |
| | | 734 | | } |
| | | 735 | | |
| | | 736 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 737 | | internal void BindContext(GravitasWorldContext context) |
| | | 738 | | { |
| | 4 | 739 | | SwiftThrowHelper.ThrowIfNull(context, nameof(context)); |
| | 3 | 740 | | SwiftThrowHelper.ThrowIfArgument( |
| | 3 | 741 | | _context != null && !ReferenceEquals(_context, context), |
| | 3 | 742 | | nameof(context), |
| | 3 | 743 | | "2D collider is already bound to a different GravitasWorldContext."); |
| | 2 | 744 | | _context = context; |
| | 2 | 745 | | } |
| | | 746 | | |
| | | 747 | | internal void ReserveCompoundPart( |
| | | 748 | | LSCompoundCollider2D owner, |
| | | 749 | | Fixed64 localRotation, |
| | | 750 | | Vector2d localScale) |
| | | 751 | | { |
| | 249 | 752 | | SwiftThrowHelper.ThrowIfNull(owner, nameof(owner)); |
| | 249 | 753 | | SwiftThrowHelper.ThrowIfArgument( |
| | 249 | 754 | | HasHostBinding, |
| | 249 | 755 | | nameof(owner), |
| | 249 | 756 | | "2D compound collider parts cannot be initialized as standalone colliders."); |
| | 248 | 757 | | SwiftThrowHelper.ThrowIfArgument( |
| | 248 | 758 | | _compoundOwner != null && !ReferenceEquals(_compoundOwner, owner), |
| | 248 | 759 | | nameof(owner), |
| | 248 | 760 | | "2D compound collider part is already owned by another compound collider."); |
| | | 761 | | |
| | 247 | 762 | | _compoundOwner = owner; |
| | 247 | 763 | | _compoundLocalRotation = localRotation; |
| | 247 | 764 | | _compoundLocalScale = localScale; |
| | 247 | 765 | | } |
| | | 766 | | |
| | | 767 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 768 | | private void ThrowIfCompoundPartLifecycle(string operation) |
| | | 769 | | { |
| | 9367 | 770 | | SwiftThrowHelper.ThrowIfTrue( |
| | 9367 | 771 | | _compoundOwner != null, |
| | 9367 | 772 | | operation, |
| | 9367 | 773 | | "2D compound collider parts are geometry owned by LSCompoundCollider2D and cannot run standalone lifecycle o |
| | 9366 | 774 | | } |
| | | 775 | | |
| | | 776 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 777 | | internal ExactMassPoint2D TransformRelativeMassPropertyPointExact( |
| | | 778 | | Vector2d partRelativePoint) |
| | | 779 | | { |
| | 35168 | 780 | | GetCurrentScaleFactors( |
| | 35168 | 781 | | out Vector2d ownerScale, |
| | 35168 | 782 | | out _); |
| | 35168 | 783 | | return _compoundOwner == null |
| | 35168 | 784 | | ? ExactMassPoint2D.CreateScaledLocalComposition( |
| | 35168 | 785 | | LocalOffset, |
| | 35168 | 786 | | ownerScale, |
| | 35168 | 787 | | Vector2d.Zero, |
| | 35168 | 788 | | Vector2d.One, |
| | 35168 | 789 | | partRelativePoint, |
| | 35168 | 790 | | Fixed64.Zero) |
| | 35168 | 791 | | : ExactMassPoint2D.CreateScaledLocalComposition( |
| | 35168 | 792 | | _compoundOwner.LocalOffset, |
| | 35168 | 793 | | ownerScale, |
| | 35168 | 794 | | LocalOffset, |
| | 35168 | 795 | | ownerScale, |
| | 35168 | 796 | | partRelativePoint, |
| | 35168 | 797 | | _compoundLocalRotation); |
| | | 798 | | } |
| | | 799 | | |
| | | 800 | | /// <summary>Rotates a planar vector and removes fixed-point epsilon residue.</summary> |
| | | 801 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 802 | | protected static Vector2d Rotate(Vector2d value, Fixed64 radians) |
| | | 803 | | { |
| | 764 | 804 | | if (radians == Fixed64.Zero) |
| | 453 | 805 | | return value; |
| | | 806 | | |
| | 311 | 807 | | return ClampNearZero(Vector2d.Rotate(value, radians)); |
| | | 808 | | } |
| | | 809 | | |
| | | 810 | | /// <summary>Replaces vector components within fixed-point epsilon of zero.</summary> |
| | | 811 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 812 | | protected static Vector2d ClampNearZero(Vector2d value) |
| | | 813 | | { |
| | 313 | 814 | | Fixed64 x = value.X.Abs() <= Fixed64.Epsilon ? Fixed64.Zero : value.X; |
| | 313 | 815 | | Fixed64 y = value.Y.Abs() <= Fixed64.Epsilon ? Fixed64.Zero : value.Y; |
| | 313 | 816 | | return new Vector2d(x, y); |
| | | 817 | | } |
| | | 818 | | |
| | | 819 | | private void SetTrigger(bool value) |
| | | 820 | | { |
| | 70 | 821 | | if (_isTrigger == value) |
| | 16 | 822 | | return; |
| | | 823 | | |
| | 54 | 824 | | if (value) |
| | 51 | 825 | | ThrowIfCannotEnableTrigger(nameof(IsTrigger)); |
| | | 826 | | |
| | 53 | 827 | | _isTrigger = value; |
| | 53 | 828 | | } |
| | | 829 | | |
| | | 830 | | private void ThrowIfCannotEnableTrigger(string operation) |
| | | 831 | | { |
| | 51 | 832 | | SwiftThrowHelper.ThrowIfArgument( |
| | 51 | 833 | | _body != null, |
| | 51 | 834 | | operation, |
| | 51 | 835 | | "2D trigger colliders must be initialized without a SolidBody2D. Use InitializeWithNoBody for trigger volume |
| | 50 | 836 | | SwiftThrowHelper.ThrowIfArgument( |
| | 50 | 837 | | _compoundOwner != null, |
| | 50 | 838 | | operation, |
| | 50 | 839 | | "2D compound collider parts are not trigger identities. Set IsTrigger on the owning compound collider."); |
| | 50 | 840 | | } |
| | | 841 | | |
| | | 842 | | private void ThrowIfTriggerWouldAttachToBody(string operation) |
| | | 843 | | { |
| | 4658 | 844 | | SwiftThrowHelper.ThrowIfArgument( |
| | 4658 | 845 | | _isTrigger, |
| | 4658 | 846 | | operation, |
| | 4658 | 847 | | "2D trigger colliders must be initialized without a SolidBody2D. Use InitializeWithNoBody for trigger volume |
| | 4657 | 848 | | } |
| | | 849 | | |
| | | 850 | | private void ThrowIfLoadedTriggerHasBody(string operation) |
| | | 851 | | { |
| | 79 | 852 | | SwiftThrowHelper.ThrowIfArgument( |
| | 79 | 853 | | _isTrigger && _body != null, |
| | 79 | 854 | | operation, |
| | 79 | 855 | | "Loaded 2D trigger state is invalid for a collider attached to a SolidBody2D."); |
| | 77 | 856 | | } |
| | | 857 | | |
| | | 858 | | /// <inheritdoc/> |
| | | 859 | | public void RecordData(IChronicler chronicler) |
| | | 860 | | { |
| | 137 | 861 | | RecordValues.Look(chronicler, ref _isActive, "Active", true); |
| | 137 | 862 | | RecordValues.Look(chronicler, ref _isTrigger, "IsTrigger", false); |
| | 137 | 863 | | RecordValues.Look(chronicler, ref _layer, "Layer", new()); |
| | 137 | 864 | | RecordValues.Look(chronicler, ref _ignoredCollisionLayers, "IgnoredCollisionLayers", PhysicsLayerMask.None); |
| | 137 | 865 | | RecordValues.Look(chronicler, ref _material, "Material", PhysicsMaterial.Default); |
| | 137 | 866 | | RecordValues.Look(chronicler, ref _localOffset, "LocalOffset", Vector2d.Zero); |
| | 137 | 867 | | RecordValues.Look(chronicler, ref _mixedHalfThicknessOverride, "MixedHalfThicknessOverride"); |
| | 137 | 868 | | RecordShapeData(chronicler); |
| | | 869 | | |
| | 135 | 870 | | if (chronicler.Mode == SerializationMode.Loading) |
| | | 871 | | { |
| | 79 | 872 | | ThrowIfLoadedTriggerHasBody(nameof(IsTrigger)); |
| | 77 | 873 | | ApplyLoadedState(); |
| | | 874 | | } |
| | 133 | 875 | | } |
| | | 876 | | |
| | | 877 | | /// <summary>Records derived authored shape data.</summary> |
| | 1 | 878 | | protected virtual void RecordShapeData(IChronicler chronicler) { } |
| | | 879 | | |
| | | 880 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 24991 | 881 | | internal bool IgnoresCollisionLayer(PhysicsLayer layer) => _ignoredCollisionLayers.Includes(layer); |
| | | 882 | | |
| | | 883 | | private void ApplyLoadedState() |
| | | 884 | | { |
| | 77 | 885 | | _runtimeShapeState.MarkDirty(); |
| | 77 | 886 | | if (_context == null) |
| | 12 | 887 | | return; |
| | | 888 | | |
| | 65 | 889 | | RebuildRuntimeShapeState(); |
| | | 890 | | |
| | 65 | 891 | | if (_id < 0) |
| | 1 | 892 | | return; |
| | | 893 | | |
| | 64 | 894 | | if (!_isActive) |
| | | 895 | | { |
| | 10 | 896 | | if (IsPartitioned) |
| | 6 | 897 | | _context.Collisions2D.ClearPartitionedCollider(this, force: true); |
| | 10 | 898 | | MarkUnpartitioned(); |
| | 10 | 899 | | ClearPartitionCoordinates(); |
| | | 900 | | |
| | 10 | 901 | | if (IsMixedPartitioned) |
| | 2 | 902 | | _context.MixedCollisions.ClearPartitioned2DCollider(this, force: true); |
| | 10 | 903 | | MarkMixedUnpartitioned(); |
| | 10 | 904 | | ClearMixedPartitionCoordinates(); |
| | 10 | 905 | | return; |
| | | 906 | | } |
| | | 907 | | |
| | 54 | 908 | | _context.Collisions2D.RefreshColliderPartition(this); |
| | 54 | 909 | | if (_context.Settings.RuntimeMode.RunsMixedContacts()) |
| | 4 | 910 | | _context.MixedCollisions.Refresh2DColliderPartition(this); |
| | 54 | 911 | | } |
| | | 912 | | } |