| 1 | // tide.cxx -- interface for tidal movement |
| 2 | // |
| 3 | // Written by Erik Hofman, Octover 2020 |
| 4 | // |
| 5 | // Copyright (C) 2020 Erik Hofman <[email protected]> |
| 6 | // |
| 7 | // This program is free software; you can redistribute it and/or |
| 8 | // modify it under the terms of the GNU General Public License as |
| 9 | // published by the Free Software Foundation; either version 2 of the |
| 10 | // License, or (at your option) any later version. |
| 11 | // |
| 12 | // This program is distributed in the hope that it will be useful, but |
| 13 | // WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | // General Public License for more details. |
| 16 | // |
| 17 | // You should have received a copy of the GNU General Public License |
| 18 | // along with this program; if not, write to the Free Software |
| 19 | // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 20 | // |
| 21 | |
| 22 | #include <simgear/constants.h> |
| 23 | #include <simgear/timing/sg_time.hxx> |
| 24 | #include <simgear/structure/SGExpression.hxx> |
| 25 | #include <Main/globals.hxx> |
| 26 | |
| 27 | #include "tide.hxx" |
| 28 | #include "light.hxx" |
| 29 | #include "bodysolver.hxx" |
| 30 | |
| 31 | void FGTide::reinit() { |
| 32 | _prev_moon_lon = -9999.0; |
| 33 | } |
| 34 | |
| 35 | void FGTide::bind() |
| 36 | { |
| 37 | SGPropertyNode *props = globals->get_props(); |
| 38 | |
| 39 | viewLon = props->getNode("sim/current-view/viewer-lon-deg", true); |
| 40 | viewLat = props->getNode("sim/current-view/viewer-lat-deg", true); |
| 41 | |
| 42 | _tideAnimation = props->getNode("/environment/sea/surface/delta-T-tide", true); |
| 43 | |
| 44 | _tideLevelNorm = props->getNode("/sim/time/tide-level-norm", true); |
| 45 | _tideLevelNorm->setDoubleValue(_tide_level); |
| 46 | } |
| 47 | |
| 48 | void FGTide::unbind() |
| 49 | { |
| 50 | viewLon.reset(); |
| 51 | viewLat.reset(); |
| 52 | |
| 53 | _tideLevelNorm.reset(); |
| 54 | _tideAnimation.reset(); |
| 55 | } |
| 56 | |
| 57 | #include <Main/fg_props.hxx> |
| 58 | void FGTide::update(double dt) |
| 59 | { |
| 60 | auto l = globals->get_subsystem<FGLight>(); |
| 61 | |
| 62 | // Don't know where the 60 degrees offset comes from but it matches |
| 63 | // the tides perfectly at EHAL. Something to figure out. |
| 64 | // Eureka: It was the latitude (53.45 degrees north). |
| 65 | // It turns out that the moon is draging the tide with an almost |
| 66 | // perfect 45 degrees 'bow-wave' along the equator. Tests at SMBQ |
| 67 | // (0 degrees latitude) confirmed this finding. |
| 68 | double viewer_lon = (viewLon->getDoubleValue() |
| 69 | + fabs( viewLat->getDoubleValue() ) |
| 70 | ) * SGD_DEGREES_TO_RADIANS; |
| 71 | double moon_lon = l->get_moon_lon() - viewer_lon; |
| 72 | if (fabs(_prev_moon_lon - moon_lon) > (SGD_PI/360.0)) |
| 73 | { |
| 74 | _prev_moon_lon = moon_lon; |
| 75 | |
| 76 | double sun_lon = l->get_sun_lon() - viewer_lon; |
| 77 | _tide_level = cos(2.0*moon_lon); |
| 78 | _tide_level += 0.15*cos(2.0*sun_lon); |
| 79 | |
| 80 | if (_tide_level < -1.0) _tide_level = -1.0; |
| 81 | else if (_tide_level > 1.0) _tide_level = 1.0; |
| 82 | |
| 83 | _tideLevelNorm->setDoubleValue(_tide_level); |
| 84 | _tideAnimation->setDoubleValue(0.5 - 0.5*_tide_level); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | // Register the subsystem. |
| 89 | SGSubsystemMgr::Registrant<FGTide> registrantFGTide; |