| | | 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 | | internal static class ContinuousCollisionContactPolicy |
| | | 15 | | { |
| | | 16 | | internal static bool TryResolveSweptSphereContact( |
| | | 17 | | LSCollider target, |
| | | 18 | | Vector3d sphereCenterAtImpact, |
| | | 19 | | Vector3d direction, |
| | | 20 | | out ContactAnchor targetAnchor, |
| | | 21 | | out Vector3d normal) |
| | | 22 | | { |
| | 7206 | 23 | | Vector3d fallbackNormal = direction.MagnitudeSquared > Fixed64.Epsilon |
| | 7206 | 24 | | ? -direction.Normalized |
| | 7206 | 25 | | : Vector3d.Right; |
| | | 26 | | |
| | 7206 | 27 | | if (target is LSCapsuleCollider capsule) |
| | | 28 | | { |
| | 25 | 29 | | normal = capsule.GetNormalAtPoint(sphereCenterAtImpact); |
| | 25 | 30 | | Vector3d localNormal = |
| | 25 | 31 | | capsule.Rotation.Inverse().Rotate(normal).Normalized; |
| | 25 | 32 | | targetAnchor = new ContactAnchor( |
| | 25 | 33 | | FixedSegment.GetSurfaceAnchorOnCenteredCapsule( |
| | 25 | 34 | | sphereCenterAtImpact, |
| | 25 | 35 | | capsule.Center, |
| | 25 | 36 | | capsule.Rotation, |
| | 25 | 37 | | Vector3d.Up, |
| | 25 | 38 | | capsule.AxisLength, |
| | 25 | 39 | | capsule.ScaledRadius, |
| | 25 | 40 | | localNormal)); |
| | 25 | 41 | | return true; |
| | | 42 | | } |
| | | 43 | | |
| | 7181 | 44 | | if (target is LSSphereCollider sphere) |
| | | 45 | | { |
| | 6648 | 46 | | normal = Vector3d.GetDirection(sphere.Center, sphereCenterAtImpact); |
| | 6648 | 47 | | if (normal == Vector3d.Zero) |
| | 3 | 48 | | normal = fallbackNormal; |
| | 6648 | 49 | | targetAnchor = new ContactAnchor( |
| | 6648 | 50 | | FixedSegment.GetCenteredCapsuleSupportAnchor( |
| | 6648 | 51 | | sphere.Center, |
| | 6648 | 52 | | sphere.Rotation, |
| | 6648 | 53 | | Fixed64.Zero, |
| | 6648 | 54 | | sphere.ScaledRadius, |
| | 6648 | 55 | | normal)); |
| | 6648 | 56 | | return true; |
| | | 57 | | } |
| | | 58 | | |
| | 533 | 59 | | if (target is LSCylinderCollider cylinder) |
| | | 60 | | { |
| | 23 | 61 | | if (!FixedSegment.TryGetClosestCenteredFiniteCylinderSurfaceAnchor( |
| | 23 | 62 | | sphereCenterAtImpact, |
| | 23 | 63 | | cylinder.Center, |
| | 23 | 64 | | cylinder.Rotation, |
| | 23 | 65 | | Vector3d.Up, |
| | 23 | 66 | | cylinder.Height, |
| | 23 | 67 | | cylinder.ScaledRadius, |
| | 23 | 68 | | Vector3d.Right, |
| | 23 | 69 | | out FixedPointAnchor targetPoint, |
| | 23 | 70 | | out Vector3d outwardNormal, |
| | 23 | 71 | | out Fixed64 signedDistance)) |
| | | 72 | | { |
| | 1 | 73 | | targetAnchor = default; |
| | 1 | 74 | | normal = default; |
| | 1 | 75 | | return false; |
| | | 76 | | } |
| | | 77 | | |
| | 22 | 78 | | targetAnchor = new ContactAnchor(targetPoint); |
| | 22 | 79 | | normal = signedDistance < Fixed64.Zero ? -outwardNormal : outwardNormal; |
| | 22 | 80 | | return true; |
| | | 81 | | } |
| | | 82 | | |
| | 510 | 83 | | if (target is LSConeCollider cone) |
| | | 84 | | { |
| | 15 | 85 | | if (!FixedSegment.TryGetClosestCenteredFiniteConeSurfaceAnchor( |
| | 15 | 86 | | sphereCenterAtImpact, |
| | 15 | 87 | | cone.Center, |
| | 15 | 88 | | cone.Rotation, |
| | 15 | 89 | | Vector3d.Up, |
| | 15 | 90 | | cone.Height, |
| | 15 | 91 | | cone.ScaledRadius, |
| | 15 | 92 | | Vector3d.Right, |
| | 15 | 93 | | out FixedPointAnchor targetPoint, |
| | 15 | 94 | | out Vector3d outwardNormal, |
| | 15 | 95 | | out Fixed64 signedDistance)) |
| | | 96 | | { |
| | 1 | 97 | | targetAnchor = default; |
| | 1 | 98 | | normal = default; |
| | 1 | 99 | | return false; |
| | | 100 | | } |
| | | 101 | | |
| | 14 | 102 | | targetAnchor = new ContactAnchor(targetPoint); |
| | 14 | 103 | | normal = signedDistance < Fixed64.Zero ? -outwardNormal : outwardNormal; |
| | 14 | 104 | | return true; |
| | | 105 | | } |
| | | 106 | | |
| | 495 | 107 | | if (target is LSCuboidCollider cuboid) |
| | | 108 | | { |
| | 430 | 109 | | FixedPointAnchor targetPoint = |
| | 430 | 110 | | cuboid.OrientedBox.GetClosestPointAnchor(sphereCenterAtImpact); |
| | 430 | 111 | | var sphereCenter = new FixedPointAnchor( |
| | 430 | 112 | | sphereCenterAtImpact, |
| | 430 | 113 | | FixedQuaternion.Identity, |
| | 430 | 114 | | Vector3d.Zero); |
| | 430 | 115 | | normal = sphereCenter.TryGetOffsetFrom( |
| | 430 | 116 | | targetPoint, |
| | 430 | 117 | | out Vector3d fromSurface) |
| | 430 | 118 | | && fromSurface.MagnitudeSquared > Fixed64.Epsilon |
| | 430 | 119 | | ? fromSurface.Normalized |
| | 430 | 120 | | : cuboid.GetNormalAtPoint(sphereCenterAtImpact); |
| | 430 | 121 | | targetAnchor = new ContactAnchor(targetPoint); |
| | 430 | 122 | | return true; |
| | | 123 | | } |
| | | 124 | | |
| | 65 | 125 | | if (target is LSMeshCollider mesh) |
| | | 126 | | { |
| | 49 | 127 | | mesh.FindClosestPointAnchor( |
| | 49 | 128 | | sphereCenterAtImpact, |
| | 49 | 129 | | out FixedPointAnchor meshPoint, |
| | 49 | 130 | | out normal); |
| | 49 | 131 | | if (Vector3d.Dot(normal, direction) > Fixed64.Zero) |
| | 4 | 132 | | normal = -normal; |
| | 49 | 133 | | targetAnchor = new ContactAnchor(meshPoint); |
| | 49 | 134 | | return true; |
| | | 135 | | } |
| | | 136 | | |
| | 16 | 137 | | Vector3d point = target.ClosestPointOnSurface(sphereCenterAtImpact); |
| | 16 | 138 | | normal = ResolveLegacyNormal(target, point, sphereCenterAtImpact, direction); |
| | 16 | 139 | | var worldPoint = new FixedPointAnchor( |
| | 16 | 140 | | point, |
| | 16 | 141 | | FixedQuaternion.Identity, |
| | 16 | 142 | | Vector3d.Zero); |
| | 16 | 143 | | if (worldPoint.TryGetLocalPointIn( |
| | 16 | 144 | | target.Center, |
| | 16 | 145 | | target.Rotation, |
| | 16 | 146 | | out Vector3d localPoint)) |
| | | 147 | | { |
| | 13 | 148 | | targetAnchor = new ContactAnchor( |
| | 13 | 149 | | target.Center, |
| | 13 | 150 | | target.Rotation, |
| | 13 | 151 | | localPoint); |
| | 13 | 152 | | return true; |
| | | 153 | | } |
| | | 154 | | |
| | 3 | 155 | | targetAnchor = default; |
| | 3 | 156 | | normal = default; |
| | 3 | 157 | | return false; |
| | | 158 | | } |
| | | 159 | | |
| | | 160 | | private static Vector3d ResolveLegacyNormal( |
| | | 161 | | LSCollider target, |
| | | 162 | | Vector3d point, |
| | | 163 | | Vector3d sphereCenterAtImpact, |
| | | 164 | | Vector3d direction) |
| | | 165 | | { |
| | 16 | 166 | | Vector3d fromPointToSphereCenter = sphereCenterAtImpact - point; |
| | 16 | 167 | | Vector3d normal = target.GetNormalAtPoint(point); |
| | 16 | 168 | | if (normal.MagnitudeSquared > Fixed64.Epsilon) |
| | 12 | 169 | | return normal.Normalized; |
| | | 170 | | |
| | 4 | 171 | | if (fromPointToSphereCenter.MagnitudeSquared > Fixed64.Epsilon) |
| | 1 | 172 | | return fromPointToSphereCenter.Normalized; |
| | | 173 | | |
| | 3 | 174 | | return direction.MagnitudeSquared > Fixed64.Epsilon |
| | 3 | 175 | | ? -direction.Normalized |
| | 3 | 176 | | : Vector3d.Zero; |
| | | 177 | | } |
| | | 178 | | } |