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

68 lines
2.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/astronomical-constants.hpp"
  23. #include "game/components/celestial-body-component.hpp"
  24. using namespace ecs;
  25. static constexpr double seconds_per_day = 24.0 * 60.0 * 60.0;
  26. solar_system::solar_system(entt::registry& registry):
  27. entity_system(registry),
  28. universal_time(0.0),
  29. days_per_timestep(1.0 / seconds_per_day),
  30. ke_tolerance(1e-6),
  31. ke_iterations(10)
  32. {}
  33. void solar_system::update(double t, double dt)
  34. {
  35. // Add scaled timestep to current time
  36. set_universal_time(universal_time + dt * days_per_timestep);
  37. // Update orbital state of intrasolar celestial bodies
  38. registry.view<celestial_body_component>().each(
  39. [&](auto entity, auto& body)
  40. {
  41. ast::orbital_elements elements = body.orbital_elements;
  42. elements.a += body.orbital_rate.a * universal_time;
  43. elements.ec += body.orbital_rate.ec * universal_time;
  44. elements.w += body.orbital_rate.w * universal_time;
  45. elements.ma += body.orbital_rate.ma * universal_time;
  46. elements.i += body.orbital_rate.i * universal_time;
  47. elements.om += body.orbital_rate.om * universal_time;
  48. // Calculate ecliptic orbital position
  49. body.orbital_state.r = ast::orbital_elements_to_ecliptic(elements, ke_tolerance, ke_iterations);
  50. });
  51. }
  52. void solar_system::set_universal_time(double time)
  53. {
  54. universal_time = time;
  55. }
  56. void solar_system::set_time_scale(double scale)
  57. {
  58. days_per_timestep = scale / seconds_per_day;
  59. }