💿🐜 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.

127 lines
3.9 KiB

  1. /*
  2. * Copyright (C) 2021 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 "ecs/systems/astronomy-system.hpp"
  20. #include "astro/coordinates.hpp"
  21. #include "astro/apparent-size.hpp"
  22. #include "ecs/components/celestial-body-component.hpp"
  23. #include "ecs/components/transform-component.hpp"
  24. namespace ecs {
  25. static constexpr double seconds_per_day = 24.0 * 60.0 * 60.0;
  26. astronomy_system::astronomy_system(ecs::registry& registry):
  27. entity_system(registry),
  28. universal_time(0.0),
  29. days_per_timestep(1.0 / seconds_per_day),
  30. observer_location{0.0, 0.0, 0.0},
  31. lst(0.0),
  32. obliquity(0.0),
  33. axial_rotation(0.0),
  34. axial_rotation_at_epoch(0.0),
  35. axial_rotation_speed(0.0)
  36. {}
  37. void astronomy_system::update(double t, double dt)
  38. {
  39. // Add scaled timestep to current time
  40. set_universal_time(universal_time + dt * days_per_timestep);
  41. // Update horizontal (topocentric) positions of intrasolar celestial bodies
  42. registry.view<celestial_body_component, transform_component>().each(
  43. [&](ecs::entity entity, auto& body, auto& transform)
  44. {
  45. // Transform orbital position from ecliptic space to horizontal space
  46. double3 horizontal = ecliptic_to_horizontal * body.orbital_state.r;
  47. // Subtract observer's radial distance (planet radius + observer's altitude)
  48. horizontal.z -= observer_location[0];
  49. // Convert rectangular horizontal coordinates to spherical
  50. double3 spherical = astro::rectangular_to_spherical(horizontal);
  51. spherical.z -= math::pi<double>;
  52. // Find angular radius
  53. double angular_radius = astro::find_angular_radius(body.radius, spherical.x);
  54. // Transform into local right-handed coordinates
  55. double3 translation = astro::horizontal_to_right_handed * horizontal;
  56. double3x3 rotation = astro::horizontal_to_right_handed * ecliptic_to_horizontal;
  57. // Set local transform of transform component
  58. transform.local.translation = math::type_cast<float>(translation);
  59. transform.local.rotation = math::type_cast<float>(math::quaternion_cast(rotation));
  60. transform.local.scale = math::type_cast<float>(double3{body.radius, body.radius, body.radius});
  61. });
  62. }
  63. void astronomy_system::set_universal_time(double time)
  64. {
  65. universal_time = time;
  66. update_axial_rotation();
  67. }
  68. void astronomy_system::set_time_scale(double scale)
  69. {
  70. days_per_timestep = scale / seconds_per_day;
  71. }
  72. void astronomy_system::set_observer_location(const double3& location)
  73. {
  74. observer_location = location;
  75. update_sidereal_time();
  76. }
  77. void astronomy_system::set_obliquity(double angle)
  78. {
  79. obliquity = angle;
  80. update_ecliptic_to_horizontal();
  81. }
  82. void astronomy_system::set_axial_rotation_speed(double speed)
  83. {
  84. axial_rotation_speed = speed;
  85. update_axial_rotation();
  86. }
  87. void astronomy_system::set_axial_rotation_at_epoch(double angle)
  88. {
  89. axial_rotation_at_epoch = angle;
  90. update_axial_rotation();
  91. }
  92. void astronomy_system::update_axial_rotation()
  93. {
  94. axial_rotation = math::wrap_radians<double>(axial_rotation_at_epoch + universal_time * axial_rotation_speed);
  95. update_sidereal_time();
  96. }
  97. void astronomy_system::update_sidereal_time()
  98. {
  99. lst = math::wrap_radians<double>(axial_rotation + observer_location[2]);
  100. update_ecliptic_to_horizontal();
  101. }
  102. void astronomy_system::update_ecliptic_to_horizontal()
  103. {
  104. ecliptic_to_horizontal = astro::ecliptic_to_horizontal(obliquity, observer_location[1], lst);
  105. }
  106. } // namespace ecs