| | | 1 | | //======================================================================= |
| | | 2 | | // SolverContact2D.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 Gravitas.Materials; |
| | | 10 | | |
| | | 11 | | namespace Gravitas.CollisionHandling; |
| | | 12 | | |
| | | 13 | | internal readonly struct ContactLever2D |
| | | 14 | | { |
| | | 15 | | private ContactLever2D(Vector2d vector, bool isExact) |
| | | 16 | | { |
| | | 17 | | Vector = vector; |
| | | 18 | | IsExact = isExact; |
| | | 19 | | } |
| | | 20 | | |
| | | 21 | | internal bool IsExact { get; } |
| | | 22 | | |
| | | 23 | | internal Vector2d Vector { get; } |
| | | 24 | | |
| | | 25 | | internal static ContactLever2D Zero => |
| | | 26 | | new(Vector2d.Zero, isExact: false); |
| | | 27 | | |
| | | 28 | | internal static ContactLever2D Create( |
| | | 29 | | in ContactAnchor2D point, |
| | | 30 | | in ContactAnchor2D origin) |
| | | 31 | | { |
| | | 32 | | if (point.TryGetOffsetFrom(origin, out Vector2d vector)) |
| | | 33 | | return new ContactLever2D(vector, isExact: false); |
| | | 34 | | |
| | | 35 | | return new ContactLever2D(default, isExact: true); |
| | | 36 | | } |
| | | 37 | | } |
| | | 38 | | |
| | | 39 | | /// <summary> |
| | | 40 | | /// Solver-ready scalar pure 2D contact data. |
| | | 41 | | /// </summary> |
| | | 42 | | internal readonly struct SolverContact2D |
| | | 43 | | { |
| | | 44 | | public SolverContact2D( |
| | | 45 | | int manifoldIndex, |
| | | 46 | | ulong contactId, |
| | | 47 | | ResponseBody2D bodyA, |
| | | 48 | | ResponseBody2D bodyB, |
| | | 49 | | ContactLever2D relativeA, |
| | | 50 | | ContactLever2D relativeB, |
| | | 51 | | Fixed64 depth, |
| | | 52 | | Vector2d normal, |
| | | 53 | | PhysicsMaterial materialA, |
| | | 54 | | PhysicsMaterial materialB, |
| | | 55 | | Fixed64 cachedNormalImpulse, |
| | | 56 | | Fixed64 cachedTangentImpulse) |
| | | 57 | | { |
| | 1011 | 58 | | ManifoldIndex = manifoldIndex; |
| | 1011 | 59 | | ContactId = contactId; |
| | 1011 | 60 | | A = bodyA; |
| | 1011 | 61 | | B = bodyB; |
| | 1011 | 62 | | RelativeA = relativeA; |
| | 1011 | 63 | | RelativeB = relativeB; |
| | 1011 | 64 | | Depth = depth; |
| | 1011 | 65 | | Normal = normal; |
| | 1011 | 66 | | MaterialA = materialA; |
| | 1011 | 67 | | MaterialB = materialB; |
| | 1011 | 68 | | PhysicsMaterial.CombineFriction(materialA, materialB, out Fixed64 staticFriction, out Fixed64 dynamicFriction); |
| | 1011 | 69 | | StaticFriction = staticFriction; |
| | 1011 | 70 | | DynamicFriction = dynamicFriction; |
| | 1011 | 71 | | Restitution = PhysicsMaterial.CombineRestitution(materialA, materialB); |
| | 1011 | 72 | | Tangent = normal.RightHandNormal; |
| | 1011 | 73 | | CachedNormalImpulse = cachedNormalImpulse; |
| | 1011 | 74 | | CachedTangentImpulse = cachedTangentImpulse; |
| | 1011 | 75 | | } |
| | | 76 | | |
| | | 77 | | public int ManifoldIndex { get; } |
| | | 78 | | |
| | | 79 | | public ulong ContactId { get; } |
| | | 80 | | |
| | | 81 | | public ResponseBody2D A { get; } |
| | | 82 | | |
| | | 83 | | public ResponseBody2D B { get; } |
| | | 84 | | |
| | | 85 | | public ContactLever2D RelativeA { get; } |
| | | 86 | | |
| | | 87 | | public ContactLever2D RelativeB { get; } |
| | | 88 | | |
| | | 89 | | public Fixed64 Depth { get; } |
| | | 90 | | |
| | | 91 | | public Vector2d Normal { get; } |
| | | 92 | | |
| | | 93 | | public PhysicsMaterial MaterialA { get; } |
| | | 94 | | |
| | | 95 | | public PhysicsMaterial MaterialB { get; } |
| | | 96 | | |
| | | 97 | | public Fixed64 StaticFriction { get; } |
| | | 98 | | |
| | | 99 | | public Fixed64 DynamicFriction { get; } |
| | | 100 | | |
| | | 101 | | public Fixed64 Restitution { get; } |
| | | 102 | | |
| | | 103 | | public Vector2d Tangent { get; } |
| | | 104 | | |
| | | 105 | | public Fixed64 CachedNormalImpulse { get; } |
| | | 106 | | |
| | | 107 | | public Fixed64 CachedTangentImpulse { get; } |
| | | 108 | | |
| | | 109 | | } |