| | | 1 | | //======================================================================= |
| | | 2 | | // ContactAnchor2D.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 System.Runtime.CompilerServices; |
| | | 11 | | |
| | | 12 | | namespace Gravitas.CollisionHandling; |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// Represents a planar contact point as a world-space origin plus a rotated |
| | | 16 | | /// local offset. |
| | | 17 | | /// </summary> |
| | | 18 | | public readonly struct ContactAnchor2D |
| | | 19 | | { |
| | | 20 | | private readonly FixedPointAnchor2d _point; |
| | | 21 | | |
| | | 22 | | /// <summary> |
| | | 23 | | /// Creates a planar contact anchor in an identity-rotation rigid frame. |
| | | 24 | | /// </summary> |
| | | 25 | | public ContactAnchor2D(Vector2d origin, Vector2d offset) |
| | 27424 | 26 | | : this(origin, Fixed64.Zero, offset) |
| | | 27 | | { |
| | 27424 | 28 | | } |
| | | 29 | | |
| | | 30 | | /// <summary> |
| | | 31 | | /// Creates a planar contact anchor from a canonical local point. |
| | | 32 | | /// </summary> |
| | | 33 | | public ContactAnchor2D( |
| | | 34 | | Vector2d origin, |
| | | 35 | | Fixed64 rotation, |
| | | 36 | | Vector2d localPoint) |
| | 32651 | 37 | | : this(origin, rotation, localPoint, Vector2d.Zero) |
| | | 38 | | { |
| | 32651 | 39 | | } |
| | | 40 | | |
| | | 41 | | /// <summary> |
| | | 42 | | /// Creates a planar anchor whose two local terms remain separate until |
| | | 43 | | /// the exact rotated point is evaluated. |
| | | 44 | | /// </summary> |
| | | 45 | | public ContactAnchor2D( |
| | | 46 | | Vector2d origin, |
| | | 47 | | Fixed64 rotation, |
| | | 48 | | Vector2d localPoint, |
| | | 49 | | Vector2d localDisplacement) |
| | | 50 | | { |
| | 32927 | 51 | | _point = new FixedPointAnchor2d( |
| | 32927 | 52 | | origin, |
| | 32927 | 53 | | rotation, |
| | 32927 | 54 | | localPoint, |
| | 32927 | 55 | | localDisplacement); |
| | 32927 | 56 | | } |
| | | 57 | | |
| | | 58 | | /// <summary> |
| | | 59 | | /// Creates a physics-domain wrapper for an exact planar point anchor. |
| | | 60 | | /// </summary> |
| | | 61 | | public ContactAnchor2D(FixedPointAnchor2d point) |
| | | 62 | | { |
| | 27056 | 63 | | _point = point; |
| | 27056 | 64 | | } |
| | | 65 | | |
| | | 66 | | /// <summary> |
| | | 67 | | /// Gets the representable world-space origin. |
| | | 68 | | /// </summary> |
| | 2795 | 69 | | public Vector2d Origin => _point.Origin; |
| | | 70 | | |
| | | 71 | | /// <summary> |
| | | 72 | | /// Gets the point in the contact frame's local coordinates. |
| | | 73 | | /// </summary> |
| | 2726 | 74 | | public Vector2d LocalPoint => _point.LocalPoint; |
| | | 75 | | |
| | | 76 | | /// <summary> |
| | | 77 | | /// Gets the secondary local displacement retained separately from |
| | | 78 | | /// <see cref="LocalPoint"/>. |
| | | 79 | | /// </summary> |
| | 2724 | 80 | | public Vector2d LocalDisplacement => _point.LocalDisplacement; |
| | | 81 | | |
| | | 82 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 83 | | internal int CompareLocalFeature(in ContactAnchor2D other) => |
| | 773 | 84 | | _point.CompareLocalFeature(other._point); |
| | | 85 | | |
| | | 86 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 87 | | internal ulong GetLocalFeatureHash64() => |
| | 1620 | 88 | | _point.GetLocalFeatureHash64(); |
| | | 89 | | |
| | | 90 | | /// <summary> |
| | | 91 | | /// Gets the rotation applied to <see cref="LocalPoint"/>. |
| | | 92 | | /// </summary> |
| | 1503 | 93 | | public Fixed64 Rotation => _point.Rotation; |
| | | 94 | | |
| | | 95 | | /// <summary> |
| | | 96 | | /// Gets the rotated composite offset from the rigid-frame origin. |
| | | 97 | | /// </summary> |
| | | 98 | | /// <exception cref="System.InvalidOperationException"> |
| | | 99 | | /// The rotated offset is outside the representable scalar domain. Use |
| | | 100 | | /// <see cref="TryGetOffset(out Vector2d)"/> when handling domain edges. |
| | | 101 | | /// </exception> |
| | | 102 | | public Vector2d Offset |
| | | 103 | | { |
| | | 104 | | get |
| | | 105 | | { |
| | 44 | 106 | | bool succeeded = TryGetOffset(out Vector2d offset); |
| | 44 | 107 | | SwiftThrowHelper.ThrowIfTrue( |
| | 44 | 108 | | !succeeded, |
| | 44 | 109 | | nameof(Offset), |
| | 44 | 110 | | "The planar contact offset is outside the representable coordinate domain."); |
| | 43 | 111 | | return offset; |
| | | 112 | | } |
| | | 113 | | } |
| | | 114 | | |
| | | 115 | | /// <summary> |
| | | 116 | | /// Creates an anchor for an already materialized world point. |
| | | 117 | | /// </summary> |
| | | 118 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 119 | | public static ContactAnchor2D FromWorldPoint(Vector2d point) => |
| | 2443 | 120 | | new(Vector2d.Zero, point); |
| | | 121 | | |
| | | 122 | | /// <summary> |
| | | 123 | | /// Attempts to materialize the rotated composite rigid-frame offset. |
| | | 124 | | /// </summary> |
| | | 125 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 126 | | public bool TryGetOffset(out Vector2d offset) => |
| | 47 | 127 | | _point.TryGetOffsetFrom( |
| | 47 | 128 | | new FixedPointAnchor2d( |
| | 47 | 129 | | Origin, |
| | 47 | 130 | | Fixed64.Zero, |
| | 47 | 131 | | Vector2d.Zero), |
| | 47 | 132 | | out offset); |
| | | 133 | | |
| | | 134 | | /// <summary> |
| | | 135 | | /// Attempts to materialize the absolute world point without saturation. |
| | | 136 | | /// </summary> |
| | | 137 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 138 | | public bool TryGetWorldPoint(out Vector2d point) => |
| | 373 | 139 | | _point.TryGetPoint(out point); |
| | | 140 | | |
| | | 141 | | /// <summary> |
| | | 142 | | /// Attempts to rebase the conceptual point onto another origin with one |
| | | 143 | | /// final exact component-wise add/subtract operation. |
| | | 144 | | /// </summary> |
| | | 145 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 146 | | public bool TryGetOffsetFrom(Vector2d origin, out Vector2d offset) => |
| | 1923 | 147 | | _point.TryGetOffsetFrom( |
| | 1923 | 148 | | new FixedPointAnchor2d( |
| | 1923 | 149 | | origin, |
| | 1923 | 150 | | Fixed64.Zero, |
| | 1923 | 151 | | Vector2d.Zero), |
| | 1923 | 152 | | out offset); |
| | | 153 | | |
| | | 154 | | /// <summary> |
| | | 155 | | /// Attempts to obtain this point's exact world-space offset from another |
| | | 156 | | /// planar contact anchor. |
| | | 157 | | /// </summary> |
| | | 158 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 159 | | public bool TryGetOffsetFrom( |
| | | 160 | | ContactAnchor2D other, |
| | | 161 | | out Vector2d offset) => |
| | 5130 | 162 | | _point.TryGetOffsetFrom(other._point, out offset); |
| | | 163 | | |
| | | 164 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 165 | | internal ExactLever3D GetXZLeverFrom(in ContactAnchor2D other) => |
| | 192 | 166 | | ExactLever3D.CreateXZ(_point, other._point); |
| | | 167 | | |
| | | 168 | | /// <summary> |
| | | 169 | | /// Attempts to express the same conceptual point relative to another |
| | | 170 | | /// representable origin. |
| | | 171 | | /// </summary> |
| | | 172 | | public bool TryRebase(Vector2d origin, out ContactAnchor2D anchor) |
| | | 173 | | { |
| | 2 | 174 | | if (!_point.TryReframe( |
| | 2 | 175 | | origin, |
| | 2 | 176 | | Fixed64.Zero, |
| | 2 | 177 | | out FixedPointAnchor2d point)) |
| | | 178 | | { |
| | 1 | 179 | | anchor = default; |
| | 1 | 180 | | return false; |
| | | 181 | | } |
| | | 182 | | |
| | 1 | 183 | | anchor = new ContactAnchor2D(point); |
| | 1 | 184 | | return true; |
| | | 185 | | } |
| | | 186 | | |
| | | 187 | | /// <summary> |
| | | 188 | | /// Attempts to express the same conceptual point in another rotated frame. |
| | | 189 | | /// </summary> |
| | | 190 | | public bool TryRebase( |
| | | 191 | | Vector2d origin, |
| | | 192 | | Fixed64 rotation, |
| | | 193 | | out ContactAnchor2D anchor) |
| | | 194 | | { |
| | 2 | 195 | | if (!_point.TryReframe( |
| | 2 | 196 | | origin, |
| | 2 | 197 | | rotation, |
| | 2 | 198 | | out FixedPointAnchor2d point)) |
| | | 199 | | { |
| | 1 | 200 | | anchor = default; |
| | 1 | 201 | | return false; |
| | | 202 | | } |
| | | 203 | | |
| | 1 | 204 | | anchor = new ContactAnchor2D(point); |
| | 1 | 205 | | return true; |
| | | 206 | | } |
| | | 207 | | } |