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

101 lines
3.2 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. using namespace ecs;
  26. static constexpr double seconds_per_day = 24.0 * 60.0 * 60.0;
  27. solar_system::solar_system(entt::registry& registry):
  28. entity_system(registry),
  29. julian_date(0.0),
  30. time_scale(1.0),
  31. latitude(0.0),
  32. longitude(0.0),
  33. altitude(0.0)
  34. {}
  35. void solar_system::update(double t, double dt)
  36. {
  37. // Add scaled timestep to Julian date
  38. set_julian_date(julian_date + (dt * time_scale) / seconds_per_day);
  39. // Update horizontal (topocentric) positions of orbiting bodies
  40. registry.view<orbit_component, transform_component>().each(
  41. [&](auto entity, auto& orbit, auto& transform)
  42. {
  43. double a = orbit.a;
  44. double ec = orbit.ec;
  45. double w = orbit.w;
  46. double ma = orbit.ma;
  47. double i = orbit.i;
  48. double om = orbit.om;
  49. double3 ecliptic = ast::solve_kepler(a, ec, w, ma, i, om);
  50. double3 horizontal = ecliptic_to_horizontal * ecliptic;
  51. // Subtract Earth's radius (in AU), for positon of observer
  52. horizontal.z -= 4.25875e-5;
  53. // Transform into local right-handed coordinates
  54. double3 translation = ast::horizontal_to_right_handed * horizontal;
  55. double3x3 rotation = ast::horizontal_to_right_handed * ecliptic_to_horizontal;
  56. transform.local.translation = math::type_cast<float>(translation);
  57. transform.local.rotation = math::type_cast<float>(math::quaternion_cast(rotation));
  58. });
  59. }
  60. void solar_system::set_julian_date(double jd)
  61. {
  62. julian_date = jd;
  63. // Recalculate obliquity of the ecliptic
  64. ecl = ast::approx_ecliptic_obliquity(julian_date);
  65. // Recalculate LMST
  66. lmst = ast::jd_to_lmst(julian_date, longitude);
  67. // Recalculate ecliptic to horizontal transformation matrix
  68. ecliptic_to_horizontal = ast::ecliptic_to_horizontal(ecl, latitude, lmst);
  69. }
  70. void solar_system::set_time_scale(double scale)
  71. {
  72. time_scale = scale;
  73. }
  74. void solar_system::set_observer_location(double latitude, double longitude, double altitude)
  75. {
  76. this->latitude = latitude;
  77. this->longitude = longitude;
  78. this->altitude = altitude;
  79. // Recalculate LMST
  80. lmst = ast::jd_to_lmst(julian_date, longitude);
  81. // Recalculate ecliptic to horizontal transformation matrix
  82. ecliptic_to_horizontal = ast::ecliptic_to_horizontal(ecl, latitude, lmst);
  83. }