| | | 1 | | //======================================================================= |
| | | 2 | | // ColliderShapeDefinition2D.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 Gravitas.Materials; |
| | | 10 | | using System; |
| | | 11 | | using System.Runtime.CompilerServices; |
| | | 12 | | |
| | | 13 | | namespace Gravitas.Colliders; |
| | | 14 | | |
| | | 15 | | /// <summary> |
| | | 16 | | /// Describes authoritative pure 2D collider shape input without runtime |
| | | 17 | | /// collider lifecycle state such as IDs, bodies, partitions, pairs, or events. |
| | | 18 | | /// </summary> |
| | | 19 | | public readonly struct ColliderShapeDefinition2D : IEquatable<ColliderShapeDefinition2D> |
| | | 20 | | { |
| | | 21 | | private readonly Vector2d[]? _polygonVertices; |
| | | 22 | | private readonly PhysicsMaterial _material; |
| | | 23 | | private readonly bool _hasMaterial; |
| | | 24 | | |
| | | 25 | | private ColliderShapeDefinition2D( |
| | | 26 | | ColliderShapeDefinition2DKind kind, |
| | | 27 | | Fixed64 radius, |
| | | 28 | | Vector2d size, |
| | | 29 | | Vector2d[]? polygonVertices, |
| | | 30 | | PhysicsMaterial? material) |
| | | 31 | | { |
| | 294 | 32 | | Kind = kind; |
| | 294 | 33 | | Radius = radius; |
| | 294 | 34 | | Size = size; |
| | 294 | 35 | | _polygonVertices = polygonVertices; |
| | 294 | 36 | | _material = material ?? PhysicsMaterial.Default; |
| | 294 | 37 | | _hasMaterial = material.HasValue; |
| | 294 | 38 | | } |
| | | 39 | | |
| | | 40 | | /// <summary> |
| | | 41 | | /// Gets the shape family represented by this definition. |
| | | 42 | | /// </summary> |
| | | 43 | | public ColliderShapeDefinition2DKind Kind { get; } |
| | | 44 | | |
| | | 45 | | /// <summary> |
| | | 46 | | /// Gets the unscaled radius used by circle definitions. |
| | | 47 | | /// </summary> |
| | | 48 | | public Fixed64 Radius { get; } |
| | | 49 | | |
| | | 50 | | /// <summary> |
| | | 51 | | /// Gets the unscaled size used by sized definitions. |
| | | 52 | | /// </summary> |
| | | 53 | | public Vector2d Size { get; } |
| | | 54 | | |
| | | 55 | | /// <summary> |
| | | 56 | | /// Gets the authored surface material used when this definition creates a |
| | | 57 | | /// runtime 2D collider directly. |
| | | 58 | | /// </summary> |
| | 290 | 59 | | public PhysicsMaterial Material => _hasMaterial ? _material : PhysicsMaterial.Default; |
| | | 60 | | |
| | 374 | 61 | | internal bool HasMaterial => _hasMaterial; |
| | | 62 | | |
| | | 63 | | /// <summary> |
| | | 64 | | /// Gets the number of local polygon vertices held by this definition. |
| | | 65 | | /// </summary> |
| | | 66 | | public int PolygonVertexCount |
| | | 67 | | { |
| | | 68 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 165 | 69 | | get => _polygonVertices?.Length ?? 0; |
| | | 70 | | } |
| | | 71 | | |
| | | 72 | | /// <summary> |
| | | 73 | | /// Creates a circle shape definition. |
| | | 74 | | /// </summary> |
| | | 75 | | public static ColliderShapeDefinition2D Circle(Fixed64 radius, PhysicsMaterial? material = null) |
| | | 76 | | { |
| | 176 | 77 | | ValidateRadius(radius); |
| | 176 | 78 | | Fixed64 diameter = radius * (Fixed64)2; |
| | 176 | 79 | | return new( |
| | 176 | 80 | | ColliderShapeDefinition2DKind.Circle, |
| | 176 | 81 | | radius, |
| | 176 | 82 | | new Vector2d(diameter, diameter), |
| | 176 | 83 | | null, |
| | 176 | 84 | | material); |
| | | 85 | | } |
| | | 86 | | |
| | | 87 | | /// <summary> |
| | | 88 | | /// Creates a vertical local-axis capsule shape definition. |
| | | 89 | | /// </summary> |
| | | 90 | | public static ColliderShapeDefinition2D Capsule(Fixed64 radius, Fixed64 height, PhysicsMaterial? material = null) |
| | | 91 | | { |
| | 38 | 92 | | ValidateCapsule(radius, height); |
| | 35 | 93 | | return new( |
| | 35 | 94 | | ColliderShapeDefinition2DKind.Capsule, |
| | 35 | 95 | | radius, |
| | 35 | 96 | | new Vector2d(radius * (Fixed64)2, height), |
| | 35 | 97 | | null, |
| | 35 | 98 | | material); |
| | | 99 | | } |
| | | 100 | | |
| | | 101 | | /// <summary> |
| | | 102 | | /// Creates an axis-aligned box shape definition. |
| | | 103 | | /// </summary> |
| | | 104 | | public static ColliderShapeDefinition2D AABBox(Vector2d size, PhysicsMaterial? material = null) |
| | | 105 | | { |
| | 50 | 106 | | ValidateSize(size); |
| | 48 | 107 | | return new(ColliderShapeDefinition2DKind.AABBox, Fixed64.Zero, size, null, material); |
| | | 108 | | } |
| | | 109 | | |
| | | 110 | | /// <summary> |
| | | 111 | | /// Creates a convex polygon shape definition. |
| | | 112 | | /// </summary> |
| | | 113 | | public static ColliderShapeDefinition2D ConvexPolygon(params Vector2d[] vertices) => |
| | 28 | 114 | | ConvexPolygon(null, vertices); |
| | | 115 | | |
| | | 116 | | /// <summary> |
| | | 117 | | /// Creates a convex polygon shape definition. |
| | | 118 | | /// </summary> |
| | | 119 | | public static ColliderShapeDefinition2D ConvexPolygon(PhysicsMaterial? material, params Vector2d[] vertices) |
| | | 120 | | { |
| | 36 | 121 | | SwiftThrowHelper.ThrowIfNull(vertices, nameof(vertices)); |
| | 36 | 122 | | LSPolygonCollider2D.ValidateConvexPolygon(vertices); |
| | | 123 | | |
| | 35 | 124 | | var vertexSnapshot = new Vector2d[vertices.Length]; |
| | 35 | 125 | | Array.Copy(vertices, vertexSnapshot, vertices.Length); |
| | 35 | 126 | | return new( |
| | 35 | 127 | | ColliderShapeDefinition2DKind.ConvexPolygon, |
| | 35 | 128 | | Fixed64.Zero, |
| | 35 | 129 | | Vector2d.Zero, |
| | 35 | 130 | | vertexSnapshot, |
| | 35 | 131 | | material); |
| | | 132 | | } |
| | | 133 | | |
| | | 134 | | /// <summary> |
| | | 135 | | /// Creates a triangle authoring definition backed by the convex-polygon runtime shape. |
| | | 136 | | /// </summary> |
| | | 137 | | public static ColliderShapeDefinition2D Triangle(Vector2d a, Vector2d b, Vector2d c, PhysicsMaterial? material = nul |
| | 4 | 138 | | ConvexPolygon(material, a, b, c); |
| | | 139 | | |
| | | 140 | | /// <summary> |
| | | 141 | | /// Gets a local polygon vertex by stable source order. |
| | | 142 | | /// </summary> |
| | | 143 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 144 | | public Vector2d GetPolygonVertex(int index) |
| | | 145 | | { |
| | 33 | 146 | | EnsurePolygonDefinition(); |
| | 31 | 147 | | SwiftThrowHelper.ThrowIfArrayIndexInvalid(index, _polygonVertices!.Length, nameof(index)); |
| | 31 | 148 | | return _polygonVertices[index]; |
| | | 149 | | } |
| | | 150 | | |
| | | 151 | | /// <summary> |
| | | 152 | | /// Creates a new unbound runtime 2D collider from this shape definition. |
| | | 153 | | /// </summary> |
| | | 154 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 12 | 155 | | public LSCollider2D CreateCollider() => CreateRuntimeCollider(); |
| | | 156 | | |
| | | 157 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 158 | | internal LSCollider2D CreateRuntimeCollider() |
| | | 159 | | { |
| | 259 | 160 | | return Kind switch |
| | 259 | 161 | | { |
| | 167 | 162 | | ColliderShapeDefinition2DKind.Circle => new LSCircleCollider2D(this), |
| | 40 | 163 | | ColliderShapeDefinition2DKind.AABBox => new LSAABBoxCollider2D(this), |
| | 25 | 164 | | ColliderShapeDefinition2DKind.ConvexPolygon => new LSPolygonCollider2D(this), |
| | 26 | 165 | | ColliderShapeDefinition2DKind.Capsule => new LSCapsuleCollider2D(this), |
| | 1 | 166 | | _ => throw new ArgumentException( |
| | 1 | 167 | | "2D collider shape definition cannot be default.", |
| | 1 | 168 | | nameof(ColliderShapeDefinition2D)) |
| | 259 | 169 | | }; |
| | | 170 | | } |
| | | 171 | | |
| | | 172 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 173 | | internal Vector2d[] GetPolygonVerticesForRuntime() |
| | | 174 | | { |
| | 25 | 175 | | EnsurePolygonDefinition(); |
| | 25 | 176 | | return _polygonVertices!; |
| | | 177 | | } |
| | | 178 | | |
| | | 179 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 180 | | internal void EnsureDefined() |
| | | 181 | | { |
| | 576 | 182 | | SwiftThrowHelper.ThrowIfArgument( |
| | 576 | 183 | | Kind == ColliderShapeDefinition2DKind.Undefined, |
| | 576 | 184 | | nameof(ColliderShapeDefinition2D), |
| | 576 | 185 | | "2D collider shape definition cannot be default."); |
| | 575 | 186 | | } |
| | | 187 | | |
| | | 188 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 189 | | internal void EnsureKind(ColliderShapeDefinition2DKind expectedKind) |
| | | 190 | | { |
| | 318 | 191 | | EnsureDefined(); |
| | 317 | 192 | | SwiftThrowHelper.ThrowIfArgument( |
| | 317 | 193 | | Kind != expectedKind, |
| | 317 | 194 | | nameof(ColliderShapeDefinition2D), |
| | 317 | 195 | | $"2D collider shape definition must be {expectedKind}."); |
| | 315 | 196 | | } |
| | | 197 | | |
| | | 198 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 199 | | private void EnsurePolygonDefinition() => |
| | 58 | 200 | | EnsureKind(ColliderShapeDefinition2DKind.ConvexPolygon); |
| | | 201 | | |
| | | 202 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 203 | | private static void ValidateRadius(Fixed64 radius) => |
| | 214 | 204 | | SwiftThrowHelper.ThrowIfArgument( |
| | 214 | 205 | | radius <= Fixed64.Zero, |
| | 214 | 206 | | nameof(radius), |
| | 214 | 207 | | "2D collider radius must be greater than zero."); |
| | | 208 | | |
| | | 209 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 210 | | private static void ValidateSize(Vector2d size) => |
| | 50 | 211 | | SwiftThrowHelper.ThrowIfArgument( |
| | 50 | 212 | | size.X <= Fixed64.Zero || size.Y <= Fixed64.Zero, |
| | 50 | 213 | | nameof(size), |
| | 50 | 214 | | "2D collider size components must be greater than zero."); |
| | | 215 | | |
| | | 216 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 217 | | private static void ValidateCapsule(Fixed64 radius, Fixed64 height) |
| | | 218 | | { |
| | 38 | 219 | | ValidateRadius(radius); |
| | 36 | 220 | | SwiftThrowHelper.ThrowIfArgument( |
| | 36 | 221 | | height < radius * (Fixed64)2, |
| | 36 | 222 | | nameof(height), |
| | 36 | 223 | | "2D capsule height must be at least the capsule diameter."); |
| | 35 | 224 | | } |
| | | 225 | | |
| | | 226 | | /// <inheritdoc/> |
| | | 227 | | public bool Equals(ColliderShapeDefinition2D other) |
| | | 228 | | { |
| | 16 | 229 | | if (Kind != other.Kind |
| | 16 | 230 | | || Radius != other.Radius |
| | 16 | 231 | | || Size != other.Size |
| | 16 | 232 | | || _hasMaterial != other._hasMaterial |
| | 16 | 233 | | || (_hasMaterial && _material != other._material) |
| | 16 | 234 | | || PolygonVertexCount != other.PolygonVertexCount) |
| | | 235 | | { |
| | 9 | 236 | | return false; |
| | | 237 | | } |
| | | 238 | | |
| | 46 | 239 | | for (int i = 0; i < PolygonVertexCount; i++) |
| | | 240 | | { |
| | 17 | 241 | | if (_polygonVertices![i] != other._polygonVertices![i]) |
| | 1 | 242 | | return false; |
| | | 243 | | } |
| | | 244 | | |
| | 6 | 245 | | return true; |
| | | 246 | | } |
| | | 247 | | |
| | | 248 | | /// <inheritdoc/> |
| | | 249 | | public override bool Equals(object? obj) => |
| | 16 | 250 | | obj is ColliderShapeDefinition2D other && Equals(other); |
| | | 251 | | |
| | | 252 | | /// <inheritdoc/> |
| | | 253 | | public override int GetHashCode() |
| | | 254 | | { |
| | | 255 | | unchecked |
| | | 256 | | { |
| | 6 | 257 | | int hash = 17; |
| | 6 | 258 | | hash = hash * 31 + Kind.GetHashCode(); |
| | 6 | 259 | | hash = hash * 31 + Radius.GetHashCode(); |
| | 6 | 260 | | hash = hash * 31 + Size.GetHashCode(); |
| | 6 | 261 | | hash = hash * 31 + _hasMaterial.GetHashCode(); |
| | 6 | 262 | | if (_hasMaterial) |
| | 2 | 263 | | hash = hash * 31 + _material.GetHashCode(); |
| | | 264 | | |
| | 28 | 265 | | for (int i = 0; i < PolygonVertexCount; i++) |
| | 8 | 266 | | hash = hash * 31 + _polygonVertices![i].GetHashCode(); |
| | | 267 | | |
| | 6 | 268 | | return hash; |
| | | 269 | | } |
| | | 270 | | } |
| | | 271 | | |
| | | 272 | | /// <summary>Determines whether two authored 2D shape definitions are equal.</summary> |
| | | 273 | | public static bool operator ==(ColliderShapeDefinition2D left, ColliderShapeDefinition2D right) => |
| | 1 | 274 | | left.Equals(right); |
| | | 275 | | |
| | | 276 | | /// <summary>Determines whether two authored 2D shape definitions are not equal.</summary> |
| | | 277 | | public static bool operator !=(ColliderShapeDefinition2D left, ColliderShapeDefinition2D right) => |
| | 1 | 278 | | !left.Equals(right); |
| | | 279 | | } |