💿🐜 Antkeeper source code https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

133 lines
4.3 KiB

  1. /*
  2. * Copyright (C) 2020 Christopher J. Howard
  3. *
  4. * This file is part of Antkeeper source code.
  5. *
  6. * Antkeeper source code is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Antkeeper source code is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "game/systems/astronomy-system.hpp"
  20. #include "game/astronomy/celestial-coordinates.hpp"
  21. #include "game/astronomy/celestial-mechanics.hpp"
  22. #include "game/astronomy/celestial-time.hpp"
  23. #include "game/astronomy/astronomical-constants.hpp"
  24. #include "game/components/orbit-component.hpp"
  25. #include "game/components/transform-component.hpp"
  26. using namespace ecs;
  27. static constexpr double seconds_per_day = 24.0 * 60.0 * 60.0;
  28. astronomy_system::astronomy_system(entt::registry& registry):
  29. entity_system(registry),
  30. universal_time(0.0),
  31. days_per_timestep(1.0 / seconds_per_day),
  32. observer_location{0.0, 0.0, 0.0},
  33. lst(0.0),
  34. obliquity(0.0),
  35. ke_tolerance(1e-6),
  36. ke_iterations(10)
  37. {}
  38. void astronomy_system::update(double t, double dt)
  39. {
  40. const double dt_days = dt * days_per_timestep;
  41. // Add scaled timestep to current time
  42. set_universal_time(universal_time + dt_days);
  43. // Update horizontal (topocentric) positions of orbiting bodies
  44. registry.view<orbit_component, transform_component>().each(
  45. [&](auto entity, auto& orbit, auto& transform)
  46. {
  47. ast::orbital_elements orbital_elements;
  48. orbital_elements.a = orbit.a + orbit.d_a * universal_time;
  49. orbital_elements.ec = orbit.ec + orbit.d_ec * universal_time;
  50. orbital_elements.w = orbit.w + orbit.d_w * universal_time;
  51. orbital_elements.ma = math::wrap_radians(orbit.ma + orbit.d_ma * universal_time);
  52. orbital_elements.i = orbit.i + orbit.d_i * universal_time;
  53. orbital_elements.om = math::wrap_radians(orbit.om + orbit.d_om * universal_time);
  54. // Calculate ecliptic orbital position
  55. double3 ecliptic = ast::orbital_elements_to_ecliptic(orbital_elements, ke_tolerance, ke_iterations);
  56. // Transform orbital position from ecliptic space to horizontal space
  57. double3 horizontal = ecliptic_to_horizontal * ecliptic;
  58. // Subtract observer's radial distance (planet radius + observer's altitude)
  59. horizontal.z -= observer_location[0];
  60. // Calculate azimuth and elevation
  61. double3 spherical = ast::rectangular_to_spherical(horizontal);
  62. double2 az_el = {spherical.z - math::pi<double>, spherical.y};
  63. // Transform into local right-handed coordinates
  64. double3 translation = ast::horizontal_to_right_handed * horizontal;
  65. double3x3 rotation = ast::horizontal_to_right_handed * ecliptic_to_horizontal;
  66. transform.local.translation = math::type_cast<float>(translation);
  67. transform.local.rotation = math::type_cast<float>(math::quaternion_cast(rotation));
  68. });
  69. }
  70. void astronomy_system::set_universal_time(double time)
  71. {
  72. universal_time = time;
  73. update_axial_rotation();
  74. }
  75. void astronomy_system::set_time_scale(double scale)
  76. {
  77. days_per_timestep = scale / seconds_per_day;
  78. }
  79. void astronomy_system::set_observer_location(const double3& location)
  80. {
  81. observer_location = location;
  82. update_sidereal_time();
  83. }
  84. void astronomy_system::set_obliquity(double angle)
  85. {
  86. obliquity = angle;
  87. update_ecliptic_to_horizontal();
  88. }
  89. void astronomy_system::set_axial_rotation_speed(double speed)
  90. {
  91. axial_rotation_speed = speed;
  92. }
  93. void astronomy_system::set_axial_rotation_at_epoch(double angle)
  94. {
  95. axial_rotation_at_epoch = angle;
  96. update_axial_rotation();
  97. }
  98. void astronomy_system::update_axial_rotation()
  99. {
  100. axial_rotation = math::wrap_radians<double>(axial_rotation_at_epoch + universal_time * axial_rotation_speed);
  101. update_sidereal_time();
  102. }
  103. void astronomy_system::update_sidereal_time()
  104. {
  105. lst = math::wrap_radians<double>(axial_rotation + observer_location[2]);
  106. update_ecliptic_to_horizontal();
  107. }
  108. void astronomy_system::update_ecliptic_to_horizontal()
  109. {
  110. ecliptic_to_horizontal = ast::ecliptic_to_horizontal(obliquity, observer_location[1], lst);
  111. }