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

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