| | | 1 | | //======================================================================= |
| | | 2 | | // MixedContact.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.Materials; |
| | | 11 | | using System; |
| | | 12 | | using System.Runtime.CompilerServices; |
| | | 13 | | |
| | | 14 | | namespace Gravitas.CollisionHandling; |
| | | 15 | | |
| | | 16 | | /// <summary> |
| | | 17 | | /// Contact generated between one 3D collider and one embedded 2D collider. |
| | | 18 | | /// </summary> |
| | | 19 | | public readonly struct MixedContact |
| | | 20 | | { |
| | | 21 | | /// <summary>Creates a mixed contact from world-space witness points.</summary> |
| | | 22 | | public MixedContact( |
| | | 23 | | Vector3d point3D, |
| | | 24 | | Vector3d point2D, |
| | | 25 | | Vector3d normal3DTo2D, |
| | | 26 | | Fixed64 depth, |
| | | 27 | | bool depthIsClamped = false) |
| | 47 | 28 | | : this( |
| | 47 | 29 | | ContactAnchor.FromWorldPoint(point3D), |
| | 47 | 30 | | ContactAnchor.FromWorldPoint(point2D), |
| | 47 | 31 | | normal3DTo2D, |
| | 47 | 32 | | depth, |
| | 47 | 33 | | depthIsClamped, |
| | 47 | 34 | | hasMaterialOverride: false, |
| | 47 | 35 | | default, |
| | 47 | 36 | | default) |
| | 47 | 37 | | { } |
| | | 38 | | |
| | | 39 | | /// <summary> |
| | | 40 | | /// Creates a mixed contact with authoritative rigid-frame witnesses. |
| | | 41 | | /// </summary> |
| | | 42 | | /// <exception cref="ArgumentException"> |
| | | 43 | | /// Either rigid-frame anchor is invalid. |
| | | 44 | | /// </exception> |
| | | 45 | | public MixedContact( |
| | | 46 | | ContactAnchor anchor3D, |
| | | 47 | | ContactAnchor anchor2D, |
| | | 48 | | Vector3d normal3DTo2D, |
| | | 49 | | Fixed64 depth, |
| | | 50 | | bool depthIsClamped = false) |
| | 3476 | 51 | | : this( |
| | 3476 | 52 | | anchor3D, |
| | 3476 | 53 | | anchor2D, |
| | 3476 | 54 | | normal3DTo2D, |
| | 3476 | 55 | | depth, |
| | 3476 | 56 | | depthIsClamped, |
| | 3476 | 57 | | hasMaterialOverride: false, |
| | 3476 | 58 | | default, |
| | 3476 | 59 | | default) |
| | 3474 | 60 | | { } |
| | | 61 | | |
| | | 62 | | internal MixedContact( |
| | | 63 | | Vector3d point3D, |
| | | 64 | | Vector3d point2D, |
| | | 65 | | Vector3d normal3DTo2D, |
| | | 66 | | Fixed64 depth, |
| | | 67 | | PhysicsMaterial material3D, |
| | | 68 | | PhysicsMaterial material2D, |
| | | 69 | | bool depthIsClamped = false) |
| | 1 | 70 | | : this( |
| | 1 | 71 | | ContactAnchor.FromWorldPoint(point3D), |
| | 1 | 72 | | ContactAnchor.FromWorldPoint(point2D), |
| | 1 | 73 | | normal3DTo2D, |
| | 1 | 74 | | depth, |
| | 1 | 75 | | depthIsClamped, |
| | 1 | 76 | | hasMaterialOverride: true, |
| | 1 | 77 | | material3D, |
| | 1 | 78 | | material2D) |
| | 1 | 79 | | { } |
| | | 80 | | |
| | | 81 | | internal MixedContact( |
| | | 82 | | ContactAnchor anchor3D, |
| | | 83 | | ContactAnchor anchor2D, |
| | | 84 | | Vector3d normal3DTo2D, |
| | | 85 | | Fixed64 depth, |
| | | 86 | | PhysicsMaterial material3D, |
| | | 87 | | PhysicsMaterial material2D, |
| | | 88 | | bool depthIsClamped = false) |
| | 14 | 89 | | : this( |
| | 14 | 90 | | anchor3D, |
| | 14 | 91 | | anchor2D, |
| | 14 | 92 | | normal3DTo2D, |
| | 14 | 93 | | depth, |
| | 14 | 94 | | depthIsClamped, |
| | 14 | 95 | | hasMaterialOverride: true, |
| | 14 | 96 | | material3D, |
| | 14 | 97 | | material2D) |
| | 14 | 98 | | { } |
| | | 99 | | |
| | | 100 | | private MixedContact( |
| | | 101 | | ContactAnchor anchor3D, |
| | | 102 | | ContactAnchor anchor2D, |
| | | 103 | | Vector3d normal3DTo2D, |
| | | 104 | | Fixed64 depth, |
| | | 105 | | bool depthIsClamped, |
| | | 106 | | bool hasMaterialOverride, |
| | | 107 | | PhysicsMaterial material3D, |
| | | 108 | | PhysicsMaterial material2D) |
| | | 109 | | { |
| | 3538 | 110 | | if (!anchor3D.IsValid) |
| | 1 | 111 | | throw new ArgumentException("The 3D contact anchor must be valid.", nameof(anchor3D)); |
| | 3537 | 112 | | if (!anchor2D.IsValid) |
| | 1 | 113 | | throw new ArgumentException("The 2D contact anchor must be valid.", nameof(anchor2D)); |
| | | 114 | | |
| | 3536 | 115 | | Anchor3D = anchor3D; |
| | 3536 | 116 | | Anchor2D = anchor2D; |
| | 3536 | 117 | | Normal3DTo2D = normal3DTo2D; |
| | 3536 | 118 | | Depth = depth; |
| | 3536 | 119 | | DepthIsClamped = depthIsClamped; |
| | 3536 | 120 | | HasMaterialOverride = hasMaterialOverride; |
| | 3536 | 121 | | Material3D = hasMaterialOverride ? material3D : PhysicsMaterial.Default; |
| | 3536 | 122 | | Material2D = hasMaterialOverride ? material2D : PhysicsMaterial.Default; |
| | 3536 | 123 | | HasContact = true; |
| | 3536 | 124 | | } |
| | | 125 | | |
| | | 126 | | /// <summary>Gets whether this value contains a valid mixed contact.</summary> |
| | | 127 | | public bool HasContact { get; } |
| | | 128 | | |
| | | 129 | | /// <summary>Gets the authoritative contact anchor on the 3D collider.</summary> |
| | | 130 | | public ContactAnchor Anchor3D { get; } |
| | | 131 | | |
| | | 132 | | /// <summary>Gets the authoritative contact anchor on the embedded 2D collider.</summary> |
| | | 133 | | public ContactAnchor Anchor2D { get; } |
| | | 134 | | |
| | | 135 | | /// <summary>Gets the world-space witness point on the 3D collider.</summary> |
| | 20 | 136 | | public Vector3d Point3D => GetRequiredWorldPoint(Anchor3D, nameof(Point3D)); |
| | | 137 | | |
| | | 138 | | /// <summary>Gets the world-space witness point on the embedded 2D collider.</summary> |
| | 25 | 139 | | public Vector3d Point2D => GetRequiredWorldPoint(Anchor2D, nameof(Point2D)); |
| | | 140 | | |
| | | 141 | | /// <summary>Attempts to resolve the 3D anchor to a world-space point.</summary> |
| | | 142 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 170 | 143 | | public bool TryGetPoint3D(out Vector3d point) => Anchor3D.TryGetWorldPoint(out point); |
| | | 144 | | |
| | | 145 | | /// <summary>Attempts to resolve the embedded 2D anchor to a world-space point.</summary> |
| | | 146 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 173 | 147 | | public bool TryGetPoint2D(out Vector3d point) => Anchor2D.TryGetWorldPoint(out point); |
| | | 148 | | |
| | | 149 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 150 | | internal bool TryGetPlanarOffset2DFrom( |
| | | 151 | | Vector2d origin, |
| | | 152 | | out Vector2d offset) => |
| | 1 | 153 | | TryGetPlanarOffset2DFrom( |
| | 1 | 154 | | origin, |
| | 1 | 155 | | Fixed64.Zero, |
| | 1 | 156 | | Vector2d.Zero, |
| | 1 | 157 | | out offset); |
| | | 158 | | |
| | | 159 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 160 | | internal bool TryGetPlanarOffset2DFrom( |
| | | 161 | | Vector2d origin, |
| | | 162 | | Fixed64 rotation, |
| | | 163 | | Vector2d localPoint, |
| | | 164 | | out Vector2d offset) |
| | | 165 | | { |
| | 379 | 166 | | ContactAnchor reference = |
| | 379 | 167 | | CreatePlanarReference(origin, rotation, localPoint); |
| | 379 | 168 | | if (!Anchor2D.TryGetOffsetFrom( |
| | 379 | 169 | | reference, |
| | 379 | 170 | | out Vector3d offset3D)) |
| | | 171 | | { |
| | 2 | 172 | | offset = default; |
| | 2 | 173 | | return false; |
| | | 174 | | } |
| | | 175 | | |
| | 377 | 176 | | offset = new Vector2d(offset3D.X, offset3D.Z); |
| | 377 | 177 | | return true; |
| | | 178 | | } |
| | | 179 | | |
| | | 180 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 181 | | internal ExactLever3D GetPlanarXZLeverFrom( |
| | | 182 | | Vector2d origin, |
| | | 183 | | Fixed64 rotation, |
| | | 184 | | Vector2d localPoint) => |
| | 149 | 185 | | Anchor2D.GetLeverFrom( |
| | 149 | 186 | | CreatePlanarReference(origin, rotation, localPoint)); |
| | | 187 | | |
| | | 188 | | /// <summary> |
| | | 189 | | /// Contact normal pointing from the 3D collider toward the embedded 2D collider volume. |
| | | 190 | | /// </summary> |
| | | 191 | | public Vector3d Normal3DTo2D { get; } |
| | | 192 | | |
| | | 193 | | /// <summary>Gets the penetration depth along <see cref="Normal3DTo2D"/>.</summary> |
| | | 194 | | public Fixed64 Depth { get; } |
| | | 195 | | |
| | | 196 | | /// <summary> |
| | | 197 | | /// Gets whether the exact penetration depth exceeded the scalar domain and |
| | | 198 | | /// <see cref="Depth"/> therefore contains <see cref="Fixed64.MaxValue"/>. |
| | | 199 | | /// </summary> |
| | | 200 | | public bool DepthIsClamped { get; } |
| | | 201 | | |
| | | 202 | | /// <summary>Gets whether the contact carries per-feature material values.</summary> |
| | | 203 | | public bool HasMaterialOverride { get; } |
| | | 204 | | |
| | | 205 | | /// <summary>Gets the material sampled from the 3D contact feature.</summary> |
| | | 206 | | public PhysicsMaterial Material3D { get; } |
| | | 207 | | |
| | | 208 | | /// <summary>Gets the material sampled from the embedded 2D contact feature.</summary> |
| | | 209 | | public PhysicsMaterial Material2D { get; } |
| | | 210 | | |
| | | 211 | | internal MixedContact WithMaterialOverride(PhysicsMaterial material3D, PhysicsMaterial material2D) => |
| | 14 | 212 | | new( |
| | 14 | 213 | | Anchor3D, |
| | 14 | 214 | | Anchor2D, |
| | 14 | 215 | | Normal3DTo2D, |
| | 14 | 216 | | Depth, |
| | 14 | 217 | | material3D, |
| | 14 | 218 | | material2D, |
| | 14 | 219 | | DepthIsClamped); |
| | | 220 | | |
| | | 221 | | internal MixedContact WithFallbackMaterials(PhysicsMaterial material3D, PhysicsMaterial material2D) => |
| | 13 | 222 | | HasMaterialOverride ? this : WithMaterialOverride(material3D, material2D); |
| | | 223 | | |
| | | 224 | | private ContactAnchor CreatePlanarReference( |
| | | 225 | | Vector2d origin, |
| | | 226 | | Fixed64 rotation, |
| | | 227 | | Vector2d localPoint) => |
| | 528 | 228 | | new( |
| | 528 | 229 | | new Vector3d( |
| | 528 | 230 | | origin.X, |
| | 528 | 231 | | Anchor2D.Origin.Y, |
| | 528 | 232 | | origin.Y), |
| | 528 | 233 | | FixedQuaternion.FromAxisAngle(Vector3d.Up, -rotation), |
| | 528 | 234 | | new Vector3d( |
| | 528 | 235 | | localPoint.X, |
| | 528 | 236 | | Fixed64.Zero, |
| | 528 | 237 | | localPoint.Y)); |
| | | 238 | | |
| | | 239 | | private static Vector3d GetRequiredWorldPoint(ContactAnchor anchor, string propertyName) |
| | | 240 | | { |
| | 45 | 241 | | if (anchor.TryGetWorldPoint(out Vector3d point)) |
| | 43 | 242 | | return point; |
| | | 243 | | |
| | 2 | 244 | | throw new InvalidOperationException( |
| | 2 | 245 | | $"{propertyName} is outside the representable coordinate range. Use the corresponding TryGet method."); |
| | | 246 | | } |
| | | 247 | | } |