12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- using System;
- using UnityEngine;
- public class BicyclePhysics
- {
- private const float K_A = 0.5f;
- private const float A = 0.6f;
- private const float D = 1.226f;
-
-
-
-
-
-
-
-
-
- public static float SpeedAtGradientForSpeedAtFlat(float speedFlat, float systemMass, float gradient)
- {
- var g = -Physics.gravity.y;
- const float k = K_A * A * D;
- var b = g * gradient * systemMass;
- float result;
- if (gradient < -0.01f)
- {
- result = 2 * Mathf.Sqrt(-b / (3 * k)) *
- Mathf.Cos(
- Mathf.Acos(3 * Mathf.Sqrt(-3 * k / b) * k * speedFlat
- / (2 * b))
- / 3);
- }
- else
- {
- var sqrtTerm = Mathf.Sqrt(
- Mathf.Pow(b, 3f)
- /
- (27f * Mathf.Pow(k, 3f))
- +
- Mathf.Pow(speedFlat, 6f)
- /
- 4f
- );
- var secondTerm = Mathf.Pow(speedFlat, 3f) / 2f;
- result = Qbrt(sqrtTerm + secondTerm) + Qbrt(-sqrtTerm + secondTerm);
- }
- if (float.IsNaN(result))
- {
- Debug.LogWarning(
- $"BicyclePhysics: result is NaN for k = {k} and b = {b} (gradient = {gradient}, mass = {systemMass})");
- return speedFlat;
- }
- return result;
- }
- private static float Qbrt(float x) => x < 0 ? -Mathf.Pow(-x, 1f / 3f) : Mathf.Pow(x, 1f / 3f);
- }
|