| | | 1 | | //======================================================================= |
| | | 2 | | // LSCompoundCollider.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 FixedMathSharp; |
| | | 11 | | using FixedMathSharp.Geometry; |
| | | 12 | | using Gravitas.Materials; |
| | | 13 | | using Gravitas.Queries; |
| | | 14 | | using SwiftCollections; |
| | | 15 | | |
| | | 16 | | namespace Gravitas.Colliders; |
| | | 17 | | |
| | | 18 | | /// <summary> |
| | | 19 | | /// Represents one collider identity whose collision shape is composed from |
| | | 20 | | /// deterministic internal primitive and convex-mesh parts. |
| | | 21 | | /// </summary> |
| | | 22 | | public sealed class LSCompoundCollider : LSCollider |
| | | 23 | | { |
| | | 24 | | private readonly CompoundColliderPart[] _parts; |
| | | 25 | | private readonly LSCollider[] _partColliders; |
| | | 26 | | private readonly ExactMassPoint3D[] _massPointScratch; |
| | | 27 | | private readonly ExactMassWeight[] _massWeightScratch; |
| | | 28 | | private bool _hasMassPropertyCenter, _massPropertyCommitted; |
| | | 29 | | private Vector3d _massPropertyCenter, _massPropertyOwnerScale, _massPropertyOffset; |
| | | 30 | | private bool _inertiaGeometryValid, _inertiaGeometryCommitted; |
| | | 31 | | private Vector3d _inertiaGeometryScale, _inertiaGeometryOffset, _inertiaGeometryReference; |
| | | 32 | | private ExactMassWeight _inertiaGeometryWeight; |
| | | 33 | | private CompoundInertiaTensor _inertiaGeometry; |
| | | 34 | | |
| | | 35 | | |
| | | 36 | | /// <summary>Creates a runtime compound collider from authored 3D parts.</summary> |
| | 505 | 37 | | public LSCompoundCollider(params CompoundColliderPart[] parts) |
| | | 38 | | { |
| | 505 | 39 | | ChronicleGuard.ThrowIfNull(parts, nameof(parts)); |
| | 505 | 40 | | ChronicleGuard.ThrowIfArgument(parts.Length == 0, nameof(parts), "Compound collider must contain at least one pa |
| | | 41 | | |
| | 2606 | 42 | | for (int i = 0; i < parts.Length; i++) |
| | 799 | 43 | | ValidatePart(parts[i]); |
| | | 44 | | |
| | 504 | 45 | | _parts = new CompoundColliderPart[parts.Length]; |
| | 504 | 46 | | _partColliders = new LSCollider[parts.Length]; |
| | 504 | 47 | | _massPointScratch = new ExactMassPoint3D[parts.Length]; |
| | 504 | 48 | | _massWeightScratch = new ExactMassWeight[parts.Length]; |
| | 2604 | 49 | | for (int i = 0; i < parts.Length; i++) |
| | | 50 | | { |
| | 798 | 51 | | _parts[i] = parts[i]; |
| | 798 | 52 | | _partColliders[i] = MaterializePartCollider(parts[i]); |
| | 798 | 53 | | _partColliders[i].ReserveCompoundPart( |
| | 798 | 54 | | this, |
| | 798 | 55 | | parts[i].LocalRotation, |
| | 798 | 56 | | parts[i].LocalScale); |
| | | 57 | | } |
| | 504 | 58 | | } |
| | | 59 | | |
| | | 60 | | /// <inheritdoc/> |
| | 491407 | 61 | | public override ColliderType Shape => ColliderType.Compound; |
| | | 62 | | |
| | | 63 | | /// <inheritdoc/> |
| | 469 | 64 | | public override int Priority => ColliderSettings.GetPriority(Shape); |
| | | 65 | | |
| | | 66 | | /// <inheritdoc/> |
| | | 67 | | public override Fixed64 ScaledRadius |
| | | 68 | | { |
| | | 69 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 35 | 70 | | get => HasCommittedShape |
| | 35 | 71 | | ? CanonicalCenteredProxyRadius |
| | 35 | 72 | | : ColliderCanonicalBounds |
| | 35 | 73 | | .GetCurrentCenteredProxyRadius(this); |
| | | 74 | | } |
| | | 75 | | |
| | | 76 | | /// <summary>Gets the authored parts in stable source order.</summary> |
| | 132 | 77 | | public ReadOnlySpan<CompoundColliderPart> Parts => _parts; |
| | | 78 | | |
| | | 79 | | /// <summary>Gets the number of authored compound parts.</summary> |
| | | 80 | | public int PartCount |
| | | 81 | | { |
| | | 82 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 91888 | 83 | | get => _parts.Length; |
| | | 84 | | } |
| | | 85 | | |
| | | 86 | | /// <summary>Gets the stable runtime part identifier for a source-order index.</summary> |
| | | 87 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 88 | | public int GetPartId(int index) |
| | | 89 | | { |
| | 2 | 90 | | ChronicleGuard.ThrowIfArrayIndexInvalid(index, _parts.Length, nameof(index)); |
| | 2 | 91 | | return index; |
| | | 92 | | } |
| | | 93 | | |
| | | 94 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 95 | | internal LSCollider GetPartCollider(int index) |
| | | 96 | | { |
| | 57325 | 97 | | ChronicleGuard.ThrowIfArrayIndexInvalid(index, _parts.Length, nameof(index)); |
| | 57325 | 98 | | return _partColliders[index]; |
| | | 99 | | } |
| | | 100 | | |
| | | 101 | | internal Fixed64 GetMutationGeometryReach(LSCollider changed, in ColliderShapeSnapshot candidate) |
| | | 102 | | { |
| | 3 | 103 | | Fixed64 reach = Fixed64.Zero; |
| | 12 | 104 | | for (int i = 0; i < _parts.Length; i++) |
| | | 105 | | { |
| | 3 | 106 | | LSCollider part = _partColliders[i]; |
| | 3 | 107 | | ColliderShapeSnapshot snapshot = ReferenceEquals(part, changed) ? candidate |
| | 3 | 108 | | : new ColliderShapeSnapshot(default, default, candidate.OwnerScale, _parts[i].LocalScale, |
| | 3 | 109 | | part.LocalOffset, part.Size, part.Radius); |
| | 3 | 110 | | reach = FixedMath.Max(reach, ColliderGeometryPolicy.Add( |
| | 3 | 111 | | ColliderGeometryPolicy.Attachment(snapshot.LocalOffset, snapshot.OwnerScale), |
| | 3 | 112 | | part.GetCandidateGeometryReach(snapshot))); |
| | | 113 | | } |
| | 3 | 114 | | return reach; |
| | | 115 | | } |
| | | 116 | | |
| | | 117 | | private protected override void PrepareShape(in ColliderShapeSnapshot snapshot) |
| | | 118 | | { |
| | 20173 | 119 | | Vector3d min = Vector3d.Zero; |
| | 20173 | 120 | | Vector3d max = Vector3d.Zero; |
| | | 121 | | |
| | 20173 | 122 | | Fixed64 reach = Fixed64.Zero; |
| | 81812 | 123 | | for (int i = 0; i < _parts.Length; i++) |
| | | 124 | | { |
| | 20738 | 125 | | CompoundColliderPart part = _parts[i]; |
| | 20738 | 126 | | LSCollider partCollider = _partColliders[i]; |
| | 20738 | 127 | | partCollider.PrepareCompoundPart( |
| | 20738 | 128 | | snapshot, |
| | 20738 | 129 | | part.LocalRotation, |
| | 20738 | 130 | | part.LocalScale, |
| | 20738 | 131 | | PreparedContext); |
| | 20733 | 132 | | reach = FixedMath.Max(reach, ColliderGeometryPolicy.Add( |
| | 20733 | 133 | | partCollider.PreparedAttachmentReach, |
| | 20733 | 134 | | partCollider.PreparedGeometryReach)); |
| | | 135 | | |
| | 20733 | 136 | | if (i == 0) |
| | | 137 | | { |
| | 20168 | 138 | | min = partCollider.PreparedShapeBounds.Min; |
| | 20168 | 139 | | max = partCollider.PreparedShapeBounds.Max; |
| | | 140 | | } |
| | | 141 | | else |
| | | 142 | | { |
| | 565 | 143 | | min = Vector3d.Min(min, partCollider.PreparedShapeBounds.Min); |
| | 565 | 144 | | max = Vector3d.Max(max, partCollider.PreparedShapeBounds.Max); |
| | | 145 | | } |
| | | 146 | | } |
| | | 147 | | |
| | 20168 | 148 | | _ = CalculatePreparedLocalMassPoint(); |
| | 20167 | 149 | | _ = CalculatePreparedMassPropertyWeight(); |
| | 20167 | 150 | | SetPreparedGeometryReach(reach); |
| | 20167 | 151 | | SetPreparedBounds(FixedBoundBox.FromMinMax(min, max)); |
| | 20167 | 152 | | } |
| | | 153 | | |
| | | 154 | | private protected override void PublishShape() |
| | | 155 | | { |
| | 20164 | 156 | | Fixed64 area = Fixed64.Zero; |
| | 81786 | 157 | | for (int i = 0; i < _parts.Length; i++) |
| | | 158 | | { |
| | 20729 | 159 | | CompoundColliderPart part = _parts[i]; |
| | 20729 | 160 | | _partColliders[i].PublishCompoundPart( |
| | 20729 | 161 | | part.LocalRotation, |
| | 20729 | 162 | | part.LocalScale, |
| | 20729 | 163 | | PreparedContext); |
| | 20729 | 164 | | area += _partColliders[i].Area; |
| | | 165 | | } |
| | | 166 | | |
| | 20164 | 167 | | Area = area; |
| | 20164 | 168 | | } |
| | | 169 | | |
| | | 170 | | internal override ExactMassPoint3D CalculateLocalMassPoint() |
| | | 171 | | { |
| | 9953 | 172 | | GetCurrentShapeScales(out Vector3d scale, out _); |
| | 9953 | 173 | | return _hasMassPropertyCenter && _massPropertyOwnerScale == scale |
| | 9953 | 174 | | && _massPropertyOffset == LocalOffset && _massPropertyCommitted == HasCommittedShape |
| | 9953 | 175 | | ? ExactMassPoint3D.FromPoint(_massPropertyCenter) |
| | 9953 | 176 | | : CalculateAggregateMassPoint(usePrepared: false); |
| | | 177 | | } |
| | | 178 | | |
| | | 179 | | internal override ExactMassPoint3D CalculatePreparedLocalMassPoint() => |
| | 20168 | 180 | | CalculateAggregateMassPoint(usePrepared: true); |
| | | 181 | | |
| | | 182 | | private ExactMassPoint3D CalculateAggregateMassPoint(bool usePrepared) |
| | | 183 | | { |
| | 20409 | 184 | | bool hasPositiveWeight = false; |
| | 83152 | 185 | | for (int i = 0; i < _partColliders.Length; i++) |
| | | 186 | | { |
| | 21167 | 187 | | LSCollider part = _partColliders[i]; |
| | 21167 | 188 | | ExactMassWeight weight = usePrepared |
| | 21167 | 189 | | ? part.CalculatePreparedMassPropertyWeight() |
| | 21167 | 190 | | : part.CalculateMassPropertyWeight(); |
| | 21167 | 191 | | _massPointScratch[i] = usePrepared |
| | 21167 | 192 | | ? part.CalculatePreparedLocalMassPoint() |
| | 21167 | 193 | | : part.CalculateLocalMassPoint(); |
| | 21167 | 194 | | _massWeightScratch[i] = weight; |
| | 21167 | 195 | | hasPositiveWeight |= !weight.IsZero; |
| | | 196 | | } |
| | | 197 | | |
| | 20409 | 198 | | if (!hasPositiveWeight) |
| | | 199 | | { |
| | | 200 | | // Admitted solids and shells have positive exact weights. Only |
| | | 201 | | // unsupported closed-mesh parts need this equal-point fallback. |
| | 78 | 202 | | for (int i = 0; i < _massWeightScratch.Length; i++) |
| | 22 | 203 | | _massWeightScratch[i] = ExactMassWeight.One; |
| | | 204 | | } |
| | | 205 | | |
| | 20409 | 206 | | if (!ExactMassPoint3D.TryGetWeightedAverage( |
| | 20409 | 207 | | _massPointScratch, |
| | 20409 | 208 | | _massWeightScratch, |
| | 20409 | 209 | | out Vector3d center)) |
| | | 210 | | { |
| | 2 | 211 | | throw new InvalidOperationException( |
| | 2 | 212 | | usePrepared |
| | 2 | 213 | | ? "Prepared compound mass-property point is outside the Fixed64 coordinate domain." |
| | 2 | 214 | | : "The compound collider's center of mass is outside the Fixed64 coordinate domain."); |
| | | 215 | | } |
| | 20407 | 216 | | if (!usePrepared) |
| | | 217 | | { |
| | 240 | 218 | | GetCurrentShapeScales(out _massPropertyOwnerScale, out _); |
| | 240 | 219 | | _massPropertyOffset = LocalOffset; |
| | 240 | 220 | | _massPropertyCommitted = HasCommittedShape; |
| | 240 | 221 | | _massPropertyCenter = center; |
| | 240 | 222 | | _hasMassPropertyCenter = true; |
| | | 223 | | } |
| | 20407 | 224 | | return ExactMassPoint3D.FromPoint(center); |
| | | 225 | | } |
| | | 226 | | |
| | | 227 | | internal override ExactMassWeight CalculateMassPropertyWeight() => |
| | 1 | 228 | | CalculateAggregateMassWeight(usePrepared: false); |
| | | 229 | | |
| | | 230 | | internal override ExactMassWeight CalculatePreparedMassPropertyWeight() => |
| | 20167 | 231 | | CalculateAggregateMassWeight(usePrepared: true); |
| | | 232 | | |
| | | 233 | | private ExactMassWeight CalculateAggregateMassWeight(bool usePrepared) |
| | | 234 | | { |
| | 20168 | 235 | | ExactMassWeight totalWeight = ExactMassWeight.Zero; |
| | 81802 | 236 | | for (int i = 0; i < _parts.Length; i++) |
| | | 237 | | { |
| | 20733 | 238 | | ExactMassWeight weight = usePrepared |
| | 20733 | 239 | | ? _partColliders[i].CalculatePreparedMassPropertyWeight() |
| | 20733 | 240 | | : _partColliders[i].CalculateMassPropertyWeight(); |
| | 20733 | 241 | | totalWeight = totalWeight.Add(weight); |
| | | 242 | | } |
| | 20168 | 243 | | return totalWeight; |
| | | 244 | | } |
| | | 245 | | |
| | | 246 | | internal override Fixed3x3 CalculateCenterOfMassInertiaTensor(Fixed64 mass) => |
| | 1 | 247 | | CalculateInertiaTensor(mass, CalculateLocalCenterOfMassOffset()); |
| | | 248 | | |
| | | 249 | | /// <inheritdoc/> |
| | | 250 | | public override Fixed3x3 CalculateInertiaTensor(Fixed64 mass, Vector3d localCenterOfMassOffset) |
| | | 251 | | { |
| | 2437 | 252 | | if (mass <= Fixed64.Zero) |
| | 3 | 253 | | return Fixed3x3.Zero; |
| | 2434 | 254 | | GetCurrentShapeScales(out Vector3d scale, out _); |
| | 2434 | 255 | | if (!_inertiaGeometryValid || _inertiaGeometryCommitted != HasCommittedShape || _inertiaGeometryScale != scale |
| | 2434 | 256 | | || _inertiaGeometryOffset != LocalOffset || _inertiaGeometryReference != localCenterOfMassOffset) |
| | | 257 | | { |
| | 184 | 258 | | ExactMassWeight total = ExactMassWeight.Zero; |
| | 184 | 259 | | CompoundInertiaTensor geometry = default; |
| | 948 | 260 | | for (int i = 0; i < _partColliders.Length; i++) |
| | | 261 | | { |
| | 294 | 262 | | LSCollider part = _partColliders[i]; |
| | 294 | 263 | | if (!part.SupportsMassProperties) |
| | | 264 | | continue; |
| | 286 | 265 | | ExactMassWeight weight = part.CalculateMassPropertyWeight(); |
| | 285 | 266 | | total = total.Add(weight); |
| | 285 | 267 | | CompoundInertiaMath3D.Add(ref geometry, part, weight, localCenterOfMassOffset); |
| | | 268 | | } |
| | 180 | 269 | | if (total.IsZero) |
| | 3 | 270 | | throw new InvalidOperationException("Compound inertia requires at least one part with valid mass propert |
| | | 271 | | |
| | | 272 | | // Cache geometry only after a complete evaluation. Neither a mass |
| | | 273 | | // change nor world pose changes the local intrinsic contribution. |
| | 177 | 274 | | _inertiaGeometry = geometry; |
| | 177 | 275 | | _inertiaGeometryWeight = total; |
| | 177 | 276 | | _inertiaGeometryScale = scale; |
| | 177 | 277 | | _inertiaGeometryOffset = LocalOffset; |
| | 177 | 278 | | _inertiaGeometryReference = localCenterOfMassOffset; |
| | 177 | 279 | | _inertiaGeometryCommitted = HasCommittedShape; |
| | 177 | 280 | | _inertiaGeometryValid = true; |
| | | 281 | | } |
| | 2427 | 282 | | return _inertiaGeometry.Materialize(mass, _inertiaGeometryWeight); |
| | | 283 | | } |
| | | 284 | | |
| | | 285 | | /// <inheritdoc/> |
| | | 286 | | public override Fixed64 GetFrontalArea(Vector3d direction) |
| | | 287 | | { |
| | 3 | 288 | | Fixed64 area = Fixed64.Zero; |
| | 18 | 289 | | for (int i = 0; i < _parts.Length; i++) |
| | 6 | 290 | | area += _partColliders[i].GetFrontalArea(direction); |
| | 3 | 291 | | return area; |
| | | 292 | | } |
| | | 293 | | |
| | | 294 | | /// <inheritdoc/> |
| | | 295 | | public override Vector3d ClosestPointOnSurface(Vector3d other) |
| | | 296 | | { |
| | 4 | 297 | | FixedPointAnchor anchor = |
| | 4 | 298 | | GetClosestSurfaceAnchor(other, out _); |
| | 4 | 299 | | if (anchor.TryGetPoint(out Vector3d point)) |
| | 3 | 300 | | return point; |
| | | 301 | | |
| | 1 | 302 | | throw new InvalidOperationException( |
| | 1 | 303 | | "The closest compound surface point is outside the representable coordinate domain."); |
| | | 304 | | } |
| | | 305 | | |
| | | 306 | | /// <inheritdoc/> |
| | | 307 | | public override Vector3d GetNormalAtPoint(Vector3d point) |
| | | 308 | | { |
| | 10 | 309 | | _ = GetClosestSurfaceAnchor( |
| | 10 | 310 | | point, |
| | 10 | 311 | | out Vector3d normal); |
| | 10 | 312 | | return normal; |
| | | 313 | | } |
| | | 314 | | |
| | | 315 | | internal override FixedPointAnchor GetClosestSurfaceAnchor( |
| | | 316 | | Vector3d point, |
| | | 317 | | out Vector3d normal) => |
| | 14 | 318 | | GetClosestSurfaceAnchor(point, out normal, out _); |
| | | 319 | | |
| | | 320 | | internal FixedPointAnchor GetClosestSurfaceAnchor( |
| | | 321 | | Vector3d point, |
| | | 322 | | out Vector3d normal, |
| | | 323 | | out LSCollider surfaceOwner) |
| | | 324 | | { |
| | 1237 | 325 | | var reference = new FixedPointAnchor( |
| | 1237 | 326 | | point, |
| | 1237 | 327 | | FixedQuaternion.Identity, |
| | 1237 | 328 | | Vector3d.Zero); |
| | 1237 | 329 | | surfaceOwner = _partColliders[0]; |
| | 1237 | 330 | | FixedPointAnchor closest = |
| | 1237 | 331 | | surfaceOwner.GetClosestSurfaceAnchor( |
| | 1237 | 332 | | point, |
| | 1237 | 333 | | out normal); |
| | 2614 | 334 | | for (int i = 1; i < _partColliders.Length; i++) |
| | | 335 | | { |
| | 70 | 336 | | FixedPointAnchor candidate = |
| | 70 | 337 | | _partColliders[i].GetClosestSurfaceAnchor( |
| | 70 | 338 | | point, |
| | 70 | 339 | | out Vector3d candidateNormal); |
| | 70 | 340 | | if (reference.CompareSquaredDistance( |
| | 70 | 341 | | candidate, |
| | 70 | 342 | | closest) >= 0) |
| | | 343 | | { |
| | | 344 | | continue; |
| | | 345 | | } |
| | | 346 | | |
| | 29 | 347 | | closest = candidate; |
| | 29 | 348 | | normal = candidateNormal; |
| | 29 | 349 | | surfaceOwner = _partColliders[i]; |
| | | 350 | | } |
| | | 351 | | |
| | 1237 | 352 | | return closest; |
| | | 353 | | } |
| | | 354 | | |
| | | 355 | | /// <inheritdoc/> |
| | | 356 | | public override bool ColliderOverlapsRay(RaycastSegmentWorker worker, ref SwiftList<Vector3d> outputIntersectionPoin |
| | | 357 | | { |
| | 10 | 358 | | bool hit = false; |
| | 56 | 359 | | for (int i = 0; i < _parts.Length; i++) |
| | 18 | 360 | | hit |= _partColliders[i].ColliderOverlapsRay(worker, ref outputIntersectionPoints); |
| | 10 | 361 | | return hit; |
| | | 362 | | } |
| | | 363 | | |
| | | 364 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 365 | | internal bool ContainsPartCollider(LSCollider collider) => |
| | 11380 | 366 | | ReferenceEquals(collider.CompoundOwner, this); |
| | | 367 | | |
| | | 368 | | private static LSCollider MaterializePartCollider(CompoundColliderPart part) |
| | | 369 | | { |
| | 798 | 370 | | LSCollider collider = part.Shape.CreateRuntimeCollider(); |
| | 798 | 371 | | collider.LocalOffset = part.LocalOffset; |
| | 798 | 372 | | collider.Material = part.ResolveMaterial(PhysicsMaterial.Default); |
| | 798 | 373 | | return collider; |
| | | 374 | | } |
| | | 375 | | |
| | | 376 | | /// <inheritdoc/> |
| | | 377 | | protected override void OnMaterialChanged() |
| | | 378 | | { |
| | 598 | 379 | | for (int i = 0; i < _parts.Length; i++) |
| | 168 | 380 | | _partColliders[i].Material = _parts[i].ResolveMaterial(Material); |
| | 131 | 381 | | } |
| | | 382 | | |
| | | 383 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 384 | | private static void ValidatePart(CompoundColliderPart part) => |
| | 799 | 385 | | ChronicleGuard.ThrowIfArgument(part.IsDefault, nameof(part), "Compound collider part cannot be default."); |
| | | 386 | | } |