1
0

BicyclePhysics.cs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. using System;
  2. using UnityEngine;
  3. public class BicyclePhysics
  4. {
  5. private const float K_A = 0.5f; //wind resistance coefficient
  6. private const float A = 0.6f; //frontal area bike + rider; m2
  7. private const float D = 1.226f; //air density; kg/m3
  8. /// <summary>
  9. /// Returns a speed in m/s for a bike riding a gradient with the same power it would be ridden in the flat with the input speed.
  10. /// Using formula from http://www.sportsci.org/jour/9804/dps.html. Assuming v=s and neglecting rolling resistance
  11. /// The speed is calculated by solving k_aAs_1^3d = k_aAs_2^3d + giMs_2 for s_2
  12. /// </summary>
  13. /// <param name="speedFlat">Speed the bike would have in flat terrain</param>
  14. /// <param name="systemMass">Mass of bike + rider</param>
  15. /// <param name="gradient">gradient in vertical meters gained per meter travelled forward</param>
  16. /// <returns></returns>
  17. public static float SpeedAtGradientForSpeedAtFlat(float speedFlat, float systemMass, float gradient)
  18. {
  19. var g = -Physics.gravity.y;
  20. var divisor = Mathf.Pow(g * gradient * systemMass, 3f);
  21. var dividend = 27f * Mathf.Pow(A * D * K_A, 3f);
  22. Console.WriteLine($"Divisor: {divisor}");
  23. Console.WriteLine($"Dividend: {dividend}");
  24. var divisor2 = Mathf.Pow(speedFlat, 6f);
  25. var dividend2 = 4f;
  26. Console.WriteLine($"Divisor2: {divisor2}");
  27. Console.WriteLine($"Dividend2: {dividend2}");
  28. var sqrtTerm = Mathf.Sqrt(
  29. divisor
  30. /
  31. dividend
  32. +
  33. divisor2
  34. /
  35. dividend2
  36. );
  37. Console.WriteLine($"Sqrt: {sqrtTerm}");
  38. var divisor3 = Mathf.Pow(speedFlat, 3f);
  39. var dividend3 = 2f;
  40. Console.WriteLine($"Divisor3: {divisor3}");
  41. Console.WriteLine($"Dividend3: {dividend3}");
  42. var secondTerm = divisor3 / dividend3;
  43. Console.WriteLine($"Second term: {secondTerm}");
  44. Console.WriteLine($"SqrtTerm + secondTerm: {sqrtTerm+secondTerm}");
  45. var firstqbrt = Qbrt(sqrtTerm + secondTerm);
  46. var secondqbrt = Qbrt(-sqrtTerm + secondTerm);
  47. Console.WriteLine($"firstqbrt: {firstqbrt}");
  48. Console.WriteLine($"secondqbrt: {secondqbrt}");
  49. return firstqbrt + secondqbrt;
  50. }
  51. private static float Qbrt(float x) => x < 0 ? -Mathf.Pow(-x, 1f/3f) : Mathf.Pow(x, 1f/3f);
  52. }