| | | 1 | | //======================================================================= |
| | | 2 | | // PhysicsMaterial.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 MemoryPack; |
| | | 10 | | using System; |
| | | 11 | | using System.Runtime.CompilerServices; |
| | | 12 | | using System.Text.Json.Serialization; |
| | | 13 | | |
| | | 14 | | namespace Gravitas.Materials; |
| | | 15 | | |
| | | 16 | | /// <summary> |
| | | 17 | | /// Deterministic surface material coefficients used by contact response. |
| | | 18 | | /// </summary> |
| | | 19 | | /// <remarks> |
| | | 20 | | /// Static and dynamic friction are non-negative Coulomb coefficients. |
| | | 21 | | /// Restitution is the bounce coefficient in the inclusive range [0, 1]. |
| | | 22 | | /// Dynamic friction cannot exceed static friction. |
| | | 23 | | /// </remarks> |
| | | 24 | | [Serializable] |
| | | 25 | | [MemoryPackable] |
| | | 26 | | public readonly partial struct PhysicsMaterial : IEquatable<PhysicsMaterial> |
| | | 27 | | { |
| | | 28 | | /// <summary> |
| | | 29 | | /// The release default surface: unit friction, half restitution, geometric |
| | | 30 | | /// friction combine, and minimum restitution combine. |
| | | 31 | | /// </summary> |
| | | 32 | | [JsonIgnore] |
| | | 33 | | [MemoryPackIgnore] |
| | | 34 | | public static PhysicsMaterial Default => |
| | 82789 | 35 | | new(Fixed64.One, Fixed64.One, Fixed64.Half); |
| | | 36 | | |
| | | 37 | | /// <summary> |
| | | 38 | | /// A surface with no contact friction and no restitution. |
| | | 39 | | /// </summary> |
| | | 40 | | [JsonIgnore] |
| | | 41 | | [MemoryPackIgnore] |
| | | 42 | | public static PhysicsMaterial Frictionless => |
| | 100 | 43 | | new(Fixed64.Zero, Fixed64.Zero, Fixed64.Zero); |
| | | 44 | | |
| | | 45 | | /// <summary> |
| | | 46 | | /// A high-restitution surface that keeps the default friction values. |
| | | 47 | | /// </summary> |
| | | 48 | | [JsonIgnore] |
| | | 49 | | [MemoryPackIgnore] |
| | | 50 | | public static PhysicsMaterial Bouncy => |
| | 10 | 51 | | new(Fixed64.One, Fixed64.One, Fixed64.One); |
| | | 52 | | |
| | | 53 | | /// <summary> |
| | | 54 | | /// Creates a deterministic surface material. |
| | | 55 | | /// </summary> |
| | | 56 | | [JsonConstructor] |
| | | 57 | | public PhysicsMaterial( |
| | | 58 | | Fixed64 staticFriction, |
| | | 59 | | Fixed64 dynamicFriction, |
| | | 60 | | Fixed64 restitution, |
| | | 61 | | PhysicsMaterialCombine frictionCombine = PhysicsMaterialCombine.GeometricMean, |
| | | 62 | | PhysicsMaterialCombine restitutionCombine = PhysicsMaterialCombine.Minimum) |
| | | 63 | | { |
| | 83845 | 64 | | ValidateFriction(staticFriction, nameof(staticFriction)); |
| | 83844 | 65 | | ValidateFriction(dynamicFriction, nameof(dynamicFriction)); |
| | 83843 | 66 | | if (dynamicFriction > staticFriction) |
| | 1 | 67 | | throw new ArgumentOutOfRangeException( |
| | 1 | 68 | | nameof(dynamicFriction), |
| | 1 | 69 | | dynamicFriction, |
| | 1 | 70 | | "Dynamic friction cannot exceed static friction."); |
| | 83842 | 71 | | if (restitution < Fixed64.Zero || restitution > Fixed64.One) |
| | 2 | 72 | | throw new ArgumentOutOfRangeException( |
| | 2 | 73 | | nameof(restitution), |
| | 2 | 74 | | restitution, |
| | 2 | 75 | | "Restitution must be between zero and one inclusive."); |
| | 83840 | 76 | | ValidateCombine(frictionCombine, nameof(frictionCombine)); |
| | 83838 | 77 | | ValidateCombine(restitutionCombine, nameof(restitutionCombine)); |
| | | 78 | | |
| | 83837 | 79 | | StaticFriction = staticFriction; |
| | 83837 | 80 | | DynamicFriction = dynamicFriction; |
| | 83837 | 81 | | Restitution = restitution; |
| | 83837 | 82 | | FrictionCombine = frictionCombine; |
| | 83837 | 83 | | RestitutionCombine = restitutionCombine; |
| | 83837 | 84 | | } |
| | | 85 | | |
| | | 86 | | /// <summary> |
| | | 87 | | /// Gets the friction coefficient used while tangential contact motion can be |
| | | 88 | | /// fully resisted by Coulomb static friction. |
| | | 89 | | /// </summary> |
| | | 90 | | public Fixed64 StaticFriction { get; } |
| | | 91 | | |
| | | 92 | | /// <summary> |
| | | 93 | | /// Gets the friction coefficient used after tangential motion exceeds the |
| | | 94 | | /// static friction limit. |
| | | 95 | | /// </summary> |
| | | 96 | | public Fixed64 DynamicFriction { get; } |
| | | 97 | | |
| | | 98 | | /// <summary> |
| | | 99 | | /// Gets the restitution coefficient. Zero removes closing velocity without |
| | | 100 | | /// bounce; one is fully elastic before the context restitution threshold is |
| | | 101 | | /// applied. |
| | | 102 | | /// </summary> |
| | | 103 | | public Fixed64 Restitution { get; } |
| | | 104 | | |
| | | 105 | | /// <summary> |
| | | 106 | | /// Gets this surface's preferred combine policy for static and dynamic |
| | | 107 | | /// friction coefficients. |
| | | 108 | | /// </summary> |
| | | 109 | | public PhysicsMaterialCombine FrictionCombine { get; } |
| | | 110 | | |
| | | 111 | | /// <summary> |
| | | 112 | | /// Gets this surface's preferred combine policy for restitution. |
| | | 113 | | /// </summary> |
| | | 114 | | public PhysicsMaterialCombine RestitutionCombine { get; } |
| | | 115 | | |
| | | 116 | | /// <summary> |
| | | 117 | | /// Resolves effective static and dynamic friction coefficients for a pair of |
| | | 118 | | /// contacting surfaces. |
| | | 119 | | /// </summary> |
| | | 120 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 121 | | public static void CombineFriction( |
| | | 122 | | PhysicsMaterial left, |
| | | 123 | | PhysicsMaterial right, |
| | | 124 | | out Fixed64 staticFriction, |
| | | 125 | | out Fixed64 dynamicFriction) |
| | | 126 | | { |
| | 10208 | 127 | | PhysicsMaterialCombine policy = ResolveDominantPolicy(left.FrictionCombine, right.FrictionCombine); |
| | 10208 | 128 | | staticFriction = CombineScalar(left.StaticFriction, right.StaticFriction, policy); |
| | 10208 | 129 | | dynamicFriction = CombineScalar(left.DynamicFriction, right.DynamicFriction, policy); |
| | 10208 | 130 | | } |
| | | 131 | | |
| | | 132 | | /// <summary> |
| | | 133 | | /// Resolves the effective restitution coefficient for a pair of contacting |
| | | 134 | | /// surfaces. |
| | | 135 | | /// </summary> |
| | | 136 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 137 | | public static Fixed64 CombineRestitution(PhysicsMaterial left, PhysicsMaterial right) |
| | | 138 | | { |
| | 10851 | 139 | | PhysicsMaterialCombine policy = ResolveDominantPolicy(left.RestitutionCombine, right.RestitutionCombine); |
| | 10851 | 140 | | return FixedMath.Clamp( |
| | 10851 | 141 | | CombineScalar(left.Restitution, right.Restitution, policy), |
| | 10851 | 142 | | Fixed64.Zero, |
| | 10851 | 143 | | Fixed64.One); |
| | | 144 | | } |
| | | 145 | | |
| | | 146 | | /// <summary> |
| | | 147 | | /// Combines two scalar coefficients with one explicit deterministic policy. |
| | | 148 | | /// </summary> |
| | | 149 | | public static Fixed64 CombineScalar( |
| | | 150 | | Fixed64 left, |
| | | 151 | | Fixed64 right, |
| | | 152 | | PhysicsMaterialCombine policy) |
| | | 153 | | { |
| | 31285 | 154 | | return policy switch |
| | 31285 | 155 | | { |
| | 10847 | 156 | | PhysicsMaterialCombine.Minimum => FixedMath.Min(left, right), |
| | 8 | 157 | | PhysicsMaterialCombine.Maximum => FixedMath.Max(left, right), |
| | 6 | 158 | | PhysicsMaterialCombine.Average => FixedMath.Midpoint(left, right), |
| | 1 | 159 | | PhysicsMaterialCombine.Multiply => left * right, |
| | 20422 | 160 | | PhysicsMaterialCombine.GeometricMean => GeometricMean(left, right), |
| | 1 | 161 | | _ => throw new ArgumentOutOfRangeException( |
| | 1 | 162 | | nameof(policy), |
| | 1 | 163 | | policy, |
| | 1 | 164 | | "Unsupported physics material combine policy.") |
| | 31285 | 165 | | }; |
| | | 166 | | } |
| | | 167 | | |
| | | 168 | | /// <summary> |
| | | 169 | | /// Resolves the contact policy when two surfaces specify different combine |
| | | 170 | | /// policies. The order is deterministic and independent of collider order. |
| | | 171 | | /// </summary> |
| | | 172 | | public static PhysicsMaterialCombine ResolveDominantPolicy( |
| | | 173 | | PhysicsMaterialCombine left, |
| | | 174 | | PhysicsMaterialCombine right) |
| | | 175 | | { |
| | 21071 | 176 | | int leftPriority = GetPolicyPriority(left, nameof(left)); |
| | 21070 | 177 | | int rightPriority = GetPolicyPriority(right, nameof(right)); |
| | 21069 | 178 | | return leftPriority >= rightPriority ? left : right; |
| | | 179 | | } |
| | | 180 | | |
| | | 181 | | /// <inheritdoc/> |
| | | 182 | | public bool Equals(PhysicsMaterial other) => |
| | 2373 | 183 | | StaticFriction == other.StaticFriction |
| | 2373 | 184 | | && DynamicFriction == other.DynamicFriction |
| | 2373 | 185 | | && Restitution == other.Restitution |
| | 2373 | 186 | | && FrictionCombine == other.FrictionCombine |
| | 2373 | 187 | | && RestitutionCombine == other.RestitutionCombine; |
| | | 188 | | |
| | | 189 | | /// <inheritdoc/> |
| | | 190 | | public override bool Equals(object? obj) => |
| | 72 | 191 | | obj is PhysicsMaterial other && Equals(other); |
| | | 192 | | |
| | | 193 | | /// <inheritdoc/> |
| | | 194 | | public override int GetHashCode() |
| | | 195 | | { |
| | | 196 | | unchecked |
| | | 197 | | { |
| | 6 | 198 | | int hash = StaticFriction.GetHashCode(); |
| | 6 | 199 | | hash = (hash * 397) ^ DynamicFriction.GetHashCode(); |
| | 6 | 200 | | hash = (hash * 397) ^ Restitution.GetHashCode(); |
| | 6 | 201 | | hash = (hash * 397) ^ FrictionCombine.GetHashCode(); |
| | 6 | 202 | | hash = (hash * 397) ^ RestitutionCombine.GetHashCode(); |
| | 6 | 203 | | return hash; |
| | | 204 | | } |
| | | 205 | | } |
| | | 206 | | |
| | | 207 | | /// <summary>Determines whether two materials have equal coefficients and combine policies.</summary> |
| | | 208 | | public static bool operator ==(PhysicsMaterial left, PhysicsMaterial right) => |
| | 2180 | 209 | | left.Equals(right); |
| | | 210 | | |
| | | 211 | | /// <summary>Determines whether two materials differ in any coefficient or combine policy.</summary> |
| | | 212 | | public static bool operator !=(PhysicsMaterial left, PhysicsMaterial right) => |
| | 17 | 213 | | !left.Equals(right); |
| | | 214 | | |
| | | 215 | | private static void ValidateFriction(Fixed64 value, string paramName) |
| | | 216 | | { |
| | 167689 | 217 | | if (value < Fixed64.Zero) |
| | 2 | 218 | | throw new ArgumentOutOfRangeException(paramName, value, "Friction cannot be negative."); |
| | 167687 | 219 | | } |
| | | 220 | | |
| | | 221 | | private static void ValidateCombine(PhysicsMaterialCombine value, string paramName) |
| | | 222 | | { |
| | 167678 | 223 | | if (value < PhysicsMaterialCombine.Minimum || value > PhysicsMaterialCombine.GeometricMean) |
| | 3 | 224 | | throw new ArgumentOutOfRangeException( |
| | 3 | 225 | | paramName, |
| | 3 | 226 | | value, |
| | 3 | 227 | | "Unsupported physics material combine policy."); |
| | 167675 | 228 | | } |
| | | 229 | | |
| | | 230 | | private static Fixed64 GeometricMean(Fixed64 left, Fixed64 right) |
| | | 231 | | { |
| | 20422 | 232 | | if (left <= Fixed64.Zero || right <= Fixed64.Zero) |
| | 112 | 233 | | return Fixed64.Zero; |
| | 20310 | 234 | | return left == right |
| | 20310 | 235 | | ? left |
| | 20310 | 236 | | : FixedMath.Sqrt(left) * FixedMath.Sqrt(right); |
| | | 237 | | } |
| | | 238 | | |
| | | 239 | | private static int GetPolicyPriority(PhysicsMaterialCombine policy, string paramName) => |
| | 42141 | 240 | | policy switch |
| | 42141 | 241 | | { |
| | 5 | 242 | | PhysicsMaterialCombine.Average => 0, |
| | 21702 | 243 | | PhysicsMaterialCombine.Minimum => 1, |
| | 20418 | 244 | | PhysicsMaterialCombine.GeometricMean => 2, |
| | 4 | 245 | | PhysicsMaterialCombine.Multiply => 3, |
| | 10 | 246 | | PhysicsMaterialCombine.Maximum => 4, |
| | 2 | 247 | | _ => throw new ArgumentOutOfRangeException( |
| | 2 | 248 | | paramName, |
| | 2 | 249 | | policy, |
| | 2 | 250 | | "Unsupported physics material combine policy.") |
| | 42141 | 251 | | }; |
| | | 252 | | } |