| | | 1 | | //======================================================================= |
| | | 2 | | // ColliderPlanarProjection.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.Queries; |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// Dispatches exact X/Z projected-circle relations for built-in 3D colliders. |
| | | 16 | | /// </summary> |
| | | 17 | | internal static class ColliderPlanarProjection |
| | | 18 | | { |
| | | 19 | | internal static bool TryGetRelation( |
| | | 20 | | LSCollider collider, |
| | | 21 | | Vector2d circleCenter, |
| | | 22 | | Fixed64 circleRadius, |
| | | 23 | | out ProjectedSurfaceRelation relation) |
| | | 24 | | { |
| | 1741 | 25 | | if (collider is LSCompoundCollider compound) |
| | | 26 | | { |
| | 213 | 27 | | return TryGetCompoundRelation( |
| | 213 | 28 | | compound, |
| | 213 | 29 | | circleCenter, |
| | 213 | 30 | | circleRadius, |
| | 213 | 31 | | out relation); |
| | | 32 | | } |
| | 1528 | 33 | | if (collider is LSMeshCollider mesh) |
| | | 34 | | { |
| | 217 | 35 | | return TryGetMeshRelation( |
| | 217 | 36 | | mesh, |
| | 217 | 37 | | circleCenter, |
| | 217 | 38 | | circleRadius, |
| | 217 | 39 | | out relation); |
| | | 40 | | } |
| | | 41 | | |
| | | 42 | | bool found; |
| | | 43 | | PlanarProjectionRelation planar; |
| | | 44 | | switch (collider) |
| | | 45 | | { |
| | | 46 | | case LSSphereCollider sphere: |
| | 462 | 47 | | found = WidePlanarProjection.TryGetSphereRelation( |
| | 462 | 48 | | circleCenter, |
| | 462 | 49 | | circleRadius, |
| | 462 | 50 | | sphere.Center, |
| | 462 | 51 | | sphere.ScaledRadius, |
| | 462 | 52 | | out planar); |
| | 462 | 53 | | break; |
| | | 54 | | case LSCapsuleCollider capsule: |
| | 210 | 55 | | found = WidePlanarProjection |
| | 210 | 56 | | .TryGetCenteredCapsuleRelation( |
| | 210 | 57 | | circleCenter, |
| | 210 | 58 | | circleRadius, |
| | 210 | 59 | | capsule.Center, |
| | 210 | 60 | | capsule.Rotation, |
| | 210 | 61 | | Vector3d.Up, |
| | 210 | 62 | | capsule.AxisLength, |
| | 210 | 63 | | capsule.ScaledRadius, |
| | 210 | 64 | | out planar); |
| | 210 | 65 | | break; |
| | | 66 | | case LSCylinderCollider cylinder: |
| | 214 | 67 | | found = WidePlanarProjection |
| | 214 | 68 | | .TryGetCenteredCylinderRelation( |
| | 214 | 69 | | circleCenter, |
| | 214 | 70 | | circleRadius, |
| | 214 | 71 | | cylinder.Center, |
| | 214 | 72 | | cylinder.Rotation, |
| | 214 | 73 | | cylinder.Height, |
| | 214 | 74 | | cylinder.ScaledRadius, |
| | 214 | 75 | | out planar); |
| | 214 | 76 | | break; |
| | | 77 | | case LSConeCollider cone: |
| | 211 | 78 | | found = WidePlanarProjection |
| | 211 | 79 | | .TryGetCenteredConeRelation( |
| | 211 | 80 | | circleCenter, |
| | 211 | 81 | | circleRadius, |
| | 211 | 82 | | cone.Center, |
| | 211 | 83 | | cone.Rotation, |
| | 211 | 84 | | cone.Height, |
| | 211 | 85 | | cone.ScaledRadius, |
| | 211 | 86 | | out planar); |
| | 211 | 87 | | break; |
| | | 88 | | case LSCuboidCollider cuboid: |
| | 213 | 89 | | found = WidePlanarProjection |
| | 213 | 90 | | .TryGetOrientedBoxRelation( |
| | 213 | 91 | | circleCenter, |
| | 213 | 92 | | circleRadius, |
| | 213 | 93 | | cuboid.OrientedBox, |
| | 213 | 94 | | out planar); |
| | 213 | 95 | | break; |
| | | 96 | | default: |
| | 1 | 97 | | relation = default; |
| | 1 | 98 | | return false; |
| | | 99 | | } |
| | | 100 | | |
| | 1310 | 101 | | if (!found) |
| | | 102 | | { |
| | 6 | 103 | | relation = default; |
| | 6 | 104 | | return false; |
| | | 105 | | } |
| | | 106 | | |
| | 1304 | 107 | | Vector3d queryPoint = GetCanonicalQueryPoint( |
| | 1304 | 108 | | collider, |
| | 1304 | 109 | | circleCenter); |
| | 1304 | 110 | | FixedPointAnchor anchor = |
| | 1304 | 111 | | collider.GetClosestSurfaceAnchor( |
| | 1304 | 112 | | queryPoint, |
| | 1304 | 113 | | out Vector3d normal); |
| | 1304 | 114 | | relation = new ProjectedSurfaceRelation( |
| | 1304 | 115 | | planar.Distance, |
| | 1304 | 116 | | planar.Offset, |
| | 1304 | 117 | | planar.Direction, |
| | 1304 | 118 | | anchor, |
| | 1304 | 119 | | normal, |
| | 1304 | 120 | | planar.IsContained); |
| | 1304 | 121 | | return true; |
| | | 122 | | } |
| | | 123 | | |
| | | 124 | | private static bool TryGetCompoundRelation( |
| | | 125 | | LSCompoundCollider compound, |
| | | 126 | | Vector2d circleCenter, |
| | | 127 | | Fixed64 circleRadius, |
| | | 128 | | out ProjectedSurfaceRelation relation) |
| | | 129 | | { |
| | 213 | 130 | | bool found = false; |
| | 213 | 131 | | relation = default; |
| | 862 | 132 | | for (int index = 0; index < compound.PartCount; index++) |
| | | 133 | | { |
| | 218 | 134 | | if (!TryGetRelation( |
| | 218 | 135 | | compound.GetPartCollider(index), |
| | 218 | 136 | | circleCenter, |
| | 218 | 137 | | circleRadius, |
| | 218 | 138 | | out ProjectedSurfaceRelation candidate) |
| | 218 | 139 | | || (found |
| | 218 | 140 | | && ShouldKeepCurrent(candidate, relation))) |
| | | 141 | | { |
| | | 142 | | continue; |
| | | 143 | | } |
| | | 144 | | |
| | 215 | 145 | | found = true; |
| | 215 | 146 | | relation = candidate; |
| | | 147 | | } |
| | | 148 | | |
| | 213 | 149 | | return found; |
| | | 150 | | } |
| | | 151 | | |
| | | 152 | | private static bool TryGetMeshRelation( |
| | | 153 | | LSMeshCollider collider, |
| | | 154 | | Vector2d circleCenter, |
| | | 155 | | Fixed64 circleRadius, |
| | | 156 | | out ProjectedSurfaceRelation relation) |
| | | 157 | | { |
| | 217 | 158 | | PhysicsMesh mesh = collider.Mesh; |
| | 217 | 159 | | bool found = false; |
| | 217 | 160 | | PlanarProjectionRelation best = default; |
| | 217 | 161 | | int bestTriangle = -1; |
| | 217 | 162 | | FixedTriangle bestGeometry = default; |
| | 884 | 163 | | for (int index = 0; index < mesh.TriangleCount; index++) |
| | | 164 | | { |
| | 225 | 165 | | mesh.GetLocalTriangleVertices( |
| | 225 | 166 | | index, |
| | 225 | 167 | | out Vector3d first, |
| | 225 | 168 | | out Vector3d second, |
| | 225 | 169 | | out Vector3d third); |
| | 225 | 170 | | var triangle = new FixedTriangle(first, second, third); |
| | 225 | 171 | | if (!WidePlanarProjection.TryGetTriangleRelation( |
| | 225 | 172 | | circleCenter, |
| | 225 | 173 | | circleRadius, |
| | 225 | 174 | | triangle, |
| | 225 | 175 | | mesh.Origin, |
| | 225 | 176 | | mesh.Rotation, |
| | 225 | 177 | | out PlanarProjectionRelation candidate) |
| | 225 | 178 | | || (found && ShouldKeepCurrent(candidate, best))) |
| | | 179 | | { |
| | | 180 | | continue; |
| | | 181 | | } |
| | | 182 | | |
| | 218 | 183 | | found = true; |
| | 218 | 184 | | best = candidate; |
| | 218 | 185 | | bestTriangle = index; |
| | 218 | 186 | | bestGeometry = triangle; |
| | | 187 | | } |
| | | 188 | | |
| | 217 | 189 | | if (!found) |
| | | 190 | | { |
| | 1 | 191 | | relation = default; |
| | 1 | 192 | | return false; |
| | | 193 | | } |
| | | 194 | | |
| | 216 | 195 | | var queryAnchor = new FixedPointAnchor( |
| | 216 | 196 | | GetCanonicalQueryPoint( |
| | 216 | 197 | | collider, |
| | 216 | 198 | | circleCenter), |
| | 216 | 199 | | FixedQuaternion.Identity, |
| | 216 | 200 | | Vector3d.Zero); |
| | 216 | 201 | | FixedPointAnchor contact = bestGeometry.GetClosestPointAnchor( |
| | 216 | 202 | | mesh.Origin, |
| | 216 | 203 | | mesh.Rotation, |
| | 216 | 204 | | queryAnchor); |
| | 216 | 205 | | relation = new ProjectedSurfaceRelation( |
| | 216 | 206 | | best.Distance, |
| | 216 | 207 | | best.Offset, |
| | 216 | 208 | | best.Direction, |
| | 216 | 209 | | contact, |
| | 216 | 210 | | mesh.GetFaceNormalWorld(bestTriangle), |
| | 216 | 211 | | best.IsContained); |
| | 216 | 212 | | return true; |
| | | 213 | | } |
| | | 214 | | |
| | | 215 | | private static bool ShouldKeepCurrent( |
| | | 216 | | ProjectedSurfaceRelation candidate, |
| | | 217 | | ProjectedSurfaceRelation current) => |
| | 4 | 218 | | candidate.Distance > current.Distance |
| | 4 | 219 | | || (candidate.Distance == current.Distance |
| | 4 | 220 | | && candidate.IsContained.CompareTo(current.IsContained) <= 0); |
| | | 221 | | |
| | | 222 | | private static bool ShouldKeepCurrent( |
| | | 223 | | PlanarProjectionRelation candidate, |
| | | 224 | | PlanarProjectionRelation current) => |
| | 7 | 225 | | candidate.Distance > current.Distance |
| | 7 | 226 | | || (candidate.Distance == current.Distance |
| | 7 | 227 | | && candidate.IsContained.CompareTo(current.IsContained) <= 0); |
| | | 228 | | |
| | | 229 | | private static Vector3d GetCanonicalQueryPoint( |
| | | 230 | | LSCollider collider, |
| | | 231 | | Vector2d circleCenter) => |
| | 1520 | 232 | | new( |
| | 1520 | 233 | | circleCenter.X, |
| | 1520 | 234 | | collider.Center.Y, |
| | 1520 | 235 | | circleCenter.Y); |
| | | 236 | | } |