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