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