| | | 1 | | //======================================================================= |
| | | 2 | | // CompoundColliderPart.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="LSCompoundCollider"/>. |
| | | 17 | | /// </summary> |
| | | 18 | | public readonly struct CompoundColliderPart |
| | | 19 | | { |
| | | 20 | | /// <summary>Creates a part from an authored 3D shape.</summary> |
| | | 21 | | public CompoundColliderPart(ColliderShapeDefinition shape) |
| | 3 | 22 | | : this(shape, Vector3d.Zero, FixedQuaternion.Identity, Vector3d.One, null) |
| | 3 | 23 | | { } |
| | | 24 | | |
| | | 25 | | /// <summary>Creates a part with a compound-local offset.</summary> |
| | | 26 | | public CompoundColliderPart(ColliderShapeDefinition shape, Vector3d localOffset) |
| | 220 | 27 | | : this(shape, localOffset, FixedQuaternion.Identity, Vector3d.One, null) |
| | 220 | 28 | | { } |
| | | 29 | | |
| | | 30 | | /// <summary>Creates a part with a compound-local pose.</summary> |
| | | 31 | | public CompoundColliderPart(ColliderShapeDefinition shape, Vector3d localOffset, FixedQuaternion localRotation) |
| | 2 | 32 | | : this(shape, localOffset, localRotation, Vector3d.One, null) |
| | 2 | 33 | | { } |
| | | 34 | | |
| | | 35 | | /// <summary>Creates a part with a compound-local transform and optional material override.</summary> |
| | | 36 | | public CompoundColliderPart( |
| | | 37 | | ColliderShapeDefinition shape, |
| | | 38 | | Vector3d localOffset, |
| | | 39 | | FixedQuaternion localRotation, |
| | | 40 | | Vector3d localScale, |
| | | 41 | | PhysicsMaterial? material = null) |
| | | 42 | | { |
| | 304 | 43 | | shape.EnsureDefined(); |
| | 304 | 44 | | ValidateScale(localScale); |
| | | 45 | | |
| | 302 | 46 | | Shape = shape; |
| | 302 | 47 | | LocalOffset = localOffset; |
| | 302 | 48 | | LocalRotation = localRotation.Normalized; |
| | 302 | 49 | | LocalScale = localScale; |
| | 302 | 50 | | _material = material ?? PhysicsMaterial.Default; |
| | 302 | 51 | | _hasMaterial = material.HasValue; |
| | 302 | 52 | | } |
| | | 53 | | |
| | | 54 | | private readonly PhysicsMaterial _material; |
| | | 55 | | private readonly bool _hasMaterial; |
| | | 56 | | |
| | | 57 | | /// <summary> |
| | | 58 | | /// Gets the authored data-only shape definition for this part. |
| | | 59 | | /// </summary> |
| | | 60 | | public ColliderShapeDefinition Shape { get; } |
| | | 61 | | |
| | | 62 | | /// <summary> |
| | | 63 | | /// Gets the deterministic local center offset applied relative to the |
| | | 64 | | /// owning compound collider. |
| | | 65 | | /// </summary> |
| | | 66 | | public Vector3d LocalOffset { get; } |
| | | 67 | | |
| | | 68 | | /// <summary> |
| | | 69 | | /// Gets the deterministic local rotation applied relative to the owning |
| | | 70 | | /// compound collider. Authored values are scale-safely normalized; a zero |
| | | 71 | | /// quaternion resolves to identity. |
| | | 72 | | /// </summary> |
| | | 73 | | public FixedQuaternion LocalRotation { get; } |
| | | 74 | | |
| | | 75 | | /// <summary> |
| | | 76 | | /// Gets the deterministic local scale applied relative to the owning |
| | | 77 | | /// compound collider. |
| | | 78 | | /// </summary> |
| | | 79 | | public Vector3d LocalScale { get; } |
| | | 80 | | |
| | | 81 | | /// <summary> |
| | | 82 | | /// Gets the authored material for this part, or the shape/default material |
| | | 83 | | /// when no part-level material was supplied. |
| | | 84 | | /// </summary> |
| | 6 | 85 | | public PhysicsMaterial Material => TryGetMaterial(out PhysicsMaterial material) |
| | 6 | 86 | | ? material |
| | 6 | 87 | | : PhysicsMaterial.Default; |
| | | 88 | | |
| | | 89 | | internal bool HasMaterial |
| | | 90 | | { |
| | | 91 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 92 | 92 | | get => _hasMaterial || Shape.HasMaterial; |
| | | 93 | | } |
| | | 94 | | |
| | | 95 | | /// <summary>Creates a sphere part.</summary> |
| | | 96 | | public static CompoundColliderPart Sphere(Fixed64 radius, Vector3d localOffset) => |
| | 129 | 97 | | new(ColliderShapeDefinition.Sphere(radius), localOffset); |
| | | 98 | | |
| | | 99 | | /// <summary>Creates a sphere part with a material override.</summary> |
| | | 100 | | public static CompoundColliderPart Sphere( |
| | | 101 | | Fixed64 radius, |
| | | 102 | | Vector3d localOffset, |
| | | 103 | | PhysicsMaterial material) => |
| | 7 | 104 | | new(ColliderShapeDefinition.Sphere(radius), localOffset, FixedQuaternion.Identity, Vector3d.One, material); |
| | | 105 | | |
| | | 106 | | /// <summary>Creates a transformed sphere part.</summary> |
| | | 107 | | public static CompoundColliderPart Sphere( |
| | | 108 | | Fixed64 radius, |
| | | 109 | | Vector3d localOffset, |
| | | 110 | | FixedQuaternion localRotation, |
| | | 111 | | Vector3d localScale) => |
| | 5 | 112 | | new(ColliderShapeDefinition.Sphere(radius), localOffset, localRotation, localScale); |
| | | 113 | | |
| | | 114 | | /// <summary>Creates a capsule part.</summary> |
| | | 115 | | public static CompoundColliderPart Capsule(Fixed64 radius, Fixed64 height, Vector3d localOffset) => |
| | 5 | 116 | | new(ColliderShapeDefinition.Capsule(radius, height), localOffset); |
| | | 117 | | |
| | | 118 | | /// <summary>Creates a capsule part with a material override.</summary> |
| | | 119 | | public static CompoundColliderPart Capsule( |
| | | 120 | | Fixed64 radius, |
| | | 121 | | Fixed64 height, |
| | | 122 | | Vector3d localOffset, |
| | | 123 | | PhysicsMaterial material) => |
| | 1 | 124 | | new(ColliderShapeDefinition.Capsule(radius, height), localOffset, FixedQuaternion.Identity, Vector3d.One, materi |
| | | 125 | | |
| | | 126 | | /// <summary>Creates a transformed capsule part.</summary> |
| | | 127 | | public static CompoundColliderPart Capsule( |
| | | 128 | | Fixed64 radius, |
| | | 129 | | Fixed64 height, |
| | | 130 | | Vector3d localOffset, |
| | | 131 | | FixedQuaternion localRotation, |
| | | 132 | | Vector3d localScale) => |
| | 12 | 133 | | new(ColliderShapeDefinition.Capsule(radius, height), localOffset, localRotation, localScale); |
| | | 134 | | |
| | | 135 | | /// <summary>Creates a cuboid part.</summary> |
| | | 136 | | public static CompoundColliderPart Cuboid(Vector3d size, Vector3d localOffset) => |
| | 18 | 137 | | new(ColliderShapeDefinition.Cuboid(size), localOffset); |
| | | 138 | | |
| | | 139 | | /// <summary>Creates a cuboid part with a material override.</summary> |
| | | 140 | | public static CompoundColliderPart Cuboid( |
| | | 141 | | Vector3d size, |
| | | 142 | | Vector3d localOffset, |
| | | 143 | | PhysicsMaterial material) => |
| | 1 | 144 | | new(ColliderShapeDefinition.Cuboid(size), localOffset, FixedQuaternion.Identity, Vector3d.One, material); |
| | | 145 | | |
| | | 146 | | /// <summary>Creates a transformed cuboid part.</summary> |
| | | 147 | | public static CompoundColliderPart Cuboid( |
| | | 148 | | Vector3d size, |
| | | 149 | | Vector3d localOffset, |
| | | 150 | | FixedQuaternion localRotation, |
| | | 151 | | Vector3d localScale) => |
| | 5 | 152 | | new(ColliderShapeDefinition.Cuboid(size), localOffset, localRotation, localScale); |
| | | 153 | | |
| | | 154 | | /// <summary>Creates a cylinder part.</summary> |
| | | 155 | | public static CompoundColliderPart Cylinder(Fixed64 radius, Fixed64 height, Vector3d localOffset) => |
| | 6 | 156 | | new(ColliderShapeDefinition.Cylinder(radius, height), localOffset); |
| | | 157 | | |
| | | 158 | | /// <summary>Creates a cylinder part with a material override.</summary> |
| | | 159 | | public static CompoundColliderPart Cylinder( |
| | | 160 | | Fixed64 radius, |
| | | 161 | | Fixed64 height, |
| | | 162 | | Vector3d localOffset, |
| | | 163 | | PhysicsMaterial material) => |
| | 1 | 164 | | new(ColliderShapeDefinition.Cylinder(radius, height), localOffset, FixedQuaternion.Identity, Vector3d.One, mater |
| | | 165 | | |
| | | 166 | | /// <summary>Creates a transformed cylinder part.</summary> |
| | | 167 | | public static CompoundColliderPart Cylinder( |
| | | 168 | | Fixed64 radius, |
| | | 169 | | Fixed64 height, |
| | | 170 | | Vector3d localOffset, |
| | | 171 | | FixedQuaternion localRotation, |
| | | 172 | | Vector3d localScale) => |
| | 9 | 173 | | new(ColliderShapeDefinition.Cylinder(radius, height), localOffset, localRotation, localScale); |
| | | 174 | | |
| | | 175 | | /// <summary>Creates a cone part.</summary> |
| | | 176 | | public static CompoundColliderPart Cone(Fixed64 radius, Fixed64 height, Vector3d localOffset) => |
| | 6 | 177 | | new(ColliderShapeDefinition.Cone(radius, height), localOffset); |
| | | 178 | | |
| | | 179 | | /// <summary>Creates a cone part with a material override.</summary> |
| | | 180 | | public static CompoundColliderPart Cone( |
| | | 181 | | Fixed64 radius, |
| | | 182 | | Fixed64 height, |
| | | 183 | | Vector3d localOffset, |
| | | 184 | | PhysicsMaterial material) => |
| | 1 | 185 | | new(ColliderShapeDefinition.Cone(radius, height), localOffset, FixedQuaternion.Identity, Vector3d.One, material) |
| | | 186 | | |
| | | 187 | | /// <summary>Creates a transformed cone part.</summary> |
| | | 188 | | public static CompoundColliderPart Cone( |
| | | 189 | | Fixed64 radius, |
| | | 190 | | Fixed64 height, |
| | | 191 | | Vector3d localOffset, |
| | | 192 | | FixedQuaternion localRotation, |
| | | 193 | | Vector3d localScale) => |
| | 13 | 194 | | new(ColliderShapeDefinition.Cone(radius, height), localOffset, localRotation, localScale); |
| | | 195 | | |
| | | 196 | | /// <summary>Creates a convex-mesh part.</summary> |
| | | 197 | | public static CompoundColliderPart ConvexMesh( |
| | | 198 | | Vector3d[] vertices, |
| | | 199 | | int[] triangles, |
| | | 200 | | Vector3d localOffset, |
| | | 201 | | MeshInertiaPolicy inertiaPolicy = MeshInertiaPolicy.RequireClosedVolume) => |
| | 9 | 202 | | new(ColliderShapeDefinition.ConvexMesh(vertices, triangles, inertiaPolicy), localOffset); |
| | | 203 | | |
| | | 204 | | /// <summary>Creates a convex-mesh part with a material override.</summary> |
| | | 205 | | public static CompoundColliderPart ConvexMesh( |
| | | 206 | | Vector3d[] vertices, |
| | | 207 | | int[] triangles, |
| | | 208 | | Vector3d localOffset, |
| | | 209 | | PhysicsMaterial material, |
| | | 210 | | MeshInertiaPolicy inertiaPolicy = MeshInertiaPolicy.RequireClosedVolume) => |
| | 1 | 211 | | new(ColliderShapeDefinition.ConvexMesh(vertices, triangles, inertiaPolicy), localOffset, FixedQuaternion.Identit |
| | | 212 | | |
| | | 213 | | /// <summary>Creates a transformed convex-mesh part.</summary> |
| | | 214 | | public static CompoundColliderPart ConvexMesh( |
| | | 215 | | Vector3d[] vertices, |
| | | 216 | | int[] triangles, |
| | | 217 | | Vector3d localOffset, |
| | | 218 | | FixedQuaternion localRotation, |
| | | 219 | | Vector3d localScale, |
| | | 220 | | MeshInertiaPolicy inertiaPolicy = MeshInertiaPolicy.RequireClosedVolume) => |
| | 6 | 221 | | new(ColliderShapeDefinition.ConvexMesh(vertices, triangles, inertiaPolicy), localOffset, localRotation, localSca |
| | | 222 | | |
| | | 223 | | internal bool IsDefault |
| | | 224 | | { |
| | | 225 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 288 | 226 | | get => Shape.Kind == ColliderShapeDefinitionKind.Undefined; |
| | | 227 | | } |
| | | 228 | | |
| | | 229 | | internal bool TryGetMaterial(out PhysicsMaterial material) |
| | | 230 | | { |
| | 406 | 231 | | if (_hasMaterial) |
| | | 232 | | { |
| | 34 | 233 | | material = _material; |
| | 34 | 234 | | return true; |
| | | 235 | | } |
| | | 236 | | |
| | 372 | 237 | | if (Shape.HasMaterial) |
| | | 238 | | { |
| | 40 | 239 | | material = Shape.Material; |
| | 40 | 240 | | return true; |
| | | 241 | | } |
| | | 242 | | |
| | 332 | 243 | | material = PhysicsMaterial.Default; |
| | 332 | 244 | | return false; |
| | | 245 | | } |
| | | 246 | | |
| | | 247 | | internal PhysicsMaterial ResolveMaterial(PhysicsMaterial ownerMaterial) => |
| | 308 | 248 | | TryGetMaterial(out PhysicsMaterial material) ? material : ownerMaterial; |
| | | 249 | | |
| | | 250 | | private static void ValidateScale(Vector3d localScale) |
| | | 251 | | { |
| | 304 | 252 | | SwiftThrowHelper.ThrowIfArgument( |
| | 304 | 253 | | localScale.X <= Fixed64.Zero || localScale.Y <= Fixed64.Zero || localScale.Z <= Fixed64.Zero, |
| | 304 | 254 | | nameof(localScale), |
| | 304 | 255 | | "Compound collider part scale components must be greater than zero."); |
| | 302 | 256 | | } |
| | | 257 | | } |