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