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

132 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/solar-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/components/orbit-component.hpp"
  24. #include "game/components/transform-component.hpp"
  25. #include <iostream>
  26. using namespace ecs;
  27. static constexpr double seconds_per_day = 24.0 * 60.0 * 60.0;
  28. solar_system::solar_system(entt::registry& registry):
  29. entity_system(registry),
  30. julian_date(0.0),
  31. time_scale(1.0),
  32. latitude(0.0),
  33. longitude(0.0),
  34. altitude(0.0)
  35. {}
  36. void solar_system::update(double t, double dt)
  37. {
  38. // Add scaled timestep to Julian date
  39. set_julian_date(julian_date + (dt * time_scale) / seconds_per_day);
  40. const double ke_tolerance = 1e-6;
  41. const std::size_t ke_iterations = 10;
  42. ast::orbital_elements sun_elements;
  43. sun_elements.a = 1.0;
  44. sun_elements.ec = 0.016709;
  45. sun_elements.w = math::radians(282.9404);
  46. sun_elements.ma = math::radians(356.0470);
  47. sun_elements.i = math::radians(0.0);
  48. sun_elements.om = math::radians(0.0);
  49. const double j2k_day = julian_date - 2451545.0;
  50. const double j2k_century = (julian_date - 2451545.0) / 36525.0;
  51. sun_elements.ec += math::radians(-1.151e-9) * j2k_day;
  52. sun_elements.w += math::radians(4.70935e-5) * j2k_day;
  53. sun_elements.ma += math::radians(0.9856002585) * j2k_day;
  54. ast::orbital_state sun_ecliptic = ast::orbital_elements_to_state(sun_elements, ke_tolerance, ke_iterations);
  55. double3 sun_horizontal = ecliptic_to_horizontal * sun_ecliptic.r;
  56. sun_horizontal.z -= 4.25875e-5;
  57. double3 sun_spherical = ast::rectangular_to_spherical(sun_horizontal);
  58. double2 sun_az_el = {sun_spherical.z - math::pi<double>, sun_spherical.y};
  59. //std::cout << "new azel: " << math::degrees(sun_az_el.x) << ", " << math::degrees(sun_az_el.y) << std::endl;
  60. // Update horizontal (topocentric) positions of orbiting bodies
  61. registry.view<orbit_component, transform_component>().each(
  62. [&](auto entity, auto& orbit, auto& transform)
  63. {
  64. /*
  65. double a = orbit.a;
  66. double ec = orbit.ec;
  67. double w = orbit.w;
  68. double ma = orbit.ma;
  69. double i = orbit.i;
  70. double om = orbit.om;
  71. double3 ecliptic = ast::solve_kepler(a, ec, w, ma, i, om);
  72. double3 horizontal = ecliptic_to_horizontal * ecliptic;
  73. // Subtract Earth's radius (in AU), for positon of observer
  74. horizontal.z -= 4.25875e-5;
  75. // Transform into local right-handed coordinates
  76. double3 translation = ast::horizontal_to_right_handed * horizontal;
  77. double3x3 rotation = ast::horizontal_to_right_handed * ecliptic_to_horizontal;
  78. transform.local.translation = math::type_cast<float>(translation);
  79. transform.local.rotation = math::type_cast<float>(math::quaternion_cast(rotation));
  80. */
  81. });
  82. }
  83. void solar_system::set_julian_date(double jd)
  84. {
  85. julian_date = jd;
  86. // Recalculate obliquity of the ecliptic
  87. ecl = ast::approx_ecliptic_obliquity(julian_date);
  88. // Recalculate LMST
  89. lmst = ast::jd_to_lmst(julian_date, longitude);
  90. // Recalculate ecliptic to horizontal transformation matrix
  91. ecliptic_to_horizontal = ast::ecliptic_to_horizontal(ecl, latitude, lmst);
  92. }
  93. void solar_system::set_time_scale(double scale)
  94. {
  95. time_scale = scale;
  96. }
  97. void solar_system::set_observer_location(double latitude, double longitude, double altitude)
  98. {
  99. this->latitude = latitude;
  100. this->longitude = longitude;
  101. this->altitude = altitude;
  102. // Recalculate LMST
  103. lmst = ast::jd_to_lmst(julian_date, longitude);
  104. // Recalculate ecliptic to horizontal transformation matrix
  105. ecliptic_to_horizontal = ast::ecliptic_to_horizontal(ecl, latitude, lmst);
  106. }