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

91 lines
3.0 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/orbit-system.hpp"
  20. #include "ecs/components/orbit-component.hpp"
  21. #include "ecs/entity.hpp"
  22. #include "physics/orbit/orbit.hpp"
  23. namespace ecs {
  24. orbit_system::orbit_system(ecs::registry& registry):
  25. entity_system(registry),
  26. universal_time(0.0),
  27. time_scale(1.0),
  28. ke_iterations(10),
  29. ke_tolerance(1e-6)
  30. {}
  31. void orbit_system::update(double t, double dt)
  32. {
  33. // Add scaled timestep to current time
  34. set_universal_time(universal_time + dt * time_scale);
  35. // Update the orbital state of orbiting bodies
  36. registry.view<orbit_component>().each(
  37. [&](ecs::entity entity, auto& orbit)
  38. {
  39. // Calculate semi-minor axis (b)
  40. const double b = physics::orbit::derive_semiminor_axis(orbit.elements.a, orbit.elements.e);
  41. // Solve Kepler's equation for eccentric anomaly (E)
  42. const double ea = physics::orbit::kepler_ea(orbit.elements.e, orbit.elements.ta, ke_iterations, ke_tolerance);
  43. // Calculate radial distance and true anomaly (nu)
  44. const double xv = orbit.elements.a * (std::cos(ea) - orbit.elements.e);
  45. const double yv = b * std::sin(ea);
  46. const double distance = std::sqrt(xv * xv + yv * yv);
  47. const double ta = std::atan2(yv, xv);
  48. // Calculate Cartesian position (r) in perifocal space
  49. const math::vector3<double> r_perifocal = math::quaternion<double>::rotate_z(ta) * math::vector3<double>{distance, 0, 0};
  50. /// @TODO Calculate Cartesian velocity (v) in perifocal space
  51. //const math::vector3<double> v_perifocal = ...
  52. // Construct perifocal to inertial reference frame
  53. const physics::frame<double> perifocal_to_inertial = physics::orbit::inertial::to_perifocal
  54. (
  55. {0, 0, 0},
  56. orbit.elements.raan,
  57. orbit.elements.i,
  58. orbit.elements.w
  59. ).inverse();
  60. // Transform orbital state vectors from perifocal space to the parent inertial space
  61. const math::vector3<double> r_inertial = perifocal_to_inertial.transform(r_perifocal);
  62. //const math::vector3<double> v_inertial = perifocal_frame.transform(v_perifocal);
  63. // Update orbital state of component
  64. orbit.state.r = r_inertial;
  65. //orbit.state.v = v_inertial;
  66. });
  67. }
  68. void orbit_system::set_universal_time(double time)
  69. {
  70. universal_time = time;
  71. }
  72. void orbit_system::set_time_scale(double scale)
  73. {
  74. time_scale = scale;
  75. }
  76. } // namespace ecs