diff --git a/src/game/astronomy/celestial-mechanics.cpp b/src/game/astronomy/celestial-mechanics.cpp index 7a63bf7..74fd563 100644 --- a/src/game/astronomy/celestial-mechanics.cpp +++ b/src/game/astronomy/celestial-mechanics.cpp @@ -177,7 +177,7 @@ double3 solve_kepler(double a, double ec, double w, double ma, double i, double // Eccentric anomaly double ea = ma + ec * std::sin(ma) * (1.0 + ec * std::cos(ma)); - // Find radial distance (r) and true anomaly (v) + // Radial distance (r) and true anomaly (v) double x = a * (std::cos(ea) - ec); double y = b * std::sin(ea); double r = std::sqrt(x * x + y * y); diff --git a/src/game/astronomy/celestial-mechanics.hpp b/src/game/astronomy/celestial-mechanics.hpp index f61eaef..60443a8 100644 --- a/src/game/astronomy/celestial-mechanics.hpp +++ b/src/game/astronomy/celestial-mechanics.hpp @@ -69,6 +69,15 @@ struct kepler_orbit double3 solve_kepler(const kepler_orbit& orbit, double t); +/** + * + * @param a Semi-major axis, a. + * @param ec Eccentricity, e. + * @param w Argument of periapsis, w (radians). + * @param ma Mean anomaly, M (radians). + * @param i Inclination, i (radians). + * @param om Longitude of the ascending node, OMEGA (radians). + */ double3 solve_kepler(double a, double ec, double w, double ma, double i, double om); } // namespace ast diff --git a/src/game/bootloader.cpp b/src/game/bootloader.cpp index a0a0894..f7159e7 100644 --- a/src/game/bootloader.cpp +++ b/src/game/bootloader.cpp @@ -79,6 +79,7 @@ #include "game/systems/tracking-system.hpp" #include "game/systems/painting-system.hpp" #include "game/systems/weather-system.hpp" +#include "game/systems/solar-system.hpp" #include "game/components/marker-component.hpp" #include "game/entity-commands.hpp" #include "utility/paths.hpp" @@ -886,6 +887,13 @@ void setup_systems(game_context* ctx) ctx->weather_system->set_time_scale(ctx->config->get("time_scale")); } + // Setup solar system + ctx->solar_system = new solar_system(*ctx->ecs_registry); + if (ctx->config->has("time_scale")) + { + ctx->solar_system->set_time_scale(ctx->config->get("time_scale")); + } + // Setup render system ctx->render_system = new ::render_system(*ctx->ecs_registry); ctx->render_system->add_layer(ctx->overworld_scene); @@ -1183,6 +1191,7 @@ void setup_controls(game_context* ctx) [ctx, time_scale]() { ctx->weather_system->set_time_scale(time_scale * 50.0f); + ctx->solar_system->set_time_scale(time_scale * 50.0f); } ); ctx->control_system->get_fast_forward_control()->set_deactivated_callback @@ -1190,6 +1199,7 @@ void setup_controls(game_context* ctx) [ctx, time_scale]() { ctx->weather_system->set_time_scale(time_scale); + ctx->solar_system->set_time_scale(time_scale); } ); ctx->control_system->get_rewind_control()->set_activated_callback @@ -1197,6 +1207,7 @@ void setup_controls(game_context* ctx) [ctx, time_scale]() { ctx->weather_system->set_time_scale(time_scale * -50.0f); + ctx->solar_system->set_time_scale(time_scale * -50.0f); } ); ctx->control_system->get_rewind_control()->set_deactivated_callback @@ -1204,6 +1215,7 @@ void setup_controls(game_context* ctx) [ctx, time_scale]() { ctx->weather_system->set_time_scale(time_scale); + ctx->solar_system->set_time_scale(time_scale); } ); @@ -1264,6 +1276,7 @@ void setup_callbacks(game_context* ctx) ctx->tracking_system->update(t, dt); ctx->painting_system->update(t, dt); ctx->weather_system->update(t, dt); + ctx->solar_system->update(t, dt); //(*ctx->focal_point_tween)[1] = ctx->orbit_cam->get_focal_point(); diff --git a/src/game/game-context.hpp b/src/game/game-context.hpp index 8b8b232..71162dd 100644 --- a/src/game/game-context.hpp +++ b/src/game/game-context.hpp @@ -87,6 +87,7 @@ class outline_pass; class tracking_system; class painting_system; class weather_system; +class solar_system; struct biome; template class animation; template class material_property; @@ -244,6 +245,7 @@ struct game_context tracking_system* tracking_system; painting_system* painting_system; weather_system* weather_system; + solar_system* solar_system; // Game biome* biome; diff --git a/src/game/states/play-state.cpp b/src/game/states/play-state.cpp index efc3349..2ccfb6f 100644 --- a/src/game/states/play-state.cpp +++ b/src/game/states/play-state.cpp @@ -55,9 +55,11 @@ #include "game/systems/render-system.hpp" #include "game/systems/tool-system.hpp" #include "game/systems/weather-system.hpp" +#include "game/systems/solar-system.hpp" #include "game/biome.hpp" #include "utility/fundamental-types.hpp" #include "utility/gamma.hpp" +#include "game/astronomy/celestial-time.hpp" void play_state_enter(game_context* ctx) { @@ -87,6 +89,10 @@ void play_state_enter(game_context* ctx) ctx->weather_system->set_ambient_palette(ctx->biome->ambient_palette); ctx->weather_system->set_moon_palette(ctx->biome->moon_palette); ctx->weather_system->set_shadow_palette(ctx->biome->shadow_palette); + + ctx->solar_system->set_observer_location(ctx->biome->location[0], ctx->biome->location[1], ctx->biome->location[2]); + double jd = ast::ut_to_jd(2017, 6, 1, 5, 0, 0.0) - -7.0 / 24.0; + ctx->solar_system->set_julian_date(jd); resource_manager* resource_manager = ctx->resource_manager; entt::registry& ecs_registry = *ctx->ecs_registry; diff --git a/src/game/systems/solar-system.cpp b/src/game/systems/solar-system.cpp index 5d303cd..59e13a3 100644 --- a/src/game/systems/solar-system.cpp +++ b/src/game/systems/solar-system.cpp @@ -44,28 +44,28 @@ void solar_system::update(double t, double dt) // Update horizontal (topocentric) positions of orbiting bodies registry.view().each( - [&](auto entity, auto& orbit, auto& transform) - { - double a = orbit.a; - double ec = orbit.ec; - double w = orbit.w; - double ma = orbit.ma; - double i = orbit.i; - double om = orbit.om; - - double3 ecliptic = ast::solve_kepler(a, ec, w, ma, i, om); - double3 horizontal = ecliptic_to_horizontal * ecliptic; - - // Subtract Earth's radius (in AU), for positon of observer - horizontal.z -= 4.25875e-5; - - // Transform into local right-handed coordinates - double3 translation = ast::horizontal_to_right_handed * horizontal; - double3x3 rotation = ast::horizontal_to_right_handed * ecliptic_to_horizontal; - - transform.local.translation = math::type_cast(translation); - transform.local.rotation = math::type_cast(math::quaternion_cast(rotation)); - }); + [&](auto entity, auto& orbit, auto& transform) + { + double a = orbit.a; + double ec = orbit.ec; + double w = orbit.w; + double ma = orbit.ma; + double i = orbit.i; + double om = orbit.om; + + double3 ecliptic = ast::solve_kepler(a, ec, w, ma, i, om); + double3 horizontal = ecliptic_to_horizontal * ecliptic; + + // Subtract Earth's radius (in AU), for positon of observer + horizontal.z -= 4.25875e-5; + + // Transform into local right-handed coordinates + double3 translation = ast::horizontal_to_right_handed * horizontal; + double3x3 rotation = ast::horizontal_to_right_handed * ecliptic_to_horizontal; + + transform.local.translation = math::type_cast(translation); + transform.local.rotation = math::type_cast(math::quaternion_cast(rotation)); + }); } void solar_system::set_julian_date(double jd) diff --git a/src/game/systems/solar-system.hpp b/src/game/systems/solar-system.hpp index e6d1ed5..e599664 100644 --- a/src/game/systems/solar-system.hpp +++ b/src/game/systems/solar-system.hpp @@ -23,6 +23,9 @@ #include "entity-system.hpp" #include "utility/fundamental-types.hpp" +/** + * + */ class solar_system: public entity_system { diff --git a/src/game/systems/weather-system.cpp b/src/game/systems/weather-system.cpp index 59df97b..0e6a6dc 100644 --- a/src/game/systems/weather-system.cpp +++ b/src/game/systems/weather-system.cpp @@ -90,7 +90,7 @@ void weather_system::update(double t, double dt) float3 moon_position = math::normalize(math::type_cast(moon_positiond)); //double3 moon_sphericald = ast::rectangular_to_spherical(moon_positiond); - //std::cout << "old azel: " << math::degrees(moon_az_el.x) << ", " << math::degrees(moon_az_el.y) << std::endl; + std::cout << "old azel: " << math::degrees(sun_az_el.x) << ", " << math::degrees(sun_az_el.y) << std::endl; //std::cout << "new azel: " << math::degrees(moon_sphericald.z) << ", " << math::degrees(moon_sphericald.y) << std::endl; double3x3 moon_rotation_matrix = ast::horizontal_to_right_handed * ecliptic_to_horizontal;