| | | 1 | | //======================================================================= |
| | | 2 | | // LSPolygonCollider2D.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 Chronicler; |
| | | 10 | | using FixedMathSharp; |
| | | 11 | | using FixedMathSharp.Geometry; |
| | | 12 | | |
| | | 13 | | namespace Gravitas.Colliders; |
| | | 14 | | |
| | | 15 | | /// <summary> |
| | | 16 | | /// Pure 2D convex polygon collider with deterministic vertex ordering. |
| | | 17 | | /// </summary> |
| | | 18 | | public sealed class LSPolygonCollider2D : LSCollider2D, IConvexVertexSource2D |
| | | 19 | | { |
| | | 20 | | private Vector2d[] _localVertices; |
| | | 21 | | private Vector2d[] _scaledLocalVertices; |
| | | 22 | | private Vector2d[] _scaledLocalVerticesScratch; |
| | | 23 | | private PolygonMassProperties2D _massProperties; |
| | | 24 | | private PolygonMassProperties2D _preparedMassProperties; |
| | | 25 | | |
| | | 26 | | /// <summary>Creates a pure 2D convex polygon from authored local vertices.</summary> |
| | 1118 | 27 | | public LSPolygonCollider2D(params Vector2d[] vertices) |
| | | 28 | | { |
| | 1118 | 29 | | _localVertices = Array.Empty<Vector2d>(); |
| | 1118 | 30 | | _scaledLocalVertices = Array.Empty<Vector2d>(); |
| | 1118 | 31 | | _scaledLocalVerticesScratch = Array.Empty<Vector2d>(); |
| | 1118 | 32 | | SetLocalVertices(vertices, markDirty: true); |
| | 1114 | 33 | | } |
| | | 34 | | |
| | | 35 | | /// <summary>Creates a runtime convex polygon from an authored shape definition.</summary> |
| | 112 | 36 | | public LSPolygonCollider2D(ColliderShapeDefinition2D definition) |
| | | 37 | | { |
| | 112 | 38 | | definition.EnsureKind(ColliderShapeDefinition2DKind.ConvexPolygon); |
| | 112 | 39 | | Material = definition.Material; |
| | 112 | 40 | | _localVertices = Array.Empty<Vector2d>(); |
| | 112 | 41 | | _scaledLocalVertices = Array.Empty<Vector2d>(); |
| | 112 | 42 | | _scaledLocalVerticesScratch = Array.Empty<Vector2d>(); |
| | 112 | 43 | | SetLocalVertices(definition.GetPolygonVerticesForRuntime(), markDirty: true); |
| | 112 | 44 | | } |
| | | 45 | | |
| | | 46 | | /// <inheritdoc/> |
| | 142375 | 47 | | public override ColliderType2D Shape => ColliderType2D.ConvexPolygon; |
| | | 48 | | |
| | | 49 | | /// <summary>Gets the polygon vertex count.</summary> |
| | 268 | 50 | | public int Count => _scaledLocalVertices.Length; |
| | | 51 | | |
| | | 52 | | /// <summary> |
| | | 53 | | /// Gets the committed vertices in the collider's scaled local frame. |
| | | 54 | | /// </summary> |
| | 19741 | 55 | | internal ReadOnlySpan<Vector2d> ScaledLocalVertices => _scaledLocalVertices; |
| | | 56 | | |
| | 105341 | 57 | | int IConvexVertexSource2D.VertexCount => _scaledLocalVertices.Length; |
| | | 58 | | |
| | 100923 | 59 | | Fixed64 IConvexVertexSource2D.Rotation => Rotation; |
| | | 60 | | |
| | | 61 | | /// <summary> |
| | | 62 | | /// Gets a world-space vertex when the conceptual point is representable. |
| | | 63 | | /// </summary> |
| | | 64 | | /// <exception cref="InvalidOperationException"> |
| | | 65 | | /// Thrown when the conceptual vertex lies outside the fixed-point scalar |
| | | 66 | | /// domain. Use <see cref="TryGetWorldVertex(int, out Vector2d)"/> when |
| | | 67 | | /// querying geometry near a scalar boundary. |
| | | 68 | | /// </exception> |
| | | 69 | | public Vector2d GetWorldVertex(int index) |
| | | 70 | | { |
| | 238 | 71 | | if (TryGetWorldVertex(index, out Vector2d vertex)) |
| | 236 | 72 | | return vertex; |
| | | 73 | | |
| | 2 | 74 | | throw new InvalidOperationException( |
| | 2 | 75 | | "The polygon vertex is outside the representable coordinate range. Use TryGetWorldVertex."); |
| | | 76 | | } |
| | | 77 | | |
| | | 78 | | /// <summary> |
| | | 79 | | /// Attempts to materialize a committed polygon vertex in world space |
| | | 80 | | /// without saturation. |
| | | 81 | | /// </summary> |
| | | 82 | | public bool TryGetWorldVertex(int index, out Vector2d vertex) |
| | | 83 | | { |
| | 244 | 84 | | ChronicleGuard.ThrowIfArrayIndexInvalid( |
| | 244 | 85 | | index, |
| | 244 | 86 | | _scaledLocalVertices.Length, |
| | 244 | 87 | | nameof(index)); |
| | 244 | 88 | | return TryGetVertex(index, out vertex); |
| | | 89 | | } |
| | | 90 | | |
| | | 91 | | /// <inheritdoc/> |
| | | 92 | | public override bool ContainsPoint(Vector2d point) => |
| | 1846 | 93 | | FixedConvex2dRelations.ContainsPoint( |
| | 1846 | 94 | | point, |
| | 1846 | 95 | | Center, |
| | 1846 | 96 | | Rotation, |
| | 1846 | 97 | | _scaledLocalVertices); |
| | | 98 | | |
| | | 99 | | /// <inheritdoc/> |
| | | 100 | | public override Vector2d GetClosestPoint(Vector2d point) |
| | | 101 | | { |
| | 4 | 102 | | if (ContainsPoint(point)) |
| | 2 | 103 | | return point; |
| | | 104 | | |
| | 2 | 105 | | FixedPointAnchor2d anchor = |
| | 2 | 106 | | FixedConvex2dRelations.GetClosestPointAnchor( |
| | 2 | 107 | | point, |
| | 2 | 108 | | Center, |
| | 2 | 109 | | Rotation, |
| | 2 | 110 | | _scaledLocalVertices); |
| | 2 | 111 | | if (anchor.TryGetPoint(out Vector2d closest)) |
| | | 112 | | { |
| | 1 | 113 | | return closest; |
| | | 114 | | } |
| | | 115 | | |
| | 1 | 116 | | throw new InvalidOperationException( |
| | 1 | 117 | | "The closest polygon point is outside the Fixed64 coordinate domain."); |
| | | 118 | | } |
| | | 119 | | |
| | | 120 | | /// <inheritdoc/> |
| | | 121 | | public override Vector2d GetSupportPoint(Vector2d direction) |
| | | 122 | | { |
| | 6 | 123 | | FixedPointAnchor2d anchor = FixedConvex2dRelations.GetSupportAnchor( |
| | 6 | 124 | | Center, |
| | 6 | 125 | | Rotation, |
| | 6 | 126 | | _scaledLocalVertices, |
| | 6 | 127 | | direction); |
| | 6 | 128 | | if (anchor.TryGetPoint(out Vector2d support)) |
| | | 129 | | { |
| | 4 | 130 | | return support; |
| | | 131 | | } |
| | | 132 | | |
| | 2 | 133 | | throw new InvalidOperationException( |
| | 2 | 134 | | "The polygon support point is outside the Fixed64 coordinate domain."); |
| | | 135 | | } |
| | | 136 | | |
| | | 137 | | Vector2d IConvexVertexSource2D.GetScaledLocalVertexUnchecked(int index) => |
| | 84104 | 138 | | _scaledLocalVertices[index]; |
| | | 139 | | |
| | | 140 | | FixedPointAnchor2d IConvexVertexSource2D.GetSupportAnchor(Vector2d direction) => |
| | 2 | 141 | | FixedConvex2dRelations.GetSupportAnchor( |
| | 2 | 142 | | Center, |
| | 2 | 143 | | Rotation, |
| | 2 | 144 | | _scaledLocalVertices, |
| | 2 | 145 | | direction); |
| | | 146 | | |
| | | 147 | | internal override ExactMassPoint2D CalculateLocalMassPoint() |
| | | 148 | | { |
| | 13370 | 149 | | return TransformRelativeMassPropertyPointExact(GetMassProperties().Centroid); |
| | | 150 | | } |
| | | 151 | | |
| | | 152 | | internal override ExactMassPoint2D CalculatePreparedLocalMassPoint() |
| | | 153 | | { |
| | 982 | 154 | | return TransformPreparedPartMassPropertyPointExact( |
| | 982 | 155 | | _preparedMassProperties.Centroid); |
| | | 156 | | } |
| | | 157 | | |
| | | 158 | | internal override ExactMassWeight CalculateAreaForMassProperties() |
| | | 159 | | { |
| | 148 | 160 | | return GetMassProperties().Weight; |
| | | 161 | | } |
| | | 162 | | |
| | | 163 | | internal override ExactMassWeight CalculatePreparedAreaForMassProperties() |
| | | 164 | | { |
| | 1964 | 165 | | return _preparedMassProperties.Weight; |
| | | 166 | | } |
| | | 167 | | |
| | | 168 | | internal override Fixed64 CalculateCenterOfMassMoment(Fixed64 mass) |
| | | 169 | | { |
| | 1 | 170 | | return GetMassProperties().CalculateMoment(mass); |
| | | 171 | | } |
| | | 172 | | |
| | | 173 | | /// <inheritdoc/> |
| | | 174 | | public override Fixed64 CalculateMomentOfInertia(Fixed64 mass, Vector2d localReferencePoint) |
| | | 175 | | { |
| | 13327 | 176 | | if (mass <= Fixed64.Zero) |
| | 1 | 177 | | return Fixed64.Zero; |
| | | 178 | | |
| | 13326 | 179 | | PolygonMassProperties2D properties = GetMassProperties(); |
| | | 180 | | // Detached scaling may collapse area before shape admission. Retain the |
| | | 181 | | // base point-mass limit rather than dividing by a zero polygon weight. |
| | 13326 | 182 | | if (properties.Weight.IsZero) |
| | 1 | 183 | | return base.CalculateMomentOfInertia(mass, localReferencePoint); |
| | | 184 | | |
| | 13325 | 185 | | ExactMassPoint2D point = TransformRelativeMassPropertyPointExact(properties.Centroid); |
| | 13325 | 186 | | ExactMassPoint2D reference = ExactMassPoint2D.FromPoint(localReferencePoint); |
| | 13325 | 187 | | Signed320 dx = WideArithmetic.SubtractSigned320(point.XNumerator, reference.XNumerator); |
| | 13325 | 188 | | Signed320 dy = WideArithmetic.SubtractSigned320(point.YNumerator, reference.YNumerator); |
| | 13325 | 189 | | if (dx.IsZero && dy.IsZero) |
| | 13143 | 190 | | return properties.CalculateMoment(mass); |
| | | 191 | | |
| | 182 | 192 | | if (properties.TryCalculateMoment(mass, CompoundLocalRotation, dx, dy, out Fixed64 moment)) |
| | 178 | 193 | | return moment; |
| | | 194 | | |
| | 4 | 195 | | if (!point.TryGetPoint(out _)) |
| | 1 | 196 | | throw new InvalidOperationException("The requested moment of inertia is outside the Fixed64 scalar domain.") |
| | 3 | 197 | | return Fixed64.MaxValue; |
| | | 198 | | } |
| | | 199 | | |
| | | 200 | | internal Fixed64 GetCurrentGeometryReach() |
| | | 201 | | { |
| | 9 | 202 | | GetCurrentScaleFactors(out Vector2d ownerScale, out Vector2d partScale); |
| | 9 | 203 | | return GetCandidateGeometryReach(ownerScale, partScale); |
| | | 204 | | } |
| | | 205 | | |
| | | 206 | | internal Fixed64 GetCandidateGeometryReach(Vector2d ownerScale, Vector2d partScale) |
| | | 207 | | { |
| | 14 | 208 | | Fixed64 reach = Fixed64.Zero; |
| | 110 | 209 | | for (int i = 0; i < _localVertices.Length; i++) |
| | | 210 | | { |
| | 44 | 211 | | Vector2d vertex = _localVertices[i]; |
| | | 212 | | // Detached metadata can precede a compensating host scale; do not |
| | | 213 | | // publish or reject runtime geometry while measuring its enclosure. |
| | 44 | 214 | | if (!Fixed64.TryMultiplyDivide(vertex.X, ownerScale.X, partScale.X, |
| | 44 | 215 | | Fixed64.One, out Fixed64 x) |
| | 44 | 216 | | || !Fixed64.TryMultiplyDivide(vertex.Y, ownerScale.Y, partScale.Y, |
| | 44 | 217 | | Fixed64.One, out Fixed64 y) |
| | 44 | 218 | | || !new Vector2d(x, y).TryGetMagnitudeCeiling(out Fixed64 vertexReach)) |
| | 3 | 219 | | return Fixed64.MaxValue; |
| | 41 | 220 | | reach = FixedMath.Max(reach, vertexReach); |
| | | 221 | | } |
| | 11 | 222 | | return reach; |
| | | 223 | | } |
| | | 224 | | |
| | | 225 | | private protected override void PrepareShape(in ColliderShapeSnapshot2D snapshot) |
| | | 226 | | { |
| | 15629 | 227 | | Fixed64 reach = Fixed64.Zero; |
| | 15629 | 228 | | bool verticesChanged = !HasCommittedShape |
| | 15629 | 229 | | || _scaledLocalVertices.Length != _localVertices.Length; |
| | 157118 | 230 | | for (int i = 0; i < _localVertices.Length; i++) |
| | | 231 | | { |
| | 62930 | 232 | | Vector2d scaledVertex = ColliderScalePolicy.Scale( |
| | 62930 | 233 | | _localVertices[i], |
| | 62930 | 234 | | snapshot.OwnerScale, |
| | 62930 | 235 | | snapshot.PartScale); |
| | 62930 | 236 | | verticesChanged = verticesChanged || scaledVertex != _scaledLocalVertices[i]; |
| | 62930 | 237 | | _scaledLocalVerticesScratch[i] = scaledVertex; |
| | 62930 | 238 | | reach = FixedMath.Max(reach, ColliderGeometryPolicy.Magnitude(scaledVertex)); |
| | | 239 | | } |
| | | 240 | | |
| | | 241 | | // Positive scaling can collapse a small edge through rounding. Validate |
| | | 242 | | // before publication, but reuse admission when only the pose changes. |
| | 15629 | 243 | | if (verticesChanged) |
| | | 244 | | { |
| | 1199 | 245 | | ValidateConvexPolygon(_scaledLocalVerticesScratch); |
| | 1195 | 246 | | _preparedMassProperties = PolygonMassProperties2D.Create(_scaledLocalVerticesScratch); |
| | | 247 | | } |
| | | 248 | | else |
| | | 249 | | { |
| | | 250 | | // Intrinsic integrals depend on effective geometry, not world pose. |
| | 14430 | 251 | | _preparedMassProperties = _massProperties; |
| | | 252 | | } |
| | | 253 | | |
| | 15625 | 254 | | SetPreparedGeometryReach(reach); |
| | 15625 | 255 | | SetPreparedBounds(FixedBoundArea.FromRotatedOffsetsClippedToDomain( |
| | 15625 | 256 | | snapshot.Center, |
| | 15625 | 257 | | snapshot.Rotation, |
| | 15625 | 258 | | _scaledLocalVerticesScratch)); |
| | 15625 | 259 | | } |
| | | 260 | | |
| | | 261 | | private protected override void PublishShape() |
| | | 262 | | { |
| | 15389 | 263 | | Vector2d[] offsets = _scaledLocalVertices; |
| | 15389 | 264 | | if (offsets.Length != _scaledLocalVerticesScratch.Length) |
| | 3 | 265 | | offsets = new Vector2d[_scaledLocalVerticesScratch.Length]; |
| | 15389 | 266 | | _scaledLocalVertices = _scaledLocalVerticesScratch; |
| | 15389 | 267 | | _scaledLocalVerticesScratch = offsets; |
| | 15389 | 268 | | _massProperties = _preparedMassProperties; |
| | 15389 | 269 | | } |
| | | 270 | | |
| | | 271 | | /// <inheritdoc/> |
| | | 272 | | protected override void RecordShapeData(IChronicler chronicler) |
| | | 273 | | { |
| | 8 | 274 | | Vector2d[] vertices = _localVertices; |
| | 8 | 275 | | RecordValues.Look(chronicler, ref vertices, "Vertices", Array.Empty<Vector2d>()); |
| | 8 | 276 | | if (chronicler.Mode == SerializationMode.Loading && vertices.Length > 0) |
| | 5 | 277 | | SetLocalVertices(vertices, markDirty: false); |
| | 8 | 278 | | } |
| | | 279 | | |
| | | 280 | | private void SetLocalVertices(Vector2d[] vertices, bool markDirty) |
| | | 281 | | { |
| | 1235 | 282 | | ChronicleGuard.ThrowIfNull(vertices, nameof(vertices)); |
| | 1235 | 283 | | ChronicleGuard.ThrowIfArgument(vertices.Length < 3, nameof(vertices), "2D polygon must contain at least three ve |
| | 1234 | 284 | | ValidateConvexPolygon(vertices); |
| | | 285 | | |
| | 1231 | 286 | | if (_localVertices.Length != vertices.Length) |
| | | 287 | | { |
| | 1229 | 288 | | _localVertices = new Vector2d[vertices.Length]; |
| | | 289 | | // A load may change vertex count. Retain committed query geometry |
| | | 290 | | // until the replacement has passed scaled admission and publishes. |
| | 1229 | 291 | | if (!HasCommittedShape) |
| | 1226 | 292 | | _scaledLocalVertices = new Vector2d[vertices.Length]; |
| | 1229 | 293 | | _scaledLocalVerticesScratch = new Vector2d[vertices.Length]; |
| | | 294 | | } |
| | | 295 | | |
| | 1231 | 296 | | Array.Copy(vertices, _localVertices, vertices.Length); |
| | 1231 | 297 | | if (markDirty) |
| | 1226 | 298 | | MarkShapeDirty(); |
| | 1231 | 299 | | } |
| | | 300 | | |
| | | 301 | | internal static void ValidateConvexPolygon(Vector2d[] vertices) |
| | | 302 | | { |
| | 2556 | 303 | | ChronicleGuard.ThrowIfArgument(vertices.Length < 3, nameof(vertices), "2D polygon must contain at least three ve |
| | 2556 | 304 | | ChronicleGuard.ThrowIfArgument( |
| | 2556 | 305 | | !FixedConvex2dRelations.IsStrictlyConvex(vertices), |
| | 2556 | 306 | | nameof(vertices), |
| | 2556 | 307 | | "2D polygon vertices must form a strictly convex boundary."); |
| | 2548 | 308 | | } |
| | | 309 | | |
| | 26910 | 310 | | internal PolygonMassProperties2D GetMassProperties() => HasCommittedShape |
| | 26910 | 311 | | ? _massProperties |
| | 26910 | 312 | | : PolygonMassProperties2D.Create(GetMassPropertyVertices()); |
| | | 313 | | |
| | | 314 | | private ReadOnlySpan<Vector2d> GetMassPropertyVertices() |
| | | 315 | | { |
| | 259 | 316 | | GetCurrentScaleFactors( |
| | 259 | 317 | | out Vector2d ownerScale, |
| | 259 | 318 | | out Vector2d partScale); |
| | 2180 | 319 | | for (int i = 0; i < _localVertices.Length; i++) |
| | | 320 | | { |
| | 831 | 321 | | _scaledLocalVerticesScratch[i] = ColliderScalePolicy.Scale( |
| | 831 | 322 | | _localVertices[i], |
| | 831 | 323 | | ownerScale, |
| | 831 | 324 | | partScale); |
| | | 325 | | } |
| | | 326 | | |
| | 259 | 327 | | return _scaledLocalVerticesScratch; |
| | | 328 | | } |
| | | 329 | | } |