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