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

151 lines
4.5 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. #include "renderer/passes/sky-pass.hpp"
  25. namespace ecs {
  26. static constexpr double seconds_per_day = 24.0 * 60.0 * 60.0;
  27. astronomy_system::astronomy_system(ecs::registry& registry):
  28. entity_system(registry),
  29. universal_time(0.0),
  30. days_per_timestep(1.0 / seconds_per_day),
  31. observer_location{0.0, 0.0, 0.0},
  32. lst(0.0),
  33. obliquity(0.0),
  34. sky_pass(nullptr),
  35. sun(entt::null),
  36. moon(entt::null)
  37. {}
  38. void astronomy_system::update(double t, double dt)
  39. {
  40. // Add scaled timestep to current time
  41. set_universal_time(universal_time + dt * days_per_timestep);
  42. // Update horizontal (topocentric) positions of intrasolar celestial bodies
  43. registry.view<celestial_body_component, transform_component>().each(
  44. [&](ecs::entity entity, auto& body, auto& transform)
  45. {
  46. // Transform orbital position from ecliptic space to horizontal space
  47. double3 horizontal = ecliptic_to_horizontal * body.orbital_state.r;
  48. // Subtract observer's radial distance (planet radius + observer's altitude)
  49. horizontal.z -= observer_location[0];
  50. // Convert rectangular horizontal coordinates to spherical
  51. double3 spherical = astro::rectangular_to_spherical(horizontal);
  52. spherical.z -= math::pi<double>;
  53. // Find angular radius
  54. double angular_radius = astro::find_angular_radius(body.radius, spherical.x);
  55. // Transform into local right-handed coordinates
  56. double3 translation = astro::horizontal_to_right_handed * horizontal;
  57. double3x3 rotation = astro::horizontal_to_right_handed * ecliptic_to_horizontal;
  58. // Set local transform of transform component
  59. transform.local.translation = math::type_cast<float>(translation);
  60. transform.local.rotation = math::type_cast<float>(math::quaternion_cast(rotation));
  61. transform.local.scale = math::type_cast<float>(double3{body.radius, body.radius, body.radius});
  62. });
  63. if (sky_pass)
  64. {
  65. sky_pass->set_horizon_color({0, 0, 0});
  66. sky_pass->set_zenith_color({1, 1, 1});
  67. sky_pass->set_time_of_day(static_cast<float>(universal_time * 60.0 * 60.0));
  68. //sky_pass->set_observer_location(location[0], location[1], location[2]);
  69. sky_pass->set_julian_day(static_cast<float>(universal_time));
  70. }
  71. }
  72. void astronomy_system::set_universal_time(double time)
  73. {
  74. universal_time = time;
  75. update_axial_rotation();
  76. }
  77. void astronomy_system::set_time_scale(double scale)
  78. {
  79. days_per_timestep = scale / seconds_per_day;
  80. }
  81. void astronomy_system::set_observer_location(const double3& location)
  82. {
  83. observer_location = location;
  84. update_sidereal_time();
  85. }
  86. void astronomy_system::set_obliquity(double angle)
  87. {
  88. obliquity = angle;
  89. update_ecliptic_to_horizontal();
  90. }
  91. void astronomy_system::set_axial_rotation_speed(double speed)
  92. {
  93. axial_rotation_speed = speed;
  94. }
  95. void astronomy_system::set_axial_rotation_at_epoch(double angle)
  96. {
  97. axial_rotation_at_epoch = angle;
  98. update_axial_rotation();
  99. }
  100. void astronomy_system::set_sky_pass(::sky_pass* pass)
  101. {
  102. this->sky_pass = pass;
  103. }
  104. void astronomy_system::set_sun(ecs::entity entity)
  105. {
  106. sun = entity;
  107. }
  108. void astronomy_system::set_moon(ecs::entity entity)
  109. {
  110. moon = entity;
  111. }
  112. void astronomy_system::update_axial_rotation()
  113. {
  114. axial_rotation = math::wrap_radians<double>(axial_rotation_at_epoch + universal_time * axial_rotation_speed);
  115. update_sidereal_time();
  116. }
  117. void astronomy_system::update_sidereal_time()
  118. {
  119. lst = math::wrap_radians<double>(axial_rotation + observer_location[2]);
  120. update_ecliptic_to_horizontal();
  121. }
  122. void astronomy_system::update_ecliptic_to_horizontal()
  123. {
  124. ecliptic_to_horizontal = astro::ecliptic_to_horizontal(obliquity, observer_location[1], lst);
  125. }
  126. } // namespace ecs