| | | 1 | | //======================================================================= |
| | | 2 | | // ConvexColliderSupport.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; |
| | | 9 | | using System.Runtime.CompilerServices; |
| | | 10 | | using FixedMathSharp; |
| | | 11 | | using FixedMathSharp.Geometry; |
| | | 12 | | using Gravitas.Colliders; |
| | | 13 | | |
| | | 14 | | namespace Gravitas.CollisionHandling; |
| | | 15 | | |
| | | 16 | | /// <summary> |
| | | 17 | | /// Produces support witnesses and tests intersections for supported convex 3D colliders. |
| | | 18 | | /// </summary> |
| | | 19 | | internal static class ConvexColliderSupport |
| | | 20 | | { |
| | | 21 | | public static FixedPointAnchor GetSupportAnchor( |
| | | 22 | | LSCollider collider, |
| | | 23 | | Vector3d direction, |
| | | 24 | | Vector3d originOffset, |
| | | 25 | | bool preferAxialCenter = false) |
| | | 26 | | { |
| | 49158 | 27 | | Vector3d normal = ResolveSupportDirection(direction); |
| | 49158 | 28 | | Vector3d localTranslation = |
| | 49158 | 29 | | GetLocalDisplacement(collider.Rotation, originOffset); |
| | 49157 | 30 | | if (collider is LSSphereCollider sphere) |
| | | 31 | | { |
| | 2729 | 32 | | return FixedSegment.GetCenteredCapsuleSupportAnchor( |
| | 2729 | 33 | | sphere.Center, |
| | 2729 | 34 | | sphere.Rotation, |
| | 2729 | 35 | | Fixed64.Zero, |
| | 2729 | 36 | | sphere.ScaledRadius, |
| | 2729 | 37 | | normal) |
| | 2729 | 38 | | .WithLocalTranslation(localTranslation); |
| | | 39 | | } |
| | 46428 | 40 | | if (collider is LSCapsuleCollider capsule) |
| | | 41 | | { |
| | 13085 | 42 | | return FixedSegment.GetCenteredCapsuleSupportAnchor( |
| | 13085 | 43 | | capsule.Center, |
| | 13085 | 44 | | capsule.Rotation, |
| | 13085 | 45 | | capsule.AxisLength, |
| | 13085 | 46 | | capsule.ScaledRadius, |
| | 13085 | 47 | | normal) |
| | 13085 | 48 | | .WithLocalTranslation(localTranslation); |
| | | 49 | | } |
| | 33343 | 50 | | if (collider is LSCuboidCollider cuboid) |
| | | 51 | | { |
| | 13763 | 52 | | return new FixedPointAnchor( |
| | 13763 | 53 | | cuboid.Center, |
| | 13763 | 54 | | cuboid.Rotation, |
| | 13763 | 55 | | cuboid.OrientedBox.GetLocalSupportPoint(normal)) |
| | 13763 | 56 | | .WithLocalTranslation(localTranslation); |
| | | 57 | | } |
| | 19580 | 58 | | if (collider is LSCylinderCollider cylinder) |
| | | 59 | | { |
| | 3458 | 60 | | FixedPointAnchor support = preferAxialCenter |
| | 3458 | 61 | | ? WideGeometry.GetCenteredCylinderSupportAnchor( |
| | 3458 | 62 | | cylinder.Center, cylinder.Rotation, cylinder.Height, |
| | 3458 | 63 | | cylinder.ScaledRadius, normal, preferAxialCenter: true) |
| | 3458 | 64 | | : FixedSegment.GetCenteredFiniteCylinderSupportAnchor( |
| | 3458 | 65 | | cylinder.Center, cylinder.Rotation, cylinder.Height, |
| | 3458 | 66 | | cylinder.ScaledRadius, normal); |
| | 3458 | 67 | | return support.WithLocalTranslation(localTranslation); |
| | | 68 | | } |
| | 16122 | 69 | | if (collider is LSConeCollider cone) |
| | | 70 | | { |
| | 7401 | 71 | | return FixedSegment.GetCenteredFiniteConeSupportAnchor( |
| | 7401 | 72 | | cone.Center, |
| | 7401 | 73 | | cone.Rotation, |
| | 7401 | 74 | | cone.Height, |
| | 7401 | 75 | | cone.ScaledRadius, |
| | 7401 | 76 | | normal) |
| | 7401 | 77 | | .WithLocalTranslation(localTranslation); |
| | | 78 | | } |
| | 8721 | 79 | | if (collider is LSMeshCollider { Mode: MeshColliderMode.Convex } mesh) |
| | | 80 | | { |
| | 8720 | 81 | | FixedPointAnchor support = mesh.Mesh.CreatePointAnchor(mesh.Mesh.GetSupportVertexLocal(normal)); |
| | 8720 | 82 | | return support.WithLocalTranslation(support.Rotation == FixedQuaternion.Identity |
| | 8720 | 83 | | ? originOffset : localTranslation); |
| | | 84 | | } |
| | | 85 | | |
| | 1 | 86 | | throw new NotSupportedException( |
| | 1 | 87 | | $"Convex support mapping does not support {collider.GetType().Name}."); |
| | | 88 | | } |
| | | 89 | | |
| | | 90 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 91 | | public static bool IsSupported(LSCollider collider) => |
| | 5231 | 92 | | collider is LSSphereCollider |
| | 5231 | 93 | | or LSCapsuleCollider |
| | 5231 | 94 | | or LSCuboidCollider |
| | 5231 | 95 | | or LSCylinderCollider |
| | 5231 | 96 | | or LSConeCollider |
| | 5231 | 97 | | or LSMeshCollider { Mode: MeshColliderMode.Convex }; |
| | | 98 | | |
| | | 99 | | public static bool Intersects(LSCollider first, LSCollider second, int maxIterations = 32) |
| | | 100 | | { |
| | 821 | 101 | | if (!IsSupported(first) || !IsSupported(second)) |
| | 2 | 102 | | return false; |
| | | 103 | | |
| | 819 | 104 | | Span<Vector3d> simplex = stackalloc Vector3d[4]; |
| | 819 | 105 | | int count = 0; |
| | 819 | 106 | | Vector3d direction = second.Center - first.Center; |
| | 819 | 107 | | if (direction.MagnitudeSquared <= Fixed64.Epsilon) |
| | 124 | 108 | | direction = Vector3d.Right; |
| | | 109 | | |
| | 819 | 110 | | if (!TrySupportMinkowski(first, second, direction, out simplex[count])) |
| | 1 | 111 | | return false; |
| | 818 | 112 | | count++; |
| | 818 | 113 | | direction = -simplex[0]; |
| | 818 | 114 | | if (direction.MagnitudeSquared <= Fixed64.Epsilon) |
| | 4 | 115 | | return true; |
| | | 116 | | |
| | 4150 | 117 | | for (int i = 0; i < maxIterations; i++) |
| | | 118 | | { |
| | 2074 | 119 | | if (!TrySupportMinkowski(first, second, direction, out Vector3d point)) |
| | 1 | 120 | | return false; |
| | 2073 | 121 | | if (GjkSimplexPolicy.HasSeparatingSupport(point, direction)) |
| | 550 | 122 | | return false; |
| | | 123 | | |
| | 1523 | 124 | | GjkSimplexPolicy.AddPoint(simplex, ref count, point); |
| | 1523 | 125 | | if (GjkSimplexPolicy.Update(simplex, ref count, ref direction)) |
| | 260 | 126 | | return true; |
| | | 127 | | |
| | 1263 | 128 | | if (direction.MagnitudeSquared <= Fixed64.Epsilon) |
| | 2 | 129 | | return true; |
| | | 130 | | } |
| | | 131 | | |
| | | 132 | | // No separating support was found within the bounded search, so preserve touching/overlap. |
| | 1 | 133 | | return true; |
| | | 134 | | } |
| | | 135 | | |
| | | 136 | | public static bool IntersectsConeVolume( |
| | | 137 | | LSCollider collider, |
| | | 138 | | Vector3d apex, |
| | | 139 | | Vector3d baseCenter, |
| | | 140 | | Vector3d axis, |
| | | 141 | | Fixed64 endRadius, |
| | | 142 | | int maxIterations = 32) |
| | | 143 | | { |
| | 530 | 144 | | if (!IsSupported(collider)) |
| | 1 | 145 | | return false; |
| | | 146 | | |
| | 529 | 147 | | Span<Vector3d> simplex = stackalloc Vector3d[4]; |
| | 529 | 148 | | int count = 0; |
| | 529 | 149 | | Vector3d coneCenter = Vector3d.Midpoint(apex, baseCenter); |
| | 529 | 150 | | Vector3d direction = collider.Center - coneCenter; |
| | 529 | 151 | | if (direction.MagnitudeSquared <= Fixed64.Epsilon) |
| | 12 | 152 | | direction = Vector3d.Right; |
| | | 153 | | |
| | 529 | 154 | | if (!TrySupportMinkowskiConeCollider( |
| | 529 | 155 | | collider, |
| | 529 | 156 | | apex, |
| | 529 | 157 | | baseCenter, |
| | 529 | 158 | | axis, |
| | 529 | 159 | | endRadius, |
| | 529 | 160 | | direction, |
| | 529 | 161 | | out simplex[count])) |
| | | 162 | | { |
| | 1 | 163 | | return false; |
| | | 164 | | } |
| | 528 | 165 | | count++; |
| | 528 | 166 | | direction = -simplex[0]; |
| | 528 | 167 | | if (direction.MagnitudeSquared <= Fixed64.Epsilon) |
| | 17 | 168 | | return true; |
| | | 169 | | |
| | 2994 | 170 | | for (int i = 0; i < maxIterations; i++) |
| | | 171 | | { |
| | 1496 | 172 | | if (!TrySupportMinkowskiConeCollider( |
| | 1496 | 173 | | collider, |
| | 1496 | 174 | | apex, |
| | 1496 | 175 | | baseCenter, |
| | 1496 | 176 | | axis, |
| | 1496 | 177 | | endRadius, |
| | 1496 | 178 | | direction, |
| | 1496 | 179 | | out Vector3d point)) |
| | | 180 | | { |
| | 1 | 181 | | return false; |
| | | 182 | | } |
| | 1495 | 183 | | if (GjkSimplexPolicy.HasSeparatingSupport(point, direction)) |
| | 49 | 184 | | return false; |
| | | 185 | | |
| | 1446 | 186 | | GjkSimplexPolicy.AddPoint(simplex, ref count, point); |
| | 1446 | 187 | | if (GjkSimplexPolicy.Update(simplex, ref count, ref direction)) |
| | 459 | 188 | | return true; |
| | | 189 | | |
| | 987 | 190 | | if (direction.MagnitudeSquared <= Fixed64.Epsilon) |
| | 1 | 191 | | return true; |
| | | 192 | | } |
| | | 193 | | |
| | | 194 | | // No separating support was found within the bounded search, so preserve touching/overlap. |
| | 1 | 195 | | return true; |
| | | 196 | | } |
| | | 197 | | |
| | | 198 | | private static bool TrySupportMinkowski( |
| | | 199 | | LSCollider first, |
| | | 200 | | LSCollider second, |
| | | 201 | | Vector3d direction, |
| | | 202 | | out Vector3d difference) |
| | | 203 | | { |
| | 2893 | 204 | | Vector3d normal = ResolveSupportDirection(direction); |
| | 2893 | 205 | | if (first is LSCuboidCollider firstBox |
| | 2893 | 206 | | && second is LSCuboidCollider secondBox) |
| | | 207 | | { |
| | 3 | 208 | | return firstBox.OrientedBox.TryGetSupportDifference( |
| | 3 | 209 | | secondBox.OrientedBox, |
| | 3 | 210 | | normal, |
| | 3 | 211 | | out difference); |
| | | 212 | | } |
| | | 213 | | |
| | 2890 | 214 | | FixedPointAnchor supportA = |
| | 2890 | 215 | | GetSupportAnchor(first, normal, Vector3d.Zero); |
| | 2890 | 216 | | FixedPointAnchor supportB = |
| | 2890 | 217 | | GetSupportAnchor(second, -normal, Vector3d.Zero); |
| | 2890 | 218 | | return supportA.TryGetOffsetFrom(supportB, out difference); |
| | | 219 | | } |
| | | 220 | | |
| | | 221 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 222 | | private static bool TrySupportMinkowskiConeCollider( |
| | | 223 | | LSCollider collider, |
| | | 224 | | Vector3d apex, |
| | | 225 | | Vector3d baseCenter, |
| | | 226 | | Vector3d axis, |
| | | 227 | | Fixed64 endRadius, |
| | | 228 | | Vector3d direction, |
| | | 229 | | out Vector3d difference) |
| | | 230 | | { |
| | 2025 | 231 | | Vector3d normal = ResolveSupportDirection(direction); |
| | 2025 | 232 | | Vector3d radialDirection = |
| | 2025 | 233 | | Vector3d.GetNormalizedProjectionOnPlane(normal, axis); |
| | 2025 | 234 | | var apexAnchor = new FixedPointAnchor( |
| | 2025 | 235 | | apex, |
| | 2025 | 236 | | FixedQuaternion.Identity, |
| | 2025 | 237 | | Vector3d.Zero); |
| | 2025 | 238 | | var baseAnchor = new FixedPointAnchor( |
| | 2025 | 239 | | baseCenter, |
| | 2025 | 240 | | FixedQuaternion.Identity, |
| | 2025 | 241 | | radialDirection * endRadius); |
| | 2025 | 242 | | FixedPointAnchor coneSupport = |
| | 2025 | 243 | | baseAnchor.ProjectNonNegativeOffsetFrom(apexAnchor, normal) |
| | 2025 | 244 | | > Fixed64.Zero |
| | 2025 | 245 | | || apexAnchor.ProjectNonNegativeOffsetFrom(baseAnchor, normal) |
| | 2025 | 246 | | == Fixed64.Zero |
| | 2025 | 247 | | ? baseAnchor |
| | 2025 | 248 | | : apexAnchor; |
| | 2025 | 249 | | FixedPointAnchor colliderSupport = |
| | 2025 | 250 | | GetSupportAnchor(collider, -normal, Vector3d.Zero); |
| | 2025 | 251 | | return coneSupport.TryGetOffsetFrom( |
| | 2025 | 252 | | colliderSupport, |
| | 2025 | 253 | | out difference); |
| | | 254 | | } |
| | | 255 | | |
| | | 256 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 257 | | private static Vector3d ResolveSupportDirection(Vector3d direction) => |
| | 54076 | 258 | | direction != Vector3d.Zero |
| | 54076 | 259 | | ? direction.Normalized |
| | 54076 | 260 | | : Vector3d.Right; |
| | | 261 | | |
| | | 262 | | internal static Vector3d GetLocalDisplacement( |
| | | 263 | | FixedQuaternion rotation, |
| | | 264 | | Vector3d worldDisplacement) |
| | | 265 | | { |
| | 49170 | 266 | | if (rotation.Inverse().TryRotate( |
| | 49170 | 267 | | worldDisplacement, |
| | 49170 | 268 | | out Vector3d localDisplacement)) |
| | | 269 | | { |
| | 49169 | 270 | | return localDisplacement; |
| | | 271 | | } |
| | | 272 | | |
| | 1 | 273 | | throw new InvalidOperationException( |
| | 1 | 274 | | "The sweep displacement cannot be represented in the collider's canonical frame."); |
| | | 275 | | } |
| | | 276 | | |
| | | 277 | | } |