| | | 1 | | //======================================================================= |
| | | 2 | | // ContinuousCollisionContactPolicy.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 | | |
| | | 12 | | namespace Gravitas.CollisionHandling; |
| | | 13 | | |
| | | 14 | | /// <summary>Resolves target surface witnesses and normals for swept-sphere hits.</summary> |
| | | 15 | | internal static class ContinuousCollisionContactPolicy |
| | | 16 | | { |
| | | 17 | | internal static bool TryResolveSweptSphereContact( |
| | | 18 | | LSCollider target, |
| | | 19 | | Vector3d sphereCenterAtImpact, |
| | | 20 | | Vector3d direction, |
| | | 21 | | out ContactAnchor targetAnchor, |
| | | 22 | | out Vector3d normal) |
| | | 23 | | { |
| | 12413 | 24 | | if (target is LSSphereCollider or LSCapsuleCollider) |
| | | 25 | | { |
| | | 26 | | // Keep the same reducer's witness; an inverse-normal round trip can shift it. |
| | 7872 | 27 | | FixedPointAnchor surface = target.GetClosestSurfaceAnchor( |
| | 7872 | 28 | | sphereCenterAtImpact, out normal); |
| | 7872 | 29 | | targetAnchor = new ContactAnchor(surface); |
| | 7872 | 30 | | return true; |
| | | 31 | | } |
| | | 32 | | |
| | | 33 | | // Solid-volume normals remain outward for contained starts: CCD must |
| | | 34 | | // permit escape and block deeper motion. Signed distance is not a flip. |
| | 4541 | 35 | | if (target is LSCylinderCollider cylinder) |
| | | 36 | | { |
| | 363 | 37 | | if (!FixedSegment.TryGetClosestCenteredFiniteCylinderSurfaceAnchor( |
| | 363 | 38 | | sphereCenterAtImpact, |
| | 363 | 39 | | cylinder.Center, |
| | 363 | 40 | | cylinder.Rotation, |
| | 363 | 41 | | Vector3d.Up, |
| | 363 | 42 | | cylinder.Height, |
| | 363 | 43 | | cylinder.ScaledRadius, |
| | 363 | 44 | | Vector3d.Right, |
| | 363 | 45 | | out FixedPointAnchor targetPoint, |
| | 363 | 46 | | out normal, |
| | 363 | 47 | | out _)) |
| | | 48 | | { |
| | 2 | 49 | | targetAnchor = default; |
| | 2 | 50 | | normal = default; |
| | 2 | 51 | | return false; |
| | | 52 | | } |
| | | 53 | | |
| | 361 | 54 | | targetAnchor = new ContactAnchor(targetPoint); |
| | 361 | 55 | | return true; |
| | | 56 | | } |
| | | 57 | | |
| | 4178 | 58 | | if (target is LSConeCollider cone) |
| | | 59 | | { |
| | 354 | 60 | | if (!FixedSegment.TryGetClosestCenteredFiniteConeSurfaceAnchor( |
| | 354 | 61 | | sphereCenterAtImpact, |
| | 354 | 62 | | cone.Center, |
| | 354 | 63 | | cone.Rotation, |
| | 354 | 64 | | Vector3d.Up, |
| | 354 | 65 | | cone.Height, |
| | 354 | 66 | | cone.ScaledRadius, |
| | 354 | 67 | | Vector3d.Right, |
| | 354 | 68 | | out FixedPointAnchor targetPoint, |
| | 354 | 69 | | out normal, |
| | 354 | 70 | | out _)) |
| | | 71 | | { |
| | 1 | 72 | | targetAnchor = default; |
| | 1 | 73 | | normal = default; |
| | 1 | 74 | | return false; |
| | | 75 | | } |
| | | 76 | | |
| | 353 | 77 | | targetAnchor = new ContactAnchor(targetPoint); |
| | 353 | 78 | | return true; |
| | | 79 | | } |
| | | 80 | | |
| | 3824 | 81 | | if (target is LSCuboidCollider cuboid) |
| | | 82 | | { |
| | 933 | 83 | | FixedPointAnchor targetPoint = cuboid.GetClosestSurfaceAnchor( |
| | 933 | 84 | | sphereCenterAtImpact, out normal, out _); |
| | 933 | 85 | | targetAnchor = new ContactAnchor(targetPoint); |
| | 933 | 86 | | return true; |
| | | 87 | | } |
| | | 88 | | |
| | 2891 | 89 | | if (target is LSMeshCollider mesh) |
| | | 90 | | { |
| | 1663 | 91 | | mesh.FindClosestPointAnchor( |
| | 1663 | 92 | | sphereCenterAtImpact, |
| | 1663 | 93 | | out FixedPointAnchor meshPoint, |
| | 1663 | 94 | | out normal); |
| | 1663 | 95 | | normal = ResolveSurfaceNormal(meshPoint, sphereCenterAtImpact, normal); |
| | 1663 | 96 | | targetAnchor = new ContactAnchor(meshPoint); |
| | 1663 | 97 | | return true; |
| | | 98 | | } |
| | | 99 | | |
| | 1228 | 100 | | if (target is LSCompoundCollider compound) |
| | | 101 | | { |
| | 1223 | 102 | | FixedPointAnchor surfacePoint = compound.GetClosestSurfaceAnchor( |
| | 1223 | 103 | | sphereCenterAtImpact, out normal, out LSCollider surfaceOwner); |
| | 1223 | 104 | | if (surfaceOwner is LSMeshCollider) |
| | 541 | 105 | | normal = ResolveSurfaceNormal(surfacePoint, sphereCenterAtImpact, normal); |
| | | 106 | | // Keep the selected part's witness and outward primitive normal. |
| | | 107 | | // Re-querying a materialized point can select another part at a |
| | | 108 | | // shared boundary or lose an otherwise valid far-domain anchor. |
| | 1223 | 109 | | targetAnchor = new ContactAnchor(surfacePoint); |
| | 1223 | 110 | | return true; |
| | | 111 | | } |
| | | 112 | | |
| | 5 | 113 | | Vector3d point = target.ClosestPointOnSurface(sphereCenterAtImpact); |
| | 5 | 114 | | normal = ResolveLegacyNormal(target, point, sphereCenterAtImpact, direction); |
| | 5 | 115 | | var worldPoint = new FixedPointAnchor( |
| | 5 | 116 | | point, |
| | 5 | 117 | | FixedQuaternion.Identity, |
| | 5 | 118 | | Vector3d.Zero); |
| | 5 | 119 | | if (worldPoint.TryGetLocalPointIn( |
| | 5 | 120 | | target.Center, |
| | 5 | 121 | | target.Rotation, |
| | 5 | 122 | | out Vector3d localPoint)) |
| | | 123 | | { |
| | 4 | 124 | | targetAnchor = new ContactAnchor( |
| | 4 | 125 | | target.Center, |
| | 4 | 126 | | target.Rotation, |
| | 4 | 127 | | localPoint); |
| | 4 | 128 | | return true; |
| | | 129 | | } |
| | | 130 | | |
| | 1 | 131 | | targetAnchor = default; |
| | 1 | 132 | | normal = default; |
| | 1 | 133 | | return false; |
| | | 134 | | } |
| | | 135 | | |
| | | 136 | | internal static Vector3d ResolveSurfaceNormal( |
| | | 137 | | in FixedPointAnchor surfacePoint, Vector3d sourceCenter, Vector3d outwardNormal) |
| | | 138 | | { |
| | | 139 | | // Exterior apices and two-sided meshes need the exact closest-feature |
| | | 140 | | // direction; only coincidence retains the selected feature's normal. |
| | 2471 | 141 | | var center = new FixedPointAnchor(sourceCenter, FixedQuaternion.Identity, Vector3d.Zero); |
| | 2471 | 142 | | Vector3d normal = WidePointAnchor3d.GetDirection(center, surfacePoint); |
| | 2471 | 143 | | return normal == Vector3d.Zero ? outwardNormal : normal; |
| | | 144 | | } |
| | | 145 | | |
| | | 146 | | private static Vector3d ResolveLegacyNormal( |
| | | 147 | | LSCollider target, |
| | | 148 | | Vector3d point, |
| | | 149 | | Vector3d sphereCenterAtImpact, |
| | | 150 | | Vector3d direction) |
| | | 151 | | { |
| | 5 | 152 | | Vector3d fromPointToSphereCenter = sphereCenterAtImpact - point; |
| | 5 | 153 | | Vector3d normal = target.GetNormalAtPoint(point); |
| | 5 | 154 | | if (normal.MagnitudeSquared > Fixed64.Epsilon) |
| | 1 | 155 | | return normal.Normalized; |
| | | 156 | | |
| | 4 | 157 | | if (fromPointToSphereCenter.MagnitudeSquared > Fixed64.Epsilon) |
| | 1 | 158 | | return fromPointToSphereCenter.Normalized; |
| | | 159 | | |
| | 3 | 160 | | return direction.MagnitudeSquared > Fixed64.Epsilon |
| | 3 | 161 | | ? -direction.Normalized |
| | 3 | 162 | | : Vector3d.Zero; |
| | | 163 | | } |
| | | 164 | | } |