| | | 1 | | //======================================================================= |
| | | 2 | | // LSCircleCollider2D.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.Runtime.CompilerServices; |
| | | 9 | | using Chronicler; |
| | | 10 | | using FixedMathSharp; |
| | | 11 | | using FixedMathSharp.Geometry; |
| | | 12 | | |
| | | 13 | | namespace Gravitas.Colliders; |
| | | 14 | | |
| | | 15 | | /// <summary> |
| | | 16 | | /// Pure 2D circle collider. |
| | | 17 | | /// </summary> |
| | | 18 | | public sealed class LSCircleCollider2D : LSCollider2D |
| | | 19 | | { |
| | | 20 | | private Fixed64 _radius; |
| | | 21 | | private Fixed64 _scaledRadius; |
| | | 22 | | private Fixed64 _preparedRadius; |
| | | 23 | | |
| | | 24 | | /// <summary>Creates a pure 2D circle with an authored radius.</summary> |
| | 4176 | 25 | | public LSCircleCollider2D(Fixed64 radius) |
| | | 26 | | { |
| | 4176 | 27 | | Radius = radius; |
| | 4176 | 28 | | } |
| | | 29 | | |
| | | 30 | | /// <summary>Creates a runtime circle from an authored shape definition.</summary> |
| | 299 | 31 | | public LSCircleCollider2D(ColliderShapeDefinition2D definition) |
| | | 32 | | { |
| | 299 | 33 | | definition.EnsureKind(ColliderShapeDefinition2DKind.Circle); |
| | 298 | 34 | | Material = definition.Material; |
| | 298 | 35 | | Radius = definition.Radius; |
| | 298 | 36 | | } |
| | | 37 | | |
| | | 38 | | /// <inheritdoc/> |
| | 68800 | 39 | | public override ColliderType2D Shape => ColliderType2D.Circle; |
| | | 40 | | |
| | | 41 | | /// <summary>Gets or sets the unscaled circle radius.</summary> |
| | | 42 | | public Fixed64 Radius |
| | | 43 | | { |
| | | 44 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 1464 | 45 | | get => _radius; |
| | | 46 | | set |
| | | 47 | | { |
| | 4515 | 48 | | ChronicleGuard.ThrowIfArgument(value <= Fixed64.Zero, nameof(value), "2D circle radius must be greater than |
| | 4515 | 49 | | if (_radius == value) |
| | 2 | 50 | | return; |
| | | 51 | | |
| | 4513 | 52 | | ValidateGeometryMutation(radius: value); |
| | 4509 | 53 | | _radius = value; |
| | 4509 | 54 | | MarkShapeDirty(); |
| | 4509 | 55 | | } |
| | | 56 | | } |
| | | 57 | | |
| | | 58 | | /// <summary>Gets the radius after owner and compound-part scaling.</summary> |
| | | 59 | | public Fixed64 ScaledRadius |
| | | 60 | | { |
| | | 61 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 132643 | 62 | | get => GetMassPropertyRadius(); |
| | | 63 | | } |
| | | 64 | | |
| | | 65 | | /// <inheritdoc/> |
| | | 66 | | public override bool ContainsPoint(Vector2d point) => |
| | 1880 | 67 | | FixedSegment2d.ContainsPointInCenteredCapsule( |
| | 1880 | 68 | | point, |
| | 1880 | 69 | | Center, |
| | 1880 | 70 | | Vector2d.Forward, |
| | 1880 | 71 | | Fixed64.Zero, |
| | 1880 | 72 | | ScaledRadius, |
| | 1880 | 73 | | Fixed64.Zero); |
| | | 74 | | |
| | | 75 | | /// <inheritdoc/> |
| | | 76 | | public override Vector2d GetClosestPoint(Vector2d point) |
| | | 77 | | { |
| | 3 | 78 | | if (TryGetClosestBoundaryAnchor( |
| | 3 | 79 | | point, |
| | 3 | 80 | | out FixedPointAnchor2d anchor) |
| | 3 | 81 | | && anchor.TryGetPoint(out Vector2d closest)) |
| | | 82 | | { |
| | 2 | 83 | | return closest; |
| | | 84 | | } |
| | | 85 | | |
| | 1 | 86 | | throw new System.InvalidOperationException( |
| | 1 | 87 | | "The closest circle surface point is outside the Fixed64 coordinate domain."); |
| | | 88 | | } |
| | | 89 | | |
| | | 90 | | /// <inheritdoc/> |
| | | 91 | | public override Vector2d GetSupportPoint(Vector2d direction) |
| | | 92 | | { |
| | 7 | 93 | | if (direction == Vector2d.Zero) |
| | 1 | 94 | | direction = Vector2d.Right; |
| | | 95 | | |
| | 7 | 96 | | if (Vector2d.TryRotate( |
| | 7 | 97 | | direction.Normalized, |
| | 7 | 98 | | -Rotation, |
| | 7 | 99 | | out Vector2d localDirection)) |
| | | 100 | | { |
| | 7 | 101 | | FixedPointAnchor2d anchor = |
| | 7 | 102 | | FixedSegment2d.GetCenteredCapsuleSupportAnchor( |
| | 7 | 103 | | Center, |
| | 7 | 104 | | Rotation, |
| | 7 | 105 | | Vector2d.Forward, |
| | 7 | 106 | | Fixed64.Zero, |
| | 7 | 107 | | ScaledRadius, |
| | 7 | 108 | | localDirection); |
| | 7 | 109 | | if (anchor.TryGetPoint(out Vector2d support)) |
| | 5 | 110 | | return support; |
| | | 111 | | } |
| | | 112 | | |
| | 2 | 113 | | throw new System.InvalidOperationException( |
| | 2 | 114 | | "The circle support point is outside the Fixed64 coordinate domain."); |
| | | 115 | | } |
| | | 116 | | |
| | | 117 | | internal override ExactMassWeight CalculateAreaForMassProperties() |
| | | 118 | | { |
| | 525 | 119 | | Fixed64 radius = GetMassPropertyRadius(); |
| | 525 | 120 | | return ExactMassWeight.FromProduct( |
| | 525 | 121 | | Fixed64.Pi, |
| | 525 | 122 | | radius, |
| | 525 | 123 | | radius); |
| | | 124 | | } |
| | | 125 | | |
| | | 126 | | internal override ExactMassWeight CalculatePreparedAreaForMassProperties() => |
| | 858 | 127 | | ExactMassWeight.FromProduct( |
| | 858 | 128 | | Fixed64.Pi, |
| | 858 | 129 | | _preparedRadius, |
| | 858 | 130 | | _preparedRadius); |
| | | 131 | | |
| | | 132 | | internal override Fixed64 CalculateCenterOfMassMoment(Fixed64 mass) |
| | | 133 | | { |
| | 10174 | 134 | | Fixed64 radius = GetMassPropertyRadius(); |
| | 10174 | 135 | | return Fixed64.TryMultiplyDivide( |
| | 10174 | 136 | | mass, |
| | 10174 | 137 | | radius, |
| | 10174 | 138 | | radius, |
| | 10174 | 139 | | Fixed64.Two, |
| | 10174 | 140 | | out Fixed64 moment) |
| | 10174 | 141 | | ? moment |
| | 10174 | 142 | | : Fixed64.MaxValue; |
| | | 143 | | } |
| | | 144 | | |
| | | 145 | | private Fixed64 GetMassPropertyRadius() |
| | | 146 | | { |
| | 143342 | 147 | | if (HasCommittedShape) |
| | 138852 | 148 | | return _scaledRadius; |
| | | 149 | | |
| | 4490 | 150 | | GetCurrentScaleFactors( |
| | 4490 | 151 | | out Vector2d ownerScale, |
| | 4490 | 152 | | out Vector2d partScale); |
| | 4490 | 153 | | Fixed64 radiusX = ColliderScalePolicy.Scale( |
| | 4490 | 154 | | _radius, |
| | 4490 | 155 | | ownerScale.X, |
| | 4490 | 156 | | partScale.X); |
| | 4490 | 157 | | Fixed64 radiusY = ColliderScalePolicy.Scale( |
| | 4490 | 158 | | _radius, |
| | 4490 | 159 | | ownerScale.Y, |
| | 4490 | 160 | | partScale.Y); |
| | 4490 | 161 | | return FixedMath.Max(radiusX, radiusY); |
| | | 162 | | } |
| | | 163 | | |
| | | 164 | | private protected override void PrepareShape(in ColliderShapeSnapshot2D snapshot) |
| | | 165 | | { |
| | 14039 | 166 | | Fixed64 radiusX = ColliderScalePolicy.ScalePositive( |
| | 14039 | 167 | | _radius, |
| | 14039 | 168 | | snapshot.OwnerScale.X, |
| | 14039 | 169 | | snapshot.PartScale.X); |
| | 14037 | 170 | | Fixed64 radiusY = ColliderScalePolicy.ScalePositive( |
| | 14037 | 171 | | _radius, |
| | 14037 | 172 | | snapshot.OwnerScale.Y, |
| | 14037 | 173 | | snapshot.PartScale.Y); |
| | 14037 | 174 | | _preparedRadius = FixedMath.Max(radiusX, radiusY); |
| | 14037 | 175 | | SetPreparedGeometryReach(_preparedRadius); |
| | 14037 | 176 | | SetPreparedBounds(FixedBoundArea.FromCenterAndScopeClippedToDomain( |
| | 14037 | 177 | | snapshot.Center, |
| | 14037 | 178 | | new Vector2d(_preparedRadius, _preparedRadius))); |
| | 14037 | 179 | | } |
| | | 180 | | |
| | | 181 | | private protected override void PublishShape() => |
| | 12962 | 182 | | _scaledRadius = _preparedRadius; |
| | | 183 | | |
| | | 184 | | internal void CopyPreparedReconfiguration(LSCircleCollider2D candidate) |
| | | 185 | | { |
| | 35 | 186 | | _radius = candidate._radius; |
| | 35 | 187 | | _preparedRadius = candidate._preparedRadius; |
| | 35 | 188 | | } |
| | | 189 | | |
| | | 190 | | /// <inheritdoc/> |
| | | 191 | | protected override void RecordShapeData(IChronicler chronicler) |
| | | 192 | | { |
| | 175 | 193 | | Fixed64 radius = _radius; |
| | 175 | 194 | | RecordValues.Look(chronicler, ref radius, "Radius", Fixed64.Half); |
| | 175 | 195 | | if (chronicler.Mode == SerializationMode.Loading) |
| | | 196 | | { |
| | 99 | 197 | | ChronicleGuard.ThrowIfArgument(radius <= Fixed64.Zero, nameof(radius), "2D circle radius must be greater tha |
| | 99 | 198 | | _radius = radius; |
| | | 199 | | } |
| | 175 | 200 | | } |
| | | 201 | | } |