| | | 1 | | //======================================================================= |
| | | 2 | | // LSCollider.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.Runtime.CompilerServices; |
| | | 10 | | using Chronicler; |
| | | 11 | | using FixedMathSharp; |
| | | 12 | | using FixedMathSharp.Geometry; |
| | | 13 | | using Gravitas.CollisionHandling; |
| | | 14 | | using Gravitas.Materials; |
| | | 15 | | using Gravitas.Queries; |
| | | 16 | | using Gravitas.Support; |
| | | 17 | | using GridForge.Grids; |
| | | 18 | | using GridForge.Spatial; |
| | | 19 | | using SwiftCollections; |
| | | 20 | | |
| | | 21 | | namespace Gravitas.Colliders; |
| | | 22 | | |
| | | 23 | | /// <summary> |
| | | 24 | | /// Common runtime type for the closed set of Gravitas-owned 3D collider |
| | | 25 | | /// shapes. Use <see cref="ColliderShapeDefinition.CreateCollider"/> when |
| | | 26 | | /// constructing from engine-adapter shape data. |
| | | 27 | | /// </summary> |
| | | 28 | | public abstract partial class LSCollider : IRecordable, IColliderHierarchyNode, IPhysicsColliderRegistryItem |
| | | 29 | | { |
| | 29516 | 30 | | internal LSCollider() { } |
| | | 31 | | |
| | | 32 | | #region Fields and Properties |
| | | 33 | | |
| | | 34 | | /// <summary>Controls collider debug diagnostics.</summary> |
| | | 35 | | protected bool _debug; |
| | | 36 | | /// <summary>Controls collider shape debug drawing.</summary> |
| | | 37 | | protected bool _drawShape; |
| | | 38 | | private bool _drawPartitions; |
| | | 39 | | private bool _drawBoundingBox; |
| | | 40 | | |
| | 14758 | 41 | | private bool _active = true; |
| | | 42 | | private bool _deactivationInProgress; |
| | | 43 | | /// <summary>Gets or sets whether this collider participates in runtime physics.</summary> |
| | | 44 | | public bool IsActive |
| | | 45 | | { |
| | | 46 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 6377880 | 47 | | get => _active; |
| | | 48 | | set |
| | | 49 | | { |
| | 20 | 50 | | ThrowIfCompoundPartLifecycle(nameof(IsActive)); |
| | | 51 | | |
| | 20 | 52 | | if (_active == value) |
| | 2 | 53 | | return; |
| | | 54 | | |
| | 18 | 55 | | if (_context == null || _id < 0) |
| | | 56 | | { |
| | 2 | 57 | | _active = value; |
| | 2 | 58 | | return; |
| | | 59 | | } |
| | | 60 | | |
| | 16 | 61 | | if (value) |
| | | 62 | | { |
| | 5 | 63 | | RebuildRuntimeShapeState(); |
| | 5 | 64 | | _active = true; |
| | 5 | 65 | | InitialPartition(); |
| | 5 | 66 | | if (_context.Settings.RuntimeMode.RunsMixedContacts()) |
| | 3 | 67 | | _context.MixedCollisions.Refresh3DColliderPartition(this); |
| | 5 | 68 | | return; |
| | | 69 | | } |
| | | 70 | | |
| | 11 | 71 | | _active = false; |
| | 13 | 72 | | if (this is LSMeshCollider mesh) mesh.StaticSurface?.Remove(mesh); |
| | 11 | 73 | | if (IsPartitioned) |
| | 10 | 74 | | _context.Collisions.ClearPartitionedObject(this, force: true); |
| | | 75 | | |
| | 11 | 76 | | if (IsMixedPartitioned) |
| | | 77 | | { |
| | 3 | 78 | | _context.MixedCollisions.ClearPartitioned3DCollider(this, force: true); |
| | 3 | 79 | | MarkMixedUnpartitioned(); |
| | 3 | 80 | | ClearMixedPartitionCoordinates(); |
| | | 81 | | } |
| | 11 | 82 | | } |
| | | 83 | | } |
| | | 84 | | |
| | | 85 | | private bool _isTrigger; |
| | | 86 | | |
| | | 87 | | /// <summary> |
| | | 88 | | /// Gets or sets whether this bodyless collider is a trigger volume. |
| | | 89 | | /// Trigger volumes raise trigger enter/stay/exit callbacks for valid overlap |
| | | 90 | | /// pairs and never apply physical response. |
| | | 91 | | /// </summary> |
| | | 92 | | public bool IsTrigger |
| | | 93 | | { |
| | | 94 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 1789160 | 95 | | get => _isTrigger; |
| | 104 | 96 | | set => SetTrigger(value); |
| | | 97 | | } |
| | | 98 | | |
| | 14758 | 99 | | private int _id = -1; |
| | | 100 | | /// <summary>Gets the context-local runtime collider identifier.</summary> |
| | 49674104 | 101 | | public int Id => _id; |
| | | 102 | | |
| | 14758 | 103 | | private int _serviceIndex = -1; |
| | | 104 | | |
| | 14758 | 105 | | private int _replayOrder = -1; |
| | | 106 | | |
| | 14758 | 107 | | private int _replayOrdinal = -1; |
| | 20978 | 108 | | internal int ReplayOrdinal => _replayOrdinal; |
| | | 109 | | |
| | | 110 | | private long _lifetimeVersion; |
| | 1232319 | 111 | | internal long LifetimeVersion => _lifetimeVersion; |
| | | 112 | | |
| | 6040065 | 113 | | internal bool IsDeactivationInProgress => _deactivationInProgress; |
| | | 114 | | |
| | 14758 | 115 | | private int _serviceRefreshIndex = -1; |
| | 7966 | 116 | | internal int ServiceRefreshIndex => _serviceRefreshIndex; |
| | | 117 | | |
| | | 118 | | private IMatterAgent? _agent; |
| | 361573 | 119 | | internal IMatterAgent? AgentOrNull => _agent; |
| | | 120 | | |
| | | 121 | | private GravitasWorldContext? _context; |
| | | 122 | | /// <summary>Gets the world context to which this collider is bound.</summary> |
| | | 123 | | public GravitasWorldContext Context |
| | | 124 | | { |
| | | 125 | | get |
| | | 126 | | { |
| | 1131035 | 127 | | ChronicleGuard.ThrowIfTrue( |
| | 1131035 | 128 | | _context == null, |
| | 1131035 | 129 | | nameof(LSCollider), |
| | 1131035 | 130 | | "Collider is not bound to a GravitasWorldContext."); |
| | 1131033 | 131 | | return _context!; |
| | | 132 | | } |
| | | 133 | | } |
| | | 134 | | |
| | | 135 | | private SolidBody? _body; |
| | | 136 | | /// <summary>Gets the owning 3D body, or <see langword="null"/> for a bodyless collider.</summary> |
| | 13054805 | 137 | | public SolidBody? Body => _body; |
| | | 138 | | |
| | | 139 | | /// <summary> |
| | | 140 | | /// Gets whether this collider is bodyless or belongs to an explicit static |
| | | 141 | | /// body role. |
| | | 142 | | /// </summary> |
| | 682677 | 143 | | public bool IsStatic => _body == null || _body.IsStatic; |
| | | 144 | | |
| | 13328 | 145 | | internal bool RequiresServiceSideRefresh => _body == null || _body.DynamicId < 0; |
| | | 146 | | |
| | | 147 | | private LSCompoundCollider? _compoundOwner; |
| | 14758 | 148 | | private FixedQuaternion _compoundLocalRotation = FixedQuaternion.Identity; |
| | 14758 | 149 | | private Vector3d _compoundLocalScale = Vector3d.One; |
| | | 150 | | |
| | 14758 | 151 | | private readonly ColliderRuntimeShapeState<ColliderShapeSnapshot> _runtimeShapeState = new(); |
| | | 152 | | private ColliderPartitionState _partitionState; |
| | | 153 | | private ColliderPartitionState _mixedPartitionState; |
| | | 154 | | private ColliderQueryState _queryState; |
| | | 155 | | private ColliderPairState<CollisionPair> _pairState; |
| | | 156 | | private ColliderHierarchyState _hierarchyState; |
| | | 157 | | |
| | 96166 | 158 | | internal uint RuntimeShapeVersion => _runtimeShapeState.RuntimeVersion; |
| | | 159 | | |
| | 14203 | 160 | | internal bool HasHostBinding => _agent != null; |
| | | 161 | | |
| | 126510 | 162 | | internal LSCompoundCollider? CompoundOwner => _compoundOwner; |
| | | 163 | | |
| | | 164 | | /// <summary>Gets the runtime world position, or sets it for a bodyless collider.</summary> |
| | | 165 | | public Vector3d Position |
| | | 166 | | { |
| | 11425 | 167 | | get => _compoundOwner?.Center |
| | 11425 | 168 | | ?? Body?.Position3d |
| | 11425 | 169 | | ?? _agent?.Transform.WorldPosition |
| | 11425 | 170 | | ?? throw new InvalidOperationException("Collider has no body or static transform."); |
| | | 171 | | set |
| | | 172 | | { |
| | 331 | 173 | | ChronicleGuard.ThrowIfTrue( |
| | 331 | 174 | | _compoundOwner != null, |
| | 331 | 175 | | nameof(Position), |
| | 331 | 176 | | "Compound collider parts inherit position from their owning compound collider."); |
| | 330 | 177 | | ChronicleGuard.ThrowIfTrue( |
| | 330 | 178 | | _agent == null, |
| | 330 | 179 | | nameof(Position), |
| | 330 | 180 | | "Collider is not bound to a static transform."); |
| | 329 | 181 | | ChronicleGuard.ThrowIfTrue( |
| | 329 | 182 | | _body != null, |
| | 329 | 183 | | nameof(Position), |
| | 329 | 184 | | "Body-owned collider positions must be changed through their SolidBody."); |
| | | 185 | | |
| | 328 | 186 | | if (_agent.Transform.WorldPosition == value) |
| | 3 | 187 | | return; |
| | 325 | 188 | | SetBodylessWorldPose(value, default, setRotation: false, nameof(Position)); |
| | 322 | 189 | | } |
| | | 190 | | } |
| | | 191 | | |
| | | 192 | | /// <summary>Gets the committed world rotation, or sets it for a bodyless collider.</summary> |
| | | 193 | | public FixedQuaternion Rotation |
| | | 194 | | { |
| | 798549 | 195 | | get => _hasCommittedShape |
| | 798549 | 196 | | ? _committedRotation |
| | 798549 | 197 | | : throw new InvalidOperationException( |
| | 798549 | 198 | | "Collider has no committed runtime shape."); |
| | | 199 | | set |
| | | 200 | | { |
| | 13 | 201 | | ChronicleGuard.ThrowIfTrue( |
| | 13 | 202 | | _compoundOwner != null, |
| | 13 | 203 | | nameof(Rotation), |
| | 13 | 204 | | "Compound collider parts inherit rotation from their owning compound collider."); |
| | 12 | 205 | | ChronicleGuard.ThrowIfTrue( |
| | 12 | 206 | | _agent == null, |
| | 12 | 207 | | nameof(Rotation), |
| | 12 | 208 | | "Collider is not bound to a static transform."); |
| | 11 | 209 | | ChronicleGuard.ThrowIfTrue( |
| | 11 | 210 | | _body != null, |
| | 11 | 211 | | nameof(Rotation), |
| | 11 | 212 | | "Body-owned collider rotations must be changed through their SolidBody."); |
| | | 213 | | |
| | 10 | 214 | | if (_agent.Transform.WorldRotation == value) |
| | 1 | 215 | | return; |
| | 9 | 216 | | SetBodylessWorldPose(default, value, setRotation: true, nameof(Rotation)); |
| | 7 | 217 | | } |
| | | 218 | | } |
| | | 219 | | |
| | | 220 | | /// <summary>Gets the owning body's linear velocity, or zero for a bodyless collider.</summary> |
| | 3734 | 221 | | public Vector3d Velocity => Body?.LinearVelocity ?? Vector3d.Zero; |
| | | 222 | | |
| | | 223 | | /// <summary>Gets the GridForge world owned by this collider's context.</summary> |
| | 6 | 224 | | public GridWorld World => Context.World; |
| | | 225 | | |
| | | 226 | | /// <summary>Gets the transform that supplies this collider's runtime pose.</summary> |
| | 1213 | 227 | | public FixedTransform Transform => _compoundOwner?.Transform |
| | 1213 | 228 | | ?? Body?.PositionTransform |
| | 1213 | 229 | | ?? _agent?.Transform |
| | 1213 | 230 | | ?? throw new InvalidOperationException("Collider has no body or static transform."); |
| | | 231 | | |
| | | 232 | | private PhysicsLayer _layer = new(); |
| | 14758 | 233 | | private PhysicsLayerMask _ignoredCollisionLayers = PhysicsLayerMask.None; |
| | | 234 | | |
| | | 235 | | /// <summary>Gets or sets the single physics layer used for collision and query filtering.</summary> |
| | | 236 | | public PhysicsLayer Layer |
| | | 237 | | { |
| | | 238 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 11956207 | 239 | | get => _layer; |
| | | 240 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 241 | | set |
| | | 242 | | { |
| | 273 | 243 | | if (_layer == value) return; |
| | 171 | 244 | | _layer = value; |
| | 171 | 245 | | MarkContactDependencyChanged(); |
| | 171 | 246 | | _body?.Wake(); |
| | 9 | 247 | | } |
| | | 248 | | } |
| | | 249 | | |
| | | 250 | | /// <summary> |
| | | 251 | | /// Gets or sets physical layers this collider ignores for collider-to-collider |
| | | 252 | | /// interactions. Public queries continue to use the caller's query mask. |
| | | 253 | | /// </summary> |
| | | 254 | | public PhysicsLayerMask IgnoredCollisionLayers |
| | | 255 | | { |
| | | 256 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 10 | 257 | | get => _ignoredCollisionLayers; |
| | | 258 | | set |
| | | 259 | | { |
| | 47 | 260 | | if (_ignoredCollisionLayers == value) |
| | 2 | 261 | | return; |
| | | 262 | | |
| | 45 | 263 | | _ignoredCollisionLayers = value; |
| | 45 | 264 | | MarkContactDependencyChanged(); |
| | 45 | 265 | | _body?.Wake(); |
| | 32 | 266 | | } |
| | | 267 | | } |
| | | 268 | | |
| | 14758 | 269 | | private PhysicsMaterial _material = PhysicsMaterial.Default; |
| | | 270 | | |
| | | 271 | | /// <summary> |
| | | 272 | | /// Gets or sets the deterministic surface material used by collision |
| | | 273 | | /// response for this collider. |
| | | 274 | | /// </summary> |
| | | 275 | | public PhysicsMaterial Material |
| | | 276 | | { |
| | | 277 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 526496 | 278 | | get => _material; |
| | | 279 | | set |
| | | 280 | | { |
| | 5876 | 281 | | if (_material == value) |
| | 2141 | 282 | | return; |
| | | 283 | | |
| | 3735 | 284 | | _material = value; |
| | 3735 | 285 | | MarkContactDependencyChanged(surfaceNeighborsChanged: false); |
| | 3735 | 286 | | OnMaterialChanged(); |
| | 3735 | 287 | | _body?.Wake(); |
| | 680 | 288 | | } |
| | | 289 | | } |
| | | 290 | | |
| | | 291 | | /// <summary>Gets whether this collider currently has 3D broad-phase partition membership.</summary> |
| | 299593 | 292 | | public bool IsPartitioned => _partitionState.IsPartitioned; |
| | | 293 | | |
| | | 294 | | /// <summary> |
| | | 295 | | /// Used for preventing culling for the first frame this object is added to a new partition node. |
| | | 296 | | /// </summary> |
| | | 297 | | public bool PartitionChanged |
| | | 298 | | { |
| | | 299 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 3863 | 300 | | get => _partitionState.PartitionChanged; |
| | | 301 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 289272 | 302 | | set => _partitionState.PartitionChanged = value; |
| | | 303 | | } |
| | | 304 | | |
| | 472947 | 305 | | internal uint BroadPhaseVersion => _partitionState.BroadPhaseVersion; |
| | | 306 | | |
| | | 307 | | /// <summary> |
| | | 308 | | /// Center of collider in local space, used for calculating bounds and offsets. Should be set in the Setup method of |
| | | 309 | | /// </summary> |
| | | 310 | | protected Vector3d _offset; |
| | | 311 | | |
| | | 312 | | /// <summary> |
| | | 313 | | /// Gets or sets the unscaled local center offset used by bounds and shape-derived state. |
| | | 314 | | /// </summary> |
| | | 315 | | public Vector3d LocalOffset |
| | | 316 | | { |
| | | 317 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 125520 | 318 | | get => _offset; |
| | | 319 | | set |
| | | 320 | | { |
| | 969 | 321 | | if (_offset == value) |
| | 367 | 322 | | return; |
| | | 323 | | |
| | 602 | 324 | | ValidateGeometryMutation(value, _size, _radius); |
| | 596 | 325 | | _offset = value; |
| | 596 | 326 | | MarkShapeDirty(); |
| | 596 | 327 | | } |
| | | 328 | | } |
| | | 329 | | |
| | | 330 | | /// <summary>Gets the runtime 3D shape family.</summary> |
| | | 331 | | public abstract ColliderType Shape { get; } |
| | | 332 | | /// <summary>Gets the deterministic narrow-phase ordering priority.</summary> |
| | | 333 | | public abstract int Priority { get; } |
| | | 334 | | |
| | | 335 | | /// <summary>Stores the unscaled radius for radius-based collider shapes.</summary> |
| | 14758 | 336 | | protected Fixed64 _radius = Fixed64.Half; |
| | | 337 | | |
| | | 338 | | /// <summary> |
| | | 339 | | /// Gets or sets the unscaled shape radius used by radius-based colliders. |
| | | 340 | | /// </summary> |
| | | 341 | | public Fixed64 Radius |
| | | 342 | | { |
| | | 343 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 338 | 344 | | get => _radius; |
| | | 345 | | set |
| | | 346 | | { |
| | 3142 | 347 | | ChronicleGuard.ThrowIfArgument( |
| | 3142 | 348 | | value <= Fixed64.Zero, |
| | 3142 | 349 | | nameof(value), |
| | 3142 | 350 | | "Collider radius must be greater than zero."); |
| | | 351 | | |
| | 3142 | 352 | | if (_radius == value) |
| | 1430 | 353 | | return; |
| | | 354 | | |
| | 1712 | 355 | | ValidateGeometryMutation(_offset, _size, value); |
| | 1706 | 356 | | _radius = value; |
| | 1706 | 357 | | OnRadiusChanged(); |
| | 1706 | 358 | | MarkShapeDirty(); |
| | 1706 | 359 | | } |
| | | 360 | | } |
| | | 361 | | |
| | | 362 | | /// <summary> |
| | | 363 | | /// Gets the bounding circle radius. |
| | | 364 | | /// For boxes, this is half of the diagonal length of the cube |
| | | 365 | | /// </summary> |
| | | 366 | | /// <value>The radius.</value> |
| | 5 | 367 | | public virtual Fixed64 ScaledRadius => GetCurrentScaledRadius(); |
| | | 368 | | |
| | | 369 | | /// <summary>Gets the squared scaled bounding radius.</summary> |
| | 21 | 370 | | public Fixed64 ScaledRadiusSqr => ScaledRadius * ScaledRadius; |
| | | 371 | | |
| | | 372 | | /// <summary>Stores the unscaled local collider size.</summary> |
| | 14758 | 373 | | protected Vector3d _size = Vector3d.One; |
| | | 374 | | |
| | | 375 | | /// <summary> |
| | | 376 | | /// Gets or sets the unscaled local size used when rebuilding collider bounds. |
| | | 377 | | /// </summary> |
| | | 378 | | public Vector3d Size |
| | | 379 | | { |
| | | 380 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 184 | 381 | | get => _size; |
| | | 382 | | set |
| | | 383 | | { |
| | 4096 | 384 | | ValidateSize(value); |
| | 4090 | 385 | | Vector3d normalizedSize = NormalizeSize(value); |
| | 4090 | 386 | | if (_size == normalizedSize) |
| | 1381 | 387 | | return; |
| | | 388 | | |
| | 2709 | 389 | | ValidateGeometryMutation(_offset, normalizedSize, _radius); |
| | 2705 | 390 | | _size = normalizedSize; |
| | 2705 | 391 | | MarkShapeDirty(); |
| | 2705 | 392 | | } |
| | | 393 | | } |
| | | 394 | | |
| | | 395 | | /// <summary>Gets the committed shape-specific measure used as the frontal-area fallback.</summary> |
| | | 396 | | public virtual Fixed64 Area { get; protected set; } = Fixed64.Zero; |
| | | 397 | | |
| | | 398 | | #region Grid & Partition Bounds |
| | | 399 | | |
| | | 400 | | /// <summary>Gets the combined owner and compound-part scale.</summary> |
| | | 401 | | public virtual Vector3d LocalScale |
| | | 402 | | { |
| | | 403 | | get |
| | | 404 | | { |
| | 3 | 405 | | bool representable = TryGetLocalScale(out Vector3d scale); |
| | 3 | 406 | | ChronicleGuard.ThrowIfTrue( |
| | 3 | 407 | | !representable, |
| | 3 | 408 | | nameof(LocalScale), |
| | 3 | 409 | | "The combined 3D collider scale is outside the Fixed64 coordinate domain."); |
| | 2 | 410 | | return scale; |
| | | 411 | | } |
| | | 412 | | } |
| | | 413 | | |
| | | 414 | | /// <summary> |
| | | 415 | | /// Attempts to materialize the combined owner and compound-part scale. |
| | | 416 | | /// Shape preparation retains those factors separately and does not require |
| | | 417 | | /// their product to be representable. |
| | | 418 | | /// </summary> |
| | | 419 | | public bool TryGetLocalScale(out Vector3d scale) |
| | | 420 | | { |
| | 6 | 421 | | GetCurrentShapeScales( |
| | 6 | 422 | | out Vector3d ownerScale, |
| | 6 | 423 | | out Vector3d partScale); |
| | | 424 | | |
| | 6 | 425 | | bool representable = Fixed64.TryMultiplyDivide( |
| | 6 | 426 | | ownerScale.X, |
| | 6 | 427 | | partScale.X, |
| | 6 | 428 | | Fixed64.One, |
| | 6 | 429 | | out Fixed64 x) |
| | 6 | 430 | | & Fixed64.TryMultiplyDivide( |
| | 6 | 431 | | ownerScale.Y, |
| | 6 | 432 | | partScale.Y, |
| | 6 | 433 | | Fixed64.One, |
| | 6 | 434 | | out Fixed64 y) |
| | 6 | 435 | | & Fixed64.TryMultiplyDivide( |
| | 6 | 436 | | ownerScale.Z, |
| | 6 | 437 | | partScale.Z, |
| | 6 | 438 | | Fixed64.One, |
| | 6 | 439 | | out Fixed64 z); |
| | 6 | 440 | | scale = representable ? new Vector3d(x, y, z) : default; |
| | 6 | 441 | | return representable; |
| | | 442 | | } |
| | | 443 | | |
| | | 444 | | /// <summary> |
| | | 445 | | /// Gets the component-scaled local offset. |
| | | 446 | | /// </summary> |
| | | 447 | | /// <exception cref="InvalidOperationException"> |
| | | 448 | | /// The scaled offset itself is outside the Fixed64 coordinate domain. The |
| | | 449 | | /// collider may still have a representable world center through exact |
| | | 450 | | /// transform cancellation. |
| | | 451 | | /// </exception> |
| | 12 | 452 | | public Vector3d ScaledOffset => GetCurrentScaledOffset(); |
| | | 453 | | |
| | | 454 | | /// <summary> |
| | | 455 | | /// Attempts to materialize the component-scaled local offset independently |
| | | 456 | | /// from the collider's complete world transform. |
| | | 457 | | /// </summary> |
| | | 458 | | public bool TryGetScaledOffset(out Vector3d scaledOffset) => |
| | 2 | 459 | | TryGetCurrentScaledOffset(out scaledOffset); |
| | | 460 | | |
| | | 461 | | /// <summary> |
| | | 462 | | /// Bodies position in world space + collider offset (center) value |
| | | 463 | | /// </summary> |
| | 2348113 | 464 | | public Vector3d Center => _hasCommittedShape |
| | 2348113 | 465 | | ? _committedCenter |
| | 2348113 | 466 | | : throw new InvalidOperationException( |
| | 2348113 | 467 | | "Collider has no committed runtime shape."); |
| | | 468 | | |
| | | 469 | | /// <summary>Stores the committed world-space axis-aligned bounds.</summary> |
| | | 470 | | protected FixedBoundBox _bounds; |
| | | 471 | | /// <summary>Gets the committed world-space axis-aligned bounds.</summary> |
| | 442040 | 472 | | public FixedBoundBox Bounds => _bounds; |
| | | 473 | | /// <summary>Gets the minimum corner of <see cref="Bounds"/>.</summary> |
| | 26972895 | 474 | | public Vector3d BoundsMin => _bounds.Min; |
| | | 475 | | /// <summary>Gets the maximum corner of <see cref="Bounds"/>.</summary> |
| | 25904472 | 476 | | public Vector3d BoundsMax => _bounds.Max; |
| | | 477 | | |
| | 3163 | 478 | | internal SwiftList<WorldVoxelIndex>? PartitionCoordinates => _partitionState.Coordinates; |
| | | 479 | | |
| | 149887 | 480 | | internal int PartitionKind => _partitionState.LastPartitionKind; |
| | | 481 | | |
| | 19516 | 482 | | internal bool IsMixedPartitioned => _mixedPartitionState.IsPartitioned; |
| | | 483 | | |
| | 11011 | 484 | | internal SwiftList<WorldVoxelIndex>? MixedPartitionCoordinates => _mixedPartitionState.Coordinates; |
| | | 485 | | |
| | 26753 | 486 | | internal int MixedPartitionKind => _mixedPartitionState.LastPartitionKind; |
| | | 487 | | |
| | | 488 | | #endregion |
| | | 489 | | |
| | | 490 | | internal uint RaycastVersion |
| | | 491 | | { |
| | | 492 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 438752 | 493 | | get => _queryState.RaycastVersion; |
| | | 494 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 25454 | 495 | | set => _queryState.RaycastVersion = value; |
| | | 496 | | } |
| | | 497 | | |
| | | 498 | | internal uint CircleQueryVersion |
| | | 499 | | { |
| | | 500 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 38518 | 501 | | get => _queryState.CircleQueryVersion; |
| | | 502 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 1040 | 503 | | set => _queryState.CircleQueryVersion = value; |
| | | 504 | | } |
| | | 505 | | |
| | | 506 | | /// <summary>Gets whether this collider has a hierarchy parent.</summary> |
| | 2 | 507 | | public bool IsChild => _hierarchyState.IsChild; |
| | | 508 | | /// <summary>Gets whether this collider is configured as or currently owns a hierarchy parent.</summary> |
| | 4 | 509 | | public bool IsParent => _hierarchyState.IsParent; |
| | | 510 | | /// <summary>Gets the context-local identifier of the parent collider, or -1 when unparented.</summary> |
| | 9 | 511 | | public int ParentId => ParentKey.Id; |
| | | 512 | | /// <summary>Gets the 3D parent collider, when the parent belongs to the 3D runtime.</summary> |
| | 8 | 513 | | public LSCollider? Parent3D => _hierarchyState.Parent as LSCollider; |
| | | 514 | | /// <summary>Gets the 2D parent collider, when the parent belongs to the 2D runtime.</summary> |
| | 4 | 515 | | public LSCollider2D? Parent2D => _hierarchyState.Parent as LSCollider2D; |
| | 136799 | 516 | | internal LSCollider? TopParent3D => _hierarchyState.TopParent as LSCollider; |
| | 1 | 517 | | internal LSCollider2D? TopParent2D => _hierarchyState.TopParent as LSCollider2D; |
| | 11101 | 518 | | internal int HierarchyChildCount => _hierarchyState.ChildCount; |
| | 5353081 | 519 | | internal ColliderHierarchyKey HierarchyKey => Id >= 0 ? ColliderHierarchyKey.Create3D(Id) : ColliderHierarchyKey.Non |
| | 11104 | 520 | | internal ColliderHierarchyKey ParentKey => _hierarchyState.ParentKey; |
| | 11092 | 521 | | internal ColliderHierarchyKey TopParentKey => _hierarchyState.TopParentKey; |
| | 1 | 522 | | internal ColliderHierarchyState HierarchyState => _hierarchyState; |
| | 168 | 523 | | ColliderHierarchyKey IColliderHierarchyNode.HierarchyKey => HierarchyKey; |
| | 54 | 524 | | IColliderHierarchyNode? IColliderHierarchyNode.HierarchyParent => _hierarchyState.Parent; |
| | | 525 | | |
| | | 526 | | #endregion |
| | | 527 | | |
| | | 528 | | internal void Initialize(SolidBody body) |
| | | 529 | | { |
| | 8417 | 530 | | _body = body; |
| | 8417 | 531 | | InitCore(body.Agent); |
| | 8417 | 532 | | } |
| | | 533 | | |
| | | 534 | | /// <summary>Binds and registers this collider as a bodyless runtime collider.</summary> |
| | | 535 | | public void InitializeWithNoBody(IMatterAgent agent) |
| | | 536 | | { |
| | 4866 | 537 | | ThrowIfCompoundPartLifecycle(nameof(InitializeWithNoBody)); |
| | 4866 | 538 | | ChronicleGuard.ThrowIfNull(agent, nameof(agent)); |
| | 4866 | 539 | | ChronicleGuard.ThrowIfArgument( |
| | 4866 | 540 | | Id >= 0 || (HasHostBinding && !ReferenceEquals(_agent, agent)), |
| | 4866 | 541 | | nameof(agent), |
| | 4866 | 542 | | "Collider is already registered or bound to another host agent."); |
| | 4864 | 543 | | PreflightInitialization(agent); |
| | 4824 | 544 | | InitCore(agent); |
| | 4824 | 545 | | } |
| | | 546 | | |
| | | 547 | | internal void PreflightBodyInitialization( |
| | | 548 | | SolidBody body, |
| | | 549 | | Vector3d requestedPosition, |
| | | 550 | | FixedQuaternion requestedRotation) |
| | | 551 | | { |
| | 8426 | 552 | | ThrowIfCompoundPartLifecycle(nameof(Initialize)); |
| | 8426 | 553 | | ChronicleGuard.ThrowIfNull(body, nameof(body)); |
| | 8426 | 554 | | ThrowIfTriggerWouldAttachToBody(nameof(Initialize)); |
| | 8425 | 555 | | PreflightInitialization( |
| | 8425 | 556 | | body.Agent, |
| | 8425 | 557 | | useRequestedPose: true, |
| | 8425 | 558 | | requestedPosition, |
| | 8425 | 559 | | requestedRotation); |
| | 8416 | 560 | | } |
| | | 561 | | |
| | | 562 | | private void PreflightInitialization( |
| | | 563 | | IMatterAgent agent, |
| | | 564 | | bool useRequestedPose = false, |
| | | 565 | | Vector3d requestedPosition = default, |
| | | 566 | | FixedQuaternion requestedRotation = default) |
| | | 567 | | { |
| | 13289 | 568 | | ChronicleGuard.ThrowIfNull(agent, nameof(agent)); |
| | 13289 | 569 | | OnBeforeInitialize(agent); |
| | 13289 | 570 | | PrepareStandaloneInitialization( |
| | 13289 | 571 | | agent, |
| | 13289 | 572 | | useRequestedPose, |
| | 13289 | 573 | | requestedPosition, |
| | 13289 | 574 | | requestedRotation); |
| | 13240 | 575 | | } |
| | | 576 | | |
| | | 577 | | private void InitCore(IMatterAgent agent) |
| | | 578 | | { |
| | 13241 | 579 | | _lifetimeVersion++; |
| | | 580 | | |
| | 13241 | 581 | | _queryState.Reset(); |
| | | 582 | | |
| | 13241 | 583 | | _agent = agent; |
| | 13241 | 584 | | _active = true; |
| | 13241 | 585 | | _deactivationInProgress = false; |
| | 13241 | 586 | | BindContext(agent.Context); |
| | 13241 | 587 | | ContactDependencyVersion = 0; |
| | 13241 | 588 | | PublishPreparedShape(); |
| | 13241 | 589 | | Context.Physics.AssimilateCollider(this); |
| | 13241 | 590 | | _hierarchyState.Initialize(_agent.IsParent); |
| | 13241 | 591 | | OnInitialize(); |
| | | 592 | | |
| | 13241 | 593 | | _partitionState.SetPreviousGridBounds(Vector3d.Zero, Vector3d.Zero); |
| | | 594 | | |
| | 13241 | 595 | | InitialPartition(); |
| | 13241 | 596 | | } |
| | | 597 | | |
| | | 598 | | /// <summary>Validates or prepares derived shape state before runtime registration.</summary> |
| | 13289 | 599 | | protected virtual void OnBeforeInitialize(IMatterAgent agent) { } |
| | | 600 | | |
| | | 601 | | /// <summary>Runs derived initialization after runtime registration.</summary> |
| | 13241 | 602 | | protected virtual void OnInitialize() { } |
| | | 603 | | |
| | | 604 | | internal void ValidateCurrentRuntimeTransform() |
| | | 605 | | { |
| | 691 | 606 | | ColliderShapeSnapshot snapshot = CaptureShapeSnapshot(); |
| | 688 | 607 | | PrepareRuntimeShape( |
| | 688 | 608 | | snapshot, |
| | 688 | 609 | | includeBodyAttachment: true); |
| | 688 | 610 | | } |
| | | 611 | | |
| | | 612 | | private void InitialPartition() |
| | | 613 | | { |
| | 13857 | 614 | | RebuildRuntimeShapeState(); |
| | 13856 | 615 | | RepartitionCommittedShape(); |
| | 13856 | 616 | | } |
| | | 617 | | |
| | | 618 | | internal void RepartitionCommittedShape() |
| | | 619 | | { |
| | 13872 | 620 | | if (!IsActive) |
| | 1 | 621 | | return; |
| | | 622 | | |
| | 13871 | 623 | | _partitionState.Coordinates ??= new(); |
| | 13871 | 624 | | SwiftList<WorldVoxelIndex> partitionCoordinates = _partitionState.Coordinates!; |
| | 13871 | 625 | | Context.Collisions.PartitionObject(this, ref partitionCoordinates); |
| | 13871 | 626 | | _partitionState.Coordinates = partitionCoordinates; |
| | 13871 | 627 | | SetPreviousGridBounds(); |
| | 13871 | 628 | | _partitionState.MarkPartitioned(); |
| | 13871 | 629 | | MarkBroadPhaseChanged(); |
| | 13871 | 630 | | } |
| | | 631 | | |
| | | 632 | | /// <summary>Refreshes bodyless collider shape and broad-phase state for one simulation step.</summary> |
| | 236357 | 633 | | public void Simulate() => Simulate(skipUnchangedMassProperties: false); |
| | | 634 | | |
| | | 635 | | internal void Simulate(bool skipUnchangedMassProperties) |
| | | 636 | | { |
| | 289718 | 637 | | ThrowIfCompoundPartLifecycle(nameof(Simulate)); |
| | 289717 | 638 | | if (!IsActive) |
| | 3 | 639 | | return; |
| | | 640 | | |
| | 289714 | 641 | | if (!IsPartitioned) |
| | | 642 | | { |
| | 461 | 643 | | InitialPartition(); |
| | 460 | 644 | | return; |
| | | 645 | | } |
| | | 646 | | |
| | 289253 | 647 | | bool refreshPartition = RebuildRuntimeShapeState(skipUnchangedMassProperties: skipUnchangedMassProperties) |
| | 289253 | 648 | | || Context.Collisions.IsPartitionRefreshRequired(this); |
| | 289244 | 649 | | PartitionChanged = false; |
| | 289244 | 650 | | if (refreshPartition) |
| | 129281 | 651 | | UpdatePartition(); |
| | 289244 | 652 | | } |
| | | 653 | | |
| | | 654 | | private void UpdatePartition() |
| | | 655 | | { |
| | 129541 | 656 | | MarkBroadPhaseChanged(); |
| | | 657 | | |
| | 129541 | 658 | | if (!Context.Collisions.IsPartitionRefreshRequired(this)) |
| | 59 | 659 | | return; |
| | | 660 | | |
| | 129482 | 661 | | SwiftList<WorldVoxelIndex> partitionCoordinates = _partitionState.Coordinates!; |
| | 129482 | 662 | | bool partitioned = Context.Collisions.PartitionObject(this, ref partitionCoordinates); |
| | 129482 | 663 | | _partitionState.Coordinates = partitionCoordinates; |
| | 129482 | 664 | | if (!partitioned) |
| | 817 | 665 | | return; |
| | | 666 | | |
| | 128665 | 667 | | SetPreviousGridBounds(); |
| | | 668 | | |
| | 128665 | 669 | | _partitionState.MarkPartitioned(); |
| | 128665 | 670 | | } |
| | | 671 | | |
| | | 672 | | private void SetBodylessWorldPose( |
| | | 673 | | Vector3d position, |
| | | 674 | | FixedQuaternion rotation, |
| | | 675 | | bool setRotation, |
| | | 676 | | string operation) |
| | | 677 | | { |
| | 334 | 678 | | GravitasWorldContext context = _context!; |
| | 334 | 679 | | context.ThrowIfFixedStepMutationNotAllowed(); |
| | 333 | 680 | | FixedTransform transform = _agent!.Transform; |
| | 333 | 681 | | Vector3d localPosition = transform.LocalPosition; |
| | 333 | 682 | | FixedQuaternion localRotation = transform.LocalRotation; |
| | 333 | 683 | | bool transformed = setRotation |
| | 333 | 684 | | ? transform.TrySetWorldPose(transform.WorldPosition, rotation) |
| | 333 | 685 | | : transform.TrySetWorldPosition(position); |
| | 333 | 686 | | ChronicleGuard.ThrowIfTrue( |
| | 333 | 687 | | !transformed, |
| | 333 | 688 | | operation, |
| | 333 | 689 | | setRotation |
| | 333 | 690 | | ? "Static collider transform cannot represent the requested world rotation." |
| | 333 | 691 | | : "Static collider transform cannot represent the requested world position."); |
| | | 692 | | |
| | | 693 | | try |
| | | 694 | | { |
| | 331 | 695 | | RebuildRuntimeShapeState(); |
| | 329 | 696 | | } |
| | 2 | 697 | | catch |
| | | 698 | | { |
| | 2 | 699 | | transform.LocalPosition = localPosition; |
| | 2 | 700 | | transform.LocalRotation = localRotation; |
| | 2 | 701 | | throw; |
| | | 702 | | } |
| | | 703 | | |
| | 329 | 704 | | if (!IsActive) |
| | 1 | 705 | | return; |
| | | 706 | | |
| | 328 | 707 | | if (IsPartitioned) |
| | 182 | 708 | | UpdatePartition(); |
| | | 709 | | else |
| | 146 | 710 | | InitialPartition(); |
| | | 711 | | |
| | 328 | 712 | | if (context.Settings.RuntimeMode.RunsMixedContacts()) |
| | 8 | 713 | | context.MixedCollisions.Refresh3DColliderPartition(this); |
| | 328 | 714 | | } |
| | | 715 | | |
| | | 716 | | /// <summary>Assigns a 3D hierarchy parent used for collision exclusion.</summary> |
| | | 717 | | public void SetParent(LSCollider parent) |
| | | 718 | | { |
| | 31 | 719 | | ThrowIfCompoundPartLifecycle(nameof(SetParent)); |
| | 31 | 720 | | if (_hierarchyState.SetParent(this, parent)) |
| | | 721 | | { |
| | 26 | 722 | | MarkContactDependencyChanged(); |
| | 26 | 723 | | _body?.Wake(); |
| | | 724 | | } |
| | 26 | 725 | | } |
| | | 726 | | |
| | | 727 | | /// <summary>Assigns a 2D hierarchy parent used for mixed collision exclusion.</summary> |
| | | 728 | | public void SetParent(LSCollider2D parent) |
| | | 729 | | { |
| | 9 | 730 | | ThrowIfCompoundPartLifecycle(nameof(SetParent)); |
| | 9 | 731 | | if (_hierarchyState.SetParent(this, parent)) |
| | | 732 | | { |
| | 8 | 733 | | MarkContactDependencyChanged(); |
| | 8 | 734 | | _body?.Wake(); |
| | | 735 | | } |
| | 4 | 736 | | } |
| | | 737 | | |
| | | 738 | | /// <summary>Removes this collider from its current hierarchy parent.</summary> |
| | | 739 | | public void ClearParent() |
| | | 740 | | { |
| | 456 | 741 | | ThrowIfCompoundPartLifecycle(nameof(ClearParent)); |
| | 456 | 742 | | if (_hierarchyState.ClearParent(this)) |
| | | 743 | | { |
| | 5 | 744 | | MarkContactDependencyChanged(); |
| | 5 | 745 | | _body?.Wake(); |
| | | 746 | | } |
| | 455 | 747 | | } |
| | | 748 | | |
| | | 749 | | /// <summary>Gets whether hierarchy rules exclude collision with another 3D collider.</summary> |
| | | 750 | | public bool IsSibling(LSCollider other) |
| | | 751 | | { |
| | 2490152 | 752 | | return _hierarchyState.ExcludesCollisionWith(other._hierarchyState, HierarchyKey, other.HierarchyKey); |
| | | 753 | | } |
| | | 754 | | |
| | | 755 | | internal bool ExcludesMixedCollisionWith(LSCollider2D other) => |
| | 361510 | 756 | | _hierarchyState.ExcludesCollisionWith(other.HierarchyState, HierarchyKey, other.HierarchyKey); |
| | | 757 | | |
| | | 758 | | private bool RebuildRuntimeShapeState(bool refreshMassProperties = true, bool geometryOnly = false, |
| | | 759 | | bool skipUnchangedMassProperties = false) |
| | | 760 | | { |
| | 422860 | 761 | | ColliderShapeSnapshot snapshot = CaptureShapeSnapshot(); |
| | | 762 | | // CCD needs pending geometry, not an extra pose-only publication that |
| | | 763 | | // would advance runtime versions and disturb retained solver caches. |
| | 422851 | 764 | | if (geometryOnly && !_runtimeShapeState.IsDirty && snapshot.OwnerScale == _committedOwnerScale) |
| | 74429 | 765 | | return false; |
| | 348422 | 766 | | if (!_runtimeShapeState.ShouldRebuild(snapshot)) |
| | 212466 | 767 | | return false; |
| | | 768 | | |
| | 135956 | 769 | | bool contactDependencyChanged = _runtimeShapeState.IsDirty |
| | 135956 | 770 | | || snapshot.OwnerScale != _committedOwnerScale || _body?.IsDynamic != true; |
| | 135956 | 771 | | PrepareRuntimeShape( |
| | 135956 | 772 | | snapshot, |
| | 135956 | 773 | | includeBodyAttachment: |
| | 135956 | 774 | | _body != null); |
| | 135949 | 775 | | PublishPreparedShape(contactDependencyChanged); |
| | 135949 | 776 | | if (refreshMassProperties && (!skipUnchangedMassProperties || contactDependencyChanged)) |
| | 68381 | 777 | | _body?.RefreshMassPropertiesFromColliderShape(); |
| | 135949 | 778 | | return true; |
| | | 779 | | } |
| | | 780 | | |
| | | 781 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 782 | | internal bool RebuildRuntimeShapeOnly(bool refreshMassProperties = true, bool geometryOnly = false) => |
| | 119320 | 783 | | RebuildRuntimeShapeState(refreshMassProperties, geometryOnly); |
| | | 784 | | |
| | | 785 | | /// <summary>Marks derived runtime shape state for deterministic rebuilding.</summary> |
| | | 786 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 787 | | protected void MarkShapeDirty() |
| | | 788 | | { |
| | 5008 | 789 | | _runtimeShapeState.MarkDirty(); |
| | 5008 | 790 | | if (_compoundOwner != null) |
| | | 791 | | { |
| | 1 | 792 | | _compoundOwner.MarkShapeDirty(); |
| | 1 | 793 | | return; |
| | | 794 | | } |
| | | 795 | | |
| | 5007 | 796 | | _body?.Wake(); |
| | 48 | 797 | | } |
| | | 798 | | |
| | | 799 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 143412 | 800 | | private void MarkBroadPhaseChanged() => _partitionState.MarkBroadPhaseChanged(); |
| | | 801 | | |
| | | 802 | | /// <summary>Updates derived authored dimensions after <see cref="Radius"/> changes.</summary> |
| | 2 | 803 | | protected virtual void OnRadiusChanged() { } |
| | | 804 | | |
| | | 805 | | /// <summary>Updates derived material state after <see cref="Material"/> changes.</summary> |
| | 3604 | 806 | | protected virtual void OnMaterialChanged() { } |
| | | 807 | | |
| | | 808 | | /// <summary>Normalizes an authored size for the derived shape.</summary> |
| | 1129 | 809 | | protected virtual Vector3d NormalizeSize(Vector3d value) => value; |
| | | 810 | | |
| | | 811 | | private static void ValidateSize(Vector3d value) |
| | | 812 | | { |
| | 4096 | 813 | | ChronicleGuard.ThrowIfArgument( |
| | 4096 | 814 | | value.X <= Fixed64.Zero || value.Y <= Fixed64.Zero || value.Z <= Fixed64.Zero, |
| | 4096 | 815 | | nameof(value), |
| | 4096 | 816 | | "Collider size components must be greater than zero."); |
| | 4090 | 817 | | } |
| | | 818 | | |
| | | 819 | | /// <summary> |
| | | 820 | | /// Gets the point on this collider's surface nearest to a world-space point. |
| | | 821 | | /// </summary> |
| | | 822 | | /// <param name="other"></param> |
| | | 823 | | /// <returns></returns> |
| | | 824 | | public abstract Vector3d ClosestPointOnSurface(Vector3d other); |
| | | 825 | | |
| | | 826 | | /// <summary> |
| | | 827 | | /// The direction pointing outward from the surface of the collider at the point |
| | | 828 | | /// </summary> |
| | | 829 | | /// <param name="point"></param> |
| | | 830 | | /// <returns></returns> |
| | | 831 | | public abstract Vector3d GetNormalAtPoint(Vector3d point); |
| | | 832 | | |
| | | 833 | | /// <summary> |
| | | 834 | | /// Returns the semantic closest surface feature without requiring its |
| | | 835 | | /// world point or distance to fit in one <see cref="Fixed64"/> vector. |
| | | 836 | | /// </summary> |
| | | 837 | | internal abstract FixedPointAnchor GetClosestSurfaceAnchor( |
| | | 838 | | Vector3d point, |
| | | 839 | | out Vector3d normal); |
| | | 840 | | |
| | | 841 | | /// <summary> |
| | | 842 | | /// Retains exact primitive-solid containment alongside the selected feature. |
| | | 843 | | /// The default preserves surface-only query semantics. |
| | | 844 | | /// </summary> |
| | | 845 | | internal virtual FixedPointAnchor GetClosestSurfaceAnchor( |
| | | 846 | | Vector3d point, out Vector3d normal, out bool contained) |
| | | 847 | | { |
| | 2 | 848 | | contained = false; |
| | 2 | 849 | | return GetClosestSurfaceAnchor(point, out normal); |
| | | 850 | | } |
| | | 851 | | |
| | | 852 | | /// <summary> |
| | | 853 | | /// Tries to resolve a normal that is invariant across the planar surface |
| | | 854 | | /// feature containing <paramref name="point"/>. A successful result must |
| | | 855 | | /// be a nonzero outward normal. |
| | | 856 | | /// </summary> |
| | | 857 | | internal virtual bool TryGetPlanarSurfaceNormal(Vector3d point, out Vector3d normal) |
| | | 858 | | { |
| | 1473 | 859 | | normal = Vector3d.Zero; |
| | 1473 | 860 | | return false; |
| | | 861 | | } |
| | | 862 | | |
| | | 863 | | /// <summary> |
| | | 864 | | /// Checks if position is in the padded bounds of the collider, used for broad phase collision detection |
| | | 865 | | /// </summary> |
| | | 866 | | /// <param name="cellPadding">The topology cell edge used to conservatively pad the bounds.</param> |
| | | 867 | | /// <param name="position">The position to check</param> |
| | | 868 | | /// <returns>True if position within bounds</returns> |
| | | 869 | | public bool IsPositionInBounds(Fixed64 cellPadding, Vector3d position) |
| | | 870 | | { |
| | 9626848 | 871 | | return position.X + cellPadding >= BoundsMin.X |
| | 9626848 | 872 | | && position.X - cellPadding <= BoundsMax.X |
| | 9626848 | 873 | | && position.Y + cellPadding >= BoundsMin.Y |
| | 9626848 | 874 | | && position.Y - cellPadding <= BoundsMax.Y |
| | 9626848 | 875 | | && position.Z + cellPadding >= BoundsMin.Z |
| | 9626848 | 876 | | && position.Z - cellPadding <= BoundsMax.Z; |
| | | 877 | | } |
| | | 878 | | |
| | | 879 | | /// <summary> |
| | | 880 | | /// Checks if this object overlaps the line formed by p1 and p2 |
| | | 881 | | /// </summary> |
| | | 882 | | /// <param name="worker">The prepared ray-axis worker for the owning query service.</param> |
| | | 883 | | /// <param name="outputIntersectionPoints"></param> |
| | | 884 | | /// <returns></returns> |
| | | 885 | | public abstract bool ColliderOverlapsRay(RaycastSegmentWorker worker, ref SwiftList<Vector3d> outputIntersectionPoin |
| | | 886 | | |
| | | 887 | | void IColliderHierarchyNode.AddChild(ColliderHierarchyKey key) |
| | | 888 | | { |
| | 37 | 889 | | ThrowIfCompoundPartLifecycle(nameof(IColliderHierarchyNode.AddChild)); |
| | 37 | 890 | | _hierarchyState.AddChild(key); |
| | 37 | 891 | | } |
| | | 892 | | |
| | | 893 | | void IColliderHierarchyNode.RemoveChild(ColliderHierarchyKey key) |
| | | 894 | | { |
| | 6 | 895 | | ThrowIfCompoundPartLifecycle(nameof(IColliderHierarchyNode.RemoveChild)); |
| | 6 | 896 | | _hierarchyState.RemoveChild(key); |
| | 6 | 897 | | } |
| | | 898 | | |
| | | 899 | | void IColliderHierarchyNode.ClearParentReference() |
| | | 900 | | { |
| | 5 | 901 | | if (_hierarchyState.ClearParentReference()) |
| | | 902 | | { |
| | 4 | 903 | | MarkContactDependencyChanged(); |
| | 4 | 904 | | _body?.Wake(); |
| | | 905 | | } |
| | 4 | 906 | | } |
| | | 907 | | |
| | | 908 | | bool IColliderHierarchyNode.TryGetHierarchyColliderByKey(ColliderHierarchyKey key, out IColliderHierarchyNode? colli |
| | | 909 | | { |
| | 12 | 910 | | collider = null; |
| | 12 | 911 | | if (!key.IsValid || _context == null) |
| | 1 | 912 | | return false; |
| | | 913 | | |
| | 11 | 914 | | if (key.Is3D && _context.Physics.TryGetColliderById(key.Id, out LSCollider? collider3D)) |
| | | 915 | | { |
| | 4 | 916 | | collider = collider3D; |
| | 4 | 917 | | return true; |
| | | 918 | | } |
| | | 919 | | |
| | 7 | 920 | | if (key.Is2D && _context.Physics2D.TryGetColliderById(key.Id, out LSCollider2D? collider2D)) |
| | | 921 | | { |
| | 5 | 922 | | collider = collider2D; |
| | 5 | 923 | | return true; |
| | | 924 | | } |
| | | 925 | | |
| | 2 | 926 | | return false; |
| | | 927 | | } |
| | | 928 | | |
| | | 929 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 930 | | internal void SetPhysicsState(int id, int serviceIndex, int replayOrder) |
| | | 931 | | { |
| | 13254 | 932 | | ChronicleGuard.ThrowIfNegative(id, nameof(id)); |
| | 13254 | 933 | | ChronicleGuard.ThrowIfNegative(serviceIndex, nameof(serviceIndex)); |
| | 13254 | 934 | | ChronicleGuard.ThrowIfNegative(replayOrder, nameof(replayOrder)); |
| | 13254 | 935 | | _id = id; |
| | 13254 | 936 | | _serviceIndex = serviceIndex; |
| | 13254 | 937 | | _replayOrder = replayOrder; |
| | 13254 | 938 | | _replayOrdinal = -1; |
| | 13254 | 939 | | } |
| | | 940 | | |
| | | 941 | | internal void SetServiceIndex(int serviceIndex) |
| | | 942 | | { |
| | 125 | 943 | | ChronicleGuard.ThrowIfNegative(serviceIndex, nameof(serviceIndex)); |
| | 125 | 944 | | _serviceIndex = serviceIndex; |
| | 125 | 945 | | } |
| | | 946 | | |
| | | 947 | | internal void SetReplayOrdinal(int replayOrdinal) |
| | | 948 | | { |
| | 23043 | 949 | | ChronicleGuard.ThrowIfNegative(replayOrdinal, nameof(replayOrdinal)); |
| | 23043 | 950 | | _replayOrdinal = replayOrdinal; |
| | 23043 | 951 | | } |
| | | 952 | | |
| | | 953 | | internal void SetServiceRefreshIndex(int serviceRefreshIndex) |
| | | 954 | | { |
| | 5876 | 955 | | ChronicleGuard.ThrowIfNegative(serviceRefreshIndex, nameof(serviceRefreshIndex)); |
| | 5876 | 956 | | _serviceRefreshIndex = serviceRefreshIndex; |
| | 5876 | 957 | | } |
| | | 958 | | |
| | | 959 | | internal void ClearServiceRefreshIndex() |
| | | 960 | | { |
| | 7964 | 961 | | _serviceRefreshIndex = -1; |
| | 7964 | 962 | | } |
| | | 963 | | |
| | | 964 | | internal void ClearPhysicsState() |
| | | 965 | | { |
| | 550 | 966 | | _partitionState.MarkUnpartitioned(); |
| | 550 | 967 | | _partitionState.ClearCoordinates(); |
| | 550 | 968 | | _mixedPartitionState.MarkUnpartitioned(); |
| | 550 | 969 | | _mixedPartitionState.ClearCoordinates(); |
| | 550 | 970 | | _pairState.ClearCollisionPairs(); |
| | 550 | 971 | | _pairState.ClearCollisionPairHolders(); |
| | 550 | 972 | | _id = -1; |
| | 550 | 973 | | _serviceIndex = -1; |
| | 550 | 974 | | _replayOrder = -1; |
| | 550 | 975 | | _replayOrdinal = -1; |
| | 550 | 976 | | _serviceRefreshIndex = -1; |
| | 550 | 977 | | } |
| | | 978 | | |
| | | 979 | | void IPhysicsColliderRegistryItem.SetRegistryState(int id, int serviceIndex, int replayOrder) => |
| | 13254 | 980 | | SetPhysicsState(id, serviceIndex, replayOrder); |
| | | 981 | | |
| | 451 | 982 | | int IPhysicsColliderRegistryItem.ServiceIndex => _serviceIndex; |
| | | 983 | | |
| | 141279 | 984 | | int IPhysicsColliderRegistryItem.ReplayOrder => _replayOrder; |
| | | 985 | | |
| | | 986 | | void IPhysicsColliderRegistryItem.SetRegistryServiceIndex(int serviceIndex) => |
| | 125 | 987 | | SetServiceIndex(serviceIndex); |
| | | 988 | | |
| | | 989 | | void IPhysicsColliderRegistryItem.SetRegistryReplayOrdinal(int replayOrdinal) => |
| | 23043 | 990 | | SetReplayOrdinal(replayOrdinal); |
| | | 991 | | |
| | 550 | 992 | | void IPhysicsColliderRegistryItem.ClearRegistryState() => ClearPhysicsState(); |
| | | 993 | | |
| | | 994 | | internal SwiftList<WorldVoxelIndex> GetOrCreateMixedPartitionCoordinates() |
| | | 995 | | { |
| | 20604 | 996 | | _mixedPartitionState.Coordinates ??= new(); |
| | 20604 | 997 | | return _mixedPartitionState.Coordinates; |
| | | 998 | | } |
| | | 999 | | |
| | | 1000 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 1001 | | internal bool MatchesPartitionGridBounds(Vector3d min, Vector3d max, int partitionKind) => |
| | 306504 | 1002 | | _partitionState.IsPartitioned |
| | 306504 | 1003 | | && _partitionState.LastGridBoundsMin == min |
| | 306504 | 1004 | | && _partitionState.LastGridBoundsMax == max |
| | 306504 | 1005 | | && _partitionState.LastPartitionKind == partitionKind; |
| | | 1006 | | |
| | | 1007 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 3586 | 1008 | | internal void MarkUnpartitioned() => _partitionState.MarkUnpartitioned(); |
| | | 1009 | | |
| | | 1010 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 488 | 1011 | | internal void ClearPartitionCoordinates() => _partitionState.ClearCoordinates(); |
| | | 1012 | | |
| | | 1013 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 1014 | | internal bool MatchesMixedPartitionGridBounds(Vector3d min, Vector3d max, int partitionKind) => |
| | 31041 | 1015 | | _mixedPartitionState.IsPartitioned |
| | 31041 | 1016 | | && _mixedPartitionState.LastGridBoundsMin == min |
| | 31041 | 1017 | | && _mixedPartitionState.LastGridBoundsMax == max |
| | 31041 | 1018 | | && _mixedPartitionState.LastPartitionKind == partitionKind; |
| | | 1019 | | |
| | | 1020 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 1021 | | internal void MarkMixedPartitioned(Vector3d min, Vector3d max, int partitionKind) |
| | | 1022 | | { |
| | 20529 | 1023 | | _mixedPartitionState.SetPreviousGridBounds(min, max, partitionKind); |
| | 20529 | 1024 | | _mixedPartitionState.MarkPartitioned(); |
| | 20529 | 1025 | | } |
| | | 1026 | | |
| | | 1027 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 168 | 1028 | | internal void MarkMixedUnpartitioned() => _mixedPartitionState.MarkUnpartitioned(); |
| | | 1029 | | |
| | | 1030 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 93 | 1031 | | internal void ClearMixedPartitionCoordinates() => _mixedPartitionState.ClearCoordinates(); |
| | | 1032 | | |
| | | 1033 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 5653786 | 1034 | | internal bool IgnoresCollisionLayer(PhysicsLayer layer) => _ignoredCollisionLayers.Includes(layer); |
| | | 1035 | | } |