| | | 1 | | //======================================================================= |
| | | 2 | | // CompoundColliderPart2D.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.Runtime.CompilerServices; |
| | | 11 | | |
| | | 12 | | namespace Gravitas.Colliders; |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// Declares one data-only geometry part owned by an |
| | | 16 | | /// <see cref="LSCompoundCollider2D"/>. |
| | | 17 | | /// </summary> |
| | | 18 | | public readonly struct CompoundColliderPart2D |
| | | 19 | | { |
| | | 20 | | /// <summary>Creates a part from an authored pure 2D shape.</summary> |
| | | 21 | | public CompoundColliderPart2D(ColliderShapeDefinition2D shape) |
| | 1 | 22 | | : this(shape, Vector2d.Zero, Fixed64.Zero, Vector2d.One, null) |
| | 1 | 23 | | { } |
| | | 24 | | |
| | | 25 | | /// <summary>Creates a part with a compound-local offset.</summary> |
| | | 26 | | public CompoundColliderPart2D(ColliderShapeDefinition2D shape, Vector2d localOffset) |
| | 212 | 27 | | : this(shape, localOffset, Fixed64.Zero, Vector2d.One, null) |
| | 212 | 28 | | { } |
| | | 29 | | |
| | | 30 | | /// <summary>Creates a part with a compound-local pose.</summary> |
| | | 31 | | public CompoundColliderPart2D( |
| | | 32 | | ColliderShapeDefinition2D shape, |
| | | 33 | | Vector2d localOffset, |
| | | 34 | | Fixed64 localRotation) |
| | 1 | 35 | | : this(shape, localOffset, localRotation, Vector2d.One, null) |
| | 1 | 36 | | { } |
| | | 37 | | |
| | | 38 | | /// <summary>Creates a part with a compound-local transform and optional material override.</summary> |
| | | 39 | | public CompoundColliderPart2D( |
| | | 40 | | ColliderShapeDefinition2D shape, |
| | | 41 | | Vector2d localOffset, |
| | | 42 | | Fixed64 localRotation, |
| | | 43 | | Vector2d localScale, |
| | | 44 | | PhysicsMaterial? material = null) |
| | | 45 | | { |
| | 258 | 46 | | shape.EnsureDefined(); |
| | 258 | 47 | | ValidateScale(localScale); |
| | | 48 | | |
| | 256 | 49 | | Shape = shape; |
| | 256 | 50 | | LocalOffset = localOffset; |
| | 256 | 51 | | LocalRotation = localRotation; |
| | 256 | 52 | | LocalScale = localScale; |
| | 256 | 53 | | _material = material ?? PhysicsMaterial.Default; |
| | 256 | 54 | | _hasMaterial = material.HasValue; |
| | 256 | 55 | | } |
| | | 56 | | |
| | | 57 | | private readonly PhysicsMaterial _material; |
| | | 58 | | private readonly bool _hasMaterial; |
| | | 59 | | |
| | | 60 | | /// <summary> |
| | | 61 | | /// Gets the authored data-only shape definition for this part. |
| | | 62 | | /// </summary> |
| | | 63 | | public ColliderShapeDefinition2D Shape { get; } |
| | | 64 | | |
| | | 65 | | /// <summary> |
| | | 66 | | /// Gets the deterministic local center offset applied relative to the |
| | | 67 | | /// owning compound collider. |
| | | 68 | | /// </summary> |
| | | 69 | | public Vector2d LocalOffset { get; } |
| | | 70 | | |
| | | 71 | | /// <summary> |
| | | 72 | | /// Gets the deterministic local rotation applied relative to the owning |
| | | 73 | | /// compound collider. |
| | | 74 | | /// </summary> |
| | | 75 | | public Fixed64 LocalRotation { get; } |
| | | 76 | | |
| | | 77 | | /// <summary> |
| | | 78 | | /// Gets the deterministic local scale applied relative to the owning |
| | | 79 | | /// compound collider. |
| | | 80 | | /// </summary> |
| | | 81 | | public Vector2d LocalScale { get; } |
| | | 82 | | |
| | | 83 | | /// <summary> |
| | | 84 | | /// Gets the authored material for this 2D part, or the shape/default |
| | | 85 | | /// material when no part-level material was supplied. |
| | | 86 | | /// </summary> |
| | 4 | 87 | | public PhysicsMaterial Material => TryGetMaterial(out PhysicsMaterial material) |
| | 4 | 88 | | ? material |
| | 4 | 89 | | : PhysicsMaterial.Default; |
| | | 90 | | |
| | | 91 | | internal bool HasMaterial |
| | | 92 | | { |
| | | 93 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 40 | 94 | | get => _hasMaterial || Shape.HasMaterial; |
| | | 95 | | } |
| | | 96 | | |
| | | 97 | | /// <summary>Creates a circle part.</summary> |
| | | 98 | | public static CompoundColliderPart2D Circle(Fixed64 radius, Vector2d localOffset) => |
| | 142 | 99 | | new(ColliderShapeDefinition2D.Circle(radius), localOffset); |
| | | 100 | | |
| | | 101 | | /// <summary>Creates a circle part with a material override.</summary> |
| | | 102 | | public static CompoundColliderPart2D Circle( |
| | | 103 | | Fixed64 radius, |
| | | 104 | | Vector2d localOffset, |
| | | 105 | | PhysicsMaterial material) => |
| | 5 | 106 | | new(ColliderShapeDefinition2D.Circle(radius), localOffset, Fixed64.Zero, Vector2d.One, material); |
| | | 107 | | |
| | | 108 | | /// <summary>Creates a transformed circle part.</summary> |
| | | 109 | | public static CompoundColliderPart2D Circle( |
| | | 110 | | Fixed64 radius, |
| | | 111 | | Vector2d localOffset, |
| | | 112 | | Fixed64 localRotation, |
| | | 113 | | Vector2d localScale) => |
| | 8 | 114 | | new(ColliderShapeDefinition2D.Circle(radius), localOffset, localRotation, localScale); |
| | | 115 | | |
| | | 116 | | /// <summary>Creates a capsule part.</summary> |
| | | 117 | | public static CompoundColliderPart2D Capsule(Fixed64 radius, Fixed64 height, Vector2d localOffset) => |
| | 7 | 118 | | new(ColliderShapeDefinition2D.Capsule(radius, height), localOffset); |
| | | 119 | | |
| | | 120 | | /// <summary>Creates a capsule part with a material override.</summary> |
| | | 121 | | public static CompoundColliderPart2D Capsule( |
| | | 122 | | Fixed64 radius, |
| | | 123 | | Fixed64 height, |
| | | 124 | | Vector2d localOffset, |
| | | 125 | | PhysicsMaterial material) => |
| | 1 | 126 | | new(ColliderShapeDefinition2D.Capsule(radius, height), localOffset, Fixed64.Zero, Vector2d.One, material); |
| | | 127 | | |
| | | 128 | | /// <summary>Creates a transformed capsule part.</summary> |
| | | 129 | | public static CompoundColliderPart2D Capsule( |
| | | 130 | | Fixed64 radius, |
| | | 131 | | Fixed64 height, |
| | | 132 | | Vector2d localOffset, |
| | | 133 | | Fixed64 localRotation, |
| | | 134 | | Vector2d localScale) => |
| | 7 | 135 | | new(ColliderShapeDefinition2D.Capsule(radius, height), localOffset, localRotation, localScale); |
| | | 136 | | |
| | | 137 | | /// <summary>Creates an axis-aligned box part.</summary> |
| | | 138 | | public static CompoundColliderPart2D AABBox(Vector2d size, Vector2d localOffset) => |
| | 28 | 139 | | new(ColliderShapeDefinition2D.AABBox(size), localOffset); |
| | | 140 | | |
| | | 141 | | /// <summary>Creates an axis-aligned box part with a material override.</summary> |
| | | 142 | | public static CompoundColliderPart2D AABBox( |
| | | 143 | | Vector2d size, |
| | | 144 | | Vector2d localOffset, |
| | | 145 | | PhysicsMaterial material) => |
| | 1 | 146 | | new(ColliderShapeDefinition2D.AABBox(size), localOffset, Fixed64.Zero, Vector2d.One, material); |
| | | 147 | | |
| | | 148 | | /// <summary>Creates a transformed axis-aligned box part.</summary> |
| | | 149 | | public static CompoundColliderPart2D AABBox( |
| | | 150 | | Vector2d size, |
| | | 151 | | Vector2d localOffset, |
| | | 152 | | Fixed64 localRotation, |
| | | 153 | | Vector2d localScale) => |
| | 1 | 154 | | new(ColliderShapeDefinition2D.AABBox(size), localOffset, localRotation, localScale); |
| | | 155 | | |
| | | 156 | | /// <summary>Creates a convex-polygon part.</summary> |
| | | 157 | | public static CompoundColliderPart2D ConvexPolygon(Vector2d[] vertices, Vector2d localOffset) => |
| | 5 | 158 | | new(ColliderShapeDefinition2D.ConvexPolygon(vertices), localOffset); |
| | | 159 | | |
| | | 160 | | /// <summary>Creates a convex-polygon part with a material override.</summary> |
| | | 161 | | public static CompoundColliderPart2D ConvexPolygon( |
| | | 162 | | Vector2d[] vertices, |
| | | 163 | | Vector2d localOffset, |
| | | 164 | | PhysicsMaterial material) => |
| | 1 | 165 | | new(ColliderShapeDefinition2D.ConvexPolygon(vertices), localOffset, Fixed64.Zero, Vector2d.One, material); |
| | | 166 | | |
| | | 167 | | /// <summary>Creates a transformed convex-polygon part.</summary> |
| | | 168 | | public static CompoundColliderPart2D ConvexPolygon( |
| | | 169 | | Vector2d[] vertices, |
| | | 170 | | Vector2d localOffset, |
| | | 171 | | Fixed64 localRotation, |
| | | 172 | | Vector2d localScale) => |
| | 8 | 173 | | new(ColliderShapeDefinition2D.ConvexPolygon(vertices), localOffset, localRotation, localScale); |
| | | 174 | | |
| | | 175 | | internal bool IsDefault |
| | | 176 | | { |
| | | 177 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 248 | 178 | | get => Shape.Kind == ColliderShapeDefinition2DKind.Undefined; |
| | | 179 | | } |
| | | 180 | | |
| | | 181 | | internal bool TryGetMaterial(out PhysicsMaterial material) |
| | | 182 | | { |
| | 294 | 183 | | if (_hasMaterial) |
| | | 184 | | { |
| | 30 | 185 | | material = _material; |
| | 30 | 186 | | return true; |
| | | 187 | | } |
| | | 188 | | |
| | 264 | 189 | | if (Shape.HasMaterial) |
| | | 190 | | { |
| | 20 | 191 | | material = Shape.Material; |
| | 20 | 192 | | return true; |
| | | 193 | | } |
| | | 194 | | |
| | 244 | 195 | | material = PhysicsMaterial.Default; |
| | 244 | 196 | | return false; |
| | | 197 | | } |
| | | 198 | | |
| | | 199 | | internal PhysicsMaterial ResolveMaterial(PhysicsMaterial ownerMaterial) => |
| | 250 | 200 | | TryGetMaterial(out PhysicsMaterial material) ? material : ownerMaterial; |
| | | 201 | | |
| | | 202 | | private static void ValidateScale(Vector2d localScale) |
| | | 203 | | { |
| | 258 | 204 | | SwiftThrowHelper.ThrowIfArgument( |
| | 258 | 205 | | localScale.X <= Fixed64.Zero || localScale.Y <= Fixed64.Zero, |
| | 258 | 206 | | nameof(localScale), |
| | 258 | 207 | | "2D compound collider part scale components must be greater than zero."); |
| | 256 | 208 | | } |
| | | 209 | | } |