| | | 1 | | //======================================================================= |
| | | 2 | | // PhysicsEnvironment.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 | | |
| | | 10 | | namespace Gravitas; |
| | | 11 | | |
| | | 12 | | /// <summary> |
| | | 13 | | /// Stores world-local physical environment values used by deterministic simulation. |
| | | 14 | | /// </summary> |
| | | 15 | | public sealed partial class PhysicsEnvironment |
| | | 16 | | { |
| | | 17 | | /// <summary> |
| | | 18 | | /// Standard gravitational acceleration in world units per second squared. |
| | | 19 | | /// </summary> |
| | 1 | 20 | | public static readonly Fixed64 DefaultGravity = (Fixed64)9.8f; |
| | | 21 | | |
| | | 22 | | /// <summary> |
| | | 23 | | /// Standard air density used by drag calculations. |
| | | 24 | | /// </summary> |
| | 1 | 25 | | public static readonly Fixed64 DefaultAirDensity = (Fixed64)1.225f; |
| | | 26 | | |
| | | 27 | | /// <summary> |
| | | 28 | | /// Minimum speed treated as meaningful motion by the standard environment. |
| | | 29 | | /// </summary> |
| | 1 | 30 | | public static readonly Fixed64 DefaultMinSpeed = (Fixed64)0.00001f; |
| | | 31 | | |
| | | 32 | | /// <summary> |
| | | 33 | | /// Maximum linear or angular speed used by the standard environment. |
| | | 34 | | /// </summary> |
| | 1 | 35 | | public static readonly Fixed64 DefaultMaxSpeed = (Fixed64)7f; |
| | | 36 | | |
| | | 37 | | /// <summary> |
| | | 38 | | /// Maximum downward fall speed used by the standard environment. |
| | | 39 | | /// </summary> |
| | 1 | 40 | | public static readonly Fixed64 DefaultMaxFallSpeed = DefaultGravity; |
| | | 41 | | |
| | | 42 | | /// <summary> |
| | | 43 | | /// Speed threshold used when transitioning friction behavior in the standard environment. |
| | | 44 | | /// </summary> |
| | 1 | 45 | | public static readonly Fixed64 DefaultFrictionTransitionSpeed = (Fixed64)0.2f; |
| | | 46 | | |
| | | 47 | | /// <summary> |
| | | 48 | | /// Motion deceleration multiplier used by the standard environment. |
| | | 49 | | /// </summary> |
| | 1 | 50 | | public static readonly Fixed64 DefaultDecelerationMultiplier = (Fixed64)10f; |
| | | 51 | | |
| | | 52 | | /// <summary> |
| | | 53 | | /// Angular velocity damping factor used by the standard environment. |
| | | 54 | | /// </summary> |
| | 1 | 55 | | public static readonly Fixed64 DefaultDampingFactor = (Fixed64)0.95f; |
| | | 56 | | |
| | | 57 | | /// <summary> |
| | | 58 | | /// Frame-rate divisor used to derive the default maximum distance-based |
| | | 59 | | /// collision-culling score. |
| | | 60 | | /// </summary> |
| | 1 | 61 | | public static readonly int DefaultCullDistanceFrameDivisor = 3; |
| | | 62 | | |
| | | 63 | | /// <summary> |
| | | 64 | | /// Distance threshold whose square becomes the default fast-collision |
| | | 65 | | /// preservation threshold. |
| | | 66 | | /// </summary> |
| | 1 | 67 | | public static readonly int DefaultCullFastDistance = 4; |
| | | 68 | | |
| | | 69 | | /// <summary> |
| | | 70 | | /// Squared distance below which the standard environment preserves fast |
| | | 71 | | /// collision checks. |
| | | 72 | | /// </summary> |
| | 1 | 73 | | public static readonly Fixed64 DefaultCullFastDistanceMax = |
| | 1 | 74 | | Fixed64.One * DefaultCullFastDistance * (Fixed64.One * DefaultCullFastDistance); |
| | | 75 | | |
| | | 76 | | /// <summary> |
| | | 77 | | /// Velocity step used by standard collision culling. |
| | | 78 | | /// </summary> |
| | 1 | 79 | | public static readonly int DefaultCullVelocityStep = 2; |
| | | 80 | | |
| | | 81 | | /// <summary> |
| | | 82 | | /// Maximum velocity-based collision-culling score used by the standard environment. |
| | | 83 | | /// </summary> |
| | 1 | 84 | | public static readonly int DefaultCullVelocityMax = 4; |
| | | 85 | | |
| | | 86 | | /// <summary> |
| | | 87 | | /// Frame-rate multiplier used to derive the default collision-culling time step. |
| | | 88 | | /// </summary> |
| | 1 | 89 | | public static readonly int DefaultCullTimeStepFrameMultiplier = 3; |
| | | 90 | | |
| | | 91 | | /// <summary> |
| | | 92 | | /// Frame-rate divisor used to derive the default maximum time-based |
| | | 93 | | /// collision-culling score. |
| | | 94 | | /// </summary> |
| | 1 | 95 | | public static readonly int DefaultCullTimeMaxFrameDivisor = 5; |
| | | 96 | | |
| | | 97 | | /// <summary> |
| | | 98 | | /// One pound is equal to this many Newtons. |
| | | 99 | | /// </summary> |
| | 1 | 100 | | public static readonly Fixed64 PoundToNewton = (Fixed64)4.44822162f; |
| | | 101 | | |
| | | 102 | | /// <summary> |
| | | 103 | | /// One kilogram is equal to this many pounds. |
| | | 104 | | /// </summary> |
| | 1 | 105 | | public static readonly Fixed64 KilogramToPound = (Fixed64)2.20462262f; |
| | | 106 | | |
| | | 107 | | /// <summary> |
| | | 108 | | /// Gets or sets gravitational acceleration in world units per second squared. |
| | | 109 | | /// </summary> |
| | | 110 | | public Fixed64 Gravity { get; set; } |
| | | 111 | | |
| | | 112 | | /// <summary> |
| | | 113 | | /// Gets or sets air density used by drag calculations. |
| | | 114 | | /// </summary> |
| | | 115 | | public Fixed64 AirDensity { get; set; } |
| | | 116 | | |
| | | 117 | | /// <summary> |
| | | 118 | | /// Gets or sets the minimum speed treated as meaningful motion. |
| | | 119 | | /// </summary> |
| | | 120 | | public Fixed64 MinSpeed { get; set; } |
| | | 121 | | |
| | | 122 | | /// <summary> |
| | | 123 | | /// Gets or sets the maximum linear or angular speed. |
| | | 124 | | /// </summary> |
| | | 125 | | public Fixed64 MaxSpeed { get; set; } |
| | | 126 | | |
| | | 127 | | /// <summary> |
| | | 128 | | /// Gets or sets the maximum downward fall speed. |
| | | 129 | | /// </summary> |
| | | 130 | | public Fixed64 MaxFallSpeed { get; set; } |
| | | 131 | | |
| | | 132 | | /// <summary> |
| | | 133 | | /// Gets or sets the speed threshold used when transitioning friction behavior. |
| | | 134 | | /// </summary> |
| | | 135 | | public Fixed64 FrictionTransitionSpeed { get; set; } |
| | | 136 | | |
| | | 137 | | /// <summary> |
| | | 138 | | /// Gets or sets the multiplier applied when decelerating motion. |
| | | 139 | | /// </summary> |
| | | 140 | | public Fixed64 DecelerationMultiplier { get; set; } |
| | | 141 | | |
| | | 142 | | /// <summary> |
| | | 143 | | /// Gets or sets the damping factor applied to angular velocity. |
| | | 144 | | /// </summary> |
| | | 145 | | public Fixed64 DampingFactor { get; set; } |
| | | 146 | | |
| | | 147 | | /// <summary> |
| | | 148 | | /// Gets or sets the maximum distance-based collision-culling score. |
| | | 149 | | /// </summary> |
| | | 150 | | public int CullDistanceMax { get; set; } |
| | | 151 | | |
| | | 152 | | /// <summary> |
| | | 153 | | /// Gets or sets the squared distance below which fast collision checks are preserved. |
| | | 154 | | /// </summary> |
| | | 155 | | public Fixed64 CullFastDistanceMax { get; set; } |
| | | 156 | | |
| | | 157 | | /// <summary> |
| | | 158 | | /// Gets or sets the velocity step used by collision culling. |
| | | 159 | | /// </summary> |
| | | 160 | | public int CullVelocityStep { get; set; } |
| | | 161 | | |
| | | 162 | | /// <summary> |
| | | 163 | | /// Gets or sets the maximum velocity-based collision-culling score. |
| | | 164 | | /// </summary> |
| | | 165 | | public int CullVelocityMax { get; set; } |
| | | 166 | | |
| | | 167 | | /// <summary> |
| | | 168 | | /// Gets or sets the frame-count step used by collision culling. |
| | | 169 | | /// </summary> |
| | | 170 | | public int CullTimeStep { get; set; } |
| | | 171 | | |
| | | 172 | | /// <summary> |
| | | 173 | | /// Gets or sets the maximum time-based collision-culling score. |
| | | 174 | | /// </summary> |
| | | 175 | | public int CullTimeMax { get; set; } |
| | | 176 | | |
| | | 177 | | /// <summary> |
| | | 178 | | /// Initializes a new environment with explicit physical and culling values. |
| | | 179 | | /// </summary> |
| | 5084 | 180 | | public PhysicsEnvironment( |
| | 5084 | 181 | | Fixed64 gravity, |
| | 5084 | 182 | | Fixed64 airDensity, |
| | 5084 | 183 | | Fixed64 minSpeed, |
| | 5084 | 184 | | Fixed64 maxSpeed, |
| | 5084 | 185 | | Fixed64 maxFallSpeed, |
| | 5084 | 186 | | Fixed64 frictionTransitionSpeed, |
| | 5084 | 187 | | Fixed64 decelerationMultiplier, |
| | 5084 | 188 | | Fixed64 dampingFactor, |
| | 5084 | 189 | | int cullDistanceMax, |
| | 5084 | 190 | | Fixed64 cullFastDistanceMax, |
| | 5084 | 191 | | int cullVelocityStep, |
| | 5084 | 192 | | int cullVelocityMax, |
| | 5084 | 193 | | int cullTimeStep, |
| | 5084 | 194 | | int cullTimeMax) |
| | | 195 | | { |
| | 5084 | 196 | | Gravity = gravity; |
| | 5084 | 197 | | AirDensity = airDensity; |
| | 5084 | 198 | | MinSpeed = minSpeed; |
| | 5084 | 199 | | MaxSpeed = maxSpeed; |
| | 5084 | 200 | | MaxFallSpeed = maxFallSpeed; |
| | 5084 | 201 | | FrictionTransitionSpeed = frictionTransitionSpeed; |
| | 5084 | 202 | | DecelerationMultiplier = decelerationMultiplier; |
| | 5084 | 203 | | DampingFactor = dampingFactor; |
| | 5084 | 204 | | CullDistanceMax = cullDistanceMax; |
| | 5084 | 205 | | CullFastDistanceMax = cullFastDistanceMax; |
| | 5084 | 206 | | CullVelocityStep = cullVelocityStep; |
| | 5084 | 207 | | CullVelocityMax = cullVelocityMax; |
| | 5084 | 208 | | CullTimeStep = cullTimeStep; |
| | 5084 | 209 | | CullTimeMax = cullTimeMax; |
| | 5084 | 210 | | } |
| | | 211 | | |
| | | 212 | | /// <summary> |
| | | 213 | | /// Creates environment values for the standard Gravitas runtime defaults. |
| | | 214 | | /// </summary> |
| | | 215 | | /// <param name="frameRate">Frame rate used to initialize frame-derived culling thresholds.</param> |
| | | 216 | | /// <returns>A new environment instance.</returns> |
| | | 217 | | public static PhysicsEnvironment Default(int frameRate = PhysicsSettings.DefaultFrameRate) |
| | | 218 | | { |
| | 5085 | 219 | | PhysicsSettings.ThrowIfInvalidFrameRate(frameRate); |
| | | 220 | | |
| | 5084 | 221 | | return new PhysicsEnvironment( |
| | 5084 | 222 | | gravity: DefaultGravity, |
| | 5084 | 223 | | airDensity: DefaultAirDensity, |
| | 5084 | 224 | | minSpeed: DefaultMinSpeed, |
| | 5084 | 225 | | maxSpeed: DefaultMaxSpeed, |
| | 5084 | 226 | | maxFallSpeed: DefaultMaxFallSpeed, |
| | 5084 | 227 | | frictionTransitionSpeed: DefaultFrictionTransitionSpeed, |
| | 5084 | 228 | | decelerationMultiplier: DefaultDecelerationMultiplier, |
| | 5084 | 229 | | dampingFactor: DefaultDampingFactor, |
| | 5084 | 230 | | cullDistanceMax: frameRate / DefaultCullDistanceFrameDivisor, |
| | 5084 | 231 | | cullFastDistanceMax: DefaultCullFastDistanceMax, |
| | 5084 | 232 | | cullVelocityStep: DefaultCullVelocityStep, |
| | 5084 | 233 | | cullVelocityMax: DefaultCullVelocityMax, |
| | 5084 | 234 | | cullTimeStep: frameRate * DefaultCullTimeStepFrameMultiplier, |
| | 5084 | 235 | | cullTimeMax: frameRate / DefaultCullTimeMaxFrameDivisor); |
| | | 236 | | } |
| | | 237 | | } |