| | | 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 Chronicler; |
| | | 9 | | using FixedMathSharp; |
| | | 10 | | using FixedMathSharp.Geometry; |
| | | 11 | | using Gravitas.CollisionHandling; |
| | | 12 | | using System; |
| | | 13 | | |
| | | 14 | | namespace Gravitas.Colliders; |
| | | 15 | | |
| | | 16 | | /// <summary> |
| | | 17 | | /// Pure 2D convex polygon collider with deterministic vertex ordering. |
| | | 18 | | /// </summary> |
| | | 19 | | public sealed class LSPolygonCollider2D : LSCollider2D, IConvexVertexSource2D |
| | | 20 | | { |
| | | 21 | | private Vector2d[] _localVertices; |
| | | 22 | | private Vector2d[] _scaledLocalVertices; |
| | | 23 | | private Vector2d[] _scaledLocalVerticesScratch; |
| | | 24 | | |
| | | 25 | | /// <summary>Creates a pure 2D convex polygon from authored local vertices.</summary> |
| | 622 | 26 | | public LSPolygonCollider2D(params Vector2d[] vertices) |
| | | 27 | | { |
| | 622 | 28 | | _localVertices = Array.Empty<Vector2d>(); |
| | 622 | 29 | | _scaledLocalVertices = Array.Empty<Vector2d>(); |
| | 622 | 30 | | _scaledLocalVerticesScratch = Array.Empty<Vector2d>(); |
| | 622 | 31 | | SetLocalVertices(vertices, markDirty: true); |
| | 618 | 32 | | } |
| | | 33 | | |
| | | 34 | | /// <summary>Creates a runtime convex polygon from an authored shape definition.</summary> |
| | 25 | 35 | | public LSPolygonCollider2D(ColliderShapeDefinition2D definition) |
| | | 36 | | { |
| | 25 | 37 | | definition.EnsureKind(ColliderShapeDefinition2DKind.ConvexPolygon); |
| | 25 | 38 | | Material = definition.Material; |
| | 25 | 39 | | _localVertices = Array.Empty<Vector2d>(); |
| | 25 | 40 | | _scaledLocalVertices = Array.Empty<Vector2d>(); |
| | 25 | 41 | | _scaledLocalVerticesScratch = Array.Empty<Vector2d>(); |
| | 25 | 42 | | SetLocalVertices(definition.GetPolygonVerticesForRuntime(), markDirty: true); |
| | 25 | 43 | | } |
| | | 44 | | |
| | | 45 | | /// <inheritdoc/> |
| | 2918 | 46 | | public override ColliderType2D Shape => ColliderType2D.ConvexPolygon; |
| | | 47 | | |
| | | 48 | | /// <summary>Gets the polygon vertex count.</summary> |
| | 16 | 49 | | public int Count => _scaledLocalVertices.Length; |
| | | 50 | | |
| | | 51 | | /// <summary> |
| | | 52 | | /// Gets the committed vertices in the collider's scaled local frame. |
| | | 53 | | /// </summary> |
| | 6486 | 54 | | internal ReadOnlySpan<Vector2d> ScaledLocalVertices => _scaledLocalVertices; |
| | | 55 | | |
| | 60080 | 56 | | int IConvexVertexSource2D.VertexCount => _scaledLocalVertices.Length; |
| | | 57 | | |
| | 25820 | 58 | | Fixed64 IConvexVertexSource2D.Rotation => Rotation; |
| | | 59 | | |
| | | 60 | | /// <summary> |
| | | 61 | | /// Gets a world-space vertex when the conceptual point is representable. |
| | | 62 | | /// </summary> |
| | | 63 | | /// <exception cref="InvalidOperationException"> |
| | | 64 | | /// Thrown when the conceptual vertex lies outside the fixed-point scalar |
| | | 65 | | /// domain. Use <see cref="TryGetWorldVertex(int, out Vector2d)"/> when |
| | | 66 | | /// querying geometry near a scalar boundary. |
| | | 67 | | /// </exception> |
| | | 68 | | public Vector2d GetWorldVertex(int index) |
| | | 69 | | { |
| | 30 | 70 | | if (TryGetWorldVertex(index, out Vector2d vertex)) |
| | 28 | 71 | | return vertex; |
| | | 72 | | |
| | 2 | 73 | | throw new InvalidOperationException( |
| | 2 | 74 | | "The polygon vertex is outside the representable coordinate range. Use TryGetWorldVertex."); |
| | | 75 | | } |
| | | 76 | | |
| | | 77 | | /// <summary> |
| | | 78 | | /// Attempts to materialize a committed polygon vertex in world space |
| | | 79 | | /// without saturation. |
| | | 80 | | /// </summary> |
| | | 81 | | public bool TryGetWorldVertex(int index, out Vector2d vertex) |
| | | 82 | | { |
| | 40 | 83 | | SwiftThrowHelper.ThrowIfArrayIndexInvalid( |
| | 40 | 84 | | index, |
| | 40 | 85 | | _scaledLocalVertices.Length, |
| | 40 | 86 | | nameof(index)); |
| | 40 | 87 | | return TryGetVertex(index, out vertex); |
| | | 88 | | } |
| | | 89 | | |
| | | 90 | | /// <inheritdoc/> |
| | | 91 | | public override bool ContainsPoint(Vector2d point) => |
| | 1126 | 92 | | FixedConvex2dRelations.ContainsPoint( |
| | 1126 | 93 | | point, |
| | 1126 | 94 | | Center, |
| | 1126 | 95 | | Rotation, |
| | 1126 | 96 | | _scaledLocalVertices); |
| | | 97 | | |
| | | 98 | | /// <inheritdoc/> |
| | | 99 | | public override Vector2d GetClosestPoint(Vector2d point) |
| | | 100 | | { |
| | 4 | 101 | | if (ContainsPoint(point)) |
| | 2 | 102 | | return point; |
| | | 103 | | |
| | 2 | 104 | | FixedPointAnchor2d anchor = |
| | 2 | 105 | | FixedConvex2dRelations.GetClosestPointAnchor( |
| | 2 | 106 | | point, |
| | 2 | 107 | | Center, |
| | 2 | 108 | | Rotation, |
| | 2 | 109 | | _scaledLocalVertices); |
| | 2 | 110 | | if (anchor.TryGetPoint(out Vector2d closest)) |
| | | 111 | | { |
| | 1 | 112 | | return closest; |
| | | 113 | | } |
| | | 114 | | |
| | 1 | 115 | | throw new InvalidOperationException( |
| | 1 | 116 | | "The closest polygon point is outside the Fixed64 coordinate domain."); |
| | | 117 | | } |
| | | 118 | | |
| | | 119 | | /// <inheritdoc/> |
| | | 120 | | public override Vector2d GetSupportPoint(Vector2d direction) |
| | | 121 | | { |
| | 6 | 122 | | FixedPointAnchor2d anchor = FixedConvex2dRelations.GetSupportAnchor( |
| | 6 | 123 | | Center, |
| | 6 | 124 | | Rotation, |
| | 6 | 125 | | _scaledLocalVertices, |
| | 6 | 126 | | direction); |
| | 6 | 127 | | if (anchor.TryGetPoint(out Vector2d support)) |
| | | 128 | | { |
| | 4 | 129 | | return support; |
| | | 130 | | } |
| | | 131 | | |
| | 2 | 132 | | throw new InvalidOperationException( |
| | 2 | 133 | | "The polygon support point is outside the Fixed64 coordinate domain."); |
| | | 134 | | } |
| | | 135 | | |
| | | 136 | | Vector2d IConvexVertexSource2D.GetScaledLocalVertexUnchecked(int index) => |
| | 44897 | 137 | | _scaledLocalVertices[index]; |
| | | 138 | | |
| | | 139 | | FixedPointAnchor2d IConvexVertexSource2D.GetSupportAnchor(Vector2d direction) => |
| | 49 | 140 | | FixedConvex2dRelations.GetSupportAnchor( |
| | 49 | 141 | | Center, |
| | 49 | 142 | | Rotation, |
| | 49 | 143 | | _scaledLocalVertices, |
| | 49 | 144 | | direction); |
| | | 145 | | |
| | | 146 | | internal override ExactMassPoint2D CalculateLocalMassPoint() |
| | | 147 | | { |
| | 12001 | 148 | | _ = TryCalculateIntrinsicSignedAreaAndCentroid( |
| | 12001 | 149 | | out _, |
| | 12001 | 150 | | out Vector2d intrinsicCentroid); |
| | 12001 | 151 | | return TransformRelativeMassPropertyPointExact(intrinsicCentroid); |
| | | 152 | | } |
| | | 153 | | |
| | | 154 | | internal override ExactMassPoint2D CalculatePreparedLocalMassPoint() |
| | | 155 | | { |
| | 5448 | 156 | | _ = PolygonMassProperties2D.TryGetWeightAndCentroid( |
| | 5448 | 157 | | _scaledLocalVerticesScratch, |
| | 5448 | 158 | | out _, |
| | 5448 | 159 | | out Vector2d intrinsicCentroid); |
| | 5448 | 160 | | return TransformPreparedRelativeMassPropertyPointExact( |
| | 5448 | 161 | | intrinsicCentroid); |
| | | 162 | | } |
| | | 163 | | |
| | | 164 | | internal override ExactMassWeight CalculateAreaForMassProperties() |
| | | 165 | | { |
| | 53 | 166 | | ReadOnlySpan<Vector2d> vertices = GetMassPropertyVertices(); |
| | 53 | 167 | | _ = PolygonMassProperties2D.TryGetWeightAndCentroid( |
| | 53 | 168 | | vertices, |
| | 53 | 169 | | out ExactMassWeight weight, |
| | 53 | 170 | | out _); |
| | 53 | 171 | | return weight; |
| | | 172 | | } |
| | | 173 | | |
| | | 174 | | internal override ExactMassWeight CalculatePreparedAreaForMassProperties() |
| | | 175 | | { |
| | 49 | 176 | | _ = PolygonMassProperties2D.TryGetWeightAndCentroid( |
| | 49 | 177 | | _scaledLocalVerticesScratch, |
| | 49 | 178 | | out ExactMassWeight weight, |
| | 49 | 179 | | out _); |
| | 49 | 180 | | return weight; |
| | | 181 | | } |
| | | 182 | | |
| | | 183 | | internal override Fixed64 CalculateCenterOfMassMoment(Fixed64 mass) |
| | | 184 | | { |
| | 6028 | 185 | | ReadOnlySpan<Vector2d> vertices = GetMassPropertyVertices(); |
| | 6028 | 186 | | _ = FixedConvex2dRelations.TryGetAreaAndCentroid( |
| | 6028 | 187 | | vertices, |
| | 6028 | 188 | | out Fixed64 area, |
| | 6028 | 189 | | out Vector2d intrinsicCenterOfMass); |
| | 6028 | 190 | | if (area <= Fixed64.Zero) |
| | 4 | 191 | | return Fixed64.Zero; |
| | | 192 | | |
| | 6024 | 193 | | Fixed64 density = mass / area; |
| | 6024 | 194 | | Fixed64 centeredIntegral = Fixed64.Zero; |
| | 60180 | 195 | | for (int i = 0; i < vertices.Length; i++) |
| | | 196 | | { |
| | 24066 | 197 | | Vector2d a = vertices[i] - intrinsicCenterOfMass; |
| | 24066 | 198 | | Vector2d b = |
| | 24066 | 199 | | vertices[(i + 1) % vertices.Length] - intrinsicCenterOfMass; |
| | 24066 | 200 | | Fixed64 cross = Vector2d.CrossProduct(a, b); |
| | 24066 | 201 | | Fixed64 term = |
| | 24066 | 202 | | a.MagnitudeSquared + |
| | 24066 | 203 | | Vector2d.Dot(a, b) + |
| | 24066 | 204 | | b.MagnitudeSquared; |
| | 24066 | 205 | | centeredIntegral += cross * term; |
| | | 206 | | } |
| | | 207 | | |
| | 6024 | 208 | | return (density * centeredIntegral).Abs() / (Fixed64)12; |
| | | 209 | | } |
| | | 210 | | |
| | | 211 | | private protected override void PrepareShape(in ColliderShapeSnapshot2D snapshot) |
| | | 212 | | { |
| | 54502 | 213 | | for (int i = 0; i < _localVertices.Length; i++) |
| | | 214 | | { |
| | 21796 | 215 | | Vector2d scaledVertex = ColliderScalePolicy.Scale( |
| | 21796 | 216 | | _localVertices[i], |
| | 21796 | 217 | | snapshot.OwnerScale, |
| | 21796 | 218 | | snapshot.PartScale); |
| | 21796 | 219 | | _scaledLocalVerticesScratch[i] = scaledVertex; |
| | | 220 | | } |
| | | 221 | | |
| | 5455 | 222 | | SetPreparedBounds(FixedBoundArea.FromRotatedOffsetsClippedToDomain( |
| | 5455 | 223 | | snapshot.Center, |
| | 5455 | 224 | | snapshot.Rotation, |
| | 5455 | 225 | | _scaledLocalVerticesScratch)); |
| | 5455 | 226 | | } |
| | | 227 | | |
| | | 228 | | private protected override void PublishShape() |
| | | 229 | | { |
| | 5443 | 230 | | Vector2d[] offsets = _scaledLocalVertices; |
| | 5443 | 231 | | _scaledLocalVertices = _scaledLocalVerticesScratch; |
| | 5443 | 232 | | _scaledLocalVerticesScratch = offsets; |
| | 5443 | 233 | | } |
| | | 234 | | |
| | | 235 | | /// <inheritdoc/> |
| | | 236 | | protected override void RecordShapeData(IChronicler chronicler) |
| | | 237 | | { |
| | 6 | 238 | | Vector2d[] vertices = _localVertices; |
| | 6 | 239 | | RecordValues.Look(chronicler, ref vertices, "Vertices", Array.Empty<Vector2d>()); |
| | 6 | 240 | | if (chronicler.Mode == SerializationMode.Loading && vertices.Length > 0) |
| | 3 | 241 | | SetLocalVertices(vertices, markDirty: false); |
| | 6 | 242 | | } |
| | | 243 | | |
| | | 244 | | private void SetLocalVertices(Vector2d[] vertices, bool markDirty) |
| | | 245 | | { |
| | 650 | 246 | | SwiftThrowHelper.ThrowIfNull(vertices, nameof(vertices)); |
| | 650 | 247 | | SwiftThrowHelper.ThrowIfArgument(vertices.Length < 3, nameof(vertices), "2D polygon must contain at least three |
| | 649 | 248 | | ValidateConvexPolygon(vertices); |
| | | 249 | | |
| | 646 | 250 | | if (_localVertices.Length != vertices.Length) |
| | | 251 | | { |
| | 645 | 252 | | _localVertices = new Vector2d[vertices.Length]; |
| | 645 | 253 | | _scaledLocalVertices = new Vector2d[vertices.Length]; |
| | 645 | 254 | | _scaledLocalVerticesScratch = new Vector2d[vertices.Length]; |
| | | 255 | | } |
| | | 256 | | |
| | 646 | 257 | | Array.Copy(vertices, _localVertices, vertices.Length); |
| | 646 | 258 | | if (markDirty) |
| | 643 | 259 | | MarkShapeDirty(); |
| | 646 | 260 | | } |
| | | 261 | | |
| | | 262 | | internal static void ValidateConvexPolygon(Vector2d[] vertices) |
| | | 263 | | { |
| | 685 | 264 | | SwiftThrowHelper.ThrowIfArgument(vertices.Length < 3, nameof(vertices), "2D polygon must contain at least three |
| | 685 | 265 | | SwiftThrowHelper.ThrowIfArgument( |
| | 685 | 266 | | !FixedConvex2dRelations.IsStrictlyConvex(vertices), |
| | 685 | 267 | | nameof(vertices), |
| | 685 | 268 | | "2D polygon vertices must form a strictly convex boundary."); |
| | 681 | 269 | | } |
| | | 270 | | |
| | | 271 | | private bool TryCalculateIntrinsicSignedAreaAndCentroid( |
| | | 272 | | out Fixed64 signedDoubleArea, |
| | | 273 | | out Vector2d centroid) |
| | | 274 | | { |
| | 12001 | 275 | | return FixedConvex2dRelations.TryGetAreaAndCentroid( |
| | 12001 | 276 | | GetMassPropertyVertices(), |
| | 12001 | 277 | | out signedDoubleArea, |
| | 12001 | 278 | | out centroid); |
| | | 279 | | } |
| | | 280 | | |
| | | 281 | | private ReadOnlySpan<Vector2d> GetMassPropertyVertices() |
| | | 282 | | { |
| | 18082 | 283 | | if (HasCommittedShape) |
| | 16226 | 284 | | return _scaledLocalVertices; |
| | | 285 | | |
| | 1856 | 286 | | GetCurrentScaleFactors( |
| | 1856 | 287 | | out Vector2d ownerScale, |
| | 1856 | 288 | | out Vector2d partScale); |
| | 18464 | 289 | | for (int i = 0; i < _localVertices.Length; i++) |
| | | 290 | | { |
| | 7376 | 291 | | _scaledLocalVerticesScratch[i] = ColliderScalePolicy.Scale( |
| | 7376 | 292 | | _localVertices[i], |
| | 7376 | 293 | | ownerScale, |
| | 7376 | 294 | | partScale); |
| | | 295 | | } |
| | | 296 | | |
| | 1856 | 297 | | return _scaledLocalVerticesScratch; |
| | | 298 | | } |
| | | 299 | | } |