| | | 1 | | //======================================================================= |
| | | 2 | | // LSCompoundCollider2D.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 FixedMathSharp; |
| | | 9 | | using FixedMathSharp.Geometry; |
| | | 10 | | using Gravitas.CollisionHandling; |
| | | 11 | | using Gravitas.Materials; |
| | | 12 | | using System; |
| | | 13 | | using System.Runtime.CompilerServices; |
| | | 14 | | |
| | | 15 | | namespace Gravitas.Colliders; |
| | | 16 | | |
| | | 17 | | /// <summary> |
| | | 18 | | /// Represents one pure 2D collider identity whose collision shape is composed |
| | | 19 | | /// from deterministic internal primitive and convex-polygon parts. |
| | | 20 | | /// </summary> |
| | | 21 | | public sealed class LSCompoundCollider2D : LSCollider2D |
| | | 22 | | { |
| | | 23 | | private readonly CompoundColliderPart2D[] _parts; |
| | | 24 | | private readonly LSCollider2D[] _partColliders; |
| | | 25 | | private readonly ExactMassPoint2D[] _massPointScratch; |
| | | 26 | | private readonly ExactMassWeight[] _massWeightScratch; |
| | 132 | 27 | | private readonly ContactManifold2D _partManifoldScratch = new(); |
| | | 28 | | |
| | | 29 | | /// <summary>Creates a runtime compound collider from authored pure 2D parts.</summary> |
| | 132 | 30 | | public LSCompoundCollider2D(params CompoundColliderPart2D[] parts) |
| | | 31 | | { |
| | 132 | 32 | | SwiftThrowHelper.ThrowIfNull(parts, nameof(parts)); |
| | 132 | 33 | | SwiftThrowHelper.ThrowIfArgument(parts.Length == 0, nameof(parts), "2D compound collider must contain at least o |
| | | 34 | | |
| | 756 | 35 | | for (int i = 0; i < parts.Length; i++) |
| | 248 | 36 | | ValidatePart(parts[i]); |
| | | 37 | | |
| | 130 | 38 | | _parts = new CompoundColliderPart2D[parts.Length]; |
| | 130 | 39 | | _partColliders = new LSCollider2D[parts.Length]; |
| | 130 | 40 | | _massPointScratch = new ExactMassPoint2D[parts.Length]; |
| | 130 | 41 | | _massWeightScratch = new ExactMassWeight[parts.Length]; |
| | 754 | 42 | | for (int i = 0; i < parts.Length; i++) |
| | | 43 | | { |
| | 247 | 44 | | _parts[i] = parts[i]; |
| | 247 | 45 | | _partColliders[i] = MaterializePartCollider(parts[i]); |
| | 247 | 46 | | _partColliders[i].ReserveCompoundPart(this, parts[i].LocalRotation, parts[i].LocalScale); |
| | | 47 | | } |
| | 130 | 48 | | } |
| | | 49 | | |
| | | 50 | | /// <inheritdoc/> |
| | 149 | 51 | | public override ColliderType2D Shape => ColliderType2D.Compound; |
| | | 52 | | |
| | | 53 | | /// <inheritdoc/> |
| | 26 | 54 | | public override int Priority => ColliderSettings2D.GetPriority(Shape); |
| | | 55 | | |
| | | 56 | | /// <summary> |
| | | 57 | | /// Gets the radius of a circle that conservatively contains the current |
| | | 58 | | /// aggregate shape. |
| | | 59 | | /// </summary> |
| | | 60 | | public Fixed64 ScaledRadius |
| | | 61 | | { |
| | | 62 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 63 | | get |
| | | 64 | | { |
| | 9 | 65 | | return HasCommittedShape |
| | 9 | 66 | | ? CanonicalCenteredProxyRadius |
| | 9 | 67 | | : ColliderCanonicalBounds2D.GetCurrentCenteredProxyRadius(this); |
| | | 68 | | } |
| | | 69 | | } |
| | | 70 | | |
| | | 71 | | /// <summary>Gets the authored parts in stable source order.</summary> |
| | 10 | 72 | | public ReadOnlySpan<CompoundColliderPart2D> Parts => _parts; |
| | | 73 | | |
| | | 74 | | /// <summary>Gets the number of authored compound parts.</summary> |
| | | 75 | | public int PartCount |
| | | 76 | | { |
| | | 77 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 2286 | 78 | | get => _parts.Length; |
| | | 79 | | } |
| | | 80 | | |
| | | 81 | | /// <summary>Gets the stable runtime part identifier for a source-order index.</summary> |
| | | 82 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 83 | | public int GetPartId(int index) |
| | | 84 | | { |
| | 2 | 85 | | SwiftThrowHelper.ThrowIfArrayIndexInvalid(index, _parts.Length, nameof(index)); |
| | 2 | 86 | | return index; |
| | | 87 | | } |
| | | 88 | | |
| | | 89 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 90 | | internal LSCollider2D GetPartCollider(int index) |
| | | 91 | | { |
| | 1535 | 92 | | SwiftThrowHelper.ThrowIfArrayIndexInvalid(index, _parts.Length, nameof(index)); |
| | 1535 | 93 | | return _partColliders[index]; |
| | | 94 | | } |
| | | 95 | | |
| | 16 | 96 | | internal ContactManifold2D PartManifoldScratch => _partManifoldScratch; |
| | | 97 | | |
| | | 98 | | /// <inheritdoc/> |
| | | 99 | | public override bool ContainsPoint(Vector2d point) |
| | | 100 | | { |
| | 1268 | 101 | | for (int i = 0; i < _partColliders.Length; i++) |
| | | 102 | | { |
| | 424 | 103 | | if (_partColliders[i].ContainsPoint(point)) |
| | 3 | 104 | | return true; |
| | | 105 | | } |
| | | 106 | | |
| | 210 | 107 | | return false; |
| | | 108 | | } |
| | | 109 | | |
| | | 110 | | /// <inheritdoc/> |
| | | 111 | | public override Vector2d GetClosestPoint(Vector2d point) |
| | | 112 | | { |
| | 5 | 113 | | _ = TryGetClosestBoundaryAnchor( |
| | 5 | 114 | | point, |
| | 5 | 115 | | out FixedPointAnchor2d anchor); |
| | 5 | 116 | | if (anchor.TryGetPoint(out Vector2d closest)) |
| | | 117 | | { |
| | 4 | 118 | | return closest; |
| | | 119 | | } |
| | | 120 | | |
| | 1 | 121 | | throw new InvalidOperationException( |
| | 1 | 122 | | "The closest compound surface point is outside the Fixed64 coordinate domain."); |
| | | 123 | | } |
| | | 124 | | |
| | | 125 | | /// <inheritdoc/> |
| | | 126 | | public override Vector2d GetSupportPoint(Vector2d direction) |
| | | 127 | | { |
| | 4 | 128 | | Vector2d bestPoint = _partColliders[0].GetSupportPoint(direction); |
| | 4 | 129 | | Fixed64 bestProjection = Vector2d.Dot(bestPoint, direction); |
| | 12 | 130 | | for (int i = 1; i < _partColliders.Length; i++) |
| | | 131 | | { |
| | 2 | 132 | | Vector2d candidate = _partColliders[i].GetSupportPoint(direction); |
| | 2 | 133 | | Fixed64 projection = Vector2d.Dot(candidate, direction); |
| | 2 | 134 | | if (projection <= bestProjection) |
| | | 135 | | continue; |
| | | 136 | | |
| | 1 | 137 | | bestProjection = projection; |
| | 1 | 138 | | bestPoint = candidate; |
| | | 139 | | } |
| | | 140 | | |
| | 4 | 141 | | return bestPoint; |
| | | 142 | | } |
| | | 143 | | |
| | | 144 | | internal override ExactMassPoint2D CalculateLocalMassPoint() => |
| | 513 | 145 | | CalculateAggregateMassPoint(usePrepared: false); |
| | | 146 | | |
| | | 147 | | internal override ExactMassPoint2D CalculatePreparedLocalMassPoint() => |
| | 200 | 148 | | CalculateAggregateMassPoint(usePrepared: true); |
| | | 149 | | |
| | | 150 | | private ExactMassPoint2D CalculateAggregateMassPoint(bool usePrepared) |
| | | 151 | | { |
| | 713 | 152 | | bool hasPositiveWeight = false; |
| | 4022 | 153 | | for (int i = 0; i < _partColliders.Length; i++) |
| | | 154 | | { |
| | 1298 | 155 | | LSCollider2D partCollider = _partColliders[i]; |
| | 1298 | 156 | | ExactMassWeight weight = usePrepared |
| | 1298 | 157 | | ? partCollider.CalculatePreparedAreaForMassProperties() |
| | 1298 | 158 | | : partCollider.CalculateAreaForMassProperties(); |
| | 1298 | 159 | | _massPointScratch[i] = usePrepared |
| | 1298 | 160 | | ? partCollider.CalculatePreparedLocalMassPoint() |
| | 1298 | 161 | | : partCollider.CalculateLocalMassPoint(); |
| | 1298 | 162 | | _massWeightScratch[i] = weight; |
| | 1298 | 163 | | hasPositiveWeight |= !weight.IsZero; |
| | | 164 | | } |
| | | 165 | | |
| | 713 | 166 | | if (!hasPositiveWeight) |
| | | 167 | | { |
| | 32 | 168 | | for (int i = 0; i < _massWeightScratch.Length; i++) |
| | 11 | 169 | | _massWeightScratch[i] = ExactMassWeight.One; |
| | | 170 | | } |
| | | 171 | | |
| | 713 | 172 | | if (!ExactMassPoint2D.TryGetWeightedAverage( |
| | 713 | 173 | | _massPointScratch, |
| | 713 | 174 | | _massWeightScratch, |
| | 713 | 175 | | out Vector2d center)) |
| | | 176 | | { |
| | 2 | 177 | | throw new InvalidOperationException( |
| | 2 | 178 | | usePrepared |
| | 2 | 179 | | ? "Prepared 2D compound mass-property point is outside the Fixed64 coordinate domain." |
| | 2 | 180 | | : "The 2D compound collider's center of mass is outside the Fixed64 coordinate domain."); |
| | | 181 | | } |
| | 711 | 182 | | return ExactMassPoint2D.FromPoint(center); |
| | | 183 | | } |
| | | 184 | | |
| | | 185 | | internal override ExactMassWeight CalculateAreaForMassProperties() => |
| | 2 | 186 | | CalculateAggregateMassWeight(usePrepared: false); |
| | | 187 | | |
| | | 188 | | internal override ExactMassWeight CalculatePreparedAreaForMassProperties() => |
| | 113 | 189 | | CalculateAggregateMassWeight(usePrepared: true); |
| | | 190 | | |
| | | 191 | | private ExactMassWeight CalculateAggregateMassWeight(bool usePrepared) |
| | | 192 | | { |
| | 115 | 193 | | ExactMassWeight totalWeight = ExactMassWeight.Zero; |
| | 678 | 194 | | for (int i = 0; i < _partColliders.Length; i++) |
| | | 195 | | { |
| | 224 | 196 | | ExactMassWeight weight = usePrepared |
| | 224 | 197 | | ? _partColliders[i].CalculatePreparedAreaForMassProperties() |
| | 224 | 198 | | : _partColliders[i].CalculateAreaForMassProperties(); |
| | 224 | 199 | | totalWeight = totalWeight.Add(weight); |
| | | 200 | | } |
| | | 201 | | |
| | 115 | 202 | | return totalWeight; |
| | | 203 | | } |
| | | 204 | | |
| | | 205 | | internal override Fixed64 CalculateCenterOfMassMoment(Fixed64 mass) |
| | | 206 | | { |
| | 168 | 207 | | Vector2d center = CalculateLocalCenterOfMassOffset(); |
| | 168 | 208 | | ExactMassWeight totalWeight = ExactMassWeight.Zero; |
| | 168 | 209 | | int residualPartIndex = _partColliders.Length - 1; |
| | 944 | 210 | | for (int i = 0; i < _partColliders.Length; i++) |
| | | 211 | | { |
| | 304 | 212 | | ExactMassWeight weight = |
| | 304 | 213 | | _partColliders[i].CalculateAreaForMassProperties(); |
| | 304 | 214 | | totalWeight = totalWeight.Add(weight); |
| | 304 | 215 | | residualPartIndex = i; |
| | | 216 | | } |
| | | 217 | | |
| | 168 | 218 | | bool useEqualWeights = totalWeight.IsZero; |
| | 168 | 219 | | if (useEqualWeights) |
| | | 220 | | { |
| | 2 | 221 | | totalWeight = ExactMassWeight.Zero; |
| | 12 | 222 | | for (int i = 0; i < _partColliders.Length; i++) |
| | 4 | 223 | | totalWeight = totalWeight.Add(ExactMassWeight.One); |
| | | 224 | | } |
| | | 225 | | |
| | 168 | 226 | | ExactMassWeight cumulativeWeight = ExactMassWeight.Zero; |
| | 168 | 227 | | Fixed64 assignedMass = Fixed64.Zero; |
| | 168 | 228 | | Fixed64 moment = Fixed64.Zero; |
| | 940 | 229 | | for (int i = 0; i < _partColliders.Length; i++) |
| | | 230 | | { |
| | 303 | 231 | | LSCollider2D part = _partColliders[i]; |
| | 303 | 232 | | ExactMassWeight weight = useEqualWeights |
| | 303 | 233 | | ? ExactMassWeight.One |
| | 303 | 234 | | : part.CalculateAreaForMassProperties(); |
| | 303 | 235 | | cumulativeWeight = cumulativeWeight.Add(weight); |
| | | 236 | | Fixed64 partMass; |
| | 303 | 237 | | if (i == residualPartIndex) |
| | | 238 | | { |
| | 167 | 239 | | partMass = mass - assignedMass; |
| | 167 | 240 | | assignedMass = mass; |
| | | 241 | | } |
| | | 242 | | else |
| | | 243 | | { |
| | 136 | 244 | | _ = cumulativeWeight.TryGetProportionalShare( |
| | 136 | 245 | | mass, |
| | 136 | 246 | | totalWeight, |
| | 136 | 247 | | out Fixed64 cumulativeMass); |
| | 136 | 248 | | partMass = cumulativeMass - assignedMass; |
| | 136 | 249 | | assignedMass = cumulativeMass; |
| | | 250 | | } |
| | | 251 | | |
| | 303 | 252 | | if (partMass == Fixed64.Zero) |
| | | 253 | | continue; |
| | | 254 | | |
| | 300 | 255 | | if (!part.CalculateLocalMassPoint() |
| | 300 | 256 | | .TryAddParallelAxisMoment( |
| | 300 | 257 | | part.CalculateCenterOfMassMoment(partMass), |
| | 300 | 258 | | partMass, |
| | 300 | 259 | | center, |
| | 300 | 260 | | out Fixed64 contribution) |
| | 300 | 261 | | || !Fixed64.TryAdd( |
| | 300 | 262 | | moment, |
| | 300 | 263 | | contribution, |
| | 300 | 264 | | out moment)) |
| | | 265 | | { |
| | 1 | 266 | | throw new InvalidOperationException( |
| | 1 | 267 | | "The 2D compound collider's moment of inertia is outside the Fixed64 scalar domain."); |
| | | 268 | | } |
| | | 269 | | } |
| | | 270 | | |
| | 167 | 271 | | return moment; |
| | | 272 | | } |
| | | 273 | | |
| | | 274 | | private protected override void PrepareShape(in ColliderShapeSnapshot2D snapshot) |
| | | 275 | | { |
| | 117 | 276 | | Vector2d min = Vector2d.Zero; |
| | 117 | 277 | | Vector2d max = Vector2d.Zero; |
| | | 278 | | |
| | 678 | 279 | | for (int i = 0; i < _parts.Length; i++) |
| | | 280 | | { |
| | 225 | 281 | | CompoundColliderPart2D part = _parts[i]; |
| | 225 | 282 | | LSCollider2D partCollider = _partColliders[i]; |
| | 225 | 283 | | partCollider.PrepareCompoundPart( |
| | 225 | 284 | | snapshot, |
| | 225 | 285 | | part.LocalRotation, |
| | 225 | 286 | | part.LocalScale, |
| | 225 | 287 | | PreparedContext); |
| | | 288 | | |
| | 222 | 289 | | Vector2d partMin = partCollider.PreparedShapeBounds.Min; |
| | 222 | 290 | | Vector2d partMax = partCollider.PreparedShapeBounds.Max; |
| | 222 | 291 | | if (i == 0) |
| | | 292 | | { |
| | 114 | 293 | | min = partMin; |
| | 114 | 294 | | max = partMax; |
| | 114 | 295 | | continue; |
| | | 296 | | } |
| | | 297 | | |
| | 108 | 298 | | min = new Vector2d(FixedMath.Min(min.X, partMin.X), FixedMath.Min(min.Y, partMin.Y)); |
| | 108 | 299 | | max = new Vector2d(FixedMath.Max(max.X, partMax.X), FixedMath.Max(max.Y, partMax.Y)); |
| | | 300 | | } |
| | | 301 | | |
| | 114 | 302 | | _ = CalculatePreparedLocalMassPoint(); |
| | 113 | 303 | | _ = CalculatePreparedAreaForMassProperties(); |
| | 113 | 304 | | SetPreparedBounds(FixedBoundArea.FromMinMax(min, max)); |
| | 113 | 305 | | } |
| | | 306 | | |
| | | 307 | | private protected override void PublishShape() |
| | | 308 | | { |
| | 662 | 309 | | for (int i = 0; i < _parts.Length; i++) |
| | | 310 | | { |
| | 219 | 311 | | CompoundColliderPart2D part = _parts[i]; |
| | 219 | 312 | | _partColliders[i].PublishCompoundPart( |
| | 219 | 313 | | part.LocalRotation, |
| | 219 | 314 | | part.LocalScale, |
| | 219 | 315 | | PreparedContext); |
| | | 316 | | } |
| | 112 | 317 | | } |
| | | 318 | | |
| | | 319 | | private static LSCollider2D MaterializePartCollider(CompoundColliderPart2D part) |
| | | 320 | | { |
| | 247 | 321 | | LSCollider2D collider = part.Shape.CreateRuntimeCollider(); |
| | 247 | 322 | | collider.LocalOffset = part.LocalOffset; |
| | 247 | 323 | | collider.Material = part.ResolveMaterial(PhysicsMaterial.Default); |
| | 247 | 324 | | return collider; |
| | | 325 | | } |
| | | 326 | | |
| | | 327 | | /// <inheritdoc/> |
| | | 328 | | protected override void OnMaterialChanged() |
| | | 329 | | { |
| | 12 | 330 | | for (int i = 0; i < _parts.Length; i++) |
| | 3 | 331 | | _partColliders[i].Material = _parts[i].ResolveMaterial(Material); |
| | 3 | 332 | | } |
| | | 333 | | |
| | | 334 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 335 | | private static void ValidatePart(CompoundColliderPart2D part) => |
| | 248 | 336 | | SwiftThrowHelper.ThrowIfArgument(part.IsDefault, nameof(part), "2D compound collider part cannot be default."); |
| | | 337 | | } |