Antkeeper  0.0.1
orbit-system.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2023 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 
22 
23 
26  ephemeris(nullptr),
27  time(0.0),
28  time_scale(1.0)
29 {
30  registry.on_construct<::orbit_component>().connect<&orbit_system::on_orbit_construct>(this);
31  registry.on_update<::orbit_component>().connect<&orbit_system::on_orbit_update>(this);
32 }
33 
35 {
36  registry.on_construct<::orbit_component>().disconnect<&orbit_system::on_orbit_construct>(this);
37  registry.on_update<::orbit_component>().disconnect<&orbit_system::on_orbit_update>(this);
38 }
39 
40 void orbit_system::update(float t, float dt)
41 {
42  // Add scaled timestep to current time
43  set_time(time + dt * time_scale);
44 
45  if (!ephemeris)
46  return;
47 
48  // Calculate positions of ephemeris items, in meters
49  for (int i: ephemeris_indices)
50  positions[i] = ephemeris->trajectories[i].position(time) * 1000.0;
51 
52  // Propagate orbits
53  registry.view<orbit_component>().each
54  (
55  [&](entity::id entity_eid, auto& orbit)
56  {
57  orbit.position = positions[orbit.ephemeris_index] * orbit.scale;
58 
59  entity::id parent_id = orbit.parent;
60  while (parent_id != entt::null)
61  {
62  const orbit_component& parent_orbit = registry.get<orbit_component>(parent_id);
63  orbit.position += positions[parent_orbit.ephemeris_index] * parent_orbit.scale;
64  parent_id = parent_orbit.parent;
65  }
66  }
67  );
68 }
69 
71 {
72  this->ephemeris = ephemeris;
73  positions.resize((ephemeris) ? ephemeris->trajectories.size() : 0);
74 }
75 
76 void orbit_system::set_time(double time)
77 {
78  this->time = time;
79 }
80 
82 {
83  time_scale = scale;
84 }
85 
86 void orbit_system::on_orbit_construct(entity::registry& registry, entity::id entity_id)
87 {
88  const ::orbit_component& component = registry.get<::orbit_component>(entity_id);
89  ephemeris_indices.insert(component.ephemeris_index);
90 }
91 
92 void orbit_system::on_orbit_update(entity::registry& registry, entity::id entity_id)
93 {
94  const ::orbit_component& component = registry.get<::orbit_component>(entity_id);
95  ephemeris_indices.insert(component.ephemeris_index);
96 }
97 
void set_time_scale(double scale)
Sets the factor by which the timestep dt will be scaled before being added to the current time.
void set_time(double time)
Sets the current time.
virtual void update(float t, float dt)
Scales then adds the timestep dt to the current time, then recalculates the positions of orbiting bod...
void set_ephemeris(std::shared_ptr< physics::orbit::ephemeris< double >> ephemeris)
Sets the ephemeris used to calculate orbital positions.
orbit_system(entity::registry &registry)
Abstract base class for updatable systems.
entity::registry & registry
Registry on which the system operate.
entt::registry registry
Component registry type.
Definition: registry.hpp:28
entt::entity id
Entity ID type.
Definition: id.hpp:28
constexpr mat4< T > scale(const vec3< T > &v)
Constructs a scale matrix.
int ephemeris_index
Index of the orbit in the ephemeris.
math::dvec3 position
Cartesian position of the orbit, w.r.t. the ICRF frame.
entity::id parent
Entity ID of the parent orbit.
double scale
Orbit scale, for two-body orbits with one ephemeris item.
Table of orbital trajectories.
Definition: ephemeris.hpp:36